-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Add obd plugin #20818
Conversation
enum class OBDCommand( | ||
val command: String, | ||
private val responseParser: (String) -> String, | ||
val isStale: Boolean = false) { |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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>() |
There was a problem hiding this comment.
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>() |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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? { |
There was a problem hiding this comment.
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!
…gets formatting and calculations
companion object { | ||
fun fromCode(code: String): FuelType { | ||
return when (code.uppercase()) { | ||
"00" -> NO_PROVIDED |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is extension currentTimeMillis()
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 | ||
} |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
listOf
OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/obd/OBDDataComputer.kt
Show resolved
Hide resolved
|
||
override fun updateSimpleWidgetInfo(drawSettings: DrawSettings?) { | ||
val sensorData = plugin.getSensorData(fieldType) | ||
val data = widgetComputer.computeValue() |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
15 should be constant
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
No description provided.