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 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{" +

View file

@ -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,18 +366,9 @@ public class ComboPlugin implements PluginBase, PumpInterface {
return basal; return basal;
} }
// what a mess: pump integration code reading carb info from Detailed**Bolus**Info, private static ProgressReportCallback bolusProgressReportCallback = new ProgressReportCallback() {
// writing carb treatments to the history table. What's PumpEnactResult for again?
@Override @Override
public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) { public void progress(ProgressReportCallback.State state, int percent, double delivered) {
if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) {
if (detailedBolusInfo.insulin > 0) {
// 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(); EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance();
switch (state) { switch (state) {
// TODO move into enum as toString or so and make it translateb // TODO move into enum as toString or so and make it translateb
@ -398,7 +391,21 @@ public class ComboPlugin implements PluginBase, PumpInterface {
bolusingEvent.percent = percent; bolusingEvent.percent = percent;
MainApp.bus().post(bolusingEvent); 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
public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) {
if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) {
if (detailedBolusInfo.insulin > 0) {
// bolus needed, ask pump to deliver it
EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance();
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 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