a framework for hardware sensors

This commit is contained in:
Martin Ptáček
2023-07-26 20:04:08 +02:00
parent 219c156f6b
commit bac7c4e62f
3 changed files with 91 additions and 30 deletions

View File

@ -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<MetricFamilySamples> {
Log.d(TAG, "Collecting metrics now")
val mfs: MutableList<MetricFamilySamples> = 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<MetricFamilySamples>) {
// private fun collectBatteryStatus(mfs: MutableList<MetricFamilySamples>) {
// //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<MetricFamilySamples>){
//TODO
val batteryPercentageGauge = GaugeMetricFamily(
"battery_percentage", //TODO convert to ratio
"Current battery percentage", listOf(),
}
private fun collectSteps(mfs: MutableList<MetricFamilySamples>){
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")
}
}
}

View File

@ -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
// }

View File

@ -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