Skip to content

Commit

Permalink
feat: Add vehicle status reading
Browse files Browse the repository at this point in the history
  • Loading branch information
tzebrowski committed Jan 5, 2025
1 parent 36a2657 commit c41c615
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 40 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/org/obd/graphs/activity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton
import org.obd.graphs.*
import org.obd.graphs.bl.datalogger.dataLogger
import org.obd.graphs.bl.drag.dragRacingMetricsProcessor
import org.obd.graphs.bl.extra.vehicleStatusEventBroadcaster
import org.obd.graphs.bl.generator.MetricsGenerator
import org.obd.graphs.bl.trip.tripManager
import org.obd.graphs.profile.profile
Expand Down Expand Up @@ -228,6 +229,7 @@ class MainActivity : AppCompatActivity(), EasyPermissions.PermissionCallbacks {
dataLogger
.observe(dragRacingMetricsProcessor)
.observe(tripManager)
.observe(vehicleStatusEventBroadcaster)

if (BuildConfig.DEBUG){
dataLogger.observe(MetricsGenerator(BuildConfig.DEBUG))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.obd.graphs.aa.screen
import android.util.Log
import org.obd.graphs.bl.datalogger.MetricsProcessor
import org.obd.graphs.bl.query.isDynamicSelector
import org.obd.graphs.bl.query.valueToNumber
import org.obd.graphs.sendBroadcastEvent
import org.obd.metrics.api.model.ObdMetric

Expand All @@ -23,17 +24,17 @@ internal class DynamicSelectorModeEventBroadcaster: MetricsProcessor {
if (obdMetric.isDynamicSelector()) {

if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) {
Log.v(LOG_TAG, "Received=${obdMetric.value.toInt()}, current=${currentMode} ")
Log.v(LOG_TAG, "Received=${obdMetric.valueToNumber()!!.toInt()}, current=${currentMode} ")
}

if (currentMode != obdMetric.value) {
if (currentMode != obdMetric.valueToNumber()!!) {

if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) {
Log.v(LOG_TAG, "Broadcasting Dynamic Selector Mode Change, new=${obdMetric.value.toInt()}")
Log.v(LOG_TAG, "Broadcasting Dynamic Selector Mode Change, new=${obdMetric.valueToNumber()!!.toInt()}")
}

currentMode = obdMetric.value.toInt()
when (obdMetric.value.toInt()) {
currentMode = obdMetric.valueToNumber()!!.toInt()
when (obdMetric.valueToNumber()!!.toInt()) {
0 -> sendBroadcastEvent(EVENT_DYNAMIC_SELECTOR_MODE_NORMAL)
2 -> sendBroadcastEvent(EVENT_DYNAMIC_SELECTOR_MODE_SPORT)
4 -> sendBroadcastEvent(EVENT_DYNAMIC_SELECTOR_MODE_ECO)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ data class DataLoggerPreferences(
var gracefulStop: Boolean,
var dumpRawConnectorResponse: Boolean,
var delayAfterReset: Long,
var fuelTankSize: Int
var fuelTankSize: Int,
var vehicleStatusReading: Boolean
)


Expand Down Expand Up @@ -157,10 +158,11 @@ class DataLoggerPreferencesManager {

val resources = Prefs.getStringSet(PREF_MODULE_LIST, modules.getDefaultModules().keys)!!


val fuelTankSize = Prefs.getS("pref.vehicle_settings.fuelTankSize", "58").toInt()
val vehicleStatusReading = Prefs.getBoolean("pref.vehicle_settings.vehicleStatusReading", false)

val dataLoggerPreferences = DataLoggerPreferences(
vehicleStatusReading = vehicleStatusReading,
dragRacingCommandFrequency = dragRacingCommandFrequency,
otherModesBatchSize = mode22batchSize?.toInt(),
mode01BatchSize = mode01batchSize?.toInt(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.obd.graphs.bl.extra

import android.util.Log
import org.obd.graphs.bl.datalogger.MetricsProcessor
import org.obd.graphs.bl.datalogger.dataLoggerPreferences
import org.obd.graphs.bl.query.isVehicleStatus
import org.obd.graphs.sendBroadcastEvent
import org.obd.metrics.api.model.ObdMetric

const val EVENT_VEHICLE_STATUS_VEHICLE_MOVING = "event.vehicle.status.vehicle.moving"
const val EVENT_VEHICLE_STATUS_VEHICLE_IDLING = "event.vehicle.status.vehicle.idling"
const val EVENT_VEHICLE_STATUS_IGNITION_OFF = "event.vehicle.status.vehicle.ignition_off"
const val EVENT_VEHICLE_STATUS_VEHICLE_ACCELERATING = "event.vehicle.status.vehicle.accelerating"
const val EVENT_VEHICLE_STATUS_VEHICLE_DECELERATING = "event.vehicle.status.vehicle.decelerating"

val vehicleStatusEventBroadcaster:MetricsProcessor = VehicleStatusEventBroadcaster()

private const val LOG_TAG = "VehicleStatBroad"

internal class VehicleStatusEventBroadcaster: MetricsProcessor {
private var currVehicleRunning = false
private var currEngineRunning = false
private var currKeyStatusOn = false
private var currVehicleAccelerating = false
private var currVehicleDecelerating = false


override fun postValue(obdMetric: ObdMetric) {

if (dataLoggerPreferences.instance.vehicleStatusReading && obdMetric.isVehicleStatus()) {

if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) {
Log.v(LOG_TAG, "Received=${obdMetric.value}, ")
}
val value: Map<String, Boolean> = obdMetric.value as Map<String, Boolean>
val engineRunning = value["engine.running"]!!
val vehicleRunning = value["vehicle.running"]!!
val keyStatusOn = value["key.status"]!!

if (currVehicleRunning != vehicleRunning || currEngineRunning != engineRunning || currKeyStatusOn != keyStatusOn) {
currVehicleRunning = vehicleRunning
currEngineRunning = engineRunning
currKeyStatusOn = keyStatusOn
currVehicleAccelerating = value["vehicle.accelerating"]!!
currVehicleDecelerating = value["vehicle.decelerating"]!!

if (currEngineRunning && currVehicleRunning){
sendBroadcastEvent(EVENT_VEHICLE_STATUS_VEHICLE_MOVING)
}else if (currEngineRunning && !currVehicleRunning){
sendBroadcastEvent(EVENT_VEHICLE_STATUS_VEHICLE_IDLING)
}else if (!currEngineRunning && !currVehicleRunning && !currKeyStatusOn){
sendBroadcastEvent(EVENT_VEHICLE_STATUS_IGNITION_OFF)
}else if (currVehicleAccelerating){
sendBroadcastEvent(EVENT_VEHICLE_STATUS_VEHICLE_ACCELERATING)
}else if (currVehicleDecelerating){
sendBroadcastEvent(EVENT_VEHICLE_STATUS_VEHICLE_DECELERATING)
}
}
}
}
}
27 changes: 24 additions & 3 deletions datalogger/src/main/java/org/obd/graphs/bl/query/ObdMetricExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,32 @@ fun ObdMetric.valueToString(castToInt: Boolean = false, precision: Int = 2): Str
if (this.value == null) {
"No data"
} else {
if (castToInt) value.toInt().toString()
else if (this.value is Double) value.toDouble().round(precision).toString()
else this.value.toString()
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()

fun ObdMetric.isVehicleStatus(): Boolean = command.pid.id == namesRegistry.getVehicleStatusPID()

fun ObdMetric.isDynamicSelector(): Boolean = command.pid.id == namesRegistry.getDynamicSelectorPID()
fun ObdMetric.isVehicleSpeed(): Boolean = command.pid.id == namesRegistry.getVehicleSpeedPID()
fun ObdMetric.isEngineRpm(): Boolean = command.pid.id == namesRegistry.getEngineRpmPID()
Expand Down Expand Up @@ -58,6 +76,7 @@ private const val OIL_PRESSURE_PID_ID = 7018L
private const val GAS_PID_ID = 7007L
private const val OIL_DEGRADATION_PID_ID = 7015L

private const val VEHICLE_STATUS_PID_ID = 17091L

class PIDsNamesRegistry {

Expand Down Expand Up @@ -97,4 +116,6 @@ class PIDsNamesRegistry {
fun getEngineRpmPID(): Long = if (isGMEExtensionsEnabled()) EXT_ENGINE_RPM_PID_ID else ENGINE_RPM_PID_ID

fun getDynamicSelectorPID(): Long = EXT_DYNAMIC_SELECTOR_PID_ID

fun getVehicleStatusPID(): Long = VEHICLE_STATUS_PID_ID
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ package org.obd.graphs.bl.drag

import android.util.Log
import org.obd.graphs.bl.datalogger.MetricsProcessor
import org.obd.graphs.bl.query.isAmbientTemp
import org.obd.graphs.bl.query.isAtmPressure
import org.obd.graphs.bl.query.isEngineRpm
import org.obd.graphs.bl.query.isVehicleSpeed
import org.obd.graphs.bl.query.*
import org.obd.metrics.api.model.ObdMetric
import org.obd.metrics.api.model.VehicleCapabilities
import kotlin.math.min
Expand Down Expand Up @@ -70,14 +67,14 @@ internal class DragRacingMetricsProcessor(private val registry: DragRacingResult
if (Log.isLoggable(LOG_KEY, Log.VERBOSE)) {
Log.v(
LOG_KEY, "Current revLimit='${registry.getShiftLightsRevThreshold()}', " +
"current rev: ${obdMetric.value.toInt()}, rising: ${obdMetric.value.toInt() > registry.getShiftLightsRevThreshold()}"
"current rev: ${obdMetric.valueToNumber()!!.toInt()}, rising: ${obdMetric.valueToNumber()!!.toInt() > registry.getShiftLightsRevThreshold()}"
)
}
registry.enableShiftLights(obdMetric.value.toInt() > registry.getShiftLightsRevThreshold())
registry.enableShiftLights(obdMetric.valueToNumber()!!.toInt() > registry.getShiftLightsRevThreshold())
} else if (obdMetric.isAtmPressure()) {
atmosphericPressure = obdMetric.value.toInt()
atmosphericPressure = obdMetric.valueToNumber()!!.toInt()
} else if (obdMetric.isAmbientTemp()) {
ambientTemperature = obdMetric.value.toInt()
ambientTemperature = obdMetric.valueToNumber()!!.toInt()

} else if (obdMetric.isVehicleSpeed()) {

Expand All @@ -87,11 +84,11 @@ internal class DragRacingMetricsProcessor(private val registry: DragRacingResult

private fun processVehicleSpeedData(obdMetric: ObdMetric) {

if (obdMetric.value.toInt() == SPEED_0_KM_H) {
if (obdMetric.valueToNumber()!!.toInt() == SPEED_0_KM_H) {
reset0()

if (Log.isLoggable(LOG_KEY, Log.VERBOSE)) {
Log.v(LOG_KEY, "Ready to measure, current speed: ${obdMetric.value}")
Log.v(LOG_KEY, "Ready to measure, current speed: ${obdMetric.valueToNumber()!!}")
}

registry.readyToRace(true)
Expand All @@ -102,8 +99,8 @@ internal class DragRacingMetricsProcessor(private val registry: DragRacingResult
}


if (isGivenSpeedReached(obdMetric, SPEED_60_KM_H - 5) && obdMetric.value.toInt() < SPEED_60_KM_H) {
Log.i(LOG_KEY, "Reset 60-140 measurement at speed: ${obdMetric.value.toInt()}")
if (isGivenSpeedReached(obdMetric, SPEED_60_KM_H - 5) && obdMetric.valueToNumber()!!.toInt() < SPEED_60_KM_H) {
Log.i(LOG_KEY, "Reset 60-140 measurement at speed: ${obdMetric.valueToNumber()!!.toInt()}")
result60_140 = null
_60ts = null
}
Expand All @@ -121,19 +118,19 @@ internal class DragRacingMetricsProcessor(private val registry: DragRacingResult
registry.update060(
dragRacingMetric.apply {
time = result0_60!!
speed = obdMetric.value.toInt()
speed = obdMetric.valueToNumber()!!.toInt()
ambientTemp = ambientTemperature
atmPressure = atmosphericPressure
}

)
Log.i(LOG_KEY, "Current speed: ${obdMetric.value}. Result: 0-60 ${result0_60}ms")
Log.i(LOG_KEY, "Current speed: ${obdMetric.valueToNumber()!!}. Result: 0-60 ${result0_60}ms")
}
}
}

if (isGivenSpeedReached(obdMetric, SPEED_100_KM_H - 5) && obdMetric.value.toInt() < SPEED_100_KM_H) {
Log.i(LOG_KEY, "Reset 100-200 measurement at speed: ${obdMetric.value.toInt()}")
if (isGivenSpeedReached(obdMetric, SPEED_100_KM_H - 5) && obdMetric.valueToNumber()!!.toInt() < SPEED_100_KM_H) {
Log.i(LOG_KEY, "Reset 100-200 measurement at speed: ${obdMetric.valueToNumber()!!.toInt()}")
result100_200 = null
_100ts = null
}
Expand All @@ -151,15 +148,15 @@ internal class DragRacingMetricsProcessor(private val registry: DragRacingResult
registry.update0100(
dragRacingMetric.apply {
time = result0_100!!
speed = obdMetric.value.toInt()
speed = obdMetric.valueToNumber()!!.toInt()
ambientTemp = ambientTemperature
atmPressure = atmosphericPressure
}

)

if (Log.isLoggable(LOG_KEY, Log.VERBOSE)) {
Log.v(LOG_KEY, "Current speed: ${obdMetric.value}. Result: 0-100 ${result0_100}ms")
Log.v(LOG_KEY, "Current speed: ${obdMetric.valueToNumber()!!}. Result: 0-100 ${result0_100}ms")
}
}
}
Expand All @@ -171,13 +168,13 @@ internal class DragRacingMetricsProcessor(private val registry: DragRacingResult
registry.update0160(
dragRacingMetric.apply {
time = result0_160!!
speed = obdMetric.value.toInt()
speed = obdMetric.valueToNumber()!!.toInt()
ambientTemp = ambientTemperature
atmPressure = atmosphericPressure
}

)
Log.i(LOG_KEY, "Current speed: ${obdMetric.value}. Result: 0-160 ${result0_160}ms")
Log.i(LOG_KEY, "Current speed: ${obdMetric.valueToNumber()!!}. Result: 0-160 ${result0_160}ms")
}
}

Expand All @@ -187,12 +184,12 @@ internal class DragRacingMetricsProcessor(private val registry: DragRacingResult
registry.update100200(
dragRacingMetric.apply {
time = result100_200!!
speed = obdMetric.value.toInt()
speed = obdMetric.valueToNumber()!!.toInt()
ambientTemp = ambientTemperature
atmPressure = atmosphericPressure
}
)
Log.i(LOG_KEY, "Current speed: ${obdMetric.value}. Result: 100-200 ${result100_200}ms")
Log.i(LOG_KEY, "Current speed: ${obdMetric.valueToNumber()!!}. Result: 100-200 ${result100_200}ms")
}
}

Expand All @@ -202,20 +199,20 @@ internal class DragRacingMetricsProcessor(private val registry: DragRacingResult
registry.update60140(
dragRacingMetric.apply {
time = result60_140!!
speed = obdMetric.value.toInt()
speed = obdMetric.valueToNumber()!!.toInt()
ambientTemp = ambientTemperature
atmPressure = atmosphericPressure
}
)
Log.i(
LOG_KEY,
"Current speed: ${obdMetric.value}, _60ts=${_60ts}, _140ts=${obdMetric.timestamp}, Result: 60-140 ${result60_140}ms"
"Current speed: ${obdMetric.valueToNumber()!!}, _60ts=${_60ts}, _140ts=${obdMetric.timestamp}, Result: 60-140 ${result60_140}ms"
)
}
}
}

private fun isGivenSpeedReached(obdMetric: ObdMetric, givenSpeed: Int): Boolean = min(obdMetric.value.toInt(), givenSpeed) == givenSpeed
private fun isGivenSpeedReached(obdMetric: ObdMetric, givenSpeed: Int): Boolean = min(obdMetric.valueToNumber()!!.toInt(), givenSpeed) == givenSpeed

private fun reset0() {
_0ts = null
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ android.useAndroidX=true
android.enableJetifier=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
obdMetricVersion=9.33.3-SNAPSHOT
obdMetricVersion=11.0.0-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.obd.graphs.bl.collector.Metric
import org.obd.graphs.bl.drag.DragRacingEntry
import org.obd.graphs.bl.drag.DragRacingResults
import org.obd.graphs.bl.drag.VALUE_NOT_SET
import org.obd.graphs.bl.query.valueToNumber
import org.obd.graphs.renderer.AbstractDrawer
import org.obd.graphs.renderer.ScreenSettings
import org.obd.graphs.round
Expand Down Expand Up @@ -281,7 +282,7 @@ internal class DragRacingDrawer(context: Context, settings: ScreenSettings) : Ab
paint.color = color

val progress = valueScaler.scaleToNewRange(
it.source.value?.toFloat() ?: it.source.command.pid.min.toFloat(),
it.source.valueToNumber()?.toFloat() ?: it.source.command.pid.min.toFloat(),
it.source.command.pid.min.toFloat(), it.source.command.pid.max.toFloat(), left, left + width - MARGIN_END
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package org.obd.graphs.renderer.gauge
import android.content.Context
import android.graphics.*
import org.obd.graphs.bl.collector.Metric
import org.obd.graphs.bl.query.valueToNumber
import org.obd.graphs.commons.R
import org.obd.graphs.renderer.AbstractDrawer
import org.obd.graphs.renderer.GaugeProgressBarType
Expand Down Expand Up @@ -177,7 +178,7 @@ internal class GaugeDrawer(
setProgressGradient(rect)
}

val value = metric.source.value?.toFloat() ?: metric.source.command.pid.min.toFloat()
val value = metric.source.valueToNumber()?.toFloat() ?: metric.source.command.pid.min.toFloat()
val startValue = metric.source.command.pid.min.toFloat()
val endValue = metric.source.command.pid.max.toFloat()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package org.obd.graphs.renderer.giulia
import android.content.Context
import android.graphics.*
import org.obd.graphs.bl.collector.Metric
import org.obd.graphs.bl.query.valueToNumber
import org.obd.graphs.renderer.AbstractDrawer
import org.obd.graphs.renderer.ScreenSettings

Expand Down Expand Up @@ -173,7 +174,7 @@ internal class GiuliaDrawer(context: Context, settings: ScreenSettings): Abstrac
paint.color = color

val progress = valueScaler.scaleToNewRange(
it.source.value?.toFloat() ?: it.source.command.pid.min.toFloat(),
it.source.valueToNumber()?.toFloat() ?: it.source.command.pid.min.toFloat(),
it.source.command.pid.min.toFloat(), it.source.command.pid.max.toFloat(), left, left + width - MARGIN_END
)

Expand Down
Loading

0 comments on commit c41c615

Please sign in to comment.