From 3cc1113501ec08377f3b407cf8c8c8d2aad3b2fc Mon Sep 17 00:00:00 2001 From: Bart Sopers Date: Sun, 8 Dec 2019 16:38:15 +0100 Subject: [PATCH] Update last bolus and units remaning for Omnipod in PumpStatus --- app/build.gradle | 2 +- .../comm/OmnipodCommunicationService.java | 8 +-- .../omnipod/defs/state/PodSessionState.java | 7 ++- .../driver/comm/AapsOmnipodManager.java | 49 ++++++++++++++----- .../pump/omnipod/util/OmnipodUtil.java | 5 +- 5 files changed, 50 insertions(+), 21 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 4447768343..55ea40dc6e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -111,7 +111,7 @@ android { versionCode 1500 version "omnipod-0.1-SNAPSHOT" buildConfigField "String", "VERSION", '"' + version + '"' - buildConfigField "String", "DEV_VERSION", "2.6-dev" + buildConfigField "String", "DEV_VERSION", '"2.6-dev"' buildConfigField "String", "BUILDVERSION", '"' + generateGitBuild() + '-' + generateDate() + '"' buildConfigField "String", "REMOTE", '"' + generateGitRemote() + '"' buildConfigField "String", "HEAD", '"' + generateGitBuild() + '"' diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/OmnipodCommunicationService.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/OmnipodCommunicationService.java index d00a0f177b..917a4e4bce 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/OmnipodCommunicationService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/OmnipodCommunicationService.java @@ -160,14 +160,14 @@ public class OmnipodCommunicationService extends RileyLinkCommunicationManager { encodedMessage = ByteUtil.substring(encodedMessage, encodedMessageInPacket.length, encodedMessage.length - encodedMessageInPacket.length); firstPacket = false; - // If this is not the last packet, the message wasn't fully sent, - // so it's impossible for the pod to have received the message - boolean isCertainFailure = encodedMessage.length > 0; - try { // We actually ignore previous (ack) responses if it was not last packet to send response = exchangePackets(podState, packet); } catch (Exception ex) { + // If this is not the last packet, the message wasn't fully sent, + // so it's impossible for the pod to have received the message + boolean isCertainFailure = encodedMessage.length > 0; + OmnipodException newException; if (ex instanceof OmnipodException) { newException = (OmnipodException) ex; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/defs/state/PodSessionState.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/defs/state/PodSessionState.java index 0f6e9d883d..d229da252e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/defs/state/PodSessionState.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/defs/state/PodSessionState.java @@ -33,6 +33,7 @@ public class PodSessionState extends PodState { private final FirmwareVersion pmVersion; private final int lot; private final int tid; + private Double reservoirLevel; private boolean suspended; private DateTimeZone timeZone; @@ -98,7 +99,6 @@ public class PodSessionState extends PodState { return expiresAt == null ? "???" : DateUtil.dateAndTimeString(expiresAt.toDate()); } - public FirmwareVersion getPiVersion() { return piVersion; } @@ -115,6 +115,10 @@ public class PodSessionState extends PodState { return tid; } + public Double getReservoirLevel() { + return reservoirLevel; + } + public synchronized void resyncNonce(int syncWord, int sentNonce, int sequenceNumber) { int sum = (sentNonce & 0xFFFF) + OmniCRC.crc16lookup[sequenceNumber] @@ -225,6 +229,7 @@ public class PodSessionState extends PodState { suspended = (statusResponse.getDeliveryStatus() == DeliveryStatus.SUSPENDED); activeAlerts = statusResponse.getAlerts(); lastDeliveryStatus = statusResponse.getDeliveryStatus(); + reservoirLevel = statusResponse.getReservoirLevel(); store(); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/comm/AapsOmnipodManager.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/comm/AapsOmnipodManager.java index 176d9c71f7..c4e801de4c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/comm/AapsOmnipodManager.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/comm/AapsOmnipodManager.java @@ -9,7 +9,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; +import java.util.Date; import java.util.List; +import java.util.Objects; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; @@ -37,6 +39,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.defs.state.PodSessionStat import info.nightscout.androidaps.plugins.pump.omnipod.driver.OmnipodPumpStatus; import info.nightscout.androidaps.plugins.pump.omnipod.driver.db.PodDbEntryType; import info.nightscout.androidaps.plugins.pump.omnipod.events.EventOmnipodAcknowledgeAlertsChanged; +import info.nightscout.androidaps.plugins.pump.omnipod.events.EventOmnipodPumpValuesChanged; import info.nightscout.androidaps.plugins.pump.omnipod.exception.ActionInitializationException; import info.nightscout.androidaps.plugins.pump.omnipod.exception.CommandInitializationException; import info.nightscout.androidaps.plugins.pump.omnipod.exception.CommunicationException; @@ -63,17 +66,21 @@ public class AapsOmnipodManager implements OmnipodCommunicationManagerInterface private static AapsOmnipodManager instance; private OmnipodPumpStatus pumpStatus; + private Date lastBolusTime; + private Double lastBolusUnits; + public static AapsOmnipodManager getInstance() { return instance; } - public AapsOmnipodManager(OmnipodCommunicationService communicationService, PodSessionState podState, OmnipodPumpStatus pumpStatus) { + public AapsOmnipodManager(OmnipodCommunicationService communicationService, PodSessionState podState, OmnipodPumpStatus _pumpStatus) { delegate = new OmnipodManager(communicationService, podState, podSessionState -> { // Handle pod state changes OmnipodUtil.setPodSessionState(podSessionState); if (pumpStatus != null) { + // Update active alerts if (podSessionState.hasActiveAlerts()) { List alerts = translateActiveAlerts(podSessionState); String alertsText = TextUtils.join("\n", alerts); @@ -91,12 +98,27 @@ public class AapsOmnipodManager implements OmnipodCommunicationManagerInterface sendEvent(new EventOmnipodAcknowledgeAlertsChanged()); } } + + // Update other info: last bolus, units remaining + if (!Objects.equals(lastBolusTime, pumpStatus.lastBolusTime) // + || !Objects.equals(lastBolusUnits, pumpStatus.lastBolusAmount) // + || !isReservoirStatusUpToDate(pumpStatus, podSessionState.getReservoirLevel())) { + pumpStatus.lastBolusTime = lastBolusTime; + pumpStatus.lastBolusAmount = lastBolusUnits; + pumpStatus.reservoirRemainingUnits = podSessionState.getReservoirLevel() == null ? 75.0 : podSessionState.getReservoirLevel(); + sendEvent(new EventOmnipodPumpValuesChanged()); + } } }); - this.pumpStatus = pumpStatus; + this.pumpStatus = _pumpStatus; instance = this; } + private static boolean isReservoirStatusUpToDate(OmnipodPumpStatus pumpStatus, Double unitsRemaining) { + double expectedUnitsRemaining = unitsRemaining == null ? 75.0 : unitsRemaining; + return Math.abs(expectedUnitsRemaining - pumpStatus.reservoirRemainingUnits) < 0.000001; + } + @NotNull private List translateActiveAlerts(PodSessionState podSessionState) { List alerts = new ArrayList<>(); @@ -190,6 +212,7 @@ public class AapsOmnipodManager implements OmnipodCommunicationManagerInterface boolean beepsEnabled = isSmb ? isSmbBeepsEnabled() : isBolusBeepsEnabled(); + Date bolusStarted; try { bolusCommandResult = delegate.bolus(units, beepsEnabled, beepsEnabled, isSmb ? null : (estimatedUnitsDelivered, percentage) -> { @@ -198,27 +221,27 @@ public class AapsOmnipodManager implements OmnipodCommunicationManagerInterface progressUpdateEvent.setPercent(percentage); sendEvent(progressUpdateEvent); }); + + bolusStarted = new Date(); } catch (Exception ex) { String comment = handleAndTranslateException(ex); return new PumpEnactResult().success(false).enacted(false).comment(comment); } - if (OmnipodManager.CommandDeliveryStatus.UNCERTAIN_FAILURE.equals(bolusCommandResult.getCommandDeliveryStatus()) /* && !isSmb */) { + if (OmnipodManager.CommandDeliveryStatus.UNCERTAIN_FAILURE.equals(bolusCommandResult.getCommandDeliveryStatus()) && !isSmb /* TODO or should we also warn for SMB? */) { // TODO notify user about uncertain failure ---> we're unsure whether or not the bolus has been delivered // For safety reasons, we should treat this as a bolus that has been delivered, in order to prevent insulin overdose } - double unitsDelivered = units; + // Wait for the bolus to finish + OmnipodManager.BolusDeliveryResult bolusDeliveryResult = + bolusCommandResult.getDeliveryResultSubject().blockingGet(); - try { - // Wait for the bolus to finish - OmnipodManager.BolusDeliveryResult bolusDeliveryResult = - bolusCommandResult.getDeliveryResultSubject().blockingGet(); - unitsDelivered = bolusDeliveryResult.getUnitsDelivered(); - } catch (Exception ex) { - if (loggingEnabled()) { - LOG.debug("Ignoring failed status response for bolus completion verification", ex); - } + double unitsDelivered = bolusDeliveryResult.getUnitsDelivered(); + + if (pumpStatus != null && !isSmb) { + lastBolusTime = bolusStarted; + lastBolusUnits = unitsDelivered; } return new PumpEnactResult().success(true).enacted(true).bolusDelivered(unitsDelivered); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/util/OmnipodUtil.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/util/OmnipodUtil.java index 337e7c389c..d4d4075ff0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/util/OmnipodUtil.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/util/OmnipodUtil.java @@ -32,6 +32,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.defs.PodDeviceState; import info.nightscout.androidaps.plugins.pump.omnipod.defs.state.PodSessionState; import info.nightscout.androidaps.plugins.pump.omnipod.events.EventOmnipodDeviceStatusChange; import info.nightscout.androidaps.plugins.pump.omnipod.driver.OmnipodPumpStatus; +import info.nightscout.androidaps.plugins.pump.omnipod.events.EventOmnipodPumpValuesChanged; import info.nightscout.androidaps.plugins.pump.omnipod.service.RileyLinkOmnipodService; import info.nightscout.androidaps.utils.OKDialog; @@ -171,8 +172,8 @@ public class OmnipodUtil extends RileyLinkUtil { } - public static void setPodDeviceState(PodDeviceState podSessionState) { - omnipodPumpStatus.podDeviceState = podSessionState; + public static void setPodDeviceState(PodDeviceState podDeviceState) { + omnipodPumpStatus.podDeviceState = podDeviceState; }