From f0765a6704990dae96a70e5a0559d5fc1b6ee412 Mon Sep 17 00:00:00 2001 From: Bart Sopers Date: Fri, 20 Nov 2020 18:06:18 +0100 Subject: [PATCH] Technical improvements on Omnipod uncertain TBR recovery --- .../pump/omnipod/OmnipodPumpPlugin.java | 103 ++++++++++-------- .../driver/manager/PodStateManager.java | 19 +++- .../EventOmnipodUncertainTbrRecovered.kt | 8 ++ .../omnipod/manager/AapsPodStateManager.java | 5 + 4 files changed, 87 insertions(+), 48 deletions(-) create mode 100644 omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/event/EventOmnipodUncertainTbrRecovered.kt diff --git a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/OmnipodPumpPlugin.java b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/OmnipodPumpPlugin.java index 976db6a168..369c40f8e9 100644 --- a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/OmnipodPumpPlugin.java +++ b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/OmnipodPumpPlugin.java @@ -83,6 +83,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.event.EventOmnipodActiveA import info.nightscout.androidaps.plugins.pump.omnipod.event.EventOmnipodFaultEventChanged; import info.nightscout.androidaps.plugins.pump.omnipod.event.EventOmnipodPumpValuesChanged; import info.nightscout.androidaps.plugins.pump.omnipod.event.EventOmnipodTbrChanged; +import info.nightscout.androidaps.plugins.pump.omnipod.event.EventOmnipodUncertainTbrRecovered; import info.nightscout.androidaps.plugins.pump.omnipod.manager.AapsOmnipodManager; import info.nightscout.androidaps.plugins.pump.omnipod.queue.command.CommandAcknowledgeAlerts; import info.nightscout.androidaps.plugins.pump.omnipod.queue.command.CommandHandleTimeChange; @@ -155,6 +156,7 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface, private final Handler loopHandler = new Handler(Looper.getMainLooper()); private final Runnable statusChecker; + private boolean isSetTempBasalRunning; private boolean isCancelTempBasalRunning; @Inject @@ -286,7 +288,12 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface, disposables.add(rxBus .toObservable(EventOmnipodTbrChanged.class) .observeOn(Schedulers.io()) - .subscribe(event -> updateAapsTbr(), fabricPrivacy::logException) + .subscribe(event -> handleCancelledTbr(), fabricPrivacy::logException) + ); + disposables.add(rxBus + .toObservable(EventOmnipodUncertainTbrRecovered.class) + .observeOn(Schedulers.io()) + .subscribe(event -> handleUncertainTbrRecovery(), fabricPrivacy::logException) ); disposables.add(rxBus .toObservable(EventOmnipodActiveAlertsChanged.class) @@ -351,17 +358,49 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface, return rileyLinkServiceData.rileyLinkServiceState.isReady(); } - private void updateAapsTbr() { - // As per the characteristics of the Omnipod, we only know whether or not a TBR is currently active - // But it doesn't tell us the duration or amount, so we can only update TBR status in AAPS if - // The pod is not running a TBR, while AAPS thinks it is - if (!podStateManager.isTempBasalRunning()) { - // Only report TBR cancellations if they haven't been explicitly requested - if (!isCancelTempBasalRunning) { - if (activePlugin.getActiveTreatments().isTempBasalInProgress() && !aapsOmnipodManager.hasSuspendedFakeTbr()) { - aapsOmnipodManager.reportCancelledTbr(); - } + private void handleCancelledTbr() { + // Only report TBR cancellations if they haven't been explicitly requested + if (isCancelTempBasalRunning) { + return; + } + if (!podStateManager.isTempBasalRunning() && activePlugin.getActiveTreatments().isTempBasalInProgress() && !aapsOmnipodManager.hasSuspendedFakeTbr()) { + aapsOmnipodManager.reportCancelledTbr(); + } + } + + private void handleUncertainTbrRecovery() { + // Ignore changes in certainty during tbr commands; these are normal + if (isSetTempBasalRunning || isCancelTempBasalRunning) { + return; + } + + TemporaryBasal tempBasal = activePlugin.getActiveTreatments().getTempBasalFromHistory(System.currentTimeMillis()); + + if (podStateManager.isTempBasalRunning() && tempBasal == null) { + if (podStateManager.hasTempBasal()) { + aapsLogger.warn(LTag.PUMP, "Registering TBR that AAPS was unaware of"); + long pumpId = aapsOmnipodManager.addTbrSuccessToHistory(podStateManager.getTempBasalStartTime().getMillis(), + new TempBasalPair(podStateManager.getTempBasalAmount(), false, (int) podStateManager.getTempBasalDuration().getStandardMinutes())); + + TemporaryBasal temporaryBasal = new TemporaryBasal(getInjector()) // + .absolute(podStateManager.getTempBasalAmount()) // + .duration((int) podStateManager.getTempBasalDuration().getStandardMinutes()) + .date(podStateManager.getTempBasalStartTime().getMillis()) // + .source(Source.PUMP) // + .pumpId(pumpId); + + activePlugin.getActiveTreatments().addToHistoryTempBasal(temporaryBasal); + } else { + // Not sure what's going on. Notify the user + aapsLogger.error(LTag.PUMP, "Unknown TBR in both Pod state and AAPS"); + rxBus.send(new EventNewNotification(new Notification(Notification.OMNIPOD_PUMP_ALARM, resourceHelper.gs(R.string.omnipod_error_tbr_running_but_aaps_not_aware), Notification.NORMAL).sound(R.raw.boluserror))); } + } else if (!podStateManager.isTempBasalRunning() && tempBasal != null) { + aapsLogger.warn(LTag.PUMP, "Invalidating AAPS TBR that actually hadn't succeeded"); + + tempBasal.isValid = false; + activePlugin.getActiveTreatments().addToHistoryTempBasal(tempBasal); + handleCancelledTbr(); } } @@ -524,39 +563,7 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface, } private PumpEnactResult getPodStatus() { - PumpEnactResult getStatusResult = executeCommand(OmnipodCommandType.GET_POD_STATUS, aapsOmnipodManager::getPodStatus); - - // bit hacky... - if (getStatusResult.success && !activePlugin.getActiveTreatments().isTempBasalInProgress() && podStateManager.isTempBasalRunning()) { - if (podStateManager.hasTempBasal()) { - aapsLogger.warn(LTag.PUMP, "Registering TBR that AAPS was unaware of"); - - long pumpId = aapsOmnipodManager.addTbrSuccessToHistory(podStateManager.getTempBasalStartTime().getMillis(), - new TempBasalPair(podStateManager.getTempBasalAmount(), false, (int) podStateManager.getTempBasalDuration().getStandardMinutes())); - - TemporaryBasal temporaryBasal = new TemporaryBasal(getInjector()) // - .absolute(podStateManager.getTempBasalAmount()) // - .duration((int) podStateManager.getTempBasalDuration().getStandardMinutes()) - .date(podStateManager.getTempBasalStartTime().getMillis()) // - .source(Source.PUMP) // - .pumpId(pumpId); - - activePlugin.getActiveTreatments().addToHistoryTempBasal(temporaryBasal); - } else { - // Not sure what's going on. Cancel TBR on the Pod - aapsLogger.warn(LTag.PUMP, "Cancelling TBR because AAPS is not aware of any running TBR"); - - PumpEnactResult cancelTbrResult = executeCommand(OmnipodCommandType.CANCEL_TEMPORARY_BASAL, aapsOmnipodManager::cancelTemporaryBasal); - if (cancelTbrResult.success) { - aapsLogger.info(LTag.PUMP, "Successfully cancelled TBR because AAPS was not aware of any running TBR"); - } else { - aapsLogger.error(LTag.PUMP, "Failed to cancel TBR because AAPS was not aware of any running TBR"); - rxBus.send(new EventNewNotification(new Notification(Notification.OMNIPOD_PUMP_ALARM, resourceHelper.gs(R.string.omnipod_error_tbr_running_but_aaps_not_aware), Notification.NORMAL).sound(R.raw.boluserror))); - } - } - } - - return getStatusResult; + return executeCommand(OmnipodCommandType.GET_POD_STATUS, aapsOmnipodManager::getPodStatus); } @NonNull @@ -661,7 +668,13 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface, } } - PumpEnactResult result = executeCommand(OmnipodCommandType.SET_TEMPORARY_BASAL, () -> aapsOmnipodManager.setTemporaryBasal(new TempBasalPair(absoluteRate, false, durationInMinutes))); + isSetTempBasalRunning = true; + PumpEnactResult result; + try { + result = executeCommand(OmnipodCommandType.SET_TEMPORARY_BASAL, () -> aapsOmnipodManager.setTemporaryBasal(new TempBasalPair(absoluteRate, false, durationInMinutes))); + } finally { + isSetTempBasalRunning = false; + } aapsLogger.info(LTag.PUMP, "setTempBasalAbsolute - setTBR. Response: " + result.success); diff --git a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/manager/PodStateManager.java b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/manager/PodStateManager.java index 70be6b5c1e..d3519fd392 100644 --- a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/manager/PodStateManager.java +++ b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/manager/PodStateManager.java @@ -567,12 +567,18 @@ public abstract class PodStateManager { podState.setPodProgressStatus(status.getPodProgressStatus()); podState.setTimeActive(status.getTimeActive()); if (!status.getDeliveryStatus().isTbrRunning()) { - clearTempBasal(false); + if (podState.isTempBasalCertain()) { + clearTempBasal(); // Triggers onTbrChanged when appropriate + } else { + // Don't trigger onTbrChanged as we will trigger onUncertainTbrRecovered below + podState.setTempBasalStartTime(null); + podState.setTempBasalAmount(null); + podState.setTempBasalDuration(null); + } } - podState.setLastUpdatedFromResponse(DateTime.now()); if (!podState.isTempBasalCertain()) { podState.setTempBasalCertain(true); - onTbrChanged(); + onUncertainTbrRecovered(); } if (!podState.isBasalCertain()) { podState.setBasalCertain(true); @@ -587,6 +593,8 @@ public abstract class PodStateManager { } } } + + podState.setLastUpdatedFromResponse(DateTime.now()); }); } @@ -595,6 +603,11 @@ public abstract class PodStateManager { // Can be overridden in subclasses } + protected void onUncertainTbrRecovered() { + // Deliberately left empty + // Can be overridden in subclasses + } + protected void onActiveAlertsChanged() { // Deliberately left empty // Can be overridden in subclasses diff --git a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/event/EventOmnipodUncertainTbrRecovered.kt b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/event/EventOmnipodUncertainTbrRecovered.kt new file mode 100644 index 0000000000..f2c4dc855a --- /dev/null +++ b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/event/EventOmnipodUncertainTbrRecovered.kt @@ -0,0 +1,8 @@ +package info.nightscout.androidaps.plugins.pump.omnipod.event + +import info.nightscout.androidaps.events.Event + +/** + * Created by andy on 04.06.2018. + */ +class EventOmnipodUncertainTbrRecovered : Event() \ No newline at end of file diff --git a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/manager/AapsPodStateManager.java b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/manager/AapsPodStateManager.java index 551206190f..6ebaa1d3b9 100644 --- a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/manager/AapsPodStateManager.java +++ b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/manager/AapsPodStateManager.java @@ -10,6 +10,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.driver.manager.PodStateMa import info.nightscout.androidaps.plugins.pump.omnipod.event.EventOmnipodActiveAlertsChanged; import info.nightscout.androidaps.plugins.pump.omnipod.event.EventOmnipodFaultEventChanged; import info.nightscout.androidaps.plugins.pump.omnipod.event.EventOmnipodTbrChanged; +import info.nightscout.androidaps.plugins.pump.omnipod.event.EventOmnipodUncertainTbrRecovered; import info.nightscout.androidaps.utils.sharedPreferences.SP; @Singleton @@ -34,6 +35,10 @@ public class AapsPodStateManager extends PodStateManager { sp.putString(OmnipodStorageKeys.Preferences.POD_STATE, podState); } + @Override protected void onUncertainTbrRecovered() { + rxBus.send(new EventOmnipodUncertainTbrRecovered()); + } + @Override protected void onTbrChanged() { rxBus.send(new EventOmnipodTbrChanged()); }