Skip to content

Commit

Permalink
Chenged test UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Corwin-Kh committed Sep 26, 2024
1 parent 47a3d0f commit 7dfe32e
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.osmand.shared.data

import net.osmand.shared.util.Localization

data class BTDeviceInfo(val name: String, val address: String) {
companion object {
val UNKNOWN_DEVICE = BTDeviceInfo(Localization.getString("unknown_bt_device"), "")
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package net.osmand.shared.obd

enum class OBDCommand(
val commandGroup: String,
val command: String,
private val responseParser: (String) -> String,
val isStale: Boolean = false) {
OBD_SUPPORTED_LIST1_COMMAND("00", OBDUtils::parseSupportedCommandsResponse, true),
OBD_SUPPORTED_LIST2_COMMAND("20", OBDUtils::parseSupportedCommandsResponse, true),
OBD_SUPPORTED_LIST3_COMMAND("40", OBDUtils::parseSupportedCommandsResponse, true),
OBD_BATTERY_VOLTAGE_COMMAND("42", OBDUtils::parseBatteryVoltageResponse),
OBD_AMBIENT_AIR_TEMPERATURE_COMMAND("46", OBDUtils::parseAmbientTempResponse),
OBD_RPM_COMMAND("0C", OBDUtils::parseRpmResponse),
OBD_SPEED_COMMAND("0D", OBDUtils::parseSpeedResponse),
OBD_AIR_INTAKE_TEMP_COMMAND("0F", OBDUtils::parseIntakeAirTempResponse),
OBD_ENGINE_COOLANT_TEMP_COMMAND("05", OBDUtils::parseEngineCoolantTempResponse),
OBD_FUEL_CONSUMPTION_RATE_COMMAND("5E", OBDUtils::parseFuelConsumptionRateResponse),
OBD_FUEL_TYPE_COMMAND("51", OBDUtils::parseFuelTypeResponse, true),
OBD_FUEL_LEVEL_COMMAND("2F", OBDUtils::parseFuelLevelResponse);
val isStale: Boolean = false,
val isMultiPartResponse: Boolean = false) {
OBD_SUPPORTED_LIST1_COMMAND("01", "00", OBDUtils::parseSupportedCommandsResponse, true),
OBD_SUPPORTED_LIST2_COMMAND("01", "20", OBDUtils::parseSupportedCommandsResponse, true),
OBD_SUPPORTED_LIST3_COMMAND("01", "40", OBDUtils::parseSupportedCommandsResponse, true),
OBD_BATTERY_VOLTAGE_COMMAND("01", "42", OBDUtils::parseBatteryVoltageResponse),
OBD_AMBIENT_AIR_TEMPERATURE_COMMAND("01", "46", OBDUtils::parseAmbientTempResponse),
OBD_RPM_COMMAND("01", "0C", OBDUtils::parseRpmResponse),
OBD_SPEED_COMMAND("01", "0D", OBDUtils::parseSpeedResponse),
OBD_AIR_INTAKE_TEMP_COMMAND("01", "0F", OBDUtils::parseIntakeAirTempResponse),
OBD_ENGINE_COOLANT_TEMP_COMMAND("01", "05", OBDUtils::parseEngineCoolantTempResponse),
OBD_FUEL_CONSUMPTION_RATE_COMMAND("01", "5E", OBDUtils::parseFuelConsumptionRateResponse),
OBD_FUEL_TYPE_COMMAND("01", "51", OBDUtils::parseFuelTypeResponse, true),
OBD_VIN_COMMAND("09", "02", OBDUtils::parseVINResponse, true, true),
OBD_FUEL_LEVEL_COMMAND("01", "2F", OBDUtils::parseFuelLevelResponse);

fun parseResponse(response: String): String {
return responseParser.invoke(response.lowercase())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ object OBDDataComputer {
FUEL_LEFT_DISTANCE(true,
listOf(OBD_FUEL_LEVEL_COMMAND),
{ data -> OBDValue(OBD_FUEL_LEVEL_COMMAND, data) }),
// FUEL_LEFT_LITERS(false,

// FUEL_LEFT_LITERS(false,
// listOf(OBD_FUEL_LEVEL_COMMAND),
// { data -> OBDValue(OBD_FUEL_LEVEL_COMMAND, data) }),
FUEL_LEFT_PERCENT(false,
Expand All @@ -122,6 +123,9 @@ object OBDDataComputer {
FUEL_TYPE(false,
listOf(OBD_FUEL_TYPE_COMMAND),
{ data -> OBDIntValue(OBD_FUEL_TYPE_COMMAND, data) }),
VIN(false,
listOf(OBD_VIN_COMMAND),
{ data -> OBDStringValue(OBD_VIN_COMMAND, data) }),
TEMPERATURE_COOLANT(false,
listOf(OBD_ENGINE_COOLANT_TEMP_COMMAND),
{ data -> OBDIntValue(OBD_ENGINE_COOLANT_TEMP_COMMAND, data) });
Expand Down Expand Up @@ -247,6 +251,12 @@ object OBDDataComputer {
null
}
}

VIN -> if (locValues.size > 0) {
(locValues[locValues.size - 1] as OBDStringValue).value
} else {
null
}
}
}

Expand Down Expand Up @@ -317,4 +327,17 @@ object OBDDataComputer {
return accepted
}
}

class OBDStringValue(cmd: OBDCommand, data: Map<OBDCommand, OBDDataField?>) :
OBDValue(cmd, data) {
var value = ""
override fun acceptData(dataField: OBDDataField?): Boolean {
var accepted = false
dataField?.let {
value = it.getValue()
accepted = true
}
return accepted
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ enum class OBDDataFieldType(val nameId: String, private val unitNameId: String)
BATTERY_VOLTAGE("obd_battery_voltage_desc", "unit_volt"),
AIR_INTAKE_TEMP("obd_air_intake_temp_desc", "degree_celsius"),
COOLANT_TEMP("obd_engine_coolant_temp", "degree_celsius"),
VIN("obd_vin", ""),
FUEL_TYPE("obd_fuel_type", "");

fun getDisplayName(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ object OBDDispatcher {
private val log = LoggerFactory.getLogger("OBDDispatcher")
private const val TERMINATE_SYMBOL = "\r\r>"
private const val RESPONSE_LINE_TERMINATOR = "\r"
private const val READ_DATA_COMMAND_CODE = "01"
private var job: Job? = null
private var scope: CoroutineScope? = null
private var readStatusListener: OBDReadStatusListener? = null
Expand All @@ -49,12 +48,12 @@ object OBDDispatcher {
for (command in commandQueue) {
if (command.isStale) {
val cachedCommandResponse = staleCommandsCache[command]
if (cachedCommandResponse != null) {
if (cachedCommandResponse != null && cachedCommandResponse != OBDUtils.INVALID_RESPONSE_CODE) {
consumeResponse(command, cachedCommandResponse)
continue
}
}
val fullCommand = "$READ_DATA_COMMAND_CODE${command.command}\r"
val fullCommand = "${command.commandGroup}${command.command}\r"
val bufferToWrite = Buffer()
bufferToWrite.write(fullCommand.encodeToByteArray())
outStream.write(bufferToWrite, bufferToWrite.size)
Expand Down Expand Up @@ -99,7 +98,14 @@ object OBDDispatcher {
}
var response = resultRaw.toString()
response = response.replace(TERMINATE_SYMBOL, "")
val listResponses = response.split(RESPONSE_LINE_TERMINATOR)
var listResponses = response.split(RESPONSE_LINE_TERMINATOR)
// val listResponses = if(command.isMultiPartResponse) {
// listOf(response)
// } else {
// }
if(command.isMultiPartResponse) {
listResponses = listOf(response.split(RESPONSE_LINE_TERMINATOR).subList(2, listResponses.size).joinToString(separator = ""))
}
for (responseIndex in 1 until listResponses.size) {
val result = command.parseResponse(listResponses[responseIndex])
log.debug("raw_response_$responseIndex: $result")
Expand Down Expand Up @@ -161,6 +167,7 @@ object OBDDispatcher {
OBDCommand.OBD_AIR_INTAKE_TEMP_COMMAND -> OBDDataField(OBDDataFieldType.AIR_INTAKE_TEMP, result)
OBDCommand.OBD_ENGINE_COOLANT_TEMP_COMMAND -> OBDDataField(OBDDataFieldType.COOLANT_TEMP, result)
OBDCommand.OBD_FUEL_TYPE_COMMAND -> OBDDataField(OBDDataFieldType.FUEL_TYPE, result)
OBDCommand.OBD_VIN_COMMAND -> OBDDataField(OBDDataFieldType.VIN, result)
OBDCommand.OBD_FUEL_LEVEL_COMMAND -> OBDDataField(OBDDataFieldType.FUEL_LVL, result)
OBDCommand.OBD_AMBIENT_AIR_TEMPERATURE_COMMAND -> OBDDataField(OBDDataFieldType.AMBIENT_AIR_TEMP, result)
OBDCommand.OBD_BATTERY_VOLTAGE_COMMAND -> OBDDataField(OBDDataFieldType.BATTERY_VOLTAGE, result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,22 @@ object OBDUtils {
return INVALID_RESPONSE_CODE
}

fun parseVINResponse(response: String): String {
val responseParts = response.split(" ")

if (responseParts[0] == "49" &&
responseParts[1] == OBDCommand.OBD_VIN_COMMAND.command.lowercase() &&
responseParts.size > 3) {
val vinBuilder = StringBuilder()
for (i in 3 .. responseParts.size) {
val hexByte = responseParts[i]
vinBuilder.append(hexByte.toInt(16).toChar())
}
return vinBuilder.toString()
}
return INVALID_RESPONSE_CODE
}

fun parseFuelConsumptionRateResponse(response: String): String {
val hexValues = response.trim().split(" ")
if (hexValues.size >= 4 && hexValues[0] == "41" && hexValues[1] == OBDCommand.OBD_FUEL_CONSUMPTION_RATE_COMMAND.command.lowercase()) {
Expand Down
3 changes: 3 additions & 0 deletions OsmAnd/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
- For wording and consistency, please note https://docs.osmand.net/docs/technical/contributions/translating-osmand
Thx - Hardy
-->
<string name="unknown_bt_device">Unknown device</string>
<string name="obd_vin">VIN</string>
<string name="obd_vin_desc">VIN code over OBD</string>
<string name="unit_volt">V</string>
<string name="wikimedia">Wikimedia</string>
<string name="open_in_browser">Open in browser</string>
Expand Down
4 changes: 4 additions & 0 deletions OsmAnd/src/net/osmand/plus/plugins/odb/OBDTextWidget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class OBDTextWidget @JvmOverloads constructor(
obdDataWidgetType = OBDTypeWidget.FUEL_TYPE
formatter = OBDFuelTypeFormatter()
}
VIN -> {
obdDataWidgetType = OBDTypeWidget.VIN
formatter = OBDComputerWidgetFormatter("%s")
}
}
//todo implement setting correct time for widget (0 for instant)
widgetComputer = OBDDataComputer.registerWidget(obdDataWidgetType, averageTimeSeconds, formatter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ enum class OBDWidgetDataFieldType(
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),
COOLANT_TEMP(WidgetType.OBD_ENGINE_COOLANT_TEMP, R.drawable.ic_action_sensor_bicycle_power_outlined, OBDCommand.OBD_ENGINE_COOLANT_TEMP_COMMAND, OBDDataFieldType.COOLANT_TEMP),
VIN(WidgetType.OBD_VIN, R.drawable.ic_action_sensor_bicycle_power_outlined, OBDCommand.OBD_ENGINE_COOLANT_TEMP_COMMAND, OBDDataFieldType.COOLANT_TEMP),
FUEL_TYPE(WidgetType.OBD_FUEL_TYPE, R.drawable.ic_action_speed_outlined, OBDCommand.OBD_FUEL_TYPE_COMMAND, OBDDataFieldType.FUEL_TYPE)
}
14 changes: 8 additions & 6 deletions OsmAnd/src/net/osmand/plus/plugins/odb/VehicleMetricsPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import net.osmand.plus.views.mapwidgets.widgets.MapWidget
import net.osmand.plus.widgets.ctxmenu.ContextMenuAdapter
import net.osmand.plus.widgets.ctxmenu.callback.OnDataChangeUiAdapter
import net.osmand.plus.widgets.ctxmenu.data.ContextMenuItem
import net.osmand.shared.data.BTDeviceInfo
import net.osmand.shared.data.KLatLon
import net.osmand.shared.obd.OBDCommand
import net.osmand.shared.obd.OBDDataComputer
Expand Down Expand Up @@ -194,16 +195,16 @@ class VehicleMetricsPlugin(app: OsmandApplication) : OsmandPlugin(app),
}

@SuppressLint("MissingPermission")
fun getPairedOBDDevicesList(activity: Activity): List<String> {
var deviceList = listOf<String>()
fun getPairedOBDDevicesList(activity: Activity): List<BTDeviceInfo> {
var deviceList = listOf<BTDeviceInfo>()
if (BLEUtils.isBLEEnabled(activity) && AndroidUtils.hasBLEPermission(activity)) {
val bluetoothAdapter = BLEUtils.getBluetoothAdapter(activity)
bluetoothAdapter?.let { adapter ->
adapter.cancelDiscovery()
val pairedDevices = adapter.bondedDevices.toList()
deviceList = pairedDevices.filter { device ->
device.uuids?.any { parcelUuid -> parcelUuid.uuid == uuid } == true
}.map { it.name ?: "Unknown Device" }
}.map { if (it != null) BTDeviceInfo(it.name, it.address) else BTDeviceInfo.UNKNOWN_DEVICE }
}

} else {
Expand All @@ -221,10 +222,11 @@ class VehicleMetricsPlugin(app: OsmandApplication) : OsmandPlugin(app),
OBDDispatcher.stopReading()
}
}
connectedDevice = null
}

@SuppressLint("MissingPermission")
fun connectToObd(activity: Activity, name: String): Boolean {
fun connectToObd(activity: Activity, deviceInfo: BTDeviceInfo): Boolean {
if (connectedDevice == null) {
if (BLEUtils.isBLEEnabled(activity)) {
if (AndroidUtils.hasBLEPermission(activity)) {
Expand All @@ -242,7 +244,7 @@ class VehicleMetricsPlugin(app: OsmandApplication) : OsmandPlugin(app),
LOG.debug(it.name)
}
val obdDevice: BluetoothDevice? =
pairedDevices.find { it.name == name }
pairedDevices.find { it.name == deviceInfo.name && it.address == deviceInfo.address }
connectedDevice = obdDevice
connectToDevice(activity)
}
Expand All @@ -253,7 +255,7 @@ class VehicleMetricsPlugin(app: OsmandApplication) : OsmandPlugin(app),
} else {
socket?.close()
connectedDevice = null
connectToObd(activity, name)
connectToObd(activity, deviceInfo)
}
return socket != null && socket?.isConnected == true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import net.osmand.shared.obd.OBDDataComputer.OBDTypeWidget.SPEED
import net.osmand.shared.obd.OBDDataComputer.OBDTypeWidget.TEMPERATURE_AMBIENT
import net.osmand.shared.obd.OBDDataComputer.OBDTypeWidget.TEMPERATURE_COOLANT
import net.osmand.shared.obd.OBDDataComputer.OBDTypeWidget.TEMPERATURE_INTAKE
import net.osmand.shared.obd.OBDDataComputer.OBDTypeWidget.VIN

class OBDMainFragment : BaseOsmAndFragment() {
private var deviceName: EditText? = null
Expand All @@ -42,14 +43,15 @@ class OBDMainFragment : BaseOsmAndFragment() {
private var fuelTypeBtn: Button? = null
private var fuelLeftPersBtn: Button? = null
private var tempAmbientBtn: Button? = null
private var vinBtn: Button? = null
private var fuelLeftDistResp: EditText? = null
private var fuelLeftLitersResp: EditText? = null
private var fuelConsumptionResp: EditText? = null
private var rpmResp: EditText? = null
private var speedResp: EditText? = null
private var tempIntakeResp: EditText? = null
private var tempCoolantResp: EditText? = null
private var resp8: EditText? = null
private var vinResp: EditText? = null
private var fuelTypeResp: EditText? = null
private var fuelLeftPersResp: EditText? = null
private var tempAmbientResp: EditText? = null
Expand Down Expand Up @@ -106,6 +108,7 @@ class OBDMainFragment : BaseOsmAndFragment() {
fuelTypeBtn = view.findViewById(R.id.btn9)
fuelLeftPersBtn = view.findViewById(R.id.btn10)
tempAmbientBtn = view.findViewById(R.id.btn11)
vinBtn = view.findViewById(R.id.btn12)
fuelLeftDistBtn?.text = "fuel left distance"
fuelLeftLitersBtn?.text = "fuel left liters"
fuelConsumptionBtn?.text = "fuel consumption"
Expand All @@ -117,6 +120,7 @@ class OBDMainFragment : BaseOsmAndFragment() {
fuelTypeBtn?.text = "fuel type"
fuelLeftPersBtn?.text = "fuel left percent"
tempAmbientBtn?.text = "ambient air temperature"
vinBtn?.text = "vin"
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -180,6 +184,7 @@ class OBDMainFragment : BaseOsmAndFragment() {
BATTERY_VOLTAGE -> updateWidgetData(batteryVoltageResp, result)
FUEL_TYPE -> updateWidgetData(fuelTypeResp, result)
TEMPERATURE_COOLANT -> updateWidgetData(tempCoolantResp, result)
VIN -> updateWidgetData(vinResp, result)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import net.osmand.plus.plugins.PluginsHelper
import net.osmand.plus.plugins.odb.VehicleMetricsPlugin
import net.osmand.plus.utils.AndroidUtils
import net.osmand.plus.utils.ColorUtilities
import net.osmand.shared.data.BTDeviceInfo

class VehicleMetricsSettingsFragment : BaseOsmAndFragment() {
private var plugin: VehicleMetricsPlugin? = null
Expand All @@ -30,7 +31,7 @@ class VehicleMetricsSettingsFragment : BaseOsmAndFragment() {
var adapter: DeviceAdapter? = null
var emptyView: View? = null

private var items = listOf<String>()
private var items = listOf<BTDeviceInfo>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -127,23 +128,26 @@ class VehicleMetricsSettingsFragment : BaseOsmAndFragment() {
}
}

class DeviceAdapter(private val items: List<String>) :
class DeviceAdapter(private val items: List<BTDeviceInfo>) :
RecyclerView.Adapter<DeviceAdapter.ViewHolder>() {
var selectedPosition: Int = RecyclerView.NO_POSITION

class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val textView: TextView = itemView.findViewById(android.R.id.text1)
val subTextView: TextView = itemView.findViewById(android.R.id.text2)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(android.R.layout.simple_list_item_1, parent, false)
.inflate(android.R.layout.simple_list_item_2, parent, false)
return ViewHolder(view)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.textView.text = items[position]
holder.textView.text = items[position].name
holder.subTextView.text = items[position].address
holder.textView.setTextColor(Color.BLACK)
holder.subTextView.setTextColor(Color.GRAY)
if (position == selectedPosition) {
holder.itemView.setBackgroundColor(Color.LTGRAY)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public enum WidgetType {
OBD_FUEL_LEVEL("obd_fuel_level", R.string.obd_fuel_level, R.string.obd_fuel_level_desc, R.drawable.widget_weather_temperature_day, R.drawable.widget_weather_temperature_night, 0, VEHICLE_METRICS, RIGHT),
OBD_FUEL_LEFT_DISTANCE("obd_fuel_left_distance", R.string.obd_fuel_left_distance, R.string.obd_fuel_left_distance_desc, R.drawable.widget_weather_temperature_day, R.drawable.widget_weather_temperature_night, 0, VEHICLE_METRICS, RIGHT),
OBD_FUEL_CONSUMPTION_RATE("obd_fuel_consumption_rate", R.string.obd_fuel_consumption_rate, R.string.obd_fuel_consumption_rate_desc, R.drawable.widget_weather_temperature_day, R.drawable.widget_weather_temperature_night, 0, VEHICLE_METRICS, RIGHT),
OBD_VIN("obd_vin", R.string.obd_vin, R.string.obd_vin_desc, R.drawable.widget_weather_temperature_day, R.drawable.widget_weather_temperature_night, 0, VEHICLE_METRICS, RIGHT),
OBD_FUEL_TYPE("obd_fuel_type", R.string.obd_fuel_type, R.string.obd_fuel_type_desc, R.drawable.widget_weather_temperature_day, R.drawable.widget_weather_temperature_night, 0, VEHICLE_METRICS, RIGHT),

TEMPERATURE("temperature_sensor", R.string.shared_string_temperature, R.string.sensor_temperature_desc, R.drawable.widget_weather_temperature_day, R.drawable.widget_weather_temperature_night, 0, ANT_PLUS, RIGHT),
Expand Down

0 comments on commit 7dfe32e

Please sign in to comment.