Skip to content

Commit

Permalink
Fixed parsing commands that return NODATA
Browse files Browse the repository at this point in the history
  • Loading branch information
Corwin-Kh committed Oct 9, 2024
1 parent eefd9d9 commit fce9594
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ object OBDDataComputer {
// FUEL_LEFT_LITERS(false, OBD_FUEL_LEVEL_COMMAND),
FUEL_LEFT_PERCENT(false, OBD_FUEL_LEVEL_COMMAND),
FUEL_CONSUMPTION_RATE(false, OBD_FUEL_LEVEL_COMMAND),
FUEL_CONSUMPTION_RATE_SENSOR(false, OBD_FUEL_CONSUMPTION_RATE_COMMAND),
TEMPERATURE_INTAKE(false, OBD_AIR_INTAKE_TEMP_COMMAND),
TEMPERATURE_AMBIENT(false, OBD_AMBIENT_AIR_TEMPERATURE_COMMAND),
BATTERY_VOLTAGE(false, OBD_BATTERY_VOLTAGE_COMMAND),
Expand Down Expand Up @@ -153,6 +154,7 @@ object OBDDataComputer {
TEMPERATURE_INTAKE,
SPEED,
BATTERY_VOLTAGE,
FUEL_CONSUMPTION_RATE_SENSOR,
RPM -> {
if (averageTimeSeconds == 0 && locValues.size > 0) {
locValues[locValues.size - 1].value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ object OBDDispatcher {
val fullCommand = "$hexGroupCode$hexCode"
val commandResult =
obd2Connection!!.run(fullCommand, command.command, command.commandType)
sensorDataCache[command] = command.parseResponse(commandResult)
if(commandResult.isValid()) {
sensorDataCache[command] = command.parseResponse(commandResult.result)
}
}
} catch (error: IOException) {
log.error("Run OBD looper error. $error")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package net.osmand.shared.obd

data class OBDResponse(val result: IntArray){
companion object{
val OK = OBDResponse(intArrayOf(1))
val QUESTION_MARK = OBDResponse(intArrayOf(1))
val NO_DATA = OBDResponse(intArrayOf())
}

fun isValid(): Boolean {
return this != OK && this != QUESTION_MARK && this != NO_DATA
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Obd2Connection(private val connection: UnderlyingTransport) {
fun run(
fullCommand: String,
command: Int,
commandType: COMMAND_TYPE = COMMAND_TYPE.LIVE): IntArray {
commandType: COMMAND_TYPE = COMMAND_TYPE.LIVE): OBDResponse {
var response = runImpl(fullCommand)
val originalResponseValue = response
val unspacedCommand = fullCommand.replace(" ", "")
Expand All @@ -59,10 +59,11 @@ class Obd2Connection(private val connection: UnderlyingTransport) {
"BUSERROR",
"STOPPED"
)
log.debug("post-processed response without side data $response")
when (response) {
"OK" -> return intArrayOf(1)
"?" -> return intArrayOf(0)
"NODATA" -> return intArrayOf()
"OK" -> return OBDResponse.OK
"?" -> return OBDResponse.QUESTION_MARK
"NODATA" -> return OBDResponse.NO_DATA
"UNABLETOCONNECT" -> throw Exception("connection failure")
"CANERROR" -> throw Exception("CAN bus error")
}
Expand All @@ -75,7 +76,7 @@ class Obd2Connection(private val connection: UnderlyingTransport) {
} else {
hexValues = hexValues.copyOfRange(2, hexValues.size)
}
return hexValues
return OBDResponse(hexValues)
} catch (e: IllegalArgumentException) {
log.debug(
"Conversion error: command: '$fullCommand', original response: '$originalResponseValue', processed response: '$response'"
Expand Down Expand Up @@ -157,11 +158,11 @@ class Obd2Connection(private val connection: UnderlyingTransport) {
var basePid = 1
for (pid in pids) {
val responseData = run(pid, 0x01)
if (responseData.size >= 6) {
val byte0 = responseData[2].toByte()
val byte1 = responseData[3].toByte()
val byte2 = responseData[4].toByte()
val byte3 = responseData[5].toByte()
if (responseData.result.size >= 6) {
val byte0 = responseData.result[2].toByte()
val byte1 = responseData.result[3].toByte()
val byte2 = responseData.result[4].toByte()
val byte3 = responseData.result[5].toByte()
log.debug(
"Supported PID at base $basePid payload %02X%02X%02X%02X".format(
byte0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class OBDTest {
var fuelLevel = fuelLevelStart
var time: Long = currentTimeMillis()
for (i in 0 .. 600) {
val map = HashMap<OBDCommand, OBDDataField>()
val map = HashMap<OBDCommand, OBDDataField<Any>>()
map[OBDCommand.OBD_FUEL_LEVEL_COMMAND] = OBDDataField(OBDDataFieldType.FUEL_LVL, fuelLevel.toString())
OBDDataComputer.acceptValue(map)
val now = currentTimeMillis()
Expand Down
17 changes: 17 additions & 0 deletions OsmAnd/res/layout/fragment_obd_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,23 @@
android:layout_marginLeft="@dimen/content_padding_half" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">

<ToggleButton
android:id="@+id/btn13"
android:layout_width="250dp"
android:layout_height="match_parent" />

<EditText
android:id="@+id/resp13"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/content_padding_half" />
</LinearLayout>

</LinearLayout>

</LinearLayout>
5 changes: 5 additions & 0 deletions OsmAnd/src/net/osmand/plus/plugins/odb/OBDTextWidget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class OBDTextWidget @JvmOverloads constructor(
formatter = OBDComputerWidgetFormatter("%.0f")
averageTimeSeconds = 5 * 60
}
FUEL_CONSUMPTION_RATE_SENSOR -> {
obdDataWidgetType = OBDTypeWidget.FUEL_CONSUMPTION_RATE_SENSOR
formatter = OBDComputerWidgetFormatter("%.0f")
averageTimeSeconds = 5 * 60
}
FUEL_LEFT_DISTANCE -> {
obdDataWidgetType = OBDTypeWidget.FUEL_LEFT_DISTANCE
formatter = OBDComputerWidgetFormatter("%.0f")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ enum class OBDWidgetDataFieldType(
FUEL_LVL(WidgetType.OBD_FUEL_LEVEL, R.drawable.widget_battery_day, OBDCommand.OBD_FUEL_LEVEL_COMMAND, OBDDataFieldType.FUEL_LVL),
FUEL_LEFT_DISTANCE(WidgetType.OBD_FUEL_LEFT_DISTANCE, R.drawable.widget_battery_day, OBDCommand.OBD_FUEL_LEVEL_COMMAND, OBDDataFieldType.FUEL_LVL),
FUEL_CONSUMPTION_RATE(WidgetType.OBD_FUEL_CONSUMPTION_RATE, R.drawable.widget_battery_day, OBDCommand.OBD_FUEL_LEVEL_COMMAND, OBDDataFieldType.FUEL_LVL),
FUEL_CONSUMPTION_RATE_SENSOR(WidgetType.OBD_FUEL_CONSUMPTION_RATE, R.drawable.widget_battery_day, OBDCommand.OBD_FUEL_CONSUMPTION_RATE_COMMAND, OBDDataFieldType.FUEL_CONSUMPTION_RATE),
AMBIENT_AIR_TEMP(WidgetType.OBD_AMBIENT_AIR_TEMP, R.drawable.ic_action_thermometer, OBDCommand.OBD_AMBIENT_AIR_TEMPERATURE_COMMAND, OBDDataFieldType.AMBIENT_AIR_TEMP),
BATTERY_VOLTAGE(WidgetType.OBD_BATTERY_VOLTAGE, R.drawable.ic_action_thermometer, OBDCommand.OBD_BATTERY_VOLTAGE_COMMAND, OBDDataFieldType.BATTERY_VOLTAGE),
AIR_INTAKE_TEMP(WidgetType.OBD_AIR_INTAKE_TEMP, R.drawable.ic_action_signal, OBDCommand.OBD_AIR_INTAKE_TEMP_COMMAND, OBDDataFieldType.AIR_INTAKE_TEMP),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import net.osmand.plus.utils.ColorUtilities
import net.osmand.shared.obd.OBDDataComputer
import net.osmand.shared.obd.OBDDataComputer.OBDTypeWidget.BATTERY_VOLTAGE
import net.osmand.shared.obd.OBDDataComputer.OBDTypeWidget.FUEL_CONSUMPTION_RATE
import net.osmand.shared.obd.OBDDataComputer.OBDTypeWidget.FUEL_CONSUMPTION_RATE_SENSOR
import net.osmand.shared.obd.OBDDataComputer.OBDTypeWidget.FUEL_LEFT_DISTANCE
import net.osmand.shared.obd.OBDDataComputer.OBDTypeWidget.FUEL_LEFT_PERCENT
import net.osmand.shared.obd.OBDDataComputer.OBDTypeWidget.FUEL_TYPE
Expand All @@ -35,6 +36,7 @@ class OBDMainFragment : BaseOsmAndFragment() {
private var fuelLeftDistBtn: Button? = null
private var fuelLeftLitersBtn: Button? = null
private var fuelConsumptionBtn: Button? = null
private var fuelConsumptionRateBtn: Button? = null
private var rpmBtn: Button? = null
private var speedBtn: Button? = null
private var tempIntakeBtn: Button? = null
Expand All @@ -47,6 +49,7 @@ class OBDMainFragment : BaseOsmAndFragment() {
private var fuelLeftDistResp: EditText? = null
private var fuelLeftLitersResp: EditText? = null
private var fuelConsumptionResp: EditText? = null
private var fuelConsumptionRateResp: EditText? = null
private var rpmResp: EditText? = null
private var speedResp: EditText? = null
private var tempIntakeResp: EditText? = null
Expand Down Expand Up @@ -88,6 +91,7 @@ class OBDMainFragment : BaseOsmAndFragment() {
fuelLeftDistResp = view.findViewById(R.id.resp1)
fuelLeftLitersResp = view.findViewById(R.id.resp2)
fuelConsumptionResp = view.findViewById(R.id.resp3)
fuelConsumptionRateResp = view.findViewById(R.id.resp13)
rpmResp = view.findViewById(R.id.resp4)
speedResp = view.findViewById(R.id.resp5)
tempIntakeResp = view.findViewById(R.id.resp6)
Expand All @@ -101,6 +105,7 @@ class OBDMainFragment : BaseOsmAndFragment() {
fuelLeftDistBtn = view.findViewById(R.id.btn1)
fuelLeftLitersBtn = view.findViewById(R.id.btn2)
fuelConsumptionBtn = view.findViewById(R.id.btn3)
fuelConsumptionRateBtn = view.findViewById(R.id.btn13)
rpmBtn = view.findViewById(R.id.btn4)
speedBtn = view.findViewById(R.id.btn5)
tempIntakeBtn = view.findViewById(R.id.btn6)
Expand All @@ -113,6 +118,7 @@ class OBDMainFragment : BaseOsmAndFragment() {
fuelLeftDistBtn?.text = "fuel left distance"
fuelLeftLitersBtn?.text = "fuel left liters"
fuelConsumptionBtn?.text = "fuel consumption"
fuelConsumptionRateBtn?.text = "fuel consumption_rete"
rpmBtn?.text = "rpm"
speedBtn?.text = "speed"
tempIntakeBtn?.text = "intake air temp"
Expand Down Expand Up @@ -180,6 +186,7 @@ class OBDMainFragment : BaseOsmAndFragment() {
// FUEL_LEFT_LITERS -> updateWidgetData(fuelLeftLitersResp, result)
FUEL_LEFT_PERCENT -> updateWidgetData(fuelLeftPersResp, result)
FUEL_CONSUMPTION_RATE -> updateWidgetData(fuelConsumptionResp, result)
FUEL_CONSUMPTION_RATE_SENSOR -> updateWidgetData(fuelConsumptionResp, result)
TEMPERATURE_INTAKE -> updateWidgetData(tempIntakeResp, result)
TEMPERATURE_AMBIENT -> updateWidgetData(tempAmbientResp, result)
BATTERY_VOLTAGE -> updateWidgetData(batteryVoltageResp, result)
Expand Down

0 comments on commit fce9594

Please sign in to comment.