From 8bef914eac17f1da89cee3dd3349f3e45f6c8c6a Mon Sep 17 00:00:00 2001 From: Carlos Rafael Giani Date: Sun, 20 Nov 2022 19:55:04 +0100 Subject: [PATCH] combov2: Store battery and reservoir levels as plain properties This fixes https://github.com/dv1/AndroidAPS/issues/2 Signed-off-by: Carlos Rafael Giani --- .../plugins/pump/combov2/ComboV2Plugin.kt | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/pump/combov2/src/main/kotlin/info/nightscout/androidaps/plugins/pump/combov2/ComboV2Plugin.kt b/pump/combov2/src/main/kotlin/info/nightscout/androidaps/plugins/pump/combov2/ComboV2Plugin.kt index 9d7009a38a..e4faaeddeb 100644 --- a/pump/combov2/src/main/kotlin/info/nightscout/androidaps/plugins/pump/combov2/ComboV2Plugin.kt +++ b/pump/combov2/src/main/kotlin/info/nightscout/androidaps/plugins/pump/combov2/ComboV2Plugin.kt @@ -459,6 +459,7 @@ class ComboV2Plugin @Inject constructor ( ) pumpStatus = newPumpStatus + updateLevels() // Send the EventRefreshOverview to keep the overview fragment's content // up to date. Other actions like a CommandQueue.readStatus() call trigger @@ -725,20 +726,29 @@ class ComboV2Plugin @Inject constructor ( return activeBasalProfile?.get(currentHour)?.cctlBasalToIU() ?: 0.0 } - override val reservoirLevel: Double - get() = pumpStatus?.availableUnitsInReservoir?.toDouble() ?: 0.0 + // Store the levels as plain properties. That way, the last reported + // levels are shown on the UI even when the driver connects to the + // pump again and resets the current pump state. - override val batteryLevel: Int - // The Combo does not provide any numeric battery - // level, so we have to use some reasonable values - // based on the indicated battery state. - get() = when (pumpStatus?.batteryState) { - null, - BatteryState.NO_BATTERY -> 5 - BatteryState.LOW_BATTERY -> 25 - BatteryState.FULL_BATTERY -> 100 + override var reservoirLevel: Double = 0.0 + private set + override var batteryLevel: Int = 0 + private set + + private fun updateLevels() { + pumpStatus?.availableUnitsInReservoir?.let { + reservoirLevel = it.toDouble() } + pumpStatus?.batteryState?.let { + batteryLevel = when (it) { + BatteryState.NO_BATTERY -> 5 + BatteryState.LOW_BATTERY -> 25 + BatteryState.FULL_BATTERY -> 100 + } + } + } + override fun deliverTreatment(detailedBolusInfo: DetailedBolusInfo): PumpEnactResult { val oldInsulinAmount = detailedBolusInfo.insulin detailedBolusInfo.insulin = constraintChecker