From 58757735f8e684933af6b2b77c23a4b9401eeaac Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Fri, 1 Sep 2017 10:45:06 +0200 Subject: [PATCH] Refactor. (cherry picked from commit 06a454d) --- .../jotomo/ruffyscripter/RuffyScripter.java | 57 ++++--------------- .../ruffyscripter/commands/BolusCommand.java | 8 +-- .../commands/CancellableBolusCommand.java | 10 ++-- 3 files changed, 19 insertions(+), 56 deletions(-) diff --git a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java index 5b21f348c8..24aeb792eb 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java @@ -193,6 +193,16 @@ public class RuffyScripter { this.ruffyService = null; } + public void returnToMainMenu() { + // returning to main menu using the 'back' key should not cause a vibration + // TODO this is too brute-force; at least check for WARNING_OR_ERROR menu type + do { + log.debug("Going back to main menu, currently at " + getCurrentMenu().getType()); + pressBackKey(); + waitForMenuUpdate(); + } while (getCurrentMenu().getType() != MenuType.MAIN_MENU); + } + private static class Returnable { CommandResult cmdResult; } @@ -540,49 +550,11 @@ public class RuffyScripter { return true; } - public boolean goToMainTypeScreen(MenuType screen, long timeout) { - long start = System.currentTimeMillis(); - while ((currentMenu == null || currentMenu.getType() != screen) && start + timeout > System.currentTimeMillis()) { - if (currentMenu != null && currentMenu.getType() == MenuType.WARNING_OR_ERROR) { - throw new CommandException().message("Warning/errors raised by pump, please check pump"); - // since warnings and errors can occur at any time, they should be dealt with in - // a more general way, see the handleMenuUpdate callback above - //FIXME bad thing to do :D - // yup, commenting this out since I don't want an occlusionn alert to hidden by this :-) - //pressCheckKey(); - } else if (currentMenu != null && !currentMenu.getType().isMaintype()) { - pressBackKey(); - } else - pressMenuKey(); - waitForScreenUpdate(250); - } - return currentMenu != null && currentMenu.getType() == screen; - } - - public boolean enterMenu(MenuType startType, MenuType targetType, byte key, long timeout) { - if (currentMenu.getType() == targetType) - return true; - if (currentMenu == null || currentMenu.getType() != startType) - return false; - long start = System.currentTimeMillis(); - pressKey(key, 2000); - while ((currentMenu == null || currentMenu.getType() != targetType) && start + timeout > System.currentTimeMillis()) { - waitForScreenUpdate(100); - } - return currentMenu != null && currentMenu.getType() == targetType; - } - - public void step(int steps, byte key, long timeout) { - for (int i = 0; i < Math.abs(steps); i++) - pressKey(key, timeout); - } - // TODO v2, rework these two methods: waitForMenuUpdate shoud only be used by commands // then anything longer than a few seconds is an error; // only ensureConnected() uses the method with the timeout parameter; inline that code, // so we can use a custom timeout and give a better error message in case of failure - // TODO confirmAlarms? and report back which were cancelled? /** Confirms and dismisses the given alert if it's raised before the timeout */ @@ -615,10 +587,7 @@ public class RuffyScripter { return alertProcessed; } - /** - * Wait until the menu update is in - */ - // TODO donn't use this in ensureConnected + /** Wait until the menu is updated */ public void waitForMenuUpdate() { waitForMenuUpdate(60, "Timeout waiting for menu update"); } @@ -669,9 +638,7 @@ public class RuffyScripter { } } - /** - * Wait till a menu changed has completed, "away" from the menu provided as argument. - */ + /** Wait till a menu changed has completed, "away" from the menu provided as argument. */ public void waitForMenuToBeLeft(MenuType menuType) { long timeout = System.currentTimeMillis() + 60 * 1000; while (currentMenu.getType() == menuType) { diff --git a/app/src/main/java/de/jotomo/ruffyscripter/commands/BolusCommand.java b/app/src/main/java/de/jotomo/ruffyscripter/commands/BolusCommand.java index a3374a2e11..4e689a851d 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/BolusCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/BolusCommand.java @@ -100,13 +100,7 @@ public class BolusCommand extends BaseCommand { log.debug("Bolus record in history confirms delivered bolus"); if(SP.getBoolean(R.string.key_combo_enable_experimental_features, false)) { - // returning to main menu using the 'back' key should not cause a vibration - // TODO this is too brute-force; at least check for WARNING_OR_ERROR menu type - do { - log.debug("Going back to main menu, currently at " + scripter.getCurrentMenu().getType()); - scripter.pressBackKey(); - scripter.waitForMenuUpdate(); - } while (scripter.getCurrentMenu().getType() != MenuType.MAIN_MENU); + scripter.returnToMainMenu(); } else { // leave menu to go back to main menu scripter.pressCheckKey(); diff --git a/app/src/main/java/de/jotomo/ruffyscripter/commands/CancellableBolusCommand.java b/app/src/main/java/de/jotomo/ruffyscripter/commands/CancellableBolusCommand.java index 409a2eb8db..b1194ae0c6 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/CancellableBolusCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/CancellableBolusCommand.java @@ -52,7 +52,7 @@ public class CancellableBolusCommand extends BolusCommand { if (cancelRequested) { progressReportCallback.report(STOPPING, 0, 0); - scripter.goToMainTypeScreen(MenuType.MAIN_MENU, 30 * 1000); + scripter.returnToMainMenu(); progressReportCallback.report(STOPPED, 0, 0); return new CommandResult().success(true).enacted(false) .message("Bolus cancelled as per user request with no insulin delivered"); @@ -69,7 +69,7 @@ public class CancellableBolusCommand extends BolusCommand { scripter.pressUpKey(); // wait up to 1s for a BOLUS_CANCELLED alert, if it doesn't happen we missed // the window, simply continue and let the next cancel attempt try its luck - boolean alertWasCancelled = confirmAlert("BOLUS CANCELLED", 1000); + boolean alertWasCancelled = scripter.confirmAlert("BOLUS CANCELLED", 1000); if (alertWasCancelled) { progressReportCallback.report(STOPPED, 0, 0); return new CommandResult().success(true).enacted(false) @@ -120,7 +120,7 @@ public class CancellableBolusCommand extends BolusCommand { String message = (String) scripter.getCurrentMenu().getAttribute(MenuAttribute.MESSAGE); if (message.equals("LOW CARTRIDGE")) { lowCartdrigeAlarmTriggered = true; - confirmAlert("LOW CARTRIDGE", 2000); + scripter.confirmAlert("LOW CARTRIDGE", 2000); } else { // any other alert break; @@ -176,7 +176,9 @@ public class CancellableBolusCommand extends BolusCommand { } log.debug("Bolus record in history confirms delivered bolus"); - if (!scripter.goToMainTypeScreen(MenuType.MAIN_MENU, 15 * 1000)) { + // TODO how would this call fail? more generally ...... + scripter.returnToMainMenu(); + if (scripter.getCurrentMenu().getType() != MenuType.MAIN_MENU) { throw new CommandException().success(false).enacted(true) .message("Bolus was correctly delivered and checked against history, but we " + "did not return the main menu successfully.");