From 8fd68a082c81a69ce8919c63939ac46ec76e509a Mon Sep 17 00:00:00 2001 From: TebbeUbben Date: Sun, 11 Mar 2018 22:53:18 +0100 Subject: [PATCH] Show bolus progress --- .../events/EventOverviewBolusProgress.java | 2 + .../PumpInsight/InsightPumpPlugin.java | 42 +++++++++++++++++-- .../PumpInsight/connector/Connector.java | 2 +- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/events/EventOverviewBolusProgress.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/events/EventOverviewBolusProgress.java index 080dec4bd2..18c3ff1e27 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/events/EventOverviewBolusProgress.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/events/EventOverviewBolusProgress.java @@ -3,6 +3,7 @@ package info.nightscout.androidaps.plugins.Overview.events; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.events.Event; @@ -11,6 +12,7 @@ public class EventOverviewBolusProgress extends Event { public String status = ""; public Treatment t = null; public int percent = 0; + public int bolusId; private static EventOverviewBolusProgress eventOverviewBolusProgress = null; public EventOverviewBolusProgress() { 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 22f3a70c55..2894f25b8f 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 @@ -31,6 +31,7 @@ import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.plugins.Overview.events.EventDismissBolusprogressIfRunning; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.Overview.events.EventOverviewBolusProgress; @@ -55,9 +56,11 @@ import sugar.free.sightparser.applayer.descriptors.PumpStatus; import sugar.free.sightparser.applayer.descriptors.configuration_blocks.BRProfileBlock; import sugar.free.sightparser.applayer.messages.AppLayerMessage; import sugar.free.sightparser.applayer.messages.remote_control.BolusMessage; +import sugar.free.sightparser.applayer.messages.remote_control.CancelBolusMessage; import sugar.free.sightparser.applayer.messages.remote_control.CancelTBRMessage; import sugar.free.sightparser.applayer.messages.remote_control.ExtendedBolusMessage; import sugar.free.sightparser.applayer.messages.remote_control.StandardBolusMessage; +import sugar.free.sightparser.applayer.messages.status.ActiveBolusesMessage; import sugar.free.sightparser.handling.SingleMessageTaskRunner; import sugar.free.sightparser.handling.TaskRunner; import sugar.free.sightparser.handling.taskrunners.StatusTaskRunner; @@ -158,7 +161,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints pumpDescription.basalMinimumRate = 0.02d; pumpDescription.isRefillingCapable = true; - //pumpDescription.storesCarbInfo = false; + pumpDescription.storesCarbInfo = false; } @@ -479,6 +482,8 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints result.percent = 100; + int bolusId = 0; + // is there an insulin component to the treatment? if (detailedBolusInfo.insulin > 0) { final UUID cmd = deliverBolus(detailedBolusInfo.insulin); // actually request delivery @@ -490,6 +495,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints result.success = ms.success(); if (ms.success()) { detailedBolusInfo.pumpId = getRecordUniqueID(ms.getResponseID()); + bolusId = ms.getResponseID(); } } else { result.success = true; // always true with carb only treatments @@ -502,8 +508,9 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints t.isSMB = detailedBolusInfo.isSMB; final EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); bolusingEvent.t = t; - bolusingEvent.status = String.format(MainApp.sResources.getString(R.string.bolusdelivered), detailedBolusInfo.insulin); - bolusingEvent.percent = 100; + bolusingEvent.status = String.format(MainApp.sResources.getString(R.string.bolusdelivering), 0F); + bolusingEvent.bolusId = bolusId; + bolusingEvent.percent = 0; MainApp.bus().post(bolusingEvent); MainApp.getConfigBuilder().addToHistoryTreatment(detailedBolusInfo); } else { @@ -518,12 +525,39 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints lastDataTime = new Date(); connector.requestHistorySync(30000); + + if (result.success) while (true) { + try { + Thread.sleep(200); + } catch (InterruptedException e) { + e.printStackTrace(); + break; + } + final UUID uuid = aSyncSingleCommand(new ActiveBolusesMessage(), "Active boluses"); + Mstatus mstatus = async.busyWaitForCommandResult(uuid, BUSY_WAIT_TIME); + if (mstatus.success()) { + final EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); + ActiveBolusesMessage activeBolusesMessage = (ActiveBolusesMessage) mstatus.getResponseObject(); + ActiveBolus activeBolus = null; + if (activeBolusesMessage.getBolus1() != null && activeBolusesMessage.getBolus1().getBolusID() == bolusingEvent.bolusId) activeBolus = activeBolusesMessage.getBolus1(); + else if (activeBolusesMessage.getBolus2() != null && activeBolusesMessage.getBolus2().getBolusID() == bolusingEvent.bolusId) activeBolus = activeBolusesMessage.getBolus2(); + else if (activeBolusesMessage.getBolus3() != null && activeBolusesMessage.getBolus3().getBolusID() == bolusingEvent.bolusId) activeBolus = activeBolusesMessage.getBolus3(); + if (activeBolus == null) break; + else { + bolusingEvent.percent = (int) (100D / activeBolus.getInitialAmount() * (activeBolus.getInitialAmount() - activeBolus.getLeftoverAmount())); + bolusingEvent.status = String.format(MainApp.sResources.getString(R.string.bolusdelivering), activeBolus.getInitialAmount() - activeBolus.getLeftoverAmount()); + MainApp.bus().post(bolusingEvent); + } + } else break; + } return result; } @Override public void stopBolusDelivering() { - final UUID cmd = aSyncTaskRunner(new CancelBolusTaskRunner(connector.getServiceConnector(), ActiveBolusType.STANDARD), "Cancel standard bolus"); + CancelBolusMessage cancelBolusMessage = new CancelBolusMessage(); + cancelBolusMessage.setBolusId(EventOverviewBolusProgress.getInstance().bolusId); + final UUID cmd = aSyncSingleCommand(cancelBolusMessage, "Cancel standard bolus"); if (cmd == null) { return; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/connector/Connector.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/connector/Connector.java index 454795e8d4..311e487b9f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/connector/Connector.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/connector/Connector.java @@ -148,7 +148,7 @@ public class Connector { public synchronized static void connectToPump(long keep_alive) { log("Attempting to connect to pump."); - if (keep_alive > 0) { + if (keep_alive > 0 && Helpers.tsl() + keep_alive > stayConnectedTill) { stayConnectedTime = keep_alive; stayConnectedTill = Helpers.tsl() + keep_alive; log("Staying connected till: " + Helpers.dateTimeText(stayConnectedTill));