From 0c38967c00acb7456c1a070cc8d254664b413191 Mon Sep 17 00:00:00 2001 From: jbr7rr <> Date: Wed, 16 Aug 2023 19:29:47 +0200 Subject: [PATCH 1/2] Medtrum: Check for bolus in progress --- .../nightscout/pump/medtrum/MedtrumPump.kt | 30 ++++++++--------- .../medtrum/comm/packets/SynchronizePacket.kt | 6 ++-- .../pump/medtrum/services/MedtrumService.kt | 33 ++++++++++++------- 3 files changed, 38 insertions(+), 31 deletions(-) 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 fb1ce89d05..84b2c40150 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 @@ -498,25 +498,25 @@ class MedtrumPump @Inject constructor( fun alarmStateToString(alarmState: AlarmState): String { val stringId = when (alarmState) { - AlarmState.NONE -> R.string.alarm_none - AlarmState.PUMP_LOW_BATTERY -> R.string.alarm_pump_low_battery - AlarmState.PUMP_LOW_RESERVOIR -> R.string.alarm_pump_low_reservoir - AlarmState.PUMP_EXPIRES_SOON -> R.string.alarm_pump_expires_soon - AlarmState.LOW_BG_SUSPENDED -> R.string.alarm_low_bg_suspended - AlarmState.LOW_BG_SUSPENDED2 -> R.string.alarm_low_bg_suspended2 + AlarmState.NONE -> R.string.alarm_none + AlarmState.PUMP_LOW_BATTERY -> R.string.alarm_pump_low_battery + AlarmState.PUMP_LOW_RESERVOIR -> R.string.alarm_pump_low_reservoir + AlarmState.PUMP_EXPIRES_SOON -> R.string.alarm_pump_expires_soon + AlarmState.LOW_BG_SUSPENDED -> R.string.alarm_low_bg_suspended + AlarmState.LOW_BG_SUSPENDED2 -> R.string.alarm_low_bg_suspended2 AlarmState.AUTO_SUSPENDED -> R.string.alarm_auto_suspended AlarmState.HOURLY_MAX_SUSPENDED -> R.string.alarm_hourly_max_suspended AlarmState.DAILY_MAX_SUSPENDED -> R.string.alarm_daily_max_suspended AlarmState.SUSPENDED -> R.string.alarm_suspended - AlarmState.PAUSED -> R.string.alarm_paused - AlarmState.OCCLUSION -> R.string.alarm_occlusion - AlarmState.EXPIRED -> R.string.alarm_expired - AlarmState.RESERVOIR_EMPTY -> R.string.alarm_reservoir_empty - AlarmState.PATCH_FAULT -> R.string.alarm_patch_fault - AlarmState.PATCH_FAULT2 -> R.string.alarm_patch_fault2 - AlarmState.BASE_FAULT -> R.string.alarm_base_fault - AlarmState.BATTERY_OUT -> R.string.alarm_battery_out - AlarmState.NO_CALIBRATION -> R.string.alarm_no_calibration + AlarmState.PAUSED -> R.string.alarm_paused + AlarmState.OCCLUSION -> R.string.alarm_occlusion + AlarmState.EXPIRED -> R.string.alarm_expired + AlarmState.RESERVOIR_EMPTY -> R.string.alarm_reservoir_empty + AlarmState.PATCH_FAULT -> R.string.alarm_patch_fault + AlarmState.PATCH_FAULT2 -> R.string.alarm_patch_fault2 + AlarmState.BASE_FAULT -> R.string.alarm_base_fault + AlarmState.BATTERY_OUT -> R.string.alarm_battery_out + AlarmState.NO_CALIBRATION -> R.string.alarm_no_calibration } return rh.gs(stringId) } diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/SynchronizePacket.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/SynchronizePacket.kt index c7fd95acea..f3c4dc60bd 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/SynchronizePacket.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/SynchronizePacket.kt @@ -49,14 +49,12 @@ class SynchronizePacket(injector: HasAndroidInjector) : MedtrumPacket(injector) aapsLogger.debug(LTag.PUMPCOMM, "SynchronizePacket: fieldMask: $fieldMask") } - // Remove bolus fields from fieldMask if fields are present (we sync bolus trough other commands) + // Remove extended bolus field from fieldMask if field is present (extended bolus is not supported) if (fieldMask and MASK_SUSPEND != 0) { offset += 4 // If field is present, skip 4 bytes } if (fieldMask and MASK_NORMAL_BOLUS != 0) { - aapsLogger.debug(LTag.PUMPCOMM, "SynchronizePacket: Normal bolus present removing from fieldMask") - fieldMask = fieldMask and MASK_NORMAL_BOLUS.inv() - syncData = syncData.copyOfRange(0, offset) + syncData.copyOfRange(offset + 3, syncData.size) + offset += 3 // If field is present, skip 3 bytes } if (fieldMask and MASK_EXTENDED_BOLUS != 0) { aapsLogger.debug(LTag.PUMPCOMM, "SynchronizePacket: Extended bolus present removing from fieldMask") 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 da05d95f01..d90c51fa5a 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 @@ -287,20 +287,22 @@ class MedtrumService : DaggerService(), BLECommCallback { } fun setBolus(detailedBolusInfo: DetailedBolusInfo, t: EventOverviewBolusProgress.Treatment): Boolean { - if (!isConnected) return false - if (BolusProgressData.stopPressed) return false + if (!isConnected) { + aapsLogger.warn(LTag.PUMPCOMM, "Pump not connected, not setting bolus") + return false + } + if (BolusProgressData.stopPressed) { + aapsLogger.warn(LTag.PUMPCOMM, "Bolus stop pressed, not setting bolus") + return false + } + if (!medtrumPump.bolusDone) { + aapsLogger.warn(LTag.PUMPCOMM, "Bolus already in progress, not setting new one") + return false + } + val insulin = detailedBolusInfo.insulin - val bolusStart = System.currentTimeMillis() - - medtrumPump.bolusDone = false - medtrumPump.bolusingTreatment = t - medtrumPump.bolusAmountToBeDelivered = insulin - medtrumPump.bolusStopped = false - medtrumPump.bolusProgressLastTimeStamp = bolusStart - if (insulin > 0) { - val result = sendPacketAndGetResponse(SetBolusPacket(injector, insulin)) - if (!result) { + if (!sendPacketAndGetResponse(SetBolusPacket(injector, insulin))) { aapsLogger.error(LTag.PUMPCOMM, "Failed to set bolus") commandQueue.loadEvents(null) // make sure if anything is delivered (which is highly unlikely at this point) we get it t.insulin = 0.0 @@ -312,6 +314,13 @@ class MedtrumService : DaggerService(), BLECommCallback { return false } + val bolusStart = System.currentTimeMillis() + medtrumPump.bolusDone = false + medtrumPump.bolusingTreatment = t + medtrumPump.bolusAmountToBeDelivered = insulin + medtrumPump.bolusStopped = false + medtrumPump.bolusProgressLastTimeStamp = bolusStart + detailedBolusInfo.timestamp = bolusStart // Make sure the timestamp is set to the start of the bolus detailedBolusInfoStorage.add(detailedBolusInfo) // will be picked up on reading history // Sync the initial bolus From 6a922bf535c08eec1e669f0935ced3d5e1e55f15 Mon Sep 17 00:00:00 2001 From: jbr7rr <> Date: Thu, 17 Aug 2023 09:06:25 +0200 Subject: [PATCH 2/2] Medtrum: Handle turning BLE off during connection --- .../pump/medtrum/services/MedtrumService.kt | 15 +++++++++++++-- .../ui/viewmodel/MedtrumOverviewViewModel.kt | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) 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 d90c51fa5a..75b36b0097 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 @@ -164,8 +164,19 @@ class MedtrumService : DaggerService(), BLECommCallback { if (currentState is IdleState) { medtrumPump.connectionState = ConnectionState.CONNECTING return bleComm.connect(from, medtrumPump.pumpSN) + } else if (currentState is ReadyState) { + aapsLogger.error(LTag.PUMPCOMM, "Connect attempt when in ReadyState from: $from") + if (isConnected) { + aapsLogger.debug(LTag.PUMP, "connect: already connected") + return true + } else { + aapsLogger.debug(LTag.PUMP, "connect: not connected, resetting state and trying to connect") + toState(IdleState()) + medtrumPump.connectionState = ConnectionState.CONNECTING + return bleComm.connect(from, medtrumPump.pumpSN) + } } else { - aapsLogger.error(LTag.PUMPCOMM, "Connect attempt when in non Idle state from: $from") + aapsLogger.error(LTag.PUMPCOMM, "Connect attempt when in state: $currentState from: $from") return false } } @@ -650,7 +661,7 @@ class MedtrumService : DaggerService(), BLECommCallback { result = currentState.waitForResponse(timeout) SystemClock.sleep(100) } else { - aapsLogger.error(LTag.PUMPCOMM, "Send packet attempt when in non Ready state") + aapsLogger.error(LTag.PUMPCOMM, "Send packet attempt when in state: $currentState") } return result } diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/viewmodel/MedtrumOverviewViewModel.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/viewmodel/MedtrumOverviewViewModel.kt index 1cd1de8a95..c1b170ad76 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/viewmodel/MedtrumOverviewViewModel.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/viewmodel/MedtrumOverviewViewModel.kt @@ -103,7 +103,7 @@ class MedtrumOverviewViewModel @Inject constructor( ConnectionState.DISCONNECTING -> { _bleStatus.postValue("{fa-bluetooth-b spin}") - _canDoRefresh.postValue(false) + _canDoRefresh.postValue(true) } } updateGUI()