From 40487ffb06babc04df42656b131c09c71c4e6461 Mon Sep 17 00:00:00 2001 From: Bart Sopers Date: Sun, 1 Dec 2019 00:34:16 +0100 Subject: [PATCH] Add pod expiration date and expose calculateBolusDuration method in OmnipodManager --- .../pump/omnipod/comm/OmnipodManager.java | 20 +++++++----- .../omnipod/defs/state/PodSessionState.java | 32 +++++++++++++------ .../pump/omnipod/util/OmnipodConst.java | 2 +- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/OmnipodManager.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/OmnipodManager.java index 5193b10d77..c33d6d09f6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/OmnipodManager.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/OmnipodManager.java @@ -250,10 +250,6 @@ public class OmnipodManager { return podState == null ? "null" : podState.toString(); } - private Duration calculateBolusDuration(double units, double deliveryRate) { - return Duration.standardSeconds((long) Math.ceil(units / deliveryRate)); - } - private void executeDelayed(Runnable r, Duration timeout) { ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1); scheduledExecutorService.schedule(r, timeout.getMillis(), TimeUnit.MILLISECONDS); @@ -318,6 +314,18 @@ public class OmnipodManager { return CommandVerificationResult.SUCCESS; } + private boolean isLoggingEnabled() { + return L.isEnabled(L.PUMP); + } + + public static Duration calculateBolusDuration(double units) { + return calculateBolusDuration(units, OmnipodConst.POD_BOLUS_DELIVERY_RATE); + } + + private static Duration calculateBolusDuration(double units, double deliveryRate) { + return Duration.standardSeconds((long) Math.ceil(units / deliveryRate)); + } + public static boolean isCertainFailure(Exception ex) { return ex instanceof OmnipodException && ((OmnipodException) ex).isCertainFailure(); } @@ -327,8 +335,4 @@ public class OmnipodManager { CERTAIN_FAILURE, UNCERTAIN_FAILURE } - - private boolean isLoggingEnabled() { - return L.isEnabled(L.PUMP); - } } 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 6bd82435bc..854be9cea6 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 @@ -3,6 +3,7 @@ package info.nightscout.androidaps.plugins.pump.omnipod.defs.state; import com.google.gson.Gson; import org.joda.time.DateTime; +import org.joda.time.DateTimeComparator; import org.joda.time.DateTimeZone; import org.joda.time.Duration; @@ -22,11 +23,13 @@ import info.nightscout.androidaps.plugins.pump.omnipod.events.EventOmnipodDevice import info.nightscout.androidaps.plugins.pump.omnipod.util.OmniCRC; import info.nightscout.androidaps.plugins.pump.omnipod.util.OmnipodConst; import info.nightscout.androidaps.plugins.pump.omnipod.util.OmnipodUtil; +import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.SP; public class PodSessionState extends PodState { private final Map configuredAlerts; - private final DateTime activatedAt; + private DateTime activatedAt; + private DateTime expiresAt; private final FirmwareVersion piVersion; private final FirmwareVersion pmVersion; private final int lot; @@ -80,6 +83,15 @@ public class PodSessionState extends PodState { return activatedAt; } + public DateTime getExpiresAt() { + return expiresAt; + } + + public String getExpiryDateAsString() { + return expiresAt == null ? "???" : DateUtil.dateAndTimeString(expiresAt.toDate()); + } + + public FirmwareVersion getPiVersion() { return piVersion; } @@ -133,7 +145,7 @@ public class PodSessionState extends PodState { } public boolean hasActiveAlerts() { - return activeAlerts != null; + return activeAlerts != null && activeAlerts.size() > 0; } public AlertSet getActiveAlerts() { @@ -194,6 +206,15 @@ public class PodSessionState extends PodState { @Override public void updateFromStatusResponse(StatusResponse statusResponse) { + DateTime activatedAtCalculated = getTime().minus(statusResponse.getTimeActive()); + if(activatedAt == null) { + activatedAt = activatedAtCalculated; + } + DateTime expiresAtCalculated = activatedAtCalculated.plus(OmnipodConst.NOMINAL_POD_LIFE); + if(expiresAt == null || expiresAtCalculated.isBefore(expiresAt) || expiresAtCalculated.isAfter(expiresAt.plusMinutes(1))) { + expiresAt = expiresAtCalculated; + } + suspended = (statusResponse.getDeliveryStatus() == DeliveryStatus.SUSPENDED); activeAlerts = statusResponse.getAlerts(); lastDeliveryStatus = statusResponse.getDeliveryStatus(); @@ -206,13 +227,6 @@ public class PodSessionState extends PodState { OmnipodUtil.setPodSessionState(this); } - public String getExpiryDateAsString() { - // TODO - return "???"; - } - - - @Override public String toString() { return "PodSessionState{" + diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/util/OmnipodConst.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/util/OmnipodConst.java index e284093b28..287505d001 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/util/OmnipodConst.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/util/OmnipodConst.java @@ -36,9 +36,9 @@ public class OmnipodConst { public static final int DEFAULT_ADDRESS = 0xffffffff; public static final Duration SERVICE_DURATION = Duration.standardHours(80); - public static final Duration EXPIRATION_ALERT_WINDOW = Duration.standardHours(2); public static final Duration EXPIRATION_ADVISORY_WINDOW = Duration.standardHours(2); public static final Duration END_OF_SERVICE_IMMINENT_WINDOW = Duration.standardHours(1); + public static final Duration NOMINAL_POD_LIFE = SERVICE_DURATION.minus(END_OF_SERVICE_IMMINENT_WINDOW).minus(EXPIRATION_ADVISORY_WINDOW); public static final double POD_PRIME_BOLUS_UNITS = 2.6; public static final double POD_CANNULA_INSERTION_BOLUS_UNITS = 0.5;