Add sendMessage retry counter, cleanup old comments

This commit is contained in:
jbr7rr 2023-06-27 12:56:54 +02:00
parent ef2cc13dd8
commit 94e6189ab3
6 changed files with 21 additions and 21 deletions

View file

@ -276,16 +276,16 @@ import kotlin.math.round
if (connectionOK
&& medtrumPump.tempBasalInProgress
&& Math.abs(medtrumPump.tempBasalAbsoluteRate - pumpRate) <= 0.05
/*&& Math.abs(medtrumPump.tempBasalRemainingMinutes - durationInMinutes) <= 5*/) {
) {
return PumpEnactResult(injector).success(true).enacted(true).duration(/*medtrumPump.tempBasalRemainingMinutes*/durationInMinutes).absolute(medtrumPump.tempBasalAbsoluteRate)
return PumpEnactResult(injector).success(true).enacted(true).duration(durationInMinutes).absolute(medtrumPump.tempBasalAbsoluteRate)
.isPercent(false)
.isTempCancel(false)
} else {
aapsLogger.error(
LTag.PUMP,
"setTempBasalAbsolute failed, connectionOK: $connectionOK, tempBasalInProgress: ${medtrumPump.tempBasalInProgress}, tempBasalAbsoluteRate: ${medtrumPump.tempBasalAbsoluteRate}"
) //, tempBasalRemainingMinutes: ${medtrumPump.tempBasalRemainingMinutes}")
)
return PumpEnactResult(injector).success(false).enacted(false).comment("Medtrum setTempBasalAbsolute failed")
}
}
@ -453,11 +453,6 @@ import kotlin.math.round
}
override fun deactivate(): PumpEnactResult {
// if (!isInitialized()) {
// val result = PumpEnactResult(injector).success(false)
// result.comment = "pump not initialized"
// return result
// }
val connectionOK = medtrumService?.deactivatePatch() ?: false
return PumpEnactResult(injector).success(connectionOK)
}

View file

@ -207,9 +207,9 @@ class MedtrumPump @Inject constructor(
get() = _pumpSN
var needTimeUpdate = false
var lastTimeReceivedFromPump = 0L // Time in ms! // TODO: Consider removing as is not used?
var lastTimeReceivedFromPump = 0L // Time in ms!
var suspendTime = 0L // Time in ms!
var patchAge = 0L // Time in seconds?! // TODO: Not used
var patchAge = 0L // Time in seconds?! // As reported by pump, not used (yet)
// bolus status
var bolusingTreatment: EventOverviewBolusProgress.Treatment? = null // actually delivered treatment

View file

@ -99,7 +99,7 @@ class NotificationPacket(val injector: HasAndroidInjector) {
aapsLogger.debug(LTag.PUMPCOMM, "Normal bolus notification received")
var bolusData = data.copyOfRange(offset, offset + 1).toInt()
var bolusType = bolusData and 0x7F
val bolusCompleted: Boolean = ((bolusData shr 7) and 0x01) != 0 // TODO: Check for other flags here :)
val bolusCompleted: Boolean = ((bolusData shr 7) and 0x01) != 0
var bolusDelivered = data.copyOfRange(offset + 1, offset + 3).toInt() * 0.05
aapsLogger.debug(LTag.PUMPCOMM, "Bolus type: $bolusType, bolusData: $bolusData bolus completed: $bolusCompleted, bolus delivered: $bolusDelivered")
medtrumPump.handleBolusStatusUpdate(bolusType, bolusCompleted, bolusDelivered)
@ -171,8 +171,7 @@ class NotificationPacket(val injector: HasAndroidInjector) {
}
val patchId = data.copyOfRange(offset + 2, offset + 4).toLong()
if (patchId != medtrumPump.patchId) {
aapsLogger.error(LTag.PUMPCOMM, "handleMaskedMessage: WTF? We got wrong patch id!")
// TODO: We should terminate session or stop patch here? or at least throw error? THis can be thrown during activation process though
aapsLogger.warn(LTag.PUMPCOMM, "handleMaskedMessage: We got wrong patch id!")
}
aapsLogger.debug(LTag.PUMPCOMM, "Last known sequence number: ${medtrumPump.currentSequenceNumber}, patch id: ${patchId}")
offset += 4

View file

@ -599,6 +599,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
protected var responseHandled = false
protected var responseSuccess = false
protected var sendRetryCounter = 0
open fun onEnter() {}
open fun onIndication(data: ByteArray) {
@ -632,7 +633,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
toState(IdleState())
return false
}
SystemClock.sleep(100)
SystemClock.sleep(25)
}
aapsLogger.debug(LTag.PUMPCOMM, "Medtrum Service State responseHandled: $responseHandled responseSuccess: $responseSuccess")
return responseSuccess
@ -640,8 +641,16 @@ class MedtrumService : DaggerService(), BLECommCallback {
fun onSendMessageError(reason: String) {
aapsLogger.warn(LTag.PUMPCOMM, "onSendMessageError: " + this.toString() + "reason: $reason")
responseHandled = true
responseSuccess = false
// Retry 3 times
if (sendRetryCounter < 3) {
sendRetryCounter++
mPacket?.getRequest()?.let { bleComm.sendMessage(it) }
} else {
responseHandled = true
responseSuccess = false
bleComm.disconnect("onSendMessageError")
toState(IdleState())
}
}
}
@ -718,10 +727,10 @@ class MedtrumService : DaggerService(), BLECommCallback {
// Succes!
responseHandled = true
responseSuccess = true
// TODO Get device type and SN
// Place holder, not really used (yet)
val deviceType = (mPacket as GetDeviceTypePacket).deviceType
val deviceSN = (mPacket as GetDeviceTypePacket).deviceSN
aapsLogger.debug(LTag.PUMPCOMM, "GetDeviceTypeState: deviceType: $deviceType deviceSN: $deviceSN") // TODO remove me later
aapsLogger.debug(LTag.PUMPCOMM, "GetDeviceTypeState: deviceType: $deviceType deviceSN: $deviceSN")
toState(GetTimeState())
} else if (mPacket?.failed == true) {
// Failure
@ -761,7 +770,6 @@ class MedtrumService : DaggerService(), BLECommCallback {
medtrumPump.lastTimeReceivedFromPump
)
)
// TODO: Setting time cancels any TBR, so we need to handle that and cancel? or let AAPS handle time syncs?
toState(SetTimeState())
}
} else if (mPacket?.failed == true) {

View file

@ -59,7 +59,6 @@ class NotificationPacketTest : MedtrumTestBase() {
// Expected values
assertEquals(167, medtrumPump.currentSequenceNumber)
// TODO: Test error notif on wrong patch id
}
@Test fun handleNotificationGivenBolusInProgressThenDataSaved() {

View file

@ -57,7 +57,6 @@ class SynchronizePacketTest : MedtrumTestBase() {
assertEquals(true, result)
assertEquals(false, packet.failed)
assertEquals(state, packet.medtrumPump.pumpState.state)
// TODO: Maybe test cutting behavoir
}
@Test fun handleResponseGivenResponseWhenMessageTooShortThenResultFalse() {