Skip to content

Commit

Permalink
Add maxRetries
Browse files Browse the repository at this point in the history
  • Loading branch information
eltonvs committed Jun 21, 2023
1 parent 31effc6 commit 1d678eb
Showing 1 changed file with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ class ObdDeviceConnection(
suspend fun run(
command: ObdCommand,
useCache: Boolean = false,
delayTime: Long = 0
delayTime: Long = 0,
maxRetries: Int = 5,
): ObdResponse = runBlocking {
val obdRawResponse =
if (useCache && responseCache[command] != null) {
responseCache.getValue(command)
} else {
runCommand(command, delayTime).also {
runCommand(command, delayTime, maxRetries).also {
// Save response to cache
if (useCache) {
responseCache[command] = it
Expand All @@ -39,10 +40,10 @@ class ObdDeviceConnection(
command.handleResponse(obdRawResponse)
}

private suspend fun runCommand(command: ObdCommand, delayTime: Long): ObdRawResponse {
private suspend fun runCommand(command: ObdCommand, delayTime: Long, maxRetries: Int): ObdRawResponse {
var rawData = ""
val elapsedTime = measureTimeMillis {
sendCommand(command, delayTime)
sendCommand(command, delayTime, maxRetries)
rawData = readRawData()
}
return ObdRawResponse(rawData, elapsedTime)
Expand All @@ -58,14 +59,15 @@ class ObdDeviceConnection(
}
}

private suspend fun readRawData(): String = runBlocking {
private suspend fun readRawData(maxRetries: Int): String = runBlocking {
var b: Byte
var c: Char
val res = StringBuffer()
var retriesCount = 0

withContext(Dispatchers.IO) {
// read until '>' arrives OR end of stream reached (-1)
while (true) {
while (retriesCount <= maxRetries) {
if (inputStream.available() > 0) {
b = inputStream.read().toByte()
if (b < 0) {
Expand All @@ -77,6 +79,7 @@ class ObdDeviceConnection(
}
res.append(c)
} else {
retriesCount += 1
delay(500)
}
}
Expand Down

0 comments on commit 1d678eb

Please sign in to comment.