From f4c1f4a3a15bddaffe3ddfd1c333ef12ab8826a5 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Tue, 19 Dec 2017 23:43:08 +0100 Subject: [PATCH] Make recovering from command failure more robust. --- .../jotomo/ruffyscripter/RuffyScripter.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java b/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java index 73258f931c..006fd38e5f 100644 --- a/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java +++ b/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java @@ -324,12 +324,12 @@ public class RuffyScripter implements RuffyCommands { return result; } catch (CommandException e) { log.error("CommandException while executing command", e); - recoverFromCommandFailure(); - return activeCmd.getResult().success(false).state(readPumpStateInternal()); + PumpState pumpState = recoverFromCommandFailure(); + return activeCmd.getResult().success(false).state(pumpState); } catch (Exception e) { log.error("Unexpected exception communication with ruffy", e); - recoverFromCommandFailure(); - return activeCmd.getResult().success(false).state(readPumpStateInternal()); + PumpState pumpState = recoverFromCommandFailure(); + return activeCmd.getResult().success(false).state(pumpState); } finally { Menu menu = this.currentMenu; if (activeCmd.getResult().success && menu != null && menu.getType() != MenuType.MAIN_MENU) { @@ -416,12 +416,12 @@ public class RuffyScripter implements RuffyCommands { /** * Returns to the main menu (if possible) after a command failure, so that subsequent commands - * reusing the connection won't fail. + * reusing the connection won't fail and returns the current PumpState (empty if unreadable). */ - private void recoverFromCommandFailure() { + private PumpState recoverFromCommandFailure() { Menu menu = this.currentMenu; if (menu == null) { - return; + return null; } MenuType type = menu.getType(); if (type != MenuType.WARNING_OR_ERROR && type != MenuType.MAIN_MENU) { @@ -432,6 +432,12 @@ public class RuffyScripter implements RuffyCommands { log.warn("Error returning to main menu, when trying to recover from command failure", e); } } + try { + return readPumpStateInternal(); + } catch (Exception e) { + log.debug("Reading pump state during recovery failed", e); + return new PumpState(); + } } /** @@ -480,6 +486,7 @@ public class RuffyScripter implements RuffyCommands { state.timestamp = System.currentTimeMillis(); Menu menu = currentMenu; if (menu == null) { + log.debug("Returning empty PumpState, menu is unavailable"); return state; }