Initial take on bolus cancellation.

This commit is contained in:
Johannes Mockenhaupt 2017-08-15 20:42:31 +02:00
parent c1ecad1ed6
commit a664bdeaaa
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1
2 changed files with 61 additions and 29 deletions

View file

@ -15,6 +15,8 @@ import de.jotomo.ruffyscripter.PumpState;
import de.jotomo.ruffyscripter.RuffyScripter;
import static de.jotomo.ruffyscripter.commands.ProgressReportCallback.State.BOLUSING;
import static de.jotomo.ruffyscripter.commands.ProgressReportCallback.State.CANCELLED;
import static de.jotomo.ruffyscripter.commands.ProgressReportCallback.State.CANCELLING;
import static de.jotomo.ruffyscripter.commands.ProgressReportCallback.State.FINISHED;
import static de.jotomo.ruffyscripter.commands.ProgressReportCallback.State.PREPARING;
@ -23,6 +25,7 @@ public class BolusCommand implements Command {
private final double bolus;
private final ProgressReportCallback progressReportCallback;
private volatile boolean cancelRequested;
public BolusCommand(double bolus, ProgressReportCallback progressReportCallback) {
this.progressReportCallback = progressReportCallback;
@ -52,10 +55,24 @@ public class BolusCommand implements Command {
// confirm bolus
scripter.verifyMenuIsDisplayed(MenuType.BOLUS_ENTER);
scripter.pressCheckKey();
if (cancelRequested) {
scripter.goToMainTypeScreen(MenuType.MAIN_MENU, 30_000);
progressReportCallback.progress(CANCELLED, 0, 0);
return new CommandResult().success(true).enacted(false).message("Bolus cancelled as per user request with no insulin delivered");
}
progressReportCallback.progress(BOLUSING, 0, 0);
// the pump displays the entered bolus and waits a bit to let user check and cancel
// TODO pressing up (and possible other keys) cancels the bolus
scripter.waitForMenuToBeLeft(MenuType.BOLUS_ENTER);
while (scripter.currentMenu.getType() == MenuType.BOLUS_ENTER) {
if (cancelRequested) {
scripter.pressUpKey();
// TODO deal with error; write a method to wait for and cancel a specific alarm
// wait happens if the keypress comes too late? just try agoin below?
}
SystemClock.sleep(50);
}
scripter.verifyMenuIsDisplayed(MenuType.MAIN_MENU,
"Pump did not return to MAIN_MEU from BOLUS_ENTER to deliver bolus. "
@ -66,12 +83,17 @@ public class BolusCommand implements Command {
Double bolusRemaining = (Double) scripter.currentMenu.getAttribute(MenuAttribute.BOLUS_REMAINING);
double lastBolusReported = 0;
while (bolusRemaining != null) {
if (cancelRequested) {
// TODO press up 3s, deal with bolus cancelled error, retrieved amount actually delivered from history and return it
// since the cancellation takes three seconds some insulin will have definately been delivered (delivery speed is roughly 0.1U/s)
}
if (lastBolusReported != bolusRemaining) {
log.debug("Delivering bolus, remaining: " + bolusRemaining);
int percentDelivered = (int) (100 - (bolusRemaining / bolus * 100));
progressReportCallback.progress(BOLUSING, percentDelivered, bolus - bolusRemaining);
lastBolusReported = bolusRemaining;
}
// TODO deal with alarms that can arise; an oclussion with raise an oclussion alert as well as a bolus cancelled alert
SystemClock.sleep(50);
bolusRemaining = (Double) scripter.currentMenu.getAttribute(MenuAttribute.BOLUS_REMAINING);
}
@ -174,6 +196,11 @@ public class BolusCommand implements Command {
return (double) amountObj;
}
public void requestCancellation() {
cancelRequested = true;
progressReportCallback.progress(CANCELLING, 0, 0);
}
@Override
public String toString() {
return "BolusCommand{" +

View file

@ -80,6 +80,8 @@ public class ComboPlugin implements PluginBase, PumpInterface {
@NonNull
volatile Date lastCmdTime = new Date(0);
volatile PumpState pumpState = new PumpState();
@Nullable
private volatile BolusCommand runningBolusCommand;
private static PumpEnactResult OPERATION_NOT_SUPPORTED = new PumpEnactResult();
@ -364,6 +366,33 @@ public class ComboPlugin implements PluginBase, PumpInterface {
return basal;
}
private static ProgressReportCallback bolusProgressReportCallback = new ProgressReportCallback() {
@Override
public void progress(ProgressReportCallback.State state, int percent, double delivered) {
EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance();
switch (state) {
// TODO move into enum as toString or so and make it translateb
case BOLUSING:
bolusingEvent.status = String.format(MainApp.sResources.getString(R.string.bolusdelivering), delivered);
break;
case PREPARING:
bolusingEvent.status = "Preparing pump for bolus";
break;
case FINISHED:
bolusingEvent.status = "Bolus delivery finished successfully";
break;
case CANCELLED:
bolusingEvent.status = "Bolus delivery was cancelled";
break;
case CANCELLING:
bolusingEvent.status = "Cancelling bolus delivery";
break;
}
bolusingEvent.percent = percent;
MainApp.bus().post(bolusingEvent);
}
};
// what a mess: pump integration code reading carb info from Detailed**Bolus**Info,
// writing carb treatments to the history table. What's PumpEnactResult for again?
@Override
@ -373,32 +402,10 @@ public class ComboPlugin implements PluginBase, PumpInterface {
// bolus needed, ask pump to deliver it
EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance();
MainApp.bus().post(bolusingEvent);
CommandResult bolusCmdResult = runCommand(new BolusCommand(detailedBolusInfo.insulin, new ProgressReportCallback() {
@Override
public void progress(State state, int percent, double delivered) {
EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance();
switch (state) {
// TODO move into enum as toString or so and make it translateb
case BOLUSING:
bolusingEvent.status = String.format(MainApp.sResources.getString(R.string.bolusdelivering), delivered);
break;
case PREPARING:
bolusingEvent.status = "Preparing pump for bolus";
break;
case FINISHED:
bolusingEvent.status = "Bolus delivery finished successfully";
break;
case CANCELLED:
bolusingEvent.status = "Bolus delivery was cancelled";
break;
case CANCELLING:
bolusingEvent.status = "Cancelling bolus delivery";
break;
}
bolusingEvent.percent = percent;
MainApp.bus().post(bolusingEvent);
}
}));
// TODO move into enum as toString or so and make it translateb
runningBolusCommand = new BolusCommand(detailedBolusInfo.insulin, bolusProgressReportCallback);
CommandResult bolusCmdResult = runCommand(runningBolusCommand);
runningBolusCommand = null;
PumpEnactResult pumpEnactResult = new PumpEnactResult();
pumpEnactResult.success = bolusCmdResult.success;
pumpEnactResult.enacted = bolusCmdResult.enacted;
@ -484,9 +491,7 @@ public class ComboPlugin implements PluginBase, PumpInterface {
@Override
public void stopBolusDelivering() {
// there's no way to stop the combo once delivery has started
// but before that, we could interrupt the command thread ... pause
// till pump times out or raises an error
if (runningBolusCommand != null) runningBolusCommand.requestCancellation();
}
// Note: AAPS calls this only to enact OpenAPS recommendations