expose battery percentage

This commit is contained in:
Martin Ptáček
2023-04-18 13:25:54 +02:00
parent 6b7332cd23
commit 8c372e9f2a
3 changed files with 45 additions and 33 deletions

View File

@ -7,35 +7,27 @@ import kotlinx.coroutines.runBlocking
import java.util.Arrays
import kotlinx.coroutines.*
class AndroidCustomExporter(name: String) : Collector() {
class AndroidCustomExporter(metricEngine: MetricsEngine) : Collector() {
private val metricsEngineRef = metricEngine
//TODO pass context here, get baterry percentage from metrics engine
override fun collect(): List<MetricFamilySamples> {
val mfs: MutableList<MetricFamilySamples> = ArrayList()
// With no labels.
mfs.add(GaugeMetricFamily("my_gauge", "help", 42.0))
// With labels
val labeledGauge = GaugeMetricFamily("my_other_gauge", "help", Arrays.asList("labelname"))
labeledGauge.addMetric(listOf("foo"), 4.0)
labeledGauge.addMetric(listOf("bar"), 5.0)
mfs.add(labeledGauge)
println("Start blocking")
// metrics definitions
collectBatteryStatus(mfs)
return mfs
}
fun collectBaterryStatus(mfs: MutableList<MetricFamilySamples>) {
fun collectBatteryStatus(mfs: MutableList<MetricFamilySamples>) {
//TODO
val baterryPercentageGauge = GaugeMetricFamily(
"baterry_percentage",
"Baterry percentage", listOf(),
val batteryPercentageGauge = GaugeMetricFamily(
"battery_percentage",
"Current battery percentage", listOf(),
)
//baterryPercentageGauge.addMetric()
val batteryPercentage : Double = metricsEngineRef.getBatteryPercentage().toDouble()
batteryPercentageGauge.addMetric(listOf(), batteryPercentage)
mfs.add(batteryPercentageGauge)
}
}
}

View File

@ -1,6 +1,7 @@
package com.birdthedeveloper.prometheus.android.prometheus.android.exporter
import android.os.Bundle
import android.text.BoringLayout.Metrics
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
@ -22,13 +23,14 @@ import java.io.StringWriter
class MainActivity : ComponentActivity() {
// register custom prometheus exporter
val collectorRegistry: CollectorRegistry = CollectorRegistry()
val customExporter: AndroidCustomExporter = AndroidCustomExporter("dummy")
.register(collectorRegistry)
private val collectorRegistry: CollectorRegistry = CollectorRegistry()
private lateinit var metricsEngine: MetricsEngine
private lateinit var customExporter: AndroidCustomExporter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initialize()
setContent {
PrometheusAndroidExporterTheme {
// A surface container using the 'background' color from the theme
@ -42,6 +44,11 @@ class MainActivity : ComponentActivity() {
}
}
private fun initialize (){
metricsEngine = MetricsEngine(this.applicationContext)
customExporter = AndroidCustomExporter(metricsEngine).register(collectorRegistry)
}
fun CollectMetrics(): String{
val writer = StringWriter()
TextFormat.write004(writer, collectorRegistry.metricFamilySamples())

View File

@ -1,12 +1,25 @@
package com.birdthedeveloper.prometheus.android.prometheus.android.exporter
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
class MetricsEngine {
// public fun getBatteryPercentage(){
// val batteryStatus: Intent? = IntentFilter(Intent.ACTION_BATTERY_CHANGED).let { ifilter ->
// context.registerReceiver(null, ifilter)
// }
// }
}
class MetricsEngine(context: Context) {
private val contextRef = context
public fun getBatteryPercentage() : Float{
val batteryStatus: Intent? = IntentFilter(Intent.ACTION_BATTERY_CHANGED).let { intFilter ->
contextRef.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
}
}