From c96122cbed3940269740d0db19cf131f11d71d20 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Fri, 1 Sep 2017 02:56:56 +0200 Subject: [PATCH] Move confirmAlert method to scripter. (cherry picked from commit f018d42) --- .../jotomo/ruffyscripter/RuffyScripter.java | 33 +++++++++++++++++++ .../ruffyscripter/commands/BaseCommand.java | 19 +++++++++-- .../commands/CancellableBolusCommand.java | 7 ---- .../ruffyscripter/commands/SetTbrCommand.java | 31 +---------------- 4 files changed, 50 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java index 7230b5668c..5b21f348c8 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java @@ -582,6 +582,39 @@ public class RuffyScripter { // 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 */ + public boolean confirmAlert(String alertMessage, int maxWaitMs) { + long inFiveSeconds = System.currentTimeMillis() + maxWaitMs; + boolean alertProcessed = false; + while (System.currentTimeMillis() < inFiveSeconds && !alertProcessed) { + if (getCurrentMenu().getType() == MenuType.WARNING_OR_ERROR) { + // Note that the message is permanently displayed, while the error code is blinking. + // A wait till the error code can be read results in the code hanging, despite + // menu updates coming in, so just check the message. + // TODO quick try if the can't make reading the error code work .. + String errorMsg = (String) getCurrentMenu().getAttribute(MenuAttribute.MESSAGE); + if (!errorMsg.equals(alertMessage)) { + throw new CommandException().success(false).enacted(false) + .message("An alert other than the expected " + alertMessage + " was raised by the pump: " + + errorMsg + ". Please check the pump."); + } + // confirm alert + verifyMenuIsDisplayed(MenuType.WARNING_OR_ERROR); + pressCheckKey(); + // dismiss alert + verifyMenuIsDisplayed(MenuType.WARNING_OR_ERROR); + pressCheckKey(); + waitForMenuToBeLeft(MenuType.WARNING_OR_ERROR); + alertProcessed = true; + } + SystemClock.sleep(10); + } + return alertProcessed; + } + /** * Wait until the menu update is in */ diff --git a/app/src/main/java/de/jotomo/ruffyscripter/commands/BaseCommand.java b/app/src/main/java/de/jotomo/ruffyscripter/commands/BaseCommand.java index 0e83f0c1a1..45370187f0 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/BaseCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/BaseCommand.java @@ -1,17 +1,30 @@ package de.jotomo.ruffyscripter.commands; +import android.os.SystemClock; + +import org.monkey.d.ruffy.ruffy.driver.display.MenuAttribute; +import org.monkey.d.ruffy.ruffy.driver.display.MenuType; + import de.jotomo.ruffyscripter.RuffyScripter; public abstract class BaseCommand implements Command { // RS will inject itself here protected RuffyScripter scripter; - @Override public void setScripter(RuffyScripter scripter) { this.scripter = scripter; } + + @Override + public void setScripter(RuffyScripter scripter) { + this.scripter = scripter; + } // TODO upcoming protected final boolean canBeCancelled = true; protected volatile boolean cancelRequested = false; + public void requestCancellation() { - cancelRequested = true; + cancelRequested = true; + } + + public boolean isCancellable() { + return canBeCancelled; } - public boolean isCancellable() { return canBeCancelled; } } 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 dcf06424da..409a2eb8db 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/CancellableBolusCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/CancellableBolusCommand.java @@ -191,13 +191,6 @@ public class CancellableBolusCommand extends BolusCommand { } } - // TODO confirmAlarms? and report back which were cancelled? - - private boolean confirmAlert(String alertText, int maxWaitTillExpectedAlert) { - // TODO - return false; - } - public void requestCancellation() { cancelRequested = true; progressReportCallback.report(STOPPING, 0, 0); diff --git a/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java b/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java index 1d4da448ab..4ebee15fe6 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java @@ -226,36 +226,7 @@ public class SetTbrCommand extends BaseCommand { // We could read the remaining duration from MAIN_MENU, but by the time we're here, // the pump could have moved from 0:02 to 0:01, so instead, check if a "TBR CANCELLED" alert // is raised and if so dismiss it - confirmAlert("TBR CANCELLED", 5000); - } - - /** Confirms and dismisses the given alert if it's raised before the timeout */ - private void confirmAlert(String alertMessage, int maxWaitMs) { - long inFiveSeconds = System.currentTimeMillis() + maxWaitMs; - boolean alertProcessed = false; - while (System.currentTimeMillis() < inFiveSeconds && !alertProcessed) { - if (scripter.getCurrentMenu().getType() == MenuType.WARNING_OR_ERROR) { - // Note that the message is permanently displayed, while the error code is blinking. - // A wait till the error code can be read results in the code hanging, despite - // menu updates coming in, so just check the message. - // TODO quick try if the can't make reading the error code work .. - String errorMsg = (String) scripter.getCurrentMenu().getAttribute(MenuAttribute.MESSAGE); - if (!errorMsg.equals(alertMessage)) { - throw new CommandException().success(false).enacted(false) - .message("An alert other than the expected " + alertMessage + " was raised by the pump: " - + errorMsg + ". Please check the pump."); - } - // confirm alert - scripter.verifyMenuIsDisplayed(MenuType.WARNING_OR_ERROR); - scripter.pressCheckKey(); - // dismiss alert - scripter.verifyMenuIsDisplayed(MenuType.WARNING_OR_ERROR); - scripter.pressCheckKey(); - scripter.waitForMenuToBeLeft(MenuType.WARNING_OR_ERROR); - alertProcessed = true; - } - SystemClock.sleep(10); - } + scripter.confirmAlert("TBR CANCELLED", 5000); } private void verifyMainMenuShowsNoActiveTbr() {