Insight Temporary basal absolute
This commit is contained in:
parent
f036ca4547
commit
b831fd7dfb
2 changed files with 63 additions and 26 deletions
|
@ -26,10 +26,10 @@ import info.nightscout.androidaps.db.TemporaryBasal;
|
||||||
import info.nightscout.androidaps.interfaces.PluginBase;
|
import info.nightscout.androidaps.interfaces.PluginBase;
|
||||||
import info.nightscout.androidaps.interfaces.PumpDescription;
|
import info.nightscout.androidaps.interfaces.PumpDescription;
|
||||||
import info.nightscout.androidaps.interfaces.PumpInterface;
|
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.EventNewNotification;
|
||||||
import info.nightscout.androidaps.plugins.Overview.events.EventOverviewBolusProgress;
|
import info.nightscout.androidaps.plugins.Overview.events.EventOverviewBolusProgress;
|
||||||
import info.nightscout.androidaps.plugins.Overview.notifications.Notification;
|
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.CancelBolusTaskRunner;
|
||||||
import info.nightscout.androidaps.plugins.PumpInsight.connector.Connector;
|
import info.nightscout.androidaps.plugins.PumpInsight.connector.Connector;
|
||||||
import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightPumpCallback;
|
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.SetTBRTaskRunner;
|
||||||
import sugar.free.sightparser.handling.taskrunners.StatusTaskRunner;
|
import sugar.free.sightparser.handling.taskrunners.StatusTaskRunner;
|
||||||
|
|
||||||
|
import static info.nightscout.androidaps.plugins.PumpInsight.utils.Helpers.roundDouble;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by jamorham on 23/01/2018.
|
* Created by jamorham on 23/01/2018.
|
||||||
|
@ -73,7 +75,8 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
|
||||||
private static volatile InsightPumpPlugin plugin;
|
private static volatile InsightPumpPlugin plugin;
|
||||||
private final Handler handler = new Handler();
|
private final Handler handler = new Handler();
|
||||||
private final InsightPumpAsyncAdapter async = new InsightPumpAsyncAdapter();
|
private final InsightPumpAsyncAdapter async = new InsightPumpAsyncAdapter();
|
||||||
StatusTaskRunner.StatusResult statusResult;
|
private StatusTaskRunner.StatusResult statusResult;
|
||||||
|
private long statusResultTime = -1;
|
||||||
private Date lastDataTime = new Date(0);
|
private Date lastDataTime = new Date(0);
|
||||||
private TaskRunner taskRunner;
|
private TaskRunner taskRunner;
|
||||||
private boolean fragmentEnabled = true;
|
private boolean fragmentEnabled = true;
|
||||||
|
@ -96,9 +99,10 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onResult(Object result) {
|
public synchronized void onResult(Object result) {
|
||||||
log("GOT STATUS RESULT!!!");
|
log("GOT STATUS RESULT!!!");
|
||||||
statusResult = (StatusTaskRunner.StatusResult) result;
|
statusResult = (StatusTaskRunner.StatusResult) result;
|
||||||
|
statusResultTime = Helpers.tsl();
|
||||||
processStatusResult();
|
processStatusResult();
|
||||||
updateGui();
|
updateGui();
|
||||||
}
|
}
|
||||||
|
@ -401,29 +405,45 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
|
||||||
|
|
||||||
// Temporary Basals
|
// Temporary Basals
|
||||||
|
|
||||||
// TODO implement
|
|
||||||
@Override
|
@Override
|
||||||
public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, boolean enforceNew) {
|
public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, boolean enforceNew) {
|
||||||
TreatmentsInterface treatmentsInterface = MainApp.getConfigBuilder();
|
absoluteRate = Helpers.roundDouble(absoluteRate, 3);
|
||||||
TemporaryBasal tempBasal = new TemporaryBasal();
|
log("Set TBR absolute: " + absoluteRate);
|
||||||
tempBasal.date = System.currentTimeMillis();
|
|
||||||
tempBasal.isAbsolute = true;
|
final AbsoluteTBRTaskRunner task = new AbsoluteTBRTaskRunner(connector.getServiceConnector(), absoluteRate, durationInMinutes);
|
||||||
tempBasal.absoluteRate = absoluteRate;
|
final UUID cmd = aSyncTaskRunner(task, "Set TBR abs: " + absoluteRate + " " + durationInMinutes + "m");
|
||||||
tempBasal.durationInMinutes = durationInMinutes;
|
|
||||||
tempBasal.source = Source.USER;
|
if (cmd == null) {
|
||||||
PumpEnactResult result = new PumpEnactResult();
|
return pumpEnactFailure();
|
||||||
result.success = false;
|
}
|
||||||
result.enacted = true;
|
|
||||||
result.isTempCancel = false;
|
Cstatus cs = async.busyWaitForCommandResult(cmd, 10000);
|
||||||
result.absolute = absoluteRate;
|
log("Got command status: " + cs);
|
||||||
result.duration = durationInMinutes;
|
|
||||||
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
|
PumpEnactResult pumpEnactResult = new PumpEnactResult().enacted(true).isPercent(false).duration(durationInMinutes);
|
||||||
treatmentsInterface.addToHistoryTempBasal(tempBasal);
|
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)
|
if (Config.logPumpComm)
|
||||||
log.debug("Setting temp basal absolute: " + result);
|
log.debug("Setting temp basal absolute: " + pumpEnactResult.success);
|
||||||
MainApp.bus().post(new EventInsightPumpUpdateGui());
|
|
||||||
lastDataTime = new Date();
|
lastDataTime = new Date();
|
||||||
return result;
|
|
||||||
|
updateGui();
|
||||||
|
return pumpEnactResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -705,7 +725,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
|
||||||
if (statusResult != null) {
|
if (statusResult != null) {
|
||||||
batteryPercent = statusResult.getBatteryAmountMessage().getBatteryAmount();
|
batteryPercent = statusResult.getBatteryAmountMessage().getBatteryAmount();
|
||||||
reservoirInUnits = (int) statusResult.getCartridgeAmountMessage().getCartridgeAmount();
|
reservoirInUnits = (int) statusResult.getCartridgeAmountMessage().getCartridgeAmount();
|
||||||
basalRate = statusResult.getCurrentBasalMessage().getCurrentBasalAmount();
|
basalRate = roundDouble(statusResult.getCurrentBasalMessage().getCurrentBasalAmount(),2);
|
||||||
initialized = true; // basic communication test
|
initialized = true; // basic communication test
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -735,6 +755,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (statusResult != null) {
|
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 ?
|
l.add(new StatusItem(gs(R.string.pump_battery_label), batteryPercent + "%", batteryPercent < 100 ?
|
||||||
(batteryPercent < 90 ?
|
(batteryPercent < 90 ?
|
||||||
(batteryPercent < 70 ?
|
(batteryPercent < 70 ?
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package info.nightscout.androidaps.plugins.PumpInsight.connector;
|
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.AppLayerMessage;
|
||||||
import sugar.free.sightparser.applayer.remote_control.ChangeTBRMessage;
|
import sugar.free.sightparser.applayer.remote_control.ChangeTBRMessage;
|
||||||
import sugar.free.sightparser.applayer.remote_control.SetTBRMessage;
|
import sugar.free.sightparser.applayer.remote_control.SetTBRMessage;
|
||||||
|
@ -12,13 +13,25 @@ import sugar.free.sightparser.handling.TaskRunner;
|
||||||
|
|
||||||
public class AbsoluteTBRTaskRunner extends TaskRunner {
|
public class AbsoluteTBRTaskRunner extends TaskRunner {
|
||||||
|
|
||||||
private float absolute;
|
private double absolute;
|
||||||
private int amount;
|
private int amount;
|
||||||
private int duration;
|
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);
|
super(serviceConnector);
|
||||||
|
if (absolute < 0) absolute = 0;
|
||||||
this.absolute = absolute;
|
this.absolute = absolute;
|
||||||
|
this.duration = duration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCalculatedPercentage() {
|
||||||
|
return calculated_percentage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getCalculatedAbsolute() {
|
||||||
|
return calculated_absolute;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -26,9 +39,12 @@ public class AbsoluteTBRTaskRunner extends TaskRunner {
|
||||||
if (message == null) return new CurrentBasalMessage();
|
if (message == null) return new CurrentBasalMessage();
|
||||||
else if (message instanceof CurrentBasalMessage) {
|
else if (message instanceof CurrentBasalMessage) {
|
||||||
float currentBasal = ((CurrentBasalMessage) message).getCurrentBasalAmount();
|
float currentBasal = ((CurrentBasalMessage) message).getCurrentBasalAmount();
|
||||||
amount = (int) (100F / currentBasal * absolute);
|
amount = (int) (100d / currentBasal * absolute);
|
||||||
amount = ((int) amount / 10) * 10;
|
amount = ((int) amount / 10) * 10;
|
||||||
if (amount > 250) amount = 250;
|
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();
|
return new CurrentTBRMessage();
|
||||||
} else if (message instanceof CurrentTBRMessage) {
|
} else if (message instanceof CurrentTBRMessage) {
|
||||||
SetTBRMessage setTBRMessage;
|
SetTBRMessage setTBRMessage;
|
||||||
|
|
Loading…
Reference in a new issue