diff --git a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/AndroidCustomExporter.kt b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/AndroidCustomExporter.kt index d43d1dd..eddc9d4 100644 --- a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/AndroidCustomExporter.kt +++ b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/AndroidCustomExporter.kt @@ -8,29 +8,50 @@ import io.prometheus.client.GaugeMetricFamily private const val TAG = "ANDROID_EXPORTER" -class AndroidCustomExporter(metricEngine: MetricsEngine) : Collector() { - private val metricsEngineRef = metricEngine +class AndroidCustomExporter(private val metricEngine: MetricsEngine) : Collector() { override fun collect(): List { Log.d(TAG, "Collecting metrics now") val mfs: MutableList = ArrayList() + //TODO scrape_duration gauge + // metrics definitions - collectBatteryStatus(mfs) +// collectBatteryStatus(mfs) + collectGps(mfs) + collectSteps(mfs) Log.d(TAG, "Metrics collected") return mfs } - private fun collectBatteryStatus(mfs: MutableList) { +// private fun collectBatteryStatus(mfs: MutableList) { +// //TODO +// val batteryPercentageGauge = GaugeMetricFamily( +// "battery_percentage", //TODO convert to ratio +// "Current battery percentage", listOf(), +// ) +// +// val batteryPercentage: Double = metricsEngineRef.batteryChargeRatio().toDouble() +// batteryPercentageGauge.addMetric(listOf(), batteryPercentage) +// mfs.add(batteryPercentageGauge) +// } + + private fun collectGps(mfs: MutableList){ //TODO - val batteryPercentageGauge = GaugeMetricFamily( - "battery_percentage", //TODO convert to ratio - "Current battery percentage", listOf(), + } + + private fun collectSteps(mfs: MutableList){ + val gauge = GaugeMetricFamily( + "steps", + "Number of steps", listOf(), ) - val batteryPercentage: Double = metricsEngineRef.batteryChargeRatio().toDouble() - batteryPercentageGauge.addMetric(listOf(), batteryPercentage) - mfs.add(batteryPercentageGauge) + Log.d(TAG, metricEngine.hwSensorsValues().numberOfSteps.toString()) + metricEngine.hwSensorsValues().numberOfSteps?.let{ + gauge.addMetric(listOf(), it.toDouble()) + mfs.add(gauge) + Log.d(TAG, "heeeeeeeeeeeeeeeeeeeeeeeeeeeee") + } } } diff --git a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/MetricsEngine.kt b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/MetricsEngine.kt index 3b678cb..6d5787c 100644 --- a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/MetricsEngine.kt +++ b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/MetricsEngine.kt @@ -3,40 +3,63 @@ package com.birdthedeveloper.prometheus.android.exporter.worker import android.content.Context +import android.hardware.Sensor import android.content.Intent import android.content.IntentFilter import android.hardware.SensorManager import android.os.BatteryManager +import android.app.Activity +import android.hardware.SensorEvent +import android.hardware.SensorEventListener +import android.util.Log -class MetricsEngine(private val context: Context) { - private lateinit var sensorManager: SensorManager; +private const val TAG = "METRICS_ENGINE" + +class HwSensorsCache( + var batteryChargeRatio : Double? = null, + var numberOfSteps : Int? = null, +); + +class MetricsEngine(private val context: Context) : SensorEventListener { + private val sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager + private val hwSensorsCache = HwSensorsCache() init { - //sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager + registerAllHwEventHandlers() } - public fun batteryChargeRatio(): Float { - val batteryStatus: Intent? = IntentFilter(Intent.ACTION_BATTERY_CHANGED).let { intFilter -> - context.registerReceiver(null, intFilter) - } - //val status: Int = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1) ?: -1 + fun hwSensorsValues() : HwSensorsCache{ + return hwSensorsCache + } - val batteryPct: Float? = batteryStatus?.let { intent -> - val level: Int = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) - val scale: Int = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1) - level * 100 / scale.toFloat() + private fun registerAllHwEventHandlers(){ + Log.d(TAG, "Registering all hw sensors") + val sensor: Sensor? = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY) + sensor?.let{ + Log.d(TAG, "Sensor exists!") + sensorManager.registerListener(this, it, SensorManager.SENSOR_DELAY_NORMAL) + } + //sensorManager.flush(this) + } + + override fun onSensorChanged(event: SensorEvent?) { + Log.d(TAG, "Sensor Changed !!!!!!!!!!!!!!") + if(event == null){ + return + } + if (event.values == null){ + return } - batteryPct ?: return -1.0f - return batteryPct + when(event.sensor.type){ + Sensor.TYPE_PROXIMITY -> { + hwSensorsCache.numberOfSteps = event.values[0].toInt() + } + } } - public fun batteryIsCharging(): Float { - TODO("aa") - } - - public fun somethingTodo(): Float { - TODO("aa") + override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) { + Log.d(TAG, "Sensor accuracy changed.") } @@ -69,3 +92,19 @@ class MetricsEngine(private val context: Context) { // - storage information // - system information - version .. device name, doba provozu } + +// public fun batteryChargeRatio(): Float { +// val batteryStatus: Intent? = IntentFilter(Intent.ACTION_BATTERY_CHANGED).let { intFilter -> +// context.registerReceiver(null, intFilter) +// } +// //val status: Int = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1) ?: -1 +// +// val batteryPct: Float? = batteryStatus?.let { intent -> +// val level: Int = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) +// val scale: Int = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1) +// level * 100 / scale.toFloat() +// } +// +// batteryPct ?: return -1.0f +// return batteryPct +// } diff --git a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/PromWorker.kt b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/PromWorker.kt index 0e7914f..d37e6e4 100644 --- a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/PromWorker.kt +++ b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/PromWorker.kt @@ -4,6 +4,7 @@ package com.birdthedeveloper.prometheus.android.exporter.worker import android.app.NotificationManager import android.content.Context +import android.hardware.SensorManager import android.util.Log import androidx.core.app.NotificationCompat import androidx.work.CoroutineWorker