From 8e2fe7eaec53d3a76e81734c3d80d40a9913fd3c Mon Sep 17 00:00:00 2001 From: Jamorham Date: Fri, 2 Feb 2018 15:09:20 +0000 Subject: [PATCH 1/8] Insight ConstraintsInterface and Mstatus response type --- .../PumpInsight/InsightPumpAsyncAdapter.java | 27 ++-- .../PumpInsight/InsightPumpPlugin.java | 116 ++++++++++++++---- .../plugins/PumpInsight/Mstatus.java | 50 ++++++++ .../events/EventInsightPumpCallback.java | 3 +- 4 files changed, 151 insertions(+), 45 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/Mstatus.java diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpAsyncAdapter.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpAsyncAdapter.java index e28fd91707..6f48934059 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpAsyncAdapter.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpAsyncAdapter.java @@ -56,7 +56,7 @@ public class InsightPumpAsyncAdapter { } // blocking call to wait for result callback - Cstatus busyWaitForCommandResult(final UUID uuid, long wait_time) { + private Cstatus busyWaitForCommandInternal(final UUID uuid, long wait_time) { final PowerManager.WakeLock wl = getWakeLock("insight-wait-cmd", 60000); try { log("busy wait for command " + uuid); @@ -82,25 +82,14 @@ public class InsightPumpAsyncAdapter { } } - // commend field preparation for results - String getCommandComment(final UUID uuid) { - if (commandResults.containsKey(uuid)) { - if (commandResults.get(uuid).success) { - return "OK"; - } else { - return commandResults.get(uuid).message; - } - } else { - return "Unknown reference"; - } + // wait for and then package result, cleanup and return + Mstatus busyWaitForCommandResult(final UUID uuid, long wait_time) { + final Mstatus mstatus = new Mstatus(); + mstatus.cstatus = busyWaitForCommandInternal(uuid, wait_time); + mstatus.event = commandResults.get(uuid); + commandResults.remove(uuid); + return mstatus; } - int getResponseID(UUID uuid) { - if (checkCommandResult(uuid) == Cstatus.SUCCESS) { - return commandResults.get(uuid).response_id; - } else { - return -2; // invalid - } - } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java index 18ac83f261..fa84960717 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java @@ -23,6 +23,7 @@ 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.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; @@ -69,7 +70,7 @@ import static info.nightscout.androidaps.plugins.PumpInsight.utils.Helpers.round * */ -public class InsightPumpPlugin implements PluginBase, PumpInterface { +public class InsightPumpPlugin implements PluginBase, PumpInterface, ConstraintsInterface { private static final long BUSY_WAIT_TIME = 20000; static Integer batteryPercent = 0; @@ -202,7 +203,9 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface { @Override public boolean isEnabled(int type) { - return type == PUMP && fragmentEnabled; + if (type == PluginBase.PUMP) return fragmentEnabled; + else if (type == PluginBase.CONSTRAINTS) return fragmentEnabled; + return false; } @Override @@ -400,10 +403,11 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface { if (cmd == null) { return pumpEnactFailure(); } - final Cstatus cs = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME); - result.success = cs.success(); - if (cs.success()) { - detailedBolusInfo.pumpId = getRecordUniqueID(async.getResponseID(cmd)); + final Mstatus ms = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME); + + result.success = ms.success(); + if (ms.success()) { + detailedBolusInfo.pumpId = getRecordUniqueID(ms.getResponseID()); } } else { result.success = true; // always true with carb only treatments @@ -440,7 +444,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface { return; } - final Cstatus cs = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME); + final Mstatus cs = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME); log("Got command status: " + cs); } @@ -458,14 +462,14 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface { return pumpEnactFailure(); } - Cstatus cs = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME); - log("Got command status: " + cs); + Mstatus ms = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME); + log("Got command status: " + ms); PumpEnactResult pumpEnactResult = new PumpEnactResult().enacted(true).isPercent(false).duration(durationInMinutes); pumpEnactResult.absolute = absoluteRate; // TODO get converted value? - pumpEnactResult.success = cs.success(); + pumpEnactResult.success = ms.success(); pumpEnactResult.isTempCancel = false; // do we test this here? - pumpEnactResult.comment = async.getCommandComment(cmd); + pumpEnactResult.comment = ms.getCommandComment(); if (pumpEnactResult.success) { // create log entry @@ -501,14 +505,14 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface { return pumpEnactFailure(); } - Cstatus cs = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME); - log("Got command status: " + cs); + final Mstatus ms = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME); + log("Got command status: " + ms); PumpEnactResult pumpEnactResult = new PumpEnactResult().enacted(true).isPercent(true).duration(durationInMinutes); pumpEnactResult.percent = percent; - pumpEnactResult.success = cs.success(); + pumpEnactResult.success = ms.success(); pumpEnactResult.isTempCancel = percent == 100; // 100% temp basal is a cancellation - pumpEnactResult.comment = async.getCommandComment(cmd); + pumpEnactResult.comment = ms.getCommandComment(); if (pumpEnactResult.success) { // create log entry @@ -543,7 +547,13 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface { final UUID cmd; if (fauxTBRcancel) { - final int faux_percent = 90; + final TemporaryBasal activeTemp = MainApp.getConfigBuilder().getTempBasalFromHistory(System.currentTimeMillis()); + // I'm not totally sure whether based on the times that this cancellation will occur + // whether the logic should actually be inverted as we are reversing a decision. + // I believe it also affects at most 2.5% of an hourly basal rate and the pump only + // delivers basal every 3 minutes when > 0.19U per hour, below that unspecified. + final int faux_percent = (activeTemp == null) ? 90 : (activeTemp.percentRate > 100) ? 110 : 90; + final int faux_duration = 15; cmd = aSyncTaskRunner(new SetTBRTaskRunner(connector.getServiceConnector(), faux_percent, 15), "Faux Cancel TBR - setting " + faux_percent + "%" + " " + faux_duration + "m"); } else { @@ -555,7 +565,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface { // TODO isn't conditional on one apparently being in progress only the history change boolean enacted = false; - final Cstatus cs = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME); + final Mstatus ms = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME); if (MainApp.getConfigBuilder().isTempBasalInProgress()) { enacted = true; @@ -571,7 +581,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface { connector.requestHistorySync(5000); connector.tryToGetPumpStatusAgain(); - return new PumpEnactResult().success(cs.success()).enacted(true).isTempCancel(true); + return new PumpEnactResult().success(ms.success()).enacted(true).isTempCancel(true); } @@ -588,12 +598,12 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface { return pumpEnactFailure(); } - final Cstatus cs = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME); - log("Got command status: " + cs); + final Mstatus ms = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME); + log("Got command status: " + ms); PumpEnactResult pumpEnactResult = new PumpEnactResult().enacted(true).bolusDelivered(insulin).duration(durationInMinutes); - pumpEnactResult.success = cs.success(); - pumpEnactResult.comment = async.getCommandComment(cmd); + pumpEnactResult.success = ms.success(); + pumpEnactResult.comment = ms.getCommandComment(); if (pumpEnactResult.success) { // create log entry @@ -602,7 +612,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface { extendedBolus.insulin = insulin; extendedBolus.durationInMinutes = durationInMinutes; extendedBolus.source = Source.USER; - extendedBolus.pumpId = getRecordUniqueID(async.getResponseID(cmd)); + extendedBolus.pumpId = getRecordUniqueID(ms.getResponseID()); MainApp.getConfigBuilder().addToHistoryExtendedBolus(extendedBolus); } @@ -630,7 +640,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface { return pumpEnactFailure(); } - final Cstatus cs = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME); + final Mstatus ms = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME); if (MainApp.getConfigBuilder().isInHistoryExtendedBoluslInProgress()) { ExtendedBolus exStop = new ExtendedBolus(System.currentTimeMillis()); @@ -646,7 +656,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface { connector.requestHistorySync(5000); connector.tryToGetPumpStatusAgain(); - return new PumpEnactResult().success(cs.success()).enacted(true); + return new PumpEnactResult().success(ms.success()).enacted(true); } @@ -874,6 +884,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface { @Override public void onResult(Object o) { log(name + " success"); + event.response_object = o; if (o instanceof BolusMessage) { event.response_id = ((BolusMessage)o).getBolusId(); } @@ -911,6 +922,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface { @Override public void onResult(Object o) { log(name + " success"); + event.response_object = o; event.success = true; pushCallbackEvent(event); } @@ -936,5 +948,59 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface { return new PumpEnactResult().success(false).enacted(false); } + // Constraints + + @Override + public boolean isLoopEnabled() { + return true; + } + + @Override + public boolean isClosedModeEnabled() { + return true; + } + + @Override + public boolean isAutosensModeEnabled() { + return true; + } + + @Override + public boolean isAMAModeEnabled() { + return true; + } + + @Override + public boolean isSMBModeEnabled() { + return true; + } + + @Override + public Double applyBasalConstraints(Double absoluteRate) { + return Math.min(absoluteRate, 25); // Maximum pump can support + } + + @Override + public Integer applyBasalConstraints(Integer percentRate) { + return Math.min(percentRate, pumpDescription.maxTempPercent); + } + + @Override + public Double applyBolusConstraints(Double insulin) { + // TODO we could check what the current max is set on the pump and use that information here + // Pump can be reconfigured up to 50U max + return Math.min(insulin, 25); + } + + @Override + public Integer applyCarbsConstraints(Integer carbs) { + return carbs; + } + + @Override + public Double applyMaxIOBConstraints(Double maxIob) { + return maxIob; + } + } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/Mstatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/Mstatus.java new file mode 100644 index 0000000000..aceb172914 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/Mstatus.java @@ -0,0 +1,50 @@ +package info.nightscout.androidaps.plugins.PumpInsight; + +import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightPumpCallback; + +/** + * Created by jamorham on 01/02/2018. + * + * Encapsulates results from commands + */ + +class Mstatus { + + Cstatus cstatus = Cstatus.UNKNOWN; + EventInsightPumpCallback event; + + // comment field preparation for results + String getCommandComment() { + if (success()) { + return "OK"; + } else { + return (event == null) ? "EVENT IS NULL" : event.message; + } + } + + boolean success() { + return cstatus.success(); + } + + int getResponseID() { + if (success()) { + return event.response_id; + } else { + return -2; // invalid + } + } + + Object getResponseObject() { + if (success()) { + return event.response_object; + } else { + return null; + } + } + + @Override + public String toString() { + return cstatus + " " + event; + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/events/EventInsightPumpCallback.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/events/EventInsightPumpCallback.java index baa13922f5..964270cb45 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/events/EventInsightPumpCallback.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/events/EventInsightPumpCallback.java @@ -13,6 +13,7 @@ public class EventInsightPumpCallback extends Event { public boolean success = false; public String message = null; public int response_id = -1; + public Object response_object = null; public EventInsightPumpCallback() { request_uuid = UUID.randomUUID(); @@ -20,7 +21,7 @@ public class EventInsightPumpCallback extends Event { @Override public String toString() { - return "Event: " + request_uuid + " success: " + success + " msg: " + message; + return "Event: " + request_uuid + " success: " + success + " msg: " + message + " Object: " + response_object; } } From 501ca5c597911ab6d4bbe701902ae169bd67b162 Mon Sep 17 00:00:00 2001 From: Jamorham Date: Sun, 4 Feb 2018 12:57:10 +0000 Subject: [PATCH 2/8] Fix for some TBR cancels getting through --- .../PumpInsight/InsightPumpPlugin.java | 29 ++++++++++++++----- .../plugins/PumpInsight/Mstatus.java | 2 +- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java index fa84960717..5dd449f8f1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java @@ -499,6 +499,12 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints @Override public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, boolean enforceNew) { log("Set TBR %"); + + if (percent == 100) { + // This would cause a cancel if a tbr is in progress so treat as a cancel + return cancelTempBasal(false); + } + final UUID cmd = aSyncTaskRunner(new SetTBRTaskRunner(connector.getServiceConnector(), percent, durationInMinutes), "Set TBR " + percent + "%" + " " + durationInMinutes + "m"); if (cmd == null) { @@ -787,11 +793,14 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints (batteryPercent < 70 ? (StatusItem.Highlight.BAD) : StatusItem.Highlight.NOTICE) : StatusItem.Highlight.NORMAL) : StatusItem.Highlight.GOOD)); l.add(new StatusItem(gs(R.string.pump_reservoir_label), reservoirInUnits + "U")); - - if (statusResult.getCurrentTBRMessage().getPercentage() != 100) { - l.add(new StatusItem(gs(R.string.insight_active_tbr), statusResult.getCurrentTBRMessage().getPercentage() + "% " + gs(R.string.with) + " " - + Helpers.qs(statusResult.getCurrentTBRMessage().getLeftoverTime() - offset_minutes, 0) - + " " + gs(R.string.insight_min_left), StatusItem.Highlight.NOTICE)); + try { + if (statusResult.getCurrentTBRMessage().getPercentage() != 100) { + l.add(new StatusItem(gs(R.string.insight_active_tbr), statusResult.getCurrentTBRMessage().getPercentage() + "% " + gs(R.string.with) + " " + + Helpers.qs(statusResult.getCurrentTBRMessage().getLeftoverTime() - offset_minutes, 0) + + " " + gs(R.string.insight_min_left), StatusItem.Highlight.NOTICE)); + } + } catch (NullPointerException e) { + // currentTBRMessage may be null } } @@ -805,9 +814,13 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints } if (statusResult != null) { - statusActiveBolus(statusResult.getActiveBolusesMessage().getBolus1(), offset_minutes, l); - statusActiveBolus(statusResult.getActiveBolusesMessage().getBolus2(), offset_minutes, l); - statusActiveBolus(statusResult.getActiveBolusesMessage().getBolus3(), offset_minutes, l); + try { + statusActiveBolus(statusResult.getActiveBolusesMessage().getBolus1(), offset_minutes, l); + statusActiveBolus(statusResult.getActiveBolusesMessage().getBolus2(), offset_minutes, l); + statusActiveBolus(statusResult.getActiveBolusesMessage().getBolus3(), offset_minutes, l); + } catch (NullPointerException e) { + // getActiveBolusesMessage() may be null + } } if (MainApp.getConfigBuilder().isInHistoryExtendedBoluslInProgress()) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/Mstatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/Mstatus.java index aceb172914..57e6da7f2a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/Mstatus.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/Mstatus.java @@ -18,7 +18,7 @@ class Mstatus { if (success()) { return "OK"; } else { - return (event == null) ? "EVENT IS NULL" : event.message; + return (event == null) ? "EVENT DATA IS NULL - ERROR OR FIREWALL ENABLED?" : event.message; } } From 1d169bc27f479c53c495d4eb7185f11720c48b95 Mon Sep 17 00:00:00 2001 From: Jamorham Date: Sun, 4 Feb 2018 19:14:02 +0000 Subject: [PATCH 3/8] Better calculation for Absolute basal from Tebbe --- .../PumpInsight/InsightPumpPlugin.java | 59 ++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java index 5dd449f8f1..67cea869e2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java @@ -454,6 +454,61 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, boolean enforceNew) { absoluteRate = Helpers.roundDouble(absoluteRate, 3); log("Set TBR absolute: " + absoluteRate); + final double base_basal = getBaseBasalRate(); + if (base_basal == 0) { + log("Base basal rate appears to be zero!"); + return pumpEnactFailure(); + } + int amount = (int) Math.round(100d / base_basal * absoluteRate); + log("Calculated requested rate: " + absoluteRate + " base rate: " + base_basal + " percentage: " + amount + "%"); + amount = (int) Math.round(((double) amount) / 10d) * 10; + log("Calculated final rate: " + amount + "%"); + if (amount > 250) amount = 250; + + final SetTBRTaskRunner task = new SetTBRTaskRunner(connector.getServiceConnector(), amount, durationInMinutes); + final UUID cmd = aSyncTaskRunner(task, "Set TBR abs: " + absoluteRate + " " + durationInMinutes + "m"); + + if (cmd == null) { + return pumpEnactFailure(); + } + + Mstatus ms = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME); + log("Got command status: " + ms); + + PumpEnactResult pumpEnactResult = new PumpEnactResult().enacted(true).isPercent(false).duration(durationInMinutes); + pumpEnactResult.absolute = absoluteRate; // TODO get converted value? + pumpEnactResult.success = ms.success(); + pumpEnactResult.isTempCancel = false; // do we test this here? + pumpEnactResult.comment = ms.getCommandComment(); + + if (pumpEnactResult.success) { + // create log entry + final TemporaryBasal tempBasal = new TemporaryBasal(); + tempBasal.date = System.currentTimeMillis(); + tempBasal.isAbsolute = true; + tempBasal.absoluteRate = base_basal / 100d * ((double) amount); // is this the correct figure to use? + tempBasal.durationInMinutes = durationInMinutes; + tempBasal.source = Source.USER; + MainApp.getConfigBuilder().addToHistoryTempBasal(tempBasal); + } + + if (Config.logPumpComm) + log.debug("Setting temp basal absolute: " + pumpEnactResult.success); + + lastDataTime = new Date(); + + updateGui(); + + connector.requestHistorySync(5000); + connector.tryToGetPumpStatusAgain(); + + return pumpEnactResult; + } + + //@Override + public PumpEnactResult setTempBasalAbsoluteOld(Double absoluteRate, Integer durationInMinutes, boolean enforceNew) { + absoluteRate = Helpers.roundDouble(absoluteRate, 3); + log("Set TBR absolute: " + absoluteRate); final AbsoluteTBRTaskRunner task = new AbsoluteTBRTaskRunner(connector.getServiceConnector(), absoluteRate, durationInMinutes); final UUID cmd = aSyncTaskRunner(task, "Set TBR abs: " + absoluteRate + " " + durationInMinutes + "m"); @@ -500,11 +555,13 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, boolean enforceNew) { log("Set TBR %"); + percent = (int) Math.round(((double) percent) / 10d) * 10; if (percent == 100) { // This would cause a cancel if a tbr is in progress so treat as a cancel return cancelTempBasal(false); } + final UUID cmd = aSyncTaskRunner(new SetTBRTaskRunner(connector.getServiceConnector(), percent, durationInMinutes), "Set TBR " + percent + "%" + " " + durationInMinutes + "m"); if (cmd == null) { @@ -899,7 +956,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints log(name + " success"); event.response_object = o; if (o instanceof BolusMessage) { - event.response_id = ((BolusMessage)o).getBolusId(); + event.response_id = ((BolusMessage) o).getBolusId(); } event.success = true; pushCallbackEvent(event); From 44ba10e00ccd7220bb190c03e4ef1a214cfb0137 Mon Sep 17 00:00:00 2001 From: Jamorham Date: Mon, 5 Feb 2018 11:33:00 +0000 Subject: [PATCH 4/8] Insight treat 100% TBR from absolute as cancel --- .../androidaps/plugins/PumpInsight/InsightPumpPlugin.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java index 67cea869e2..0e5b37291f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java @@ -463,6 +463,11 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints log("Calculated requested rate: " + absoluteRate + " base rate: " + base_basal + " percentage: " + amount + "%"); amount = (int) Math.round(((double) amount) / 10d) * 10; log("Calculated final rate: " + amount + "%"); + + if (amount == 100) { + return cancelTempBasal(false); + } + if (amount > 250) amount = 250; final SetTBRTaskRunner task = new SetTBRTaskRunner(connector.getServiceConnector(), amount, durationInMinutes); From 6a9b293e4946a3abf655bb134a50199a79c24133 Mon Sep 17 00:00:00 2001 From: Jamorham Date: Mon, 5 Feb 2018 14:39:55 +0000 Subject: [PATCH 5/8] Improved TBR cancelling --- .../PumpInsight/InsightPumpPlugin.java | 16 ++---- .../connector/SetTBRTaskRunner.java | 51 +++++++++++++++++++ 2 files changed, 55 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/connector/SetTBRTaskRunner.java diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java index 0e5b37291f..3180e7d320 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java @@ -33,6 +33,7 @@ import info.nightscout.androidaps.plugins.Overview.notifications.Notification; import info.nightscout.androidaps.plugins.PumpInsight.connector.AbsoluteTBRTaskRunner; import info.nightscout.androidaps.plugins.PumpInsight.connector.CancelBolusTaskRunner; import info.nightscout.androidaps.plugins.PumpInsight.connector.Connector; +import info.nightscout.androidaps.plugins.PumpInsight.connector.SetTBRTaskRunner; import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightPumpCallback; import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightPumpUpdateGui; import info.nightscout.androidaps.plugins.PumpInsight.history.HistoryReceiver; @@ -52,7 +53,6 @@ import sugar.free.sightparser.applayer.messages.remote_control.ExtendedBolusMess import sugar.free.sightparser.applayer.messages.remote_control.StandardBolusMessage; import sugar.free.sightparser.handling.SingleMessageTaskRunner; import sugar.free.sightparser.handling.TaskRunner; -import sugar.free.sightparser.handling.taskrunners.SetTBRTaskRunner; import sugar.free.sightparser.handling.taskrunners.StatusTaskRunner; import static info.nightscout.androidaps.plugins.PumpInsight.history.PumpIdCache.getRecordUniqueID; @@ -85,8 +85,8 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints private long statusResultTime = -1; private Date lastDataTime = new Date(0); private TaskRunner taskRunner; - private boolean fragmentEnabled = true; - private boolean fragmentVisible = true; + private boolean fragmentEnabled = false; + private boolean fragmentVisible = false; private boolean fauxTBRcancel = true; private PumpDescription pumpDescription = new PumpDescription(); private double basalRate = 0; @@ -615,15 +615,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints final UUID cmd; if (fauxTBRcancel) { - final TemporaryBasal activeTemp = MainApp.getConfigBuilder().getTempBasalFromHistory(System.currentTimeMillis()); - // I'm not totally sure whether based on the times that this cancellation will occur - // whether the logic should actually be inverted as we are reversing a decision. - // I believe it also affects at most 2.5% of an hourly basal rate and the pump only - // delivers basal every 3 minutes when > 0.19U per hour, below that unspecified. - final int faux_percent = (activeTemp == null) ? 90 : (activeTemp.percentRate > 100) ? 110 : 90; - - final int faux_duration = 15; - cmd = aSyncTaskRunner(new SetTBRTaskRunner(connector.getServiceConnector(), faux_percent, 15), "Faux Cancel TBR - setting " + faux_percent + "%" + " " + faux_duration + "m"); + cmd = aSyncTaskRunner(new SetTBRTaskRunner(connector.getServiceConnector(), 100, 1), "Faux Cancel TBR - setting " + "90%" + " 1m"); } else { cmd = aSyncSingleCommand(new CancelTBRMessage(), "Cancel Temp Basal"); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/connector/SetTBRTaskRunner.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/connector/SetTBRTaskRunner.java new file mode 100644 index 0000000000..5d22feba8f --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/connector/SetTBRTaskRunner.java @@ -0,0 +1,51 @@ +package info.nightscout.androidaps.plugins.PumpInsight.connector; + +import sugar.free.sightparser.applayer.messages.AppLayerMessage; +import sugar.free.sightparser.applayer.messages.remote_control.ChangeTBRMessage; +import sugar.free.sightparser.applayer.messages.remote_control.SetTBRMessage; +import sugar.free.sightparser.applayer.messages.status.CurrentTBRMessage; +import sugar.free.sightparser.handling.SightServiceConnector; +import sugar.free.sightparser.handling.TaskRunner; + +// from Tebbe - note this uses 1 minute duration to silently cancel existing TBR + +public class SetTBRTaskRunner extends TaskRunner { + + private int amount; + private int duration; + + public SetTBRTaskRunner(SightServiceConnector serviceConnector, int amount, int duration) { + super(serviceConnector); + this.amount = amount; + this.duration = duration; + } + + @Override + protected AppLayerMessage run(AppLayerMessage message) throws Exception { + if (message == null) return new CurrentTBRMessage(); + else if (message instanceof CurrentTBRMessage) { + if (((CurrentTBRMessage) message).getPercentage() == 100) { + if (amount == 100) finish(amount); + else { + SetTBRMessage setTBRMessage = new SetTBRMessage(); + setTBRMessage.setDuration((short) duration); + setTBRMessage.setAmount((short) amount); + return setTBRMessage; + } + } else { + if (amount == 100) { + ChangeTBRMessage changeTBRMessage = new ChangeTBRMessage(); + changeTBRMessage.setDuration((short) 1); + changeTBRMessage.setAmount((short) 90); + return changeTBRMessage; + } else { + ChangeTBRMessage changeTBRMessage = new ChangeTBRMessage(); + changeTBRMessage.setDuration((short) duration); + changeTBRMessage.setAmount((short) amount); + return changeTBRMessage; + } + } + } else if (message instanceof SetTBRMessage) finish(amount); + return null; + } +} From 693c3ef8ad8b682e9a160e751e719b5e227496b0 Mon Sep 17 00:00:00 2001 From: Jamorham Date: Mon, 5 Feb 2018 22:11:26 +0000 Subject: [PATCH 6/8] Store in history exactly what we enact --- .../PumpInsight/InsightPumpPlugin.java | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java index 3180e7d320..3759597ab8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java @@ -459,18 +459,18 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints log("Base basal rate appears to be zero!"); return pumpEnactFailure(); } - int amount = (int) Math.round(100d / base_basal * absoluteRate); - log("Calculated requested rate: " + absoluteRate + " base rate: " + base_basal + " percentage: " + amount + "%"); - amount = (int) Math.round(((double) amount) / 10d) * 10; - log("Calculated final rate: " + amount + "%"); + int percent_amount = (int) Math.round(100d / base_basal * absoluteRate); + log("Calculated requested rate: " + absoluteRate + " base rate: " + base_basal + " percentage: " + percent_amount + "%"); + percent_amount = (int) Math.round(((double) percent_amount) / 10d) * 10; + log("Calculated final rate: " + percent_amount + "%"); - if (amount == 100) { + if (percent_amount == 100) { return cancelTempBasal(false); } - if (amount > 250) amount = 250; + if (percent_amount > 250) percent_amount = 250; - final SetTBRTaskRunner task = new SetTBRTaskRunner(connector.getServiceConnector(), amount, durationInMinutes); + final SetTBRTaskRunner task = new SetTBRTaskRunner(connector.getServiceConnector(), percent_amount, durationInMinutes); final UUID cmd = aSyncTaskRunner(task, "Set TBR abs: " + absoluteRate + " " + durationInMinutes + "m"); if (cmd == null) { @@ -480,18 +480,19 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints Mstatus ms = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME); log("Got command status: " + ms); - PumpEnactResult pumpEnactResult = new PumpEnactResult().enacted(true).isPercent(false).duration(durationInMinutes); - pumpEnactResult.absolute = absoluteRate; // TODO get converted value? + PumpEnactResult pumpEnactResult = new PumpEnactResult().enacted(true).isPercent(true).duration(durationInMinutes); + pumpEnactResult.percent = percent_amount; pumpEnactResult.success = ms.success(); - pumpEnactResult.isTempCancel = false; // do we test this here? + pumpEnactResult.isTempCancel = percent_amount == 100; // 100% temp basal is a cancellation pumpEnactResult.comment = ms.getCommandComment(); + if (pumpEnactResult.success) { // create log entry final TemporaryBasal tempBasal = new TemporaryBasal(); tempBasal.date = System.currentTimeMillis(); - tempBasal.isAbsolute = true; - tempBasal.absoluteRate = base_basal / 100d * ((double) amount); // is this the correct figure to use? + tempBasal.isAbsolute = false; + tempBasal.percentRate = percent_amount; tempBasal.durationInMinutes = durationInMinutes; tempBasal.source = Source.USER; MainApp.getConfigBuilder().addToHistoryTempBasal(tempBasal); From bda1866c6d12760d20127e424b9412ed6a20cf9f Mon Sep 17 00:00:00 2001 From: Jamorham Date: Wed, 7 Feb 2018 13:36:06 +0000 Subject: [PATCH 7/8] Do our own history merging for TBRs --- .../history/HistoryIntentAdapter.java | 10 +++--- .../history/HistoryLogAdapter.java | 35 +++++++++++++++++-- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryIntentAdapter.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryIntentAdapter.java index 8da9d22dd9..5375d88b7b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryIntentAdapter.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryIntentAdapter.java @@ -4,7 +4,6 @@ import android.content.Intent; import java.util.Date; -import info.nightscout.utils.SP; import sugar.free.sightparser.handling.HistoryBroadcast; import static info.nightscout.androidaps.plugins.PumpInsight.history.PumpIdCache.updatePumpSerialNumber; @@ -50,9 +49,12 @@ class HistoryIntentAdapter { final long record_unique_id = getRecordUniqueID(pump_serial_number, pump_record_id); // other sanity checks - log("Creating TBR record: " + pump_tbr_percent + "% " + pump_tbr_duration + "m" + " id:" + record_unique_id); - logAdapter.createTBRrecord(start_time, pump_tbr_percent, pump_tbr_duration, record_unique_id); - + if ((pump_tbr_percent == 90) && (pump_tbr_duration <= 1)) { + log("Not creating TBR record for faux cancel"); + } else { + log("Creating TBR record: " + pump_tbr_percent + "% " + pump_tbr_duration + "m" + " id:" + record_unique_id); + logAdapter.createTBRrecord(start_time, pump_tbr_percent, pump_tbr_duration, record_unique_id); + } } void processDeliveredBolusIntent(Intent intent) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryLogAdapter.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryLogAdapter.java index 73fafd6e6e..ce4f7522a7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryLogAdapter.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryLogAdapter.java @@ -17,10 +17,39 @@ import info.nightscout.androidaps.db.TemporaryBasal; class HistoryLogAdapter { + private static final long MAX_TIME_DIFFERENCE = 5000; + + private static void log(String msg) { + android.util.Log.e("HISTORYLOG", msg); + } + void createTBRrecord(Date eventDate, int percent, int duration, long record_id) { - final TemporaryBasal temporaryBasal = new TemporaryBasal(); - temporaryBasal.date = eventDate.getTime(); + TemporaryBasal temporaryBasal = new TemporaryBasal(eventDate.getTime()); + + final TemporaryBasal temporaryBasalFromHistory = MainApp.getConfigBuilder().getTempBasalFromHistory(eventDate.getTime()); + + if (temporaryBasalFromHistory == null) { + log("Create new TBR: " + eventDate + " " + percent + " " + duration); + } else { + log("Loaded existing TBR record: " + temporaryBasalFromHistory.toString()); + if (Math.abs(eventDate.getTime() - temporaryBasalFromHistory.date) < MAX_TIME_DIFFERENCE) { + if (temporaryBasalFromHistory.source != Source.PUMP) { + if (temporaryBasalFromHistory.percentRate == percent) { + log("Things seem to match: %" + percent); + temporaryBasal = temporaryBasalFromHistory; + MainApp.getDbHelper().delete(temporaryBasalFromHistory); + } else { + log("This record has different percent rates: " + temporaryBasalFromHistory.percentRate + " vs us: " + percent); + } + } else { + log("This record is already a pump record!"); + } + } else { + log("Time difference too great! : " + (eventDate.getTime() - temporaryBasalFromHistory.date)); + } + } + temporaryBasal.source = Source.PUMP; temporaryBasal.pumpId = record_id; temporaryBasal.percentRate = percent; @@ -47,6 +76,8 @@ class HistoryLogAdapter { //DetailedBolusInfo detailedBolusInfo = DetailedBolusInfoStorage.findDetailedBolusInfo(eventDate.getTime()); + // TODO do we need to do the same delete + insert that we are doing for temporary basals here too? + final DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); detailedBolusInfo.date = eventDate.getTime(); detailedBolusInfo.source = Source.PUMP; From 046de51496305396bb245513678fcb38550d3c65 Mon Sep 17 00:00:00 2001 From: Jamorham Date: Sat, 10 Feb 2018 09:29:23 +0000 Subject: [PATCH 8/8] Insight few small changes --- .../plugins/PumpInsight/history/HistoryLogAdapter.java | 2 +- app/src/main/res/values/strings.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryLogAdapter.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryLogAdapter.java index ce4f7522a7..588074b0c4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryLogAdapter.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryLogAdapter.java @@ -17,7 +17,7 @@ import info.nightscout.androidaps.db.TemporaryBasal; class HistoryLogAdapter { - private static final long MAX_TIME_DIFFERENCE = 5000; + private static final long MAX_TIME_DIFFERENCE = 61000; private static void log(String msg) { android.util.Log.e("HISTORYLOG", msg); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5639a0c713..187afb5b07 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -879,7 +879,7 @@ upfront with Stay always connected Use Real TBR cancels - Actually cancel a TBR (creates pump alarm) instead of setting 90% or 110% TBR for 15 minutes + Actually cancel a TBR (creates pump alarm) instead of setting 90% for 1 minute IDLE SYNCING BUSY