From a2460180f9a14407ae58b82c7178aa8779f54b3b Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sun, 29 Oct 2017 11:37:53 +0100 Subject: [PATCH] Remove unclear error message field from CommandResult, use codes from pump warnings/errors. --- .../plugins/PumpCombo/ComboFragment.java | 4 +- .../plugins/PumpCombo/ComboPlugin.java | 7 +-- .../de/jotomo/ruffy/spi/CommandResult.java | 9 ---- .../java/de/jotomo/ruffy/spi/PumpState.java | 24 +++------ .../de/jotomo/ruffy/spi/RuffyCommands.java | 1 + .../ruffy/spi/history/WarningOrErrorCode.java | 12 +++++ .../jotomo/ruffyscripter/RuffyScripter.java | 49 +++++++++++-------- .../ruffyscripter/commands/BolusCommand.java | 8 ++- .../ruffyscripter/commands/SetTbrCommand.java | 6 +-- 9 files changed, 60 insertions(+), 60 deletions(-) create mode 100644 ruffy-spi/src/main/java/de/jotomo/ruffy/spi/history/WarningOrErrorCode.java diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboFragment.java index 28f2f2e94c..e0e1d2f22d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboFragment.java @@ -88,9 +88,7 @@ public class ComboFragment extends SubscriberFragment implements View.OnClickLis // state stateView.setText(plugin.getStateSummary()); PumpState ps = plugin.getPump().state; - if (plugin.getPump().state.errorMsg != null - || ps.insulinState == PumpState.EMPTY - || ps.batteryState == PumpState.EMPTY) { + if ( ps.insulinState == PumpState.EMPTY || ps.batteryState == PumpState.EMPTY) { stateView.setTextColor(Color.RED); } else if (plugin.getPump().state.suspended) { stateView.setTextColor(Color.YELLOW); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java index 5af9abc346..958dff424b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java @@ -417,7 +417,6 @@ public class ComboPlugin implements PluginBase, PumpInterface { PumpEnactResult pumpEnactResult = new PumpEnactResult(); pumpEnactResult.success = bolusCmdResult.success; pumpEnactResult.enacted = bolusCmdResult.enacted; -// pumpEnactResult.comment = bolusCmdResult.message; // if enacted, add bolus and carbs to treatment history if (pumpEnactResult.enacted) { @@ -500,7 +499,6 @@ public class ComboPlugin implements PluginBase, PumpInterface { PumpEnactResult pumpEnactResult = new PumpEnactResult(); pumpEnactResult.success = commandResult.success; pumpEnactResult.enacted = commandResult.enacted; -// pumpEnactResult.comment = commandResult.message; pumpEnactResult.isPercent = true; // Combo would have bailed if this wasn't set properly. Maybe we should // have the command return this anyways ... @@ -570,7 +568,6 @@ public class ComboPlugin implements PluginBase, PumpInterface { if (commandResult != null) { pumpEnactResult.success = commandResult.success; pumpEnactResult.enacted = commandResult.enacted; -// pumpEnactResult.comment = commandResult.message; } return pumpEnactResult; } @@ -752,8 +749,8 @@ public class ComboPlugin implements PluginBase, PumpInterface { extendedJson.put("TempBasalPercent", ps.tbrPercent); extendedJson.put("TempBasalRemaining", ps.tbrRemainingDuration); } - if (ps.errorMsg != null) { - extendedJson.put("ErrorMessage", ps.errorMsg); + if (ps.alertCodes.errorCode != null) { + extendedJson.put("ErrorCode", ps.alertCodes.errorCode); } pumpJson.put("extended", extendedJson); diff --git a/ruffy-spi/src/main/java/de/jotomo/ruffy/spi/CommandResult.java b/ruffy-spi/src/main/java/de/jotomo/ruffy/spi/CommandResult.java index c780a8fd13..360c171102 100644 --- a/ruffy-spi/src/main/java/de/jotomo/ruffy/spi/CommandResult.java +++ b/ruffy-spi/src/main/java/de/jotomo/ruffy/spi/CommandResult.java @@ -13,9 +13,6 @@ public class CommandResult { public boolean enacted; /** Null unless an unhandled exception was raised. */ public Exception exception; - /** (Error)message describing the result of the command. */ - // TODO work outh this message -// public String message; /** State of the pump *after* command execution. */ public PumpState state; /** History if requested by the command. */ @@ -60,11 +57,6 @@ public class CommandResult { return this; } -// public CommandResult message(String message) { -// this.message = message; -// return this; -// } - public CommandResult state(PumpState state) { this.state = state; return this; @@ -86,7 +78,6 @@ public class CommandResult { ", success=" + success + ", enacted=" + enacted + ", exception=" + exception + -// ", message='" + message + '\'' + ", state=" + state + ", history=" + history + ", basalProfile=" + basalProfile + diff --git a/ruffy-spi/src/main/java/de/jotomo/ruffy/spi/PumpState.java b/ruffy-spi/src/main/java/de/jotomo/ruffy/spi/PumpState.java index 0e96b36887..dccb886948 100644 --- a/ruffy-spi/src/main/java/de/jotomo/ruffy/spi/PumpState.java +++ b/ruffy-spi/src/main/java/de/jotomo/ruffy/spi/PumpState.java @@ -1,9 +1,13 @@ package de.jotomo.ruffy.spi; +import de.jotomo.ruffy.spi.history.WarningOrErrorCode; + /** State displayed on the main screen of the pump. */ public class PumpState { public long timestamp; public String menu = null; + public boolean suspended; + public boolean tbrActive = false; /** TBR percentage. 100% means no TBR active, just the normal basal rate running. */ public int tbrPercent = -1; @@ -12,16 +16,10 @@ public class PumpState { /** Remaining time of an active TBR. Note that 0:01 is te lowest displayed, the pump * jumps from that to TBR end, skipping 0:00(xx). */ public int tbrRemainingDuration = -1; - /** - * This is the error message (if any) displayed by the pump if there is an alarm, - * e.g. if a "TBR cancelled alarm" is active, the value will be "TBR CANCELLED". - * Generally, an error code is also displayed, but it flashes and it might take - * longer to read that and the pump connection gets interrupted if we're not - * reacting quickly. - */ - // TODO pump errors (EXX) vs. errors talking to the pump - public String errorMsg; - public boolean suspended; + + /** Warning or error code displayed if a warning or alert alert is active, + * see {@link PumpWarningCodes}, {@link PumpErrorCodes} */ + public WarningOrErrorCode alertCodes; public static final int UNKNOWN = -1; public static final int LOW = 1; @@ -56,11 +54,6 @@ public class PumpState { return this; } - public PumpState errorMsg(String errorMsg) { - this.errorMsg = errorMsg; - return this; - } - public PumpState suspended(boolean suspended) { this.suspended = suspended; return this; @@ -89,7 +82,6 @@ public class PumpState { ", tbrPercent=" + tbrPercent + ", tbrRate=" + tbrRate + ", tbrRemainingDuration=" + tbrRemainingDuration + - ", errorMsg='" + errorMsg + '\'' + ", suspended=" + suspended + ", batteryState=" + batteryState + ", insulinState=" + insulinState + diff --git a/ruffy-spi/src/main/java/de/jotomo/ruffy/spi/RuffyCommands.java b/ruffy-spi/src/main/java/de/jotomo/ruffy/spi/RuffyCommands.java index e955e64a0a..2daaf4655e 100644 --- a/ruffy-spi/src/main/java/de/jotomo/ruffy/spi/RuffyCommands.java +++ b/ruffy-spi/src/main/java/de/jotomo/ruffy/spi/RuffyCommands.java @@ -31,6 +31,7 @@ public interface RuffyCommands { CommandResult getDateAndTime(); + // TODO see how dana does this, autosync on DST change CommandResult setDateAndTime(Date date); void requestPairing(); diff --git a/ruffy-spi/src/main/java/de/jotomo/ruffy/spi/history/WarningOrErrorCode.java b/ruffy-spi/src/main/java/de/jotomo/ruffy/spi/history/WarningOrErrorCode.java new file mode 100644 index 0000000000..371ce069ee --- /dev/null +++ b/ruffy-spi/src/main/java/de/jotomo/ruffy/spi/history/WarningOrErrorCode.java @@ -0,0 +1,12 @@ +package de.jotomo.ruffy.spi.history; + +public class WarningOrErrorCode { + public final Integer warningCode; + public final Integer errorCode; + + public WarningOrErrorCode(Integer warningCode, Integer errorCode) { + this.warningCode = warningCode; + this.errorCode = errorCode; + } +} + diff --git a/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java b/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java index 75ae1f590e..fdc0786296 100644 --- a/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java +++ b/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java @@ -30,6 +30,7 @@ import de.jotomo.ruffy.spi.CommandResult; import de.jotomo.ruffy.spi.PumpState; import de.jotomo.ruffy.spi.RuffyCommands; import de.jotomo.ruffy.spi.history.PumpHistoryRequest; +import de.jotomo.ruffy.spi.history.WarningOrErrorCode; import de.jotomo.ruffyscripter.commands.BolusCommand; import de.jotomo.ruffyscripter.commands.CancelTbrCommand; import de.jotomo.ruffyscripter.commands.Command; @@ -218,13 +219,11 @@ public class RuffyScripter implements RuffyCommands { /** Always returns a CommandResult, never throws */ private CommandResult runCommand(final Command cmd) { log.debug("Attempting to run cmd: " + cmd); - if (unrecoverableError != null) { - return new CommandResult().success(false).enacted(false).message(unrecoverableError).state(readPumpStateInternal()); - } List violations = cmd.validateArguments(); if (!violations.isEmpty()) { - return new CommandResult().message(Joiner.on("\n").join(violations)).state(readPumpStateInternal()); + log.error("Command argument violations: " + Joiner.on(", ").join(violations)); + return new CommandResult().state(readPumpStateInternal()); } // TODO simplify, hard to reason about exists @@ -240,7 +239,8 @@ public class RuffyScripter implements RuffyCommands { Menu localCurrentMenu = currentMenu; if (localCurrentMenu == null || localCurrentMenu.getType() == MenuType.STOP) { if (cmd.needsRunMode()) { - activeCmd.getResult().success(false).message("Pump is suspended but operations requires to the pump to be running"); + log.error("Requested command requires run mode, but pump is suspended"); + activeCmd.getResult().success = false; return; } } @@ -255,10 +255,11 @@ public class RuffyScripter implements RuffyCommands { long cmdEndTime = System.currentTimeMillis(); log.debug("Executing " + cmd + " took " + (cmdEndTime - cmdStartTime) + "ms"); } catch (CommandException e) { - activeCmd.getResult().message(e.getMessage()); + log.error("CommandException running command", e); + activeCmd.getResult().success = false; } catch (Exception e) { log.error("Unexpected exception running cmd", e); - activeCmd.getResult().message("Unexpected exception running cmd"); + activeCmd.getResult().success = false; } finally { lastCmdExecutionTime = System.currentTimeMillis(); } @@ -294,14 +295,13 @@ public class RuffyScripter implements RuffyCommands { cmdThread.interrupt(); SystemClock.sleep(5000); log.error("Timed out thread dead yet? " + cmdThread.isAlive()); - activeCmd.getResult().success(false).message("Command stalled, check pump!"); + activeCmd.getResult().success = false; break; } } if (now > overallTimeout) { - String msg = "Command " + cmd + " timed out after 4 min, check pump!"; - log.error(msg); - activeCmd.getResult().success(false).message(msg); + log.error("Command " + cmd + " timed out"); + activeCmd.getResult().success = false; break; } } @@ -315,12 +315,13 @@ public class RuffyScripter implements RuffyCommands { log.debug("Connect: " + connectDurationSec + "s, execution: " + executionDurationSec + "s"); } return result; + // TOD under which circumstances can these occur? } catch (CommandException e) { - return activeCmd.getResult().success(false).message(e.getMessage()).state(readPumpStateInternal()); + log.error("CommandException while executing command", e); + return activeCmd.getResult().success(false).state(readPumpStateInternal()); } catch (Exception e) { log.error("Unexpected exception communication with ruffy", e); - return activeCmd.getResult().success(false).exception(e) - .message("Unexpected exception communication with ruffy: " + e.getMessage()).state(readPumpStateInternal()); + return activeCmd.getResult().success(false).exception(e).state(readPumpStateInternal()); } finally { activeCmd = null; } @@ -473,25 +474,26 @@ public class RuffyScripter implements RuffyCommands { state.batteryState = ((int) menu.getAttribute(MenuAttribute.BATTERY_STATE)); state.insulinState = ((int) menu.getAttribute(MenuAttribute.INSULIN_STATE)); } else if (menuType == MenuType.WARNING_OR_ERROR) { - state.errorMsg = (String) menu.getAttribute(MenuAttribute.MESSAGE); + state.alertCodes = readWarningOrErrorCode(); } else if (menuType == MenuType.STOP) { state.suspended = true; state.batteryState = ((int) menu.getAttribute(MenuAttribute.BATTERY_STATE)); state.insulinState = ((int) menu.getAttribute(MenuAttribute.INSULIN_STATE)); - } else { - // just return the PumpState with the menu set } + return state; } - public int readWarningCode() { + public WarningOrErrorCode readWarningOrErrorCode() { verifyMenuIsDisplayed(MenuType.WARNING_OR_ERROR); Integer warningCode = (Integer) getCurrentMenu().getAttribute(MenuAttribute.WARNING); - while (warningCode == null) { + Integer errorCode = (Integer) getCurrentMenu().getAttribute(MenuAttribute.ERROR); + while (warningCode == null && errorCode == null) { waitForScreenUpdate(); warningCode = (Integer) getCurrentMenu().getAttribute(MenuAttribute.WARNING); + errorCode = (Integer) getCurrentMenu().getAttribute(MenuAttribute.ERROR); } - return warningCode; + return new WarningOrErrorCode(warningCode, errorCode); } // below: methods to be used by commands @@ -810,7 +812,12 @@ public class RuffyScripter implements RuffyCommands { // A wait till the error code can be read results in the code hanging, despite // menu updates coming in, so just check the message. - int displayedWarningCode = readWarningCode(); + WarningOrErrorCode warningOrErrorCode = readWarningOrErrorCode(); + if (warningOrErrorCode.errorCode != 0) { + // TODO proper way to display such things in the UI; + throw new CommandException("Pump is in error state"); + } + int displayedWarningCode = warningOrErrorCode.warningCode; String errorMsg = (String) getCurrentMenu().getAttribute(MenuAttribute.MESSAGE); if (displayedWarningCode != warningCode) { throw new CommandException("An alert other than the expected warning " + warningCode+ " was raised by the pump: " diff --git a/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/commands/BolusCommand.java b/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/commands/BolusCommand.java index 3db4c8cd8b..348bcaf489 100644 --- a/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/commands/BolusCommand.java +++ b/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/commands/BolusCommand.java @@ -13,6 +13,7 @@ import java.util.List; import de.jotomo.ruffy.spi.BolusProgressReporter; import de.jotomo.ruffy.spi.CommandResult; import de.jotomo.ruffy.spi.PumpWarningCodes; +import de.jotomo.ruffy.spi.history.WarningOrErrorCode; import de.jotomo.ruffyscripter.RuffyScripter; import static de.jotomo.ruffy.spi.BolusProgressReporter.State.DELIVERED; @@ -108,7 +109,11 @@ public class BolusCommand extends BaseCommand { } if (scripter.getCurrentMenu().getType() == MenuType.WARNING_OR_ERROR) { // confirm warning alerts and update the result to indicate alerts occurred - int warningCode = scripter.readWarningCode(); + WarningOrErrorCode warningOrErrorCode = scripter.readWarningOrErrorCode(); + if (warningOrErrorCode.errorCode != 0) { + throw new CommandException("Pump is in error state"); + } + int warningCode = warningOrErrorCode.warningCode; if (warningCode == PumpWarningCodes.BOLUS_CANCELLED) { scripter.confirmAlert(PumpWarningCodes.BOLUS_CANCELLED, 2000); bolusProgressReporter.report(STOPPED, 0, 0); @@ -128,7 +133,6 @@ public class BolusCommand extends BaseCommand { bolusProgressReporter.report(DELIVERING, percentDelivered, bolus - bolusRemaining); lastBolusReported = bolusRemaining; } - SystemClock.sleep(50); bolusRemaining = (Double) scripter.getCurrentMenu().getAttribute(MenuAttribute.BOLUS_REMAINING); } diff --git a/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java b/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java index a1dccd4b65..4a8b3a5be3 100644 --- a/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java +++ b/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java @@ -10,7 +10,6 @@ import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.List; -import java.util.Locale; import de.jotomo.ruffy.spi.PumpWarningCodes; @@ -89,11 +88,10 @@ public class SetTbrCommand extends BaseCommand { // check main menu shows the same values we just set if (cancellingTbr) { verifyMainMenuShowsNoActiveTbr(); - result.success(true).enacted(true).message("TBR was cancelled"); + result.success(true).enacted(true); } else { verifyMainMenuShowsExpectedTbrActive(); - result.success(true).enacted(true) - .message(String.format(Locale.US, "TBR set to %d%% for %d min", percentage, duration)); + result.success(true).enacted(true); } }