From 8b3a32bb57cb47038abb213caa9c31b2d0f2dc4a Mon Sep 17 00:00:00 2001 From: Sam Spycher Date: Wed, 7 Oct 2020 22:02:28 +0200 Subject: [PATCH] Validate basal duration, fixes AAPS-Omnipod / AndroidAPS#13 Basal rate durations must be non-zero, and in the case of the Omnipod Pump or multiple of 30 minutes. - Introduce OmnipodConstants.BASAL_STEP_DURATION - throw IllegalArgumentException on validation failure in RateEntry.createEntries - return failed PumpEnactResult and explanatory comments on validation failure in OmnipodPumpPlugin.setTempBasalAbsolute --- .../plugins/pump/omnipod/OmnipodPumpPlugin.java | 6 ++++++ .../pump/omnipod/driver/definition/OmnipodConstants.java | 1 + .../omnipod/driver/definition/schedule/RateEntry.java | 9 +++++++++ 3 files changed, 16 insertions(+) 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 6e1c85314b..34b32fb77d 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 @@ -97,6 +97,8 @@ import info.nightscout.androidaps.utils.sharedPreferences.SP; import io.reactivex.disposables.CompositeDisposable; import io.reactivex.schedulers.Schedulers; +import static info.nightscout.androidaps.plugins.pump.omnipod.driver.definition.OmnipodConstants.BASAL_STEP_DURATION; + /** * Created by andy on 23.04.18. * @@ -537,6 +539,10 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface, durationInMinutes, Profile profile, boolean enforceNew) { aapsLogger.info(LTag.PUMP, "setTempBasalAbsolute: rate: {}, duration={}", absoluteRate, durationInMinutes); + if (durationInMinutes <= 0 || durationInMinutes % BASAL_STEP_DURATION.getStandardMinutes() != 0) { + return new PumpEnactResult(getInjector()).success(false).comment("TBR duration must be greater than zero and a multiple of " + BASAL_STEP_DURATION.getStandardMinutes() + " minutes."); + } + // read current TBR TemporaryBasal tbrCurrent = readTBR(); diff --git a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/definition/OmnipodConstants.java b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/definition/OmnipodConstants.java index b5b65f0f58..a9504e974f 100644 --- a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/definition/OmnipodConstants.java +++ b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/definition/OmnipodConstants.java @@ -14,6 +14,7 @@ public class OmnipodConstants { public static final double MAX_RESERVOIR_READING = 50.0; public static final double MAX_BOLUS = 30.0; public static final double MAX_BASAL_RATE = 30.0; + public static final Duration BASAL_STEP_DURATION = Duration.standardMinutes(30); public static final Duration MAX_TEMP_BASAL_DURATION = Duration.standardHours(12); public static final int DEFAULT_ADDRESS = 0xffffffff; diff --git a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/definition/schedule/RateEntry.java b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/definition/schedule/RateEntry.java index e3d5d7cf28..51e6f297d0 100644 --- a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/definition/schedule/RateEntry.java +++ b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/definition/schedule/RateEntry.java @@ -9,6 +9,8 @@ import info.nightscout.androidaps.plugins.pump.common.utils.ByteUtil; import info.nightscout.androidaps.plugins.pump.omnipod.driver.communication.message.IRawRepresentable; import info.nightscout.androidaps.plugins.pump.omnipod.driver.definition.OmnipodConstants; +import static info.nightscout.androidaps.plugins.pump.omnipod.driver.definition.OmnipodConstants.BASAL_STEP_DURATION; + public class RateEntry implements IRawRepresentable { private final double totalPulses; @@ -21,6 +23,13 @@ public class RateEntry implements IRawRepresentable { } public static List createEntries(double rate, Duration duration) { + if (Duration.ZERO.equals(duration)) { + throw new IllegalArgumentException("Duration may not be 0 minutes."); + } + if (duration.getStandardMinutes() % BASAL_STEP_DURATION.getStandardMinutes() != 0) { + throw new IllegalArgumentException("Duration must be a multiple of " + BASAL_STEP_DURATION.getStandardMinutes() + " minutes."); + } + List entries = new ArrayList<>(); int remainingSegments = (int) Math.round(duration.getStandardSeconds() / 1800.0); double pulsesPerSegment = (int) Math.round(rate / OmnipodConstants.POD_PULSE_SIZE) / 2.0;