Skip to content

Commit

Permalink
feat:Unify ObdMetrics formatting interface
Browse files Browse the repository at this point in the history
  • Loading branch information
tzebrowski committed Jan 19, 2025
1 parent 0cf95de commit ffa6cc5
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 120 deletions.
14 changes: 8 additions & 6 deletions app/src/main/java/org/obd/graphs/ui/gauge/GaugeAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import org.obd.graphs.bl.collector.Metric
import org.obd.graphs.R
import org.obd.graphs.ValueConverter
import org.obd.graphs.bl.datalogger.dataLogger
import org.obd.graphs.bl.query.format
import org.obd.graphs.bl.query.valueToFloat
import org.obd.graphs.modules
import org.obd.graphs.preferences.Prefs
import org.obd.graphs.round
Expand Down Expand Up @@ -129,7 +131,7 @@ class GaugeAdapter(
position: Int
) {
val metric = data.elementAt(position)
val pid = metric.source.command.pid
val pid = metric.pid()

if (!holder.init) {
holder.label.text = pid.longDescription ?: pid.description
Expand All @@ -151,7 +153,7 @@ class GaugeAdapter(

holder.value.run {
val units = (metric.source.command as ObdCommand).pid.units?:""
val txt = "${metric.valueToString()} $units"
val txt = "${metric.source.format(castToInt = false)} $units"
text = txt

highLightText(
Expand All @@ -162,7 +164,7 @@ class GaugeAdapter(

if (pid.historgam.isMinEnabled) {
holder.minValue.run {
val txt = "min\n ${metric.toNumber(metric.min)}"
val txt = "min\n ${metric.min.format(pid)}"
text = txt
highLightText(
"min", 0.5f,
Expand All @@ -174,7 +176,7 @@ class GaugeAdapter(

if (pid.historgam.isMaxEnabled) {
holder.maxValue.run {
val txt = "max\n ${metric.toNumber(metric.max)} "
val txt = "max\n ${metric.max.format(pid)}"
text = txt
highLightText(
"max", 0.5f,
Expand All @@ -185,7 +187,7 @@ class GaugeAdapter(

if (pid.historgam.isAvgEnabled) {
holder.avgValue?.run {
val txt = "avg\n ${metric.toNumber(metric.mean)}"
val txt = "avg\n ${metric.mean.format(pid)}"
text = txt
highLightText(
"avg", 0.5f,
Expand Down Expand Up @@ -223,7 +225,7 @@ class GaugeAdapter(
endValue = it.max.toFloat()
}
}
value = metric.valueToFloat()
value = metric.source.valueToFloat()
invalidate()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import org.obd.graphs.R
import org.obd.graphs.bl.collector.toNumber
import org.obd.graphs.bl.datalogger.dataLogger
import org.obd.graphs.bl.query.format
import org.obd.graphs.bl.trip.SensorData
import org.obd.graphs.ui.common.COLOR_PHILIPPINE_GREEN
import org.obd.graphs.ui.common.setText
Expand Down Expand Up @@ -58,24 +58,22 @@ class TripDetailsViewAdapter internal constructor(
holder.metricName.setText(pid.description, COLOR_PHILIPPINE_GREEN, 1.0f)
metric.run {
holder.metricMaxValue.setText(
"${toNumber(pid, max)}",
max.format(pid),
Color.GRAY,
1.0f
)
holder.metricMinValue.setText(
"${toNumber(pid, min)}",
min.format(pid),
Color.GRAY,
1.0f
)
holder.metricMeanValue.setText(
"${toNumber(pid, mean)}",
mean.format(pid),
Color.GRAY,
1.0f
)
}
}


}

override fun getItemCount(): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import org.obd.graphs.bl.collector.MetricsCollector
import org.obd.graphs.bl.datalogger.*
import org.obd.graphs.bl.query.Query
import org.obd.graphs.bl.query.QueryStrategyType
import org.obd.graphs.bl.query.format
import org.obd.graphs.profile.PROFILE_CHANGED_EVENT
import org.obd.graphs.renderer.DynamicSelectorMode

Expand Down Expand Up @@ -231,7 +232,7 @@ internal class IotTemplateCarScreen(
metricsCollector.getMetrics().forEach {
paneBuilder.addRow(
Row.Builder()
.setImage(valueDrawable.draw(it.valueToString(),settings.getColorTheme().progressColor),
.setImage(valueDrawable.draw(it.source.format(castToInt = false),settings.getColorTheme().progressColor),
Row.IMAGE_TYPE_LARGE
)
.setMetadata(Metadata.Builder().build())
Expand All @@ -258,7 +259,8 @@ internal class IotTemplateCarScreen(
val title = StringBuilder()
title.append(metric.source.command.pid.description.replace("\n",""))
title.append("\n")
title.append("· min:${metric.toNumber(metric.min)} avg: ${metric.toNumber(metric.mean)} max: ${metric.toNumber(metric.max)}")
val pid = metric.pid()
title.append("· min:${metric.min.format(pid)} avg: ${metric.mean.format(pid)} max: ${metric.max.format(pid)}")
return SpannableString(title)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ internal class InMemoryCarMetricsCollector : MetricsCollector {
Log.d(LOG_KEY, "Rebuilding metrics configuration for: $enabled != ${metrics.keys}")
}
metricBuilder.buildFor(enabled).forEach {
val key = it.source.command.pid.id
val key = it.pid().id

if (metrics.keys.indexOf(key) ==-1) {
Log.i(LOG_KEY, "Adding PID($key = ${it.source.command.pid.description}) to metrics map.")
Log.i(LOG_KEY, "Adding PID($key = ${it.pid().description}) to metrics map.")
metrics[key] = it
}
}
Expand Down
50 changes: 1 addition & 49 deletions datalogger/src/main/java/org/obd/graphs/bl/collector/Metric.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@
**/
package org.obd.graphs.bl.collector

import org.obd.graphs.round
import org.obd.metrics.api.model.ObdMetric
import org.obd.metrics.pid.PidDefinition
import org.obd.metrics.pid.ValueType

private const val NO_DATA = "----"

data class Metric(
var source: ObdMetric,
Expand All @@ -39,33 +36,9 @@ data class Metric(
Metric(source, value = value, min = min, max = max, mean = mean, enabled = true, rate = 0.0)
}

fun toNumber(value: Double?, doublePrecision: Int = 2): String {
return toNumber(source.command.pid, value, doublePrecision = doublePrecision).toString()
}

fun isInAlert(): Boolean = source.isAlert

fun valueToString(): String =
if (source.value == null) {
NO_DATA
} else {
if (source.value is Number) {
toNumber(source.valueToDouble())
} else {
source.value.toString()
}
}

fun valueToFloat(): Float =
if (source.value == null) {
if (source.command.pid.min == null) 0f else source.command.pid.min.toFloat()
} else {
if (source.value is Number) {
source.valueToDouble().toFloat()
}else {
0f
}
}
fun pid(): PidDefinition = source.command.pid

override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand All @@ -80,24 +53,3 @@ data class Metric(
return this.source.hashCode()
}
}

fun toNumber(pid: PidDefinition, input: Number?, doublePrecision: Int = 2): Number {

if (input == null) {
return Double.NaN
}

val value = input.toDouble()
if (value.isNaN()) {
return 0.0
}
return if (pid.type == null) value.round(doublePrecision) else
pid.type.let {
return when (pid.type) {
ValueType.DOUBLE -> value.round(doublePrecision)
ValueType.INT -> value.toInt()
ValueType.SHORT -> value.toInt()
else -> value.round(1)
}
}
}
79 changes: 79 additions & 0 deletions datalogger/src/main/java/org/obd/graphs/bl/query/Formatter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.obd.graphs.bl.query

import org.obd.graphs.round
import org.obd.metrics.api.model.ObdMetric
import org.obd.metrics.pid.PidDefinition
import org.obd.metrics.pid.ValueType

private const val NO_DATA = "No data"

fun ObdMetric.format(castToInt: Boolean = false, precision: Int = 2): String = format(
input = this.value,
pid = null, precision = precision, castToInt = castToInt
)

fun ObdMetric.valueToFloat(): Float =
if (this.value == null) {
if (this.command.pid.min == null) 0f else this.command.pid.min.toFloat()
} else {
if (this.value is Number) {
this.valueToDouble().toFloat()
} else {
0f
}
}

fun ObdMetric.valueToNumber(): Number? =
if (this.value == null) {
null
} else {
if (value is Number) {
value as Number
} else {
null
}
}


fun Number.format(pid: PidDefinition, precision: Int = 2, castToInt: Boolean = false): String = format(
input = this,
pid = pid, precision = precision, castToInt = castToInt
)


private fun format(input: Any?, pid: PidDefinition? = null, precision: Int = 2, castToInt: Boolean = false): String =

if (input == null) {
NO_DATA
} else {
if (input is Number) {
val value = input.toDouble()

if (value.isNaN()) {
NO_DATA
}

val number = if (pid == null || pid.type == null) {
if (value is Number) {
if (castToInt) (value as Number).toInt().toString()
else if (value is Double) (value as Number).toDouble().round(precision).toString()
else value.toString()
} else {
value.toString()
}
} else {
pid.type.let {
when (pid.type) {
ValueType.DOUBLE -> value.round(precision)
ValueType.INT -> value.toInt()
ValueType.SHORT -> value.toInt()
else -> value.round(1)
}
}
}

number.toString()
} else {
input.toString()
}
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,10 @@
package org.obd.graphs.bl.query

import org.obd.graphs.preferences.Prefs
import org.obd.graphs.round
import org.obd.metrics.api.model.ObdMetric

fun isGMEExtensionsEnabled() = Prefs.getBoolean(PREF_PROFILE_2_0_GME_EXTENSION_ENABLED, false)


fun ObdMetric.valueToString(castToInt: Boolean = false, precision: Int = 2): String =
if (this.value == null) {
"No data"
} else {
if (value is Number) {
if (castToInt) (value as Number).toInt().toString()
else if (this.value is Double) (value as Number).toDouble().round(precision).toString()
else this.value.toString()
} else {
"No data"
}
}

fun ObdMetric.valueToNumber(): Number? =
if (this.value == null) {
null
} else {
if (value is Number) {
value as Number
} else {
null
}
}


fun ObdMetric.isAtmPressure(): Boolean = command.pid.id == namesRegistry.getAtmPressurePID()
fun ObdMetric.isAmbientTemp(): Boolean = command.pid.id == namesRegistry.getAmbientTempPID()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@ internal class DragRacingMetricsProcessor(private val registry: DragRacingResult
}
registry.enableShiftLights(obdMetric.valueToNumber()!!.toInt() > registry.getShiftLightsRevThreshold())
} else if (obdMetric.isAtmPressure()) {
atmosphericPressure = obdMetric.valueToNumber()!!.toInt()
obdMetric.valueToNumber()?.let {
atmosphericPressure = it.toInt()
}
} else if (obdMetric.isAmbientTemp()) {
ambientTemperature = obdMetric.valueToNumber()!!.toInt()

obdMetric.valueToNumber()?.let {
ambientTemperature = it.toInt()
}
} else if (obdMetric.isVehicleSpeed()) {

processVehicleSpeedData(obdMetric)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.obd.graphs.bl.collector.MetricsCollector
import org.obd.graphs.bl.datalogger.WorkflowStatus
import org.obd.graphs.bl.datalogger.dataLogger
import org.obd.graphs.bl.query.namesRegistry
import org.obd.graphs.bl.query.format
import org.obd.graphs.commons.R
import org.obd.graphs.profile.profile
import org.obd.graphs.renderer.drag.MARGIN_END
Expand Down Expand Up @@ -245,7 +246,7 @@ internal abstract class AbstractDrawer(context: Context, protected val settings:
marginLeft += getTextWidth(text, statusPaint) + 4F
drawText(
canvas,
"${it.valueToString()}${it.source.command.pid.units}",
"${it.source.format(castToInt = false)}${it.pid().units}",
marginLeft,
top,
Color.WHITE,
Expand All @@ -270,7 +271,7 @@ internal abstract class AbstractDrawer(context: Context, protected val settings:
marginLeft += getTextWidth(text, statusPaint) + 4F
drawText(
canvas,
"${it.valueToString()}${it.source.command.pid.units}",
"${it.source.format(castToInt = false)}${it.pid().units}",
marginLeft,
top,
Color.WHITE,
Expand Down
Loading

0 comments on commit ffa6cc5

Please sign in to comment.