Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add obd plugin #20818

Merged
merged 20 commits into from
Sep 21, 2024
Merged

Add obd plugin #20818

merged 20 commits into from
Sep 21, 2024

Conversation

Corwin-Kh
Copy link
Contributor

No description provided.

enum class OBDCommand(
val command: String,
private val responseParser: (String) -> String,
val isStale: Boolean = false) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not clear what is stale and why it always false

@@ -0,0 +1,21 @@
package net.osmand.shared.obd

enum class OBDCommand(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably there should be abstract class AbstractOBDCommand and subclasses for each command

fun setReadWriteStreams(readStream: Source, writeStream: Sink) {
inputStream = readStream
outputStream = writeStream
job?.cancel()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't scope?.cancel() enough?

inputStream = readStream
outputStream = writeStream
job?.cancel()
scope?.cancel()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why cancelling scope and job after streams redefined? looks like potential bug

}
log.info("response. ${command.name} **${response.replace('\\', '\\')}")
}
} catch (error: Throwable) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should process exceptions more carefully. For example CancellationException should be handled separately


object OBDDispatcher {

private val commandQueue = ConcurrentMutableList<OBDCommand>()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use just List and KCollectionUtils for modifying

object OBDDispatcher {

private val commandQueue = ConcurrentMutableList<OBDCommand>()
private val staleCommandsCache = ConcurrentMutableMap<OBDCommand, String>()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

staleCommandsCache looks useless, since isStale is always false. Also staleCommandsCache never clears.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added stale commands

}

fun addResponseListener(responseListener: OBDResponseListener) {
responseListeners.add(responseListener)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

KCollectionUtils


object BLEUtils {

fun isBLEEnabled(activity: Activity?): Boolean {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such parameter should not be nullable.
Why isBLEEnabled returns false if activity null? If activity parameter is null - means that activity is null, but not that BLE is disabled.

return false
}

fun getBluetoothAdapter(activity: Activity?): BluetoothAdapter? {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not let such parameter be nullable! Verify it on caller side!

@Corwin-Kh Corwin-Kh marked this pull request as ready for review September 20, 2024 09:29
companion object {
fun fromCode(code: String): FuelType {
return when (code.uppercase()) {
"00" -> NO_PROVIDED
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix

}

fun compute() {
val now: Long = Clock.System.now().toEpochMilliseconds()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is extension currentTimeMillis()

Comment on lines 114 to 125
private fun averageDouble(values: List<OBDValue>): Any? {
if (values.isNotEmpty()) {
var sum = 0.0
var cnt = 0
for (o in values) {
sum += o.doubleValue
cnt++
}
return sum / cnt
}
return null
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private fun averageDouble(values: List<OBDValue>): Double? = if (values.isNotEmpty()) values.sumOf { it.doubleValue } / values.size else null


private val log = LoggerFactory.getLogger("OBDDataComputer")

var locations: List<OBDLocation> = ArrayList()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

listOf


override fun updateSimpleWidgetInfo(drawSettings: DrawSettings?) {
val sensorData = plugin.getSensorData(fieldType)
val data = widgetComputer.computeValue()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cacheData needed to not setText frequently


fun registerWidget(
type: OBDTypeWidget,
averageSeconds: Int,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

averagingTimeSec?

formatter = OBDFuelTypeFormatter()
}
}
widgetComputer = OBDDataComputer.registerWidget(obdDataWidgetType, 15, formatter)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

15 should be constant

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is temporary value, should be changed when implementing UI for widgets. Added todo here

}
}
} catch (e: IOException) {
e.printStackTrace()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not insert e.printStackTrace() anywhere in code

}

fun getCommandQueue(): List<OBDCommand> {
return ArrayList(commandQueue)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commandQueue is immutable, do not create duplicate list

}
}

fun addCommandToRead(command: OBDCommand) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should not be references to OBDCommand outside ODB backend. This is completely backend class. There should be some metrics types or classes for operating in UI.

@alex-osm alex-osm merged commit e8ab908 into master Sep 21, 2024
3 checks passed
@Chumva Chumva deleted the add_obd_plugin branch September 23, 2024 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants