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 0cbb4f3d34..ffdc74eb19 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 @@ -7,6 +7,7 @@ import android.content.ServiceConnection; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; +import android.os.Looper; import android.os.SystemClock; import androidx.annotation.NonNull; @@ -34,9 +35,7 @@ import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.db.ExtendedBolus; -import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.db.TemporaryBasal; -import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.events.EventAppExit; import info.nightscout.androidaps.events.EventAppInitialized; import info.nightscout.androidaps.events.EventPreferenceChange; @@ -56,7 +55,6 @@ import info.nightscout.androidaps.plugins.general.actions.defs.CustomAction; import info.nightscout.androidaps.plugins.general.actions.defs.CustomActionType; import info.nightscout.androidaps.plugins.general.overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification; -import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress; import info.nightscout.androidaps.plugins.general.overview.notifications.Notification; import info.nightscout.androidaps.plugins.pump.common.data.TempBasalPair; import info.nightscout.androidaps.plugins.pump.common.defs.PumpType; @@ -140,9 +138,10 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface, private long nextPodCheck; private boolean sentIdToFirebase; private long lastConnectionTimeMillis; - private final Handler loopHandler = new Handler(); + private final Handler loopHandler = new Handler(Looper.getMainLooper()); private final Runnable statusChecker; + private OmnipodCommandType currentCommand; @Inject public OmnipodPumpPlugin( @@ -322,8 +321,11 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface, // 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()) { - if (activePlugin.getActiveTreatments().isTempBasalInProgress() && !aapsOmnipodManager.hasSuspendedFakeTbr()) { - aapsOmnipodManager.reportCancelledTbr(); + // Only report TBR cancellations if they haven't been explicitly requested + if (currentCommand != OmnipodCommandType.CANCEL_TEMPORARY_BASAL) { + if (activePlugin.getActiveTreatments().isTempBasalInProgress() && !aapsOmnipodManager.hasSuspendedFakeTbr()) { + aapsOmnipodManager.reportCancelledTbr(); + } } } } @@ -596,7 +598,7 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface, public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) { if (detailedBolusInfo.insulin == 0 && detailedBolusInfo.carbs == 0) { // neither carbs nor bolus requested - aapsLogger.error("deliverTreatment: Invalid input"); + aapsLogger.error("deliverTreatment: Invalid input: neither carbs nor insulin are set in treatment"); return new PumpEnactResult(getInjector()).success(false).enacted(false).bolusDelivered(0d).carbsDelivered(0d) .comment(getResourceHelper().gs(info.nightscout.androidaps.core.R.string.invalidinput)); } else if (detailedBolusInfo.insulin > 0) { @@ -606,15 +608,6 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface, // no bolus required, carb only treatment activePlugin.getActiveTreatments().addToHistoryTreatment(detailedBolusInfo, true); - // FIXME do we need this?? - EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.INSTANCE; - bolusingEvent.setT(new Treatment()); - bolusingEvent.getT().isSMB = detailedBolusInfo.isSMB; - bolusingEvent.setPercent(100); - rxBus.send(bolusingEvent); - - aapsLogger.debug(LTag.PUMP, "deliverTreatment: Carb only treatment."); - return new PumpEnactResult(getInjector()).success(true).enacted(true).bolusDelivered(0d) .carbsDelivered(detailedBolusInfo.carbs).comment(getResourceHelper().gs(info.nightscout.androidaps.core.R.string.common_resultok)); } @@ -643,7 +636,6 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface, if (tbrCurrent != null && !enforceNew) { if (Round.isSame(tbrCurrent.absoluteRate, absoluteRate)) { aapsLogger.info(LTag.PUMP, "setTempBasalAbsolute - No enforceNew and same rate. Exiting."); - rxBus.send(new EventRefreshOverview("Omnipod command: SetTemporaryBasal", false)); return new PumpEnactResult(getInjector()).success(true).enacted(false); } } @@ -665,23 +657,10 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface, if (tbrCurrent == null) { aapsLogger.info(LTag.PUMP, "cancelTempBasal - TBR already cancelled."); - rxBus.send(new EventRefreshOverview("Omnipod command: CancelTemporaryBasal", false)); return new PumpEnactResult(getInjector()).success(true).enacted(false); } - PumpEnactResult result = executeCommand(OmnipodCommandType.CANCEL_TEMPORARY_BASAL, aapsOmnipodManager::cancelTemporaryBasal); - - if (result.success) { - // TODO is this necessary? - TemporaryBasal tempBasal = new TemporaryBasal(getInjector()) // - .date(System.currentTimeMillis()) // - .duration(0) // - .source(Source.USER); - - activePlugin.getActiveTreatments().addToHistoryTempBasal(tempBasal); - } - - return result; + return executeCommand(OmnipodCommandType.CANCEL_TEMPORARY_BASAL, aapsOmnipodManager::cancelTemporaryBasal); } // TODO improve (i8n and more) @@ -934,17 +913,21 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface, } private T executeCommand(OmnipodCommandType commandType, Supplier supplier) { - aapsLogger.debug(LTag.PUMP, "Executing command: {}", commandType); + try { + currentCommand = commandType; + aapsLogger.debug(LTag.PUMP, "Executing command: {}", commandType); - rileyLinkUtil.getRileyLinkHistory().add(new RLHistoryItemOmnipod(getInjector(), commandType)); + rileyLinkUtil.getRileyLinkHistory().add(new RLHistoryItemOmnipod(getInjector(), commandType)); - T pumpEnactResult = supplier.get(); + T pumpEnactResult = supplier.get(); - rxBus.send(new EventRefreshOverview("Omnipod command: " + commandType.name(), false)); + rxBus.send(new EventRefreshOverview("Omnipod command: " + commandType.name(), false)); + rxBus.send(new EventOmnipodPumpValuesChanged()); - rxBus.send(new EventOmnipodPumpValuesChanged()); - - return pumpEnactResult; + return pumpEnactResult; + } finally { + currentCommand = null; + } } private boolean verifyPodAlertConfiguration() { diff --git a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/definition/OmnipodStorageKeys.java b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/definition/OmnipodStorageKeys.java index 272325ea90..9dae97c900 100644 --- a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/definition/OmnipodStorageKeys.java +++ b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/definition/OmnipodStorageKeys.java @@ -12,7 +12,7 @@ public class OmnipodStorageKeys { public static final int BOLUS_BEEPS_ENABLED = R.string.key_omnipod_bolus_beeps_enabled; public static final int SMB_BEEPS_ENABLED = R.string.key_omnipod_smb_beeps_enabled; public static final int TBR_BEEPS_ENABLED = R.string.key_omnipod_tbr_beeps_enabled; - public static final int SUSPEND_DELIVERY_BUTTON_ENABLED = R.string.key_omnipod_pulse_log_button_enabled; + public static final int SUSPEND_DELIVERY_BUTTON_ENABLED = R.string.key_omnipod_suspend_delivery_button_enabled; public static final int PULSE_LOG_BUTTON_ENABLED = R.string.key_omnipod_pulse_log_button_enabled; public static final int TIME_CHANGE_EVENT_ENABLED = R.string.key_omnipod_time_change_event_enabled; public static final int EXPIRATION_REMINDER_ENABLED = R.string.key_omnipod_expiration_reminder_enabled; diff --git a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/communication/action/BolusAction.java b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/communication/action/BolusAction.java index ed785add66..c87ccdb497 100644 --- a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/communication/action/BolusAction.java +++ b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/communication/action/BolusAction.java @@ -46,8 +46,8 @@ public class BolusAction implements OmnipodAction { podStateManager.getCurrentNonce(), bolusDeliverySchedule); BolusExtraCommand bolusExtraCommand = new BolusExtraCommand(units, timeBetweenPulses, acknowledgementBeep, completionBeep); - OmnipodMessage primeBolusMessage = new OmnipodMessage(podStateManager.getAddress(), + OmnipodMessage bolusMessage = new OmnipodMessage(podStateManager.getAddress(), Arrays.asList(setInsulinScheduleCommand, bolusExtraCommand), podStateManager.getMessageNumber()); - return communicationService.exchangeMessages(StatusResponse.class, podStateManager, primeBolusMessage); + return communicationService.exchangeMessages(StatusResponse.class, podStateManager, bolusMessage); } } diff --git a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/manager/AapsOmnipodManager.java b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/manager/AapsOmnipodManager.java index 9ce1e2a2a5..f8dafb3865 100644 --- a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/manager/AapsOmnipodManager.java +++ b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/manager/AapsOmnipodManager.java @@ -374,7 +374,7 @@ public class AapsOmnipodManager { sp.remove(OmnipodStorageKeys.Preferences.ACTIVE_BOLUS); - return new PumpEnactResult(injector).success(true).enacted(true).bolusDelivered(detailedBolusInfo.insulin); + return new PumpEnactResult(injector).success(true).enacted(true).carbsDelivered(detailedBolusInfo.carbs).bolusDelivered(detailedBolusInfo.insulin); } public PumpEnactResult cancelBolus() { @@ -464,13 +464,22 @@ public class AapsOmnipodManager { public PumpEnactResult cancelTemporaryBasal() { try { delegate.cancelTemporaryBasal(isTbrBeepsEnabled()); - addSuccessToHistory(PodHistoryEntryType.CANCEL_TEMPORARY_BASAL, null); } catch (Exception ex) { String comment = handleAndTranslateException(ex); addFailureToHistory(PodHistoryEntryType.CANCEL_TEMPORARY_BASAL, comment); return new PumpEnactResult(injector).success(false).enacted(false).comment(comment); } + long pumpId = addSuccessToHistory(PodHistoryEntryType.CANCEL_TEMPORARY_BASAL, null); + + TemporaryBasal tempBasal = new TemporaryBasal(injector) // + .date(System.currentTimeMillis()) // + .duration(0) // + .pumpId(pumpId) // + .source(Source.PUMP); + + activePlugin.getActiveTreatments().addToHistoryTempBasal(tempBasal); + return new PumpEnactResult(injector).success(true).enacted(true); } @@ -568,9 +577,25 @@ public class AapsOmnipodManager { return timeChangeEventEnabled; } - public void addBolusToHistory(DetailedBolusInfo detailedBolusInfo) { + public void addBolusToHistory(DetailedBolusInfo originalDetailedBolusInfo) { + DetailedBolusInfo detailedBolusInfo = originalDetailedBolusInfo.copy(); + long pumpId = addSuccessToHistory(detailedBolusInfo.date, PodHistoryEntryType.SET_BOLUS, detailedBolusInfo.insulin + ";" + detailedBolusInfo.carbs); detailedBolusInfo.pumpId = pumpId; + + if (detailedBolusInfo.carbs > 0 && detailedBolusInfo.carbTime > 0) { + // split out a separate carbs record without a pumpId + DetailedBolusInfo carbInfo = new DetailedBolusInfo(); + carbInfo.date = detailedBolusInfo.date + detailedBolusInfo.carbTime * 60L * 1000L; + carbInfo.carbs = detailedBolusInfo.carbs; + carbInfo.source = Source.USER; + activePlugin.getActiveTreatments().addToHistoryTreatment(carbInfo, false); + + // remove carbs from bolusInfo to not trigger any unwanted code paths in + // TreatmentsPlugin.addToHistoryTreatment() method + detailedBolusInfo.carbTime = 0; + detailedBolusInfo.carbs = 0; + } activePlugin.getActiveTreatments().addToHistoryTreatment(detailedBolusInfo, false); } @@ -634,7 +659,7 @@ public class AapsOmnipodManager { .date(time) // .duration(tempBasalPair.getDurationMinutes()) // .absolute(tempBasalPair.getInsulinRate()) // - .pumpId(pumpId) + .pumpId(pumpId) // .source(Source.PUMP); activePlugin.getActiveTreatments().addToHistoryTempBasal(tempStart); diff --git a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/ui/OmnipodFragment.kt b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/ui/OmnipodFragment.kt index 4aec08bb70..ca7382149e 100644 --- a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/ui/OmnipodFragment.kt +++ b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/ui/OmnipodFragment.kt @@ -4,6 +4,7 @@ import android.content.Intent import android.graphics.Color import android.os.Bundle import android.os.Handler +import android.os.Looper import android.text.TextUtils import android.view.LayoutInflater import android.view.View @@ -71,7 +72,7 @@ class OmnipodFragment : DaggerFragment() { private var disposables: CompositeDisposable = CompositeDisposable() - private val loopHandler = Handler() + private val loopHandler = Handler(Looper.getMainLooper()) private lateinit var refreshLoop: Runnable init {