Initial take on bolus cancellation.
This commit is contained in:
parent
c1ecad1ed6
commit
a664bdeaaa
|
@ -15,6 +15,8 @@ import de.jotomo.ruffyscripter.PumpState;
|
||||||
import de.jotomo.ruffyscripter.RuffyScripter;
|
import de.jotomo.ruffyscripter.RuffyScripter;
|
||||||
|
|
||||||
import static de.jotomo.ruffyscripter.commands.ProgressReportCallback.State.BOLUSING;
|
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.FINISHED;
|
||||||
import static de.jotomo.ruffyscripter.commands.ProgressReportCallback.State.PREPARING;
|
import static de.jotomo.ruffyscripter.commands.ProgressReportCallback.State.PREPARING;
|
||||||
|
|
||||||
|
@ -23,6 +25,7 @@ public class BolusCommand implements Command {
|
||||||
|
|
||||||
private final double bolus;
|
private final double bolus;
|
||||||
private final ProgressReportCallback progressReportCallback;
|
private final ProgressReportCallback progressReportCallback;
|
||||||
|
private volatile boolean cancelRequested;
|
||||||
|
|
||||||
public BolusCommand(double bolus, ProgressReportCallback progressReportCallback) {
|
public BolusCommand(double bolus, ProgressReportCallback progressReportCallback) {
|
||||||
this.progressReportCallback = progressReportCallback;
|
this.progressReportCallback = progressReportCallback;
|
||||||
|
@ -52,10 +55,24 @@ public class BolusCommand implements Command {
|
||||||
// confirm bolus
|
// confirm bolus
|
||||||
scripter.verifyMenuIsDisplayed(MenuType.BOLUS_ENTER);
|
scripter.verifyMenuIsDisplayed(MenuType.BOLUS_ENTER);
|
||||||
scripter.pressCheckKey();
|
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);
|
progressReportCallback.progress(BOLUSING, 0, 0);
|
||||||
|
|
||||||
// the pump displays the entered bolus and waits a bit to let user check and cancel
|
// 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);
|
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,
|
scripter.verifyMenuIsDisplayed(MenuType.MAIN_MENU,
|
||||||
"Pump did not return to MAIN_MEU from BOLUS_ENTER to deliver bolus. "
|
"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 bolusRemaining = (Double) scripter.currentMenu.getAttribute(MenuAttribute.BOLUS_REMAINING);
|
||||||
double lastBolusReported = 0;
|
double lastBolusReported = 0;
|
||||||
while (bolusRemaining != null) {
|
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) {
|
if (lastBolusReported != bolusRemaining) {
|
||||||
log.debug("Delivering bolus, remaining: " + bolusRemaining);
|
log.debug("Delivering bolus, remaining: " + bolusRemaining);
|
||||||
int percentDelivered = (int) (100 - (bolusRemaining / bolus * 100));
|
int percentDelivered = (int) (100 - (bolusRemaining / bolus * 100));
|
||||||
progressReportCallback.progress(BOLUSING, percentDelivered, bolus - bolusRemaining);
|
progressReportCallback.progress(BOLUSING, percentDelivered, bolus - bolusRemaining);
|
||||||
lastBolusReported = 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);
|
SystemClock.sleep(50);
|
||||||
bolusRemaining = (Double) scripter.currentMenu.getAttribute(MenuAttribute.BOLUS_REMAINING);
|
bolusRemaining = (Double) scripter.currentMenu.getAttribute(MenuAttribute.BOLUS_REMAINING);
|
||||||
}
|
}
|
||||||
|
@ -174,6 +196,11 @@ public class BolusCommand implements Command {
|
||||||
return (double) amountObj;
|
return (double) amountObj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void requestCancellation() {
|
||||||
|
cancelRequested = true;
|
||||||
|
progressReportCallback.progress(CANCELLING, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "BolusCommand{" +
|
return "BolusCommand{" +
|
||||||
|
|
|
@ -80,6 +80,8 @@ public class ComboPlugin implements PluginBase, PumpInterface {
|
||||||
@NonNull
|
@NonNull
|
||||||
volatile Date lastCmdTime = new Date(0);
|
volatile Date lastCmdTime = new Date(0);
|
||||||
volatile PumpState pumpState = new PumpState();
|
volatile PumpState pumpState = new PumpState();
|
||||||
|
@Nullable
|
||||||
|
private volatile BolusCommand runningBolusCommand;
|
||||||
|
|
||||||
private static PumpEnactResult OPERATION_NOT_SUPPORTED = new PumpEnactResult();
|
private static PumpEnactResult OPERATION_NOT_SUPPORTED = new PumpEnactResult();
|
||||||
|
|
||||||
|
@ -364,6 +366,33 @@ public class ComboPlugin implements PluginBase, PumpInterface {
|
||||||
return basal;
|
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,
|
// 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?
|
// writing carb treatments to the history table. What's PumpEnactResult for again?
|
||||||
@Override
|
@Override
|
||||||
|
@ -373,32 +402,10 @@ public class ComboPlugin implements PluginBase, PumpInterface {
|
||||||
// bolus needed, ask pump to deliver it
|
// bolus needed, ask pump to deliver it
|
||||||
EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance();
|
EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance();
|
||||||
MainApp.bus().post(bolusingEvent);
|
MainApp.bus().post(bolusingEvent);
|
||||||
CommandResult bolusCmdResult = runCommand(new BolusCommand(detailedBolusInfo.insulin, new ProgressReportCallback() {
|
// TODO move into enum as toString or so and make it translateb
|
||||||
@Override
|
runningBolusCommand = new BolusCommand(detailedBolusInfo.insulin, bolusProgressReportCallback);
|
||||||
public void progress(State state, int percent, double delivered) {
|
CommandResult bolusCmdResult = runCommand(runningBolusCommand);
|
||||||
EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance();
|
runningBolusCommand = null;
|
||||||
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);
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
PumpEnactResult pumpEnactResult = new PumpEnactResult();
|
PumpEnactResult pumpEnactResult = new PumpEnactResult();
|
||||||
pumpEnactResult.success = bolusCmdResult.success;
|
pumpEnactResult.success = bolusCmdResult.success;
|
||||||
pumpEnactResult.enacted = bolusCmdResult.enacted;
|
pumpEnactResult.enacted = bolusCmdResult.enacted;
|
||||||
|
@ -484,9 +491,7 @@ public class ComboPlugin implements PluginBase, PumpInterface {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stopBolusDelivering() {
|
public void stopBolusDelivering() {
|
||||||
// there's no way to stop the combo once delivery has started
|
if (runningBolusCommand != null) runningBolusCommand.requestCancellation();
|
||||||
// but before that, we could interrupt the command thread ... pause
|
|
||||||
// till pump times out or raises an error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note: AAPS calls this only to enact OpenAPS recommendations
|
// Note: AAPS calls this only to enact OpenAPS recommendations
|
||||||
|
|
Loading…
Reference in a new issue