add retries when sending commands

This commit is contained in:
Andrei Vereha 2021-03-21 15:12:33 +01:00
parent 7be0afda27
commit ae08e43109
4 changed files with 44 additions and 16 deletions

View file

@ -106,7 +106,7 @@ class MessageIO(private val aapsLogger: AAPSLogger, private val bleIO: BleIO) {
continue continue
} }
} }
throw MessageIOException("Ran out retries trying to receive a packet. $maxTries") throw TimeoutException()
} }
private fun readReset() { private fun readReset() {

View file

@ -0,0 +1,3 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.session
class CertainFailureException(msg: String) : Exception(msg)

View file

@ -13,6 +13,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.b
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.NakResponse import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.NakResponse
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.Response import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.Response
import info.nightscout.androidaps.utils.extensions.toHex import info.nightscout.androidaps.utils.extensions.toHex
import java.util.concurrent.TimeoutException
class Session( class Session(
private val aapsLogger: AAPSLogger, private val aapsLogger: AAPSLogger,
@ -32,21 +33,40 @@ class Session(
fun sendCommand(cmd: Command): Response { fun sendCommand(cmd: Command): Response {
sessionKeys.msgSequenceNumber++ sessionKeys.msgSequenceNumber++
aapsLogger.debug(LTag.PUMPBTCOMM, "Sending command: ${cmd.encoded.toHex()} in packet $cmd") aapsLogger.debug(LTag.PUMPBTCOMM, "Sending command: ${cmd.encoded.toHex()} in packet $cmd")
var tries = 0
val msg = getCmdMessage(cmd) var certainFailure = true
aapsLogger.debug(LTag.PUMPBTCOMM, "Sending command(wrapped): ${msg.payload.toHex()}") for (i in 0..MAX_TRIES) {
msgIO.sendMessage(msg) try {
val msg = getCmdMessage(cmd)
val responseMsg = msgIO.receiveMessage() aapsLogger.debug(LTag.PUMPBTCOMM, "Sending command(wrapped): ${msg.payload.toHex()}")
val decrypted = enDecrypt.decrypt(responseMsg) msgIO.sendMessage(msg)
aapsLogger.debug(LTag.PUMPBTCOMM, "Received response: $decrypted") } catch (e: TimeoutException) {
val response = parseResponse(decrypted) aapsLogger.info(LTag.PUMPBTCOMM,"Exception trying to send command: $e. Try: $i/$MAX_TRIES")
} // TODO filter out certain vs uncertain errors
sessionKeys.msgSequenceNumber++ }
val ack = getAck(responseMsg) certainFailure = false
aapsLogger.debug(LTag.PUMPBTCOMM, "Sending ACK: ${ack.payload.toHex()} in packet $ack") var response: Response?= null
msgIO.sendMessage(ack) for (i in 0..MAX_TRIES) {
return response try {
val responseMsg = msgIO.receiveMessage()
val decrypted = enDecrypt.decrypt(responseMsg)
aapsLogger.debug(LTag.PUMPBTCOMM, "Received response: $decrypted")
response = parseResponse(decrypted)
sessionKeys.msgSequenceNumber++
val ack = getAck(responseMsg)
aapsLogger.debug(LTag.PUMPBTCOMM, "Sending ACK: ${ack.payload.toHex()} in packet $ack")
msgIO.sendMessage(ack)
} catch (e: TimeoutException) {
aapsLogger.info(LTag.PUMPBTCOMM,"Exception trying to send command: $e. Try: $i/$MAX_TRIES")
}
}
response?.let{
return it
}
if (certainFailure) {
throw CertainFailureException("Could not send command")
}
throw UncertainFailureException("Possible failure to send commnd")
} }
private fun parseResponse(decrypted: MessagePacket): Response { private fun parseResponse(decrypted: MessagePacket): Response {
@ -95,5 +115,7 @@ class Session(
private const val COMMAND_PREFIX = "S0.0=" private const val COMMAND_PREFIX = "S0.0="
private const val COMMAND_SUFFIX = ",G0.0" private const val COMMAND_SUFFIX = ",G0.0"
private const val RESPONSE_PREFIX = "0.0=" private const val RESPONSE_PREFIX = "0.0="
private const val MAX_TRIES = 4
} }
} }

View file

@ -0,0 +1,3 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.session
class UncertainFailureException(msg: String) : Exception(msg)