Insight ConstraintsInterface and Mstatus response type

This commit is contained in:
Jamorham 2018-02-02 15:09:20 +00:00
parent cdb3bc24c2
commit 8e2fe7eaec
No known key found for this signature in database
GPG key ID: 0BC5C3E0AAD64DF9
4 changed files with 151 additions and 45 deletions

View file

@ -56,7 +56,7 @@ public class InsightPumpAsyncAdapter {
} }
// blocking call to wait for result callback // 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); final PowerManager.WakeLock wl = getWakeLock("insight-wait-cmd", 60000);
try { try {
log("busy wait for command " + uuid); log("busy wait for command " + uuid);
@ -82,25 +82,14 @@ public class InsightPumpAsyncAdapter {
} }
} }
// commend field preparation for results // wait for and then package result, cleanup and return
String getCommandComment(final UUID uuid) { Mstatus busyWaitForCommandResult(final UUID uuid, long wait_time) {
if (commandResults.containsKey(uuid)) { final Mstatus mstatus = new Mstatus();
if (commandResults.get(uuid).success) { mstatus.cstatus = busyWaitForCommandInternal(uuid, wait_time);
return "OK"; mstatus.event = commandResults.get(uuid);
} else { commandResults.remove(uuid);
return commandResults.get(uuid).message; return mstatus;
}
} else {
return "Unknown reference";
}
} }
int getResponseID(UUID uuid) {
if (checkCommandResult(uuid) == Cstatus.SUCCESS) {
return commandResults.get(uuid).response_id;
} else {
return -2; // invalid
}
}
} }

View file

@ -23,6 +23,7 @@ import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.ExtendedBolus;
import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.db.Source;
import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.db.TemporaryBasal;
import info.nightscout.androidaps.interfaces.ConstraintsInterface;
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;
@ -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; private static final long BUSY_WAIT_TIME = 20000;
static Integer batteryPercent = 0; static Integer batteryPercent = 0;
@ -202,7 +203,9 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
@Override @Override
public boolean isEnabled(int type) { 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 @Override
@ -400,10 +403,11 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
if (cmd == null) { if (cmd == null) {
return pumpEnactFailure(); return pumpEnactFailure();
} }
final Cstatus cs = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME); final Mstatus ms = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME);
result.success = cs.success();
if (cs.success()) { result.success = ms.success();
detailedBolusInfo.pumpId = getRecordUniqueID(async.getResponseID(cmd)); if (ms.success()) {
detailedBolusInfo.pumpId = getRecordUniqueID(ms.getResponseID());
} }
} else { } else {
result.success = true; // always true with carb only treatments result.success = true; // always true with carb only treatments
@ -440,7 +444,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
return; return;
} }
final Cstatus cs = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME); final Mstatus cs = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME);
log("Got command status: " + cs); log("Got command status: " + cs);
} }
@ -458,14 +462,14 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
return pumpEnactFailure(); return pumpEnactFailure();
} }
Cstatus cs = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME); Mstatus ms = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME);
log("Got command status: " + cs); log("Got command status: " + ms);
PumpEnactResult pumpEnactResult = new PumpEnactResult().enacted(true).isPercent(false).duration(durationInMinutes); PumpEnactResult pumpEnactResult = new PumpEnactResult().enacted(true).isPercent(false).duration(durationInMinutes);
pumpEnactResult.absolute = absoluteRate; // TODO get converted value? pumpEnactResult.absolute = absoluteRate; // TODO get converted value?
pumpEnactResult.success = cs.success(); pumpEnactResult.success = ms.success();
pumpEnactResult.isTempCancel = false; // do we test this here? pumpEnactResult.isTempCancel = false; // do we test this here?
pumpEnactResult.comment = async.getCommandComment(cmd); pumpEnactResult.comment = ms.getCommandComment();
if (pumpEnactResult.success) { if (pumpEnactResult.success) {
// create log entry // create log entry
@ -501,14 +505,14 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
return pumpEnactFailure(); return pumpEnactFailure();
} }
Cstatus cs = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME); final Mstatus ms = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME);
log("Got command status: " + cs); log("Got command status: " + ms);
PumpEnactResult pumpEnactResult = new PumpEnactResult().enacted(true).isPercent(true).duration(durationInMinutes); PumpEnactResult pumpEnactResult = new PumpEnactResult().enacted(true).isPercent(true).duration(durationInMinutes);
pumpEnactResult.percent = percent; pumpEnactResult.percent = percent;
pumpEnactResult.success = cs.success(); pumpEnactResult.success = ms.success();
pumpEnactResult.isTempCancel = percent == 100; // 100% temp basal is a cancellation pumpEnactResult.isTempCancel = percent == 100; // 100% temp basal is a cancellation
pumpEnactResult.comment = async.getCommandComment(cmd); pumpEnactResult.comment = ms.getCommandComment();
if (pumpEnactResult.success) { if (pumpEnactResult.success) {
// create log entry // create log entry
@ -543,7 +547,13 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
final UUID cmd; final UUID cmd;
if (fauxTBRcancel) { 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; 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(), faux_percent, 15), "Faux Cancel TBR - setting " + faux_percent + "%" + " " + faux_duration + "m");
} else { } 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 // TODO isn't conditional on one apparently being in progress only the history change
boolean enacted = false; 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()) { if (MainApp.getConfigBuilder().isTempBasalInProgress()) {
enacted = true; enacted = true;
@ -571,7 +581,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
connector.requestHistorySync(5000); connector.requestHistorySync(5000);
connector.tryToGetPumpStatusAgain(); 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(); return pumpEnactFailure();
} }
final Cstatus cs = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME); final Mstatus ms = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME);
log("Got command status: " + cs); log("Got command status: " + ms);
PumpEnactResult pumpEnactResult = new PumpEnactResult().enacted(true).bolusDelivered(insulin).duration(durationInMinutes); PumpEnactResult pumpEnactResult = new PumpEnactResult().enacted(true).bolusDelivered(insulin).duration(durationInMinutes);
pumpEnactResult.success = cs.success(); pumpEnactResult.success = ms.success();
pumpEnactResult.comment = async.getCommandComment(cmd); pumpEnactResult.comment = ms.getCommandComment();
if (pumpEnactResult.success) { if (pumpEnactResult.success) {
// create log entry // create log entry
@ -602,7 +612,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
extendedBolus.insulin = insulin; extendedBolus.insulin = insulin;
extendedBolus.durationInMinutes = durationInMinutes; extendedBolus.durationInMinutes = durationInMinutes;
extendedBolus.source = Source.USER; extendedBolus.source = Source.USER;
extendedBolus.pumpId = getRecordUniqueID(async.getResponseID(cmd)); extendedBolus.pumpId = getRecordUniqueID(ms.getResponseID());
MainApp.getConfigBuilder().addToHistoryExtendedBolus(extendedBolus); MainApp.getConfigBuilder().addToHistoryExtendedBolus(extendedBolus);
} }
@ -630,7 +640,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
return pumpEnactFailure(); return pumpEnactFailure();
} }
final Cstatus cs = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME); final Mstatus ms = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME);
if (MainApp.getConfigBuilder().isInHistoryExtendedBoluslInProgress()) { if (MainApp.getConfigBuilder().isInHistoryExtendedBoluslInProgress()) {
ExtendedBolus exStop = new ExtendedBolus(System.currentTimeMillis()); ExtendedBolus exStop = new ExtendedBolus(System.currentTimeMillis());
@ -646,7 +656,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
connector.requestHistorySync(5000); connector.requestHistorySync(5000);
connector.tryToGetPumpStatusAgain(); 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 @Override
public void onResult(Object o) { public void onResult(Object o) {
log(name + " success"); log(name + " success");
event.response_object = o;
if (o instanceof BolusMessage) { if (o instanceof BolusMessage) {
event.response_id = ((BolusMessage)o).getBolusId(); event.response_id = ((BolusMessage)o).getBolusId();
} }
@ -911,6 +922,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
@Override @Override
public void onResult(Object o) { public void onResult(Object o) {
log(name + " success"); log(name + " success");
event.response_object = o;
event.success = true; event.success = true;
pushCallbackEvent(event); pushCallbackEvent(event);
} }
@ -936,5 +948,59 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
return new PumpEnactResult().success(false).enacted(false); 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;
}
} }

View file

@ -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;
}
}

View file

@ -13,6 +13,7 @@ public class EventInsightPumpCallback extends Event {
public boolean success = false; public boolean success = false;
public String message = null; public String message = null;
public int response_id = -1; public int response_id = -1;
public Object response_object = null;
public EventInsightPumpCallback() { public EventInsightPumpCallback() {
request_uuid = UUID.randomUUID(); request_uuid = UUID.randomUUID();
@ -20,7 +21,7 @@ public class EventInsightPumpCallback extends Event {
@Override @Override
public String toString() { public String toString() {
return "Event: " + request_uuid + " success: " + success + " msg: " + message; return "Event: " + request_uuid + " success: " + success + " msg: " + message + " Object: " + response_object;
} }
} }