From a95a00377b1888cbcb0c83beffa4ac3824f00683 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Fri, 24 Nov 2017 23:03:06 +0100 Subject: [PATCH] RuffyScripter: extract and extend runPreCommandChecks(). --- .../jotomo/ruffyscripter/RuffyScripter.java | 72 ++++++++++++------- 1 file changed, 45 insertions(+), 27 deletions(-) diff --git a/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java b/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java index 80d318c46c..a17608e951 100644 --- a/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java +++ b/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java @@ -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