Bolus cancellation for all stages.

This commit is contained in:
Johannes Mockenhaupt 2017-10-31 11:01:58 +01:00
parent 39b89df484
commit 9121aa5217
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1
4 changed files with 52 additions and 17 deletions

View file

@ -56,6 +56,9 @@ public class ComboPlugin implements PluginBase, PumpInterface {
private static ComboPlugin plugin = null; private static ComboPlugin plugin = null;
private volatile boolean bolusInProgress;
private volatile boolean cancelBolus;
public static ComboPlugin getPlugin() { public static ComboPlugin getPlugin() {
if (plugin == null) if (plugin == null)
plugin = new ComboPlugin(); plugin = new ComboPlugin();
@ -400,6 +403,7 @@ public class ComboPlugin implements PluginBase, PumpInterface {
return pumpEnactResult; return pumpEnactResult;
} }
} finally { } finally {
cancelBolus = false;
MainApp.bus().post(new EventComboPumpUpdateGUI()); MainApp.bus().post(new EventComboPumpUpdateGUI());
} }
} }
@ -407,12 +411,22 @@ public class ComboPlugin implements PluginBase, PumpInterface {
@NonNull @NonNull
private PumpEnactResult deliverBolus(final DetailedBolusInfo detailedBolusInfo) { private PumpEnactResult deliverBolus(final DetailedBolusInfo detailedBolusInfo) {
// TODO // TODO
// before non-SMB: check enough insulin is available, check we're up to date on boluses // before non-SMB: check enough insulin is available, check we're up to date on boluses
// after bolus: update reservoir level and check the bolus we just did is actually there // after bolus: update reservoir level and check the bolus we just did is actually there
if (cancelBolus) {
PumpEnactResult pumpEnactResult = new PumpEnactResult();
pumpEnactResult.success = true;
pumpEnactResult.enacted = false;
return pumpEnactResult;
}
bolusInProgress = true;
// retry flag: reconnect, kill warning, check if command can be restarted, restart // retry flag: reconnect, kill warning, check if command can be restarted, restart
CommandResult bolusCmdResult = runCommand(MainApp.sResources.getString(R.string.combo_action_bolusing), () -> ruffyScripter.deliverBolus(detailedBolusInfo.insulin, CommandResult bolusCmdResult = runCommand(MainApp.sResources.getString(R.string.combo_pump_action_bolusing), 0,
detailedBolusInfo.isSMB ? nullBolusProgressReporter : bolusProgressReporter)); () -> ruffyScripter.deliverBolus(detailedBolusInfo.insulin,
detailedBolusInfo.isSMB ? nullBolusProgressReporter : bolusProgressReporter));
bolusInProgress = false;
PumpEnactResult pumpEnactResult = new PumpEnactResult(); PumpEnactResult pumpEnactResult = new PumpEnactResult();
pumpEnactResult.success = bolusCmdResult.success; pumpEnactResult.success = bolusCmdResult.success;
@ -438,9 +452,11 @@ public class ComboPlugin implements PluginBase, PumpInterface {
@Override @Override
public void stopBolusDelivering() { public void stopBolusDelivering() {
// TODO note that we requested this, so we can thandle this proper in runCommand; if (bolusInProgress) {
// or is it fine if the command returns success with noting enacted and history checks as well/**/ ruffyScripter.cancelBolus();
ruffyScripter.cancelBolus(); } else {
cancelBolus = true;
}
} }
// Note: AAPS calls this only to enact OpenAPS recommendations // Note: AAPS calls this only to enact OpenAPS recommendations

View file

@ -1,10 +1,17 @@
package de.jotomo.ruffy.spi.history; package de.jotomo.ruffy.spi.history;
import android.support.annotation.Nullable;
public class WarningOrErrorCode { public class WarningOrErrorCode {
@Nullable
public final Integer warningCode; public final Integer warningCode;
@Nullable
public final Integer errorCode; public final Integer errorCode;
public WarningOrErrorCode(Integer warningCode, Integer errorCode) { public WarningOrErrorCode(@Nullable Integer warningCode, @Nullable Integer errorCode) {
if (warningCode == null && errorCode == null) {
throw new IllegalArgumentException("Either code must be non-null");
}
this.warningCode = warningCode; this.warningCode = warningCode;
this.errorCode = errorCode; this.errorCode = errorCode;
} }

View file

@ -7,6 +7,7 @@ import android.content.ServiceConnection;
import android.os.IBinder; import android.os.IBinder;
import android.os.RemoteException; import android.os.RemoteException;
import android.os.SystemClock; import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
@ -502,6 +503,8 @@ public class RuffyScripter implements RuffyCommands {
// === pump ops === // === pump ops ===
public Menu getCurrentMenu() { public Menu getCurrentMenu() {
if (Thread.currentThread().isInterrupted())
throw new CommandException("Interrupted");
long timeout = System.currentTimeMillis() + 5 * 1000; long timeout = System.currentTimeMillis() + 5 * 1000;
// TODO this is probably due to a disconnect and rtDisconnect having nulled currentMenu. // TODO this is probably due to a disconnect and rtDisconnect having nulled currentMenu.
// This here might just work, but needs a more controlled approach when implementing // This here might just work, but needs a more controlled approach when implementing
@ -569,11 +572,13 @@ public class RuffyScripter implements RuffyCommands {
// TODO sort out usages of this method and waitForMenu update, which have the same intent, // TODO sort out usages of this method and waitForMenu update, which have the same intent,
// but approach things differently; // but approach things differently;
private void waitForScreenUpdate() { private void waitForScreenUpdate() {
if (Thread.currentThread().isInterrupted())
throw new CommandException("Interrupted");
synchronized (screenlock) { synchronized (screenlock) {
try { try {
screenlock.wait((long) 2000); // usually ~500, occassionally up to 1100ms screenlock.wait((long) 2000); // updates usually come in every ~500, occasionally up to 1100ms
} catch (Exception e) { } catch (InterruptedException e) {
log.debug("Ignoring exception in wait for screenlock", e); throw new CommandException("Interrupted");
} }
} }
} }
@ -601,6 +606,8 @@ public class RuffyScripter implements RuffyCommands {
} }
private void pressKey(final byte key) { private void pressKey(final byte key) {
if (Thread.currentThread().isInterrupted())
throw new CommandException("Interrupted");
try { try {
ruffyService.rtSendKey(key, true); ruffyService.rtSendKey(key, true);
SystemClock.sleep(150); SystemClock.sleep(150);
@ -701,6 +708,8 @@ public class RuffyScripter implements RuffyCommands {
public void cancelBolus() { public void cancelBolus() {
if (activeCmd instanceof BolusCommand) { if (activeCmd instanceof BolusCommand) {
((BolusCommand) activeCmd).requestCancellation(); ((BolusCommand) activeCmd).requestCancellation();
} else {
log.error("cancelBolus called, but active command is not a bolus:" + activeCmd);
} }
} }

View file

@ -104,18 +104,19 @@ public class BolusCommand extends BaseCommand {
// wait for bolus delivery to complete; the remaining units to deliver are counted down // wait for bolus delivery to complete; the remaining units to deliver are counted down
boolean cancelInProgress = false; boolean cancelInProgress = false;
double lastBolusReported = 0; Double lastBolusReported = 0d;
Double bolusRemaining = (Double) scripter.getCurrentMenu().getAttribute(MenuAttribute.BOLUS_REMAINING); Double bolusRemaining = (Double) scripter.getCurrentMenu().getAttribute(MenuAttribute.BOLUS_REMAINING);
while (bolusRemaining != null) { while (bolusRemaining != null || scripter.getCurrentMenu().getType() == MenuType.WARNING_OR_ERROR) {
if (cancelRequested && !cancelInProgress) { if (cancelRequested && !cancelInProgress) {
bolusProgressReporter.report(STOPPING, 0, 0); bolusProgressReporter.report(STOPPING, 0, 0);
cancelInProgress = true; cancelInProgress = true;
new Thread(() -> scripter.pressKeyMs(RuffyScripter.Key.UP, 3000), "bolus-canceller").start(); new Thread(() ->
scripter.pressKeyMs(RuffyScripter.Key.UP, 3000), "bolus-canceller").start();
} }
if (scripter.getCurrentMenu().getType() == MenuType.WARNING_OR_ERROR) { if (scripter.getCurrentMenu().getType() == MenuType.WARNING_OR_ERROR) {
// confirm warning alerts and update the result to indicate alerts occurred // confirm warning alert and update the result to indicate alerts occurred
WarningOrErrorCode warningOrErrorCode = scripter.readWarningOrErrorCode(); WarningOrErrorCode warningOrErrorCode = scripter.readWarningOrErrorCode();
if (warningOrErrorCode.errorCode != 0) { if (warningOrErrorCode.errorCode != null) {
throw new CommandException("Pump is in error state"); throw new CommandException("Pump is in error state");
} }
int warningCode = warningOrErrorCode.warningCode; int warningCode = warningOrErrorCode.warningCode;
@ -130,9 +131,11 @@ public class BolusCommand extends BaseCommand {
} else if (warningCode == PumpWarningCodes.BATTERY_LOW) { } else if (warningCode == PumpWarningCodes.BATTERY_LOW) {
scripter.confirmAlert(PumpWarningCodes.BATTERY_LOW, 2000); scripter.confirmAlert(PumpWarningCodes.BATTERY_LOW, 2000);
result.alertConfirmed = true; result.alertConfirmed = true;
} else {
throw new CommandException("Pump is showing exotic warning: " + warningCode);
} }
} }
if (lastBolusReported != bolusRemaining) { if (bolusRemaining != null && !bolusRemaining.equals(lastBolusReported)) {
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));
bolusProgressReporter.report(DELIVERING, percentDelivered, bolus - bolusRemaining); bolusProgressReporter.report(DELIVERING, percentDelivered, bolus - bolusRemaining);