From 94e6189ab32be348c11caf11977e652e1b08e271 Mon Sep 17 00:00:00 2001 From: jbr7rr <> Date: Tue, 27 Jun 2023 12:56:54 +0200 Subject: [PATCH] Add sendMessage retry counter, cleanup old comments --- .../nightscout/pump/medtrum/MedtrumPlugin.kt | 11 +++------- .../nightscout/pump/medtrum/MedtrumPump.kt | 4 ++-- .../comm/packets/NotificationPacket.kt | 5 ++--- .../pump/medtrum/services/MedtrumService.kt | 20 +++++++++++++------ .../comm/packets/NotificationPacketTest.kt | 1 - .../comm/packets/SynchronizePacketTest.kt | 1 - 6 files changed, 21 insertions(+), 21 deletions(-) diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPlugin.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPlugin.kt index 945478bfea..8700b0d8c6 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPlugin.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPlugin.kt @@ -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) } diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPump.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPump.kt index 40659c7bba..d64bce6135 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPump.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPump.kt @@ -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 diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/NotificationPacket.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/NotificationPacket.kt index 4784da2696..40305e4fe4 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/NotificationPacket.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/NotificationPacket.kt @@ -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 diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/MedtrumService.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/MedtrumService.kt index 4f27352c83..7856008f6e 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/MedtrumService.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/MedtrumService.kt @@ -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) { diff --git a/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/NotificationPacketTest.kt b/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/NotificationPacketTest.kt index 7316743155..13be194534 100644 --- a/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/NotificationPacketTest.kt +++ b/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/NotificationPacketTest.kt @@ -59,7 +59,6 @@ class NotificationPacketTest : MedtrumTestBase() { // Expected values assertEquals(167, medtrumPump.currentSequenceNumber) - // TODO: Test error notif on wrong patch id } @Test fun handleNotificationGivenBolusInProgressThenDataSaved() { diff --git a/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/SynchronizePacketTest.kt b/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/SynchronizePacketTest.kt index 3d9913aea8..6900f5a775 100644 --- a/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/SynchronizePacketTest.kt +++ b/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/SynchronizePacketTest.kt @@ -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() {