RuffyScripter: extract and extend runPreCommandChecks().

This commit is contained in:
Johannes Mockenhaupt 2017-11-24 23:03:06 +01:00
parent b8e320ae6f
commit a95a00377b
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1

View file

@ -254,35 +254,9 @@ public class RuffyScripter implements RuffyCommands {
log.debug("Connection ready to execute cmd " + cmd);
Thread cmdThread = new Thread(() -> {
try {
// check pump in a suitable state to run the requested command
if (cmd instanceof ReadPumpStateCommand) {
// always allowed, state is set at the end of runCommand method
activeCmd.getResult().success = true;
} else if (getCurrentMenu().getType() == MenuType.STOP) {
if (cmd.needsRunMode()) {
log.error("Requested command requires run mode, but pump is suspended");
activeCmd.getResult().success = false;
return;
}
} else if (getCurrentMenu().getType() == MenuType.WARNING_OR_ERROR) {
if (!(cmd instanceof ConfirmAlertCommand)) {
log.warn("Warning/alert active on pump, but requested command is not ConfirmAlertCommand");
activeCmd.getResult().success = false;
return; // active alert is returned as part of PumpState
}
} else if (getCurrentMenu().getType() != MenuType.MAIN_MENU) {
log.debug("Pump is unexpectedly not on main menu but " + getCurrentMenuName());
activeCmd.getResult().success = false;
if (!runPreCommandChecks(cmd)) {
return;
}
// if everything broke before, the pump might still be delivering a bolus, if that's the case, wait for bolus to finish
Double bolusRemaining = (Double) getCurrentMenu().getAttribute(MenuAttribute.BOLUS_REMAINING);
while (ruffyService.isConnected() && bolusRemaining != null) {
log.debug("Waiting for bolus from previous connection to complete, remaining: " + bolusRemaining);
waitForScreenUpdate();
}
PumpState pumpState = readPumpStateInternal();
log.debug("Pump state before running command: " + pumpState);
@ -370,6 +344,50 @@ public class RuffyScripter implements RuffyCommands {
}
}
private boolean runPreCommandChecks(Command cmd) {
if (cmd instanceof ReadPumpStateCommand) {
// always allowed, state is set at the end of runCommand method
activeCmd.getResult().success = true;
} else if (getCurrentMenu().getType() == MenuType.STOP) {
if (cmd.needsRunMode()) {
log.error("Requested command requires run mode, but pump is suspended");
activeCmd.getResult().success = false;
return false;
}
} else if (getCurrentMenu().getType() == MenuType.WARNING_OR_ERROR) {
if (!(cmd instanceof ConfirmAlertCommand)) {
log.warn("Warning/alert active on pump, but requested command is not ConfirmAlertCommand");
activeCmd.getResult().success = false;
return false;
}
} else if (getCurrentMenu().getType() != MenuType.MAIN_MENU) {
log.debug("Pump is unexpectedly not on main menu but " + getCurrentMenuName() + ", trying to recover");
try {
recoverFromCommandFailure();
} catch (Exception e) {
activeCmd.getResult().success = false;
return false;
}
if (getCurrentMenu().getType() != MenuType.MAIN_MENU) {
activeCmd.getResult().success = false;
return false;
}
}
// if everything broke before, the pump might still be delivering a bolus, if that's the case, wait for bolus to finish
Double bolusRemaining = (Double) getCurrentMenu().getAttribute(MenuAttribute.BOLUS_REMAINING);
try {
while (ruffyService.isConnected() && bolusRemaining != null) {
log.debug("Waiting for bolus from previous connection to complete, remaining: " + bolusRemaining);
waitForScreenUpdate();
}
} catch (Exception e) {
log.error("Exception waiting for bolus from previous command to finish", e);
return false;
}
return true;
}
/**
* On connection loss the pump raises an alert immediately (when setting a TBR or giving a bolus) -
* there's no timeout before that happens. But: a reconnect is still possible which can then