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 a9e955e699..e49b10b066 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 @@ -26,10 +26,10 @@ import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.interfaces.TreatmentsInterface; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.Overview.events.EventOverviewBolusProgress; 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.events.EventInsightPumpCallback; @@ -50,6 +50,8 @@ 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.utils.Helpers.roundDouble; + /** * Created by jamorham on 23/01/2018. @@ -73,7 +75,8 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface { private static volatile InsightPumpPlugin plugin; private final Handler handler = new Handler(); private final InsightPumpAsyncAdapter async = new InsightPumpAsyncAdapter(); - StatusTaskRunner.StatusResult statusResult; + private StatusTaskRunner.StatusResult statusResult; + private long statusResultTime = -1; private Date lastDataTime = new Date(0); private TaskRunner taskRunner; private boolean fragmentEnabled = true; @@ -96,9 +99,10 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface { } @Override - public void onResult(Object result) { + public synchronized void onResult(Object result) { log("GOT STATUS RESULT!!!"); statusResult = (StatusTaskRunner.StatusResult) result; + statusResultTime = Helpers.tsl(); processStatusResult(); updateGui(); } @@ -401,29 +405,45 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface { // Temporary Basals - // TODO implement @Override public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, boolean enforceNew) { - TreatmentsInterface treatmentsInterface = MainApp.getConfigBuilder(); - TemporaryBasal tempBasal = new TemporaryBasal(); - tempBasal.date = System.currentTimeMillis(); - tempBasal.isAbsolute = true; - tempBasal.absoluteRate = absoluteRate; - tempBasal.durationInMinutes = durationInMinutes; - tempBasal.source = Source.USER; - PumpEnactResult result = new PumpEnactResult(); - result.success = false; - result.enacted = true; - result.isTempCancel = false; - result.absolute = absoluteRate; - result.duration = durationInMinutes; - result.comment = MainApp.instance().getString(R.string.virtualpump_resultok); - treatmentsInterface.addToHistoryTempBasal(tempBasal); + 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"); + + if (cmd == null) { + return pumpEnactFailure(); + } + + Cstatus cs = async.busyWaitForCommandResult(cmd, 10000); + log("Got command status: " + cs); + + PumpEnactResult pumpEnactResult = new PumpEnactResult().enacted(true).isPercent(false).duration(durationInMinutes); + pumpEnactResult.absolute = absoluteRate; // TODO get converted value? + pumpEnactResult.success = cs == Cstatus.SUCCESS; + pumpEnactResult.isTempCancel = false; // do we test this here? + pumpEnactResult.comment = async.getCommandComment(cmd); + + if (pumpEnactResult.success) { + // create log entry + final TemporaryBasal tempBasal = new TemporaryBasal(); + tempBasal.date = System.currentTimeMillis(); + tempBasal.isAbsolute = true; + tempBasal.absoluteRate = task.getCalculatedAbsolute(); // 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: " + result); - MainApp.bus().post(new EventInsightPumpUpdateGui()); + log.debug("Setting temp basal absolute: " + pumpEnactResult.success); + lastDataTime = new Date(); - return result; + + updateGui(); + return pumpEnactResult; } @@ -705,7 +725,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface { if (statusResult != null) { batteryPercent = statusResult.getBatteryAmountMessage().getBatteryAmount(); reservoirInUnits = (int) statusResult.getCartridgeAmountMessage().getCartridgeAmount(); - basalRate = statusResult.getCurrentBasalMessage().getCurrentBasalAmount(); + basalRate = roundDouble(statusResult.getCurrentBasalMessage().getCurrentBasalAmount(),2); initialized = true; // basic communication test } } @@ -735,6 +755,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface { } if (statusResult != null) { + l.add(new StatusItem("Status Updated", Helpers.niceTimeScalar(Helpers.msSince(statusResultTime)) + " ago")); l.add(new StatusItem(gs(R.string.pump_battery_label), batteryPercent + "%", batteryPercent < 100 ? (batteryPercent < 90 ? (batteryPercent < 70 ? diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/connector/AbsoluteTBRTaskRunner.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/connector/AbsoluteTBRTaskRunner.java index e5f5645cb4..d5e1207543 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/connector/AbsoluteTBRTaskRunner.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/connector/AbsoluteTBRTaskRunner.java @@ -1,5 +1,6 @@ package info.nightscout.androidaps.plugins.PumpInsight.connector; +import info.nightscout.androidaps.plugins.PumpInsight.utils.Helpers; import sugar.free.sightparser.applayer.AppLayerMessage; import sugar.free.sightparser.applayer.remote_control.ChangeTBRMessage; import sugar.free.sightparser.applayer.remote_control.SetTBRMessage; @@ -12,13 +13,25 @@ import sugar.free.sightparser.handling.TaskRunner; public class AbsoluteTBRTaskRunner extends TaskRunner { - private float absolute; + private double absolute; private int amount; private int duration; + private int calculated_percentage; + private double calculated_absolute; - public AbsoluteTBRTaskRunner(SightServiceConnector serviceConnector, float absolute, int duration) { + public AbsoluteTBRTaskRunner(SightServiceConnector serviceConnector, double absolute, int duration) { super(serviceConnector); + if (absolute < 0) absolute = 0; this.absolute = absolute; + this.duration = duration; + } + + public int getCalculatedPercentage() { + return calculated_percentage; + } + + public double getCalculatedAbsolute() { + return calculated_absolute; } @Override @@ -26,9 +39,12 @@ public class AbsoluteTBRTaskRunner extends TaskRunner { if (message == null) return new CurrentBasalMessage(); else if (message instanceof CurrentBasalMessage) { float currentBasal = ((CurrentBasalMessage) message).getCurrentBasalAmount(); - amount = (int) (100F / currentBasal * absolute); + amount = (int) (100d / currentBasal * absolute); amount = ((int) amount / 10) * 10; if (amount > 250) amount = 250; + calculated_percentage = amount; + calculated_absolute = Helpers.roundDouble(calculated_percentage * (double) currentBasal / 100d, 3); + Connector.log("Asked: " + absolute + " current: " + currentBasal + " calculated as: " + amount + "%" + " = " + calculated_absolute); return new CurrentTBRMessage(); } else if (message instanceof CurrentTBRMessage) { SetTBRMessage setTBRMessage;