mirror of
https://github.com/mii443/prometheus-android-exporter.git
synced 2025-08-23 23:49:27 +00:00
a framework for hardware sensors
This commit is contained in:
@ -8,29 +8,50 @@ import io.prometheus.client.GaugeMetricFamily
|
|||||||
|
|
||||||
private const val TAG = "ANDROID_EXPORTER"
|
private const val TAG = "ANDROID_EXPORTER"
|
||||||
|
|
||||||
class AndroidCustomExporter(metricEngine: MetricsEngine) : Collector() {
|
class AndroidCustomExporter(private val metricEngine: MetricsEngine) : Collector() {
|
||||||
private val metricsEngineRef = metricEngine
|
|
||||||
|
|
||||||
override fun collect(): List<MetricFamilySamples> {
|
override fun collect(): List<MetricFamilySamples> {
|
||||||
Log.d(TAG, "Collecting metrics now")
|
Log.d(TAG, "Collecting metrics now")
|
||||||
val mfs: MutableList<MetricFamilySamples> = ArrayList()
|
val mfs: MutableList<MetricFamilySamples> = ArrayList()
|
||||||
|
|
||||||
|
//TODO scrape_duration gauge
|
||||||
|
|
||||||
// metrics definitions
|
// metrics definitions
|
||||||
collectBatteryStatus(mfs)
|
// collectBatteryStatus(mfs)
|
||||||
|
collectGps(mfs)
|
||||||
|
collectSteps(mfs)
|
||||||
|
|
||||||
Log.d(TAG, "Metrics collected")
|
Log.d(TAG, "Metrics collected")
|
||||||
return mfs
|
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
|
//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()
|
Log.d(TAG, metricEngine.hwSensorsValues().numberOfSteps.toString())
|
||||||
batteryPercentageGauge.addMetric(listOf(), batteryPercentage)
|
metricEngine.hwSensorsValues().numberOfSteps?.let{
|
||||||
mfs.add(batteryPercentageGauge)
|
gauge.addMetric(listOf(), it.toDouble())
|
||||||
|
mfs.add(gauge)
|
||||||
|
Log.d(TAG, "heeeeeeeeeeeeeeeeeeeeeeeeeeeee")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,40 +3,63 @@
|
|||||||
package com.birdthedeveloper.prometheus.android.exporter.worker
|
package com.birdthedeveloper.prometheus.android.exporter.worker
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.hardware.Sensor
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.IntentFilter
|
import android.content.IntentFilter
|
||||||
import android.hardware.SensorManager
|
import android.hardware.SensorManager
|
||||||
import android.os.BatteryManager
|
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 const val TAG = "METRICS_ENGINE"
|
||||||
private lateinit var sensorManager: SensorManager;
|
|
||||||
|
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 {
|
init {
|
||||||
//sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
|
registerAllHwEventHandlers()
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun batteryChargeRatio(): Float {
|
fun hwSensorsValues() : HwSensorsCache{
|
||||||
val batteryStatus: Intent? = IntentFilter(Intent.ACTION_BATTERY_CHANGED).let { intFilter ->
|
return hwSensorsCache
|
||||||
context.registerReceiver(null, intFilter)
|
}
|
||||||
}
|
|
||||||
//val status: Int = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1) ?: -1
|
|
||||||
|
|
||||||
val batteryPct: Float? = batteryStatus?.let { intent ->
|
private fun registerAllHwEventHandlers(){
|
||||||
val level: Int = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
|
Log.d(TAG, "Registering all hw sensors")
|
||||||
val scale: Int = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
|
val sensor: Sensor? = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY)
|
||||||
level * 100 / scale.toFloat()
|
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
|
when(event.sensor.type){
|
||||||
return batteryPct
|
Sensor.TYPE_PROXIMITY -> {
|
||||||
|
hwSensorsCache.numberOfSteps = event.values[0].toInt()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun batteryIsCharging(): Float {
|
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
|
||||||
TODO("aa")
|
Log.d(TAG, "Sensor accuracy changed.")
|
||||||
}
|
|
||||||
|
|
||||||
public fun somethingTodo(): Float {
|
|
||||||
TODO("aa")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -69,3 +92,19 @@ class MetricsEngine(private val context: Context) {
|
|||||||
// - storage information
|
// - storage information
|
||||||
// - system information - version .. device name, doba provozu
|
// - 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
|
||||||
|
// }
|
||||||
|
@ -4,6 +4,7 @@ package com.birdthedeveloper.prometheus.android.exporter.worker
|
|||||||
|
|
||||||
import android.app.NotificationManager
|
import android.app.NotificationManager
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.hardware.SensorManager
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.core.app.NotificationCompat
|
import androidx.core.app.NotificationCompat
|
||||||
import androidx.work.CoroutineWorker
|
import androidx.work.CoroutineWorker
|
||||||
|
Reference in New Issue
Block a user