From 785a01a056ed76b90eb8acd41b50eab06f75894c Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Wed, 23 Aug 2017 13:02:49 +0200 Subject: [PATCH] Extract methods to read blinking values. --- .../jotomo/ruffyscripter/RuffyScripter.java | 20 ++++ .../ruffyscripter/commands/BaseCommand.java | 2 + .../ruffyscripter/commands/BolusCommand.java | 17 +--- .../ruffyscripter/commands/SetTbrCommand.java | 91 ++++++------------- .../commands/SetTbrCommandAlt.java | 44 ++------- 5 files changed, 63 insertions(+), 111 deletions(-) diff --git a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java index 460743ab7e..de1694f639 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java @@ -597,6 +597,26 @@ public class RuffyScripter { } } + @SuppressWarnings("unchecked") + public T readBlinkingValue(Class expectedType, MenuAttribute attribute) { + int retries = 5; + Object value = currentMenu.getAttribute(attribute); + while (!value.getClass().isAssignableFrom(expectedType.getClass())) { + value = currentMenu.getAttribute(attribute); + waitForScreenUpdate(1000); + retries--; + if (retries == 0) { + throw new CommandException().message("Failed to read blinkng value: " + attribute); + } + } + return (T) value; + } + + public long readDisplayedDuration() { + MenuTime duration = readBlinkingValue(MenuTime.class, MenuAttribute.RUNTIME); + return duration.getHour() * 60 + duration.getMinute(); + } + // TODO v2 add remaining info we can extract from the main menu, low battery and low // cartridge warnings, running extended bolus (how does that look if a TBR is active as well?) 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 ab951c9185..0e83f0c1a1 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/BaseCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/BaseCommand.java @@ -8,8 +8,10 @@ public abstract class BaseCommand implements Command { @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; } + public boolean isCancellable() { return canBeCancelled; } } 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 4424b964f4..947d31ed6b 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/BolusCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/BolusCommand.java @@ -130,7 +130,7 @@ public class BolusCommand extends BaseCommand { private void verifyDisplayedBolusAmount() { scripter.verifyMenuIsDisplayed(MenuType.BOLUS_ENTER); - double displayedBolus = readDisplayedBolusAmount(); + double displayedBolus = scripter.readBlinkingValue(Double.class, MenuAttribute.BOLUS); log.debug("Final bolus: " + displayedBolus); if (Math.abs(displayedBolus - bolus) > 0.05) { throw new CommandException().message("Failed to set correct bolus. Expected: " + bolus + ", actual: " + displayedBolus); @@ -138,25 +138,14 @@ public class BolusCommand extends BaseCommand { // check again to ensure the displayed value hasn't change due to due scrolling taking extremely long SystemClock.sleep(2000); - double refreshedDisplayedBolus = readDisplayedBolusAmount(); + scripter.verifyMenuIsDisplayed(MenuType.BOLUS_ENTER); + double refreshedDisplayedBolus = scripter.readBlinkingValue(Double.class, MenuAttribute.BOLUS); if (Math.abs(displayedBolus - refreshedDisplayedBolus) > 0.05) { throw new CommandException().message("Failed to set bolus: bolus changed after input stopped from " + displayedBolus + " -> " + refreshedDisplayedBolus); } } - private double readDisplayedBolusAmount() { - // TODO v2 add timeout? Currently the command execution timeout would trigger if exceeded - scripter.verifyMenuIsDisplayed(MenuType.BOLUS_ENTER); - // bolus amount is blinking, so we need to make sure we catch it at the right moment - Object amountObj = scripter.currentMenu.getAttribute(MenuAttribute.BOLUS); - while (!(amountObj instanceof Double)) { - scripter.waitForMenuUpdate(); - amountObj = scripter.currentMenu.getAttribute(MenuAttribute.BOLUS); - } - return (double) amountObj; - } - @Override public String toString() { return "BolusCommand{" + 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 efe61a4897..6a494e34ff 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java @@ -93,25 +93,17 @@ public class SetTbrCommand extends BaseCommand { log.debug("SetTbrCommand: 3. getting/setting basal percentage in " + scripter.currentMenu); retries = 30; - double currentPercentage = -100; + double currentPercentage = scripter.readBlinkingValue(Double.class, MenuAttribute.BASAL_RATE); while (currentPercentage != percentage && retries >= 0) { retries--; - Object percentageObj = scripter.currentMenu.getAttribute(MenuAttribute.BASAL_RATE); - - if (percentageObj != null && (percentageObj instanceof Double)) { - currentPercentage = ((Double) percentageObj).doubleValue(); - - if (currentPercentage != percentage) { - int requestedPercentage = (int) percentage; - int actualPercentage = (int) currentPercentage; - int steps = (requestedPercentage - actualPercentage) / 10; - log.debug("Adjusting basal(" + requestedPercentage + "/" + actualPercentage + ") with " + steps + " steps and " + retries + " retries left"); - scripter.step(steps, (steps < 0 ? RuffyScripter.Key.DOWN : RuffyScripter.Key.UP), 500); - scripter.waitForScreenUpdate(1000); - } - - } else { - currentPercentage = -100; + currentPercentage = scripter.readBlinkingValue(Double.class, MenuAttribute.BASAL_RATE); + if (currentPercentage != percentage) { + int requestedPercentage = (int) percentage; + int actualPercentage = (int) currentPercentage; + int steps = (requestedPercentage - actualPercentage) / 10; + log.debug("Adjusting basal(" + requestedPercentage + "/" + actualPercentage + ") with " + steps + " steps and " + retries + " retries left"); + scripter.step(steps, (steps < 0 ? RuffyScripter.Key.DOWN : RuffyScripter.Key.UP), 500); + scripter.waitForScreenUpdate(1000); } scripter.waitForScreenUpdate(1000); } @@ -120,20 +112,8 @@ public class SetTbrCommand extends BaseCommand { log.debug("4. checking basal percentage in " + scripter.currentMenu); scripter.waitForScreenUpdate(1000); - currentPercentage = -1000; - retries = 10; - while (currentPercentage < 0 && retries >= 0) { - retries--; - Object percentageObj = scripter.currentMenu.getAttribute(MenuAttribute.BASAL_RATE); - - if (percentageObj != null && (percentageObj instanceof Double)) { - currentPercentage = ((Double) percentageObj).doubleValue(); - } else { - scripter.waitForScreenUpdate(1000); - } - } - - if (retries < 0 || currentPercentage != percentage) + currentPercentage = scripter.readBlinkingValue(Double.class, MenuAttribute.BASAL_RATE); + if (currentPercentage != percentage) throw new CommandException().message("Unable to set percentage. Requested: " + percentage + ", value displayed on pump: " + currentPercentage); if (currentPercentage != 100) { @@ -155,47 +135,31 @@ public class SetTbrCommand extends BaseCommand { log.debug("6. getting/setting duration in " + scripter.currentMenu); retries = 30; - double currentDuration = -100; + double currentDuration = scripter.readDisplayedDuration(); while (currentDuration != duration && retries >= 0) { retries--; - Object durationObj = scripter.currentMenu.getAttribute(MenuAttribute.RUNTIME); - log.debug("Requested time: " + duration + " actual time: " + durationObj); - if (durationObj != null && durationObj instanceof MenuTime) { - MenuTime time = (MenuTime) durationObj; - currentDuration = (time.getHour() * 60) + time.getMinute(); - if (currentDuration != duration) { - int requestedDuration = (int) duration; - int actualDuration = (int) currentDuration; - int steps = (requestedDuration - actualDuration) / 15; - if (currentDuration + (steps * 15) < requestedDuration) - steps++; - else if (currentDuration + (steps * 15) > requestedDuration) - steps--; - log.debug("Adjusting duration(" + requestedDuration + "/" + actualDuration + ") with " + steps + " steps and " + retries + " retries left"); - scripter.step(steps, (steps > 0 ? RuffyScripter.Key.UP : RuffyScripter.Key.DOWN), 500); - scripter.waitForScreenUpdate(1000); - } + currentDuration = scripter.readDisplayedDuration(); + log.debug("Requested time: " + duration + " actual time: " + currentDuration); + if (currentDuration != duration) { + int requestedDuration = (int) duration; + int actualDuration = (int) currentDuration; + int steps = (requestedDuration - actualDuration) / 15; + if (currentDuration + (steps * 15) < requestedDuration) + steps++; + else if (currentDuration + (steps * 15) > requestedDuration) + steps--; + log.debug("Adjusting duration(" + requestedDuration + "/" + actualDuration + ") with " + steps + " steps and " + retries + " retries left"); + scripter.step(steps, (steps > 0 ? RuffyScripter.Key.UP : RuffyScripter.Key.DOWN), 500); + scripter.waitForScreenUpdate(1000); } - scripter.waitForScreenUpdate(1000); } if (currentDuration < 0 || retries < 0) throw new CommandException().message("unable to set duration, requested:" + duration + ", displayed on pump: " + currentDuration); log.debug("7. checking duration in " + scripter.currentMenu); scripter.waitForScreenUpdate(1000); - currentDuration = -1000; - retries = 10; - while (currentDuration < 0 && retries >= 0) { - retries--; - Object durationObj = scripter.currentMenu.getAttribute(MenuAttribute.RUNTIME); - - if (durationObj != null && durationObj instanceof MenuTime) { - MenuTime time = (MenuTime) durationObj; - currentDuration = (time.getHour() * 60) + time.getMinute(); - } else - scripter.waitForScreenUpdate(1000); - } - if (retries < 0 || currentDuration != duration) + currentDuration = scripter.readDisplayedDuration(); + if (currentDuration != duration) throw new CommandException().message("wrong duration! Requested: " + duration + ", displayed on pump: " + currentDuration); } @@ -216,6 +180,7 @@ public class SetTbrCommand extends BaseCommand { // TODO how probable is it, that a totally unrelated error (like occlusion alert) // is raised at this point, which we'd cancel together with the TBR cancelled alert? if (percentage == 100 && scripter.currentMenu.getType() == WARNING_OR_ERROR) { + // TODO extract method confirmAlert(alert) scripter.pressCheckKey(); retries++; cancelledError = true; diff --git a/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommandAlt.java b/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommandAlt.java index 151a78cc7a..2ad5985dfc 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommandAlt.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommandAlt.java @@ -12,8 +12,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Locale; -import de.jotomo.ruffyscripter.RuffyScripter; - public class SetTbrCommandAlt extends BaseCommand { private static final Logger log = LoggerFactory.getLogger(SetTbrCommand.class); @@ -106,7 +104,7 @@ public class SetTbrCommandAlt extends BaseCommand { private void inputTbrPercentage() { scripter.verifyMenuIsDisplayed(MenuType.TBR_SET); - long currentPercent = readDisplayedTbrPercentage(); + long currentPercent = scripter.readBlinkingValue(Long.class, MenuAttribute.BASAL_RATE); log.debug("Current TBR %: " + currentPercent); long percentageChange = percentage - currentPercent; long percentageSteps = percentageChange / 10; @@ -130,7 +128,7 @@ public class SetTbrCommandAlt extends BaseCommand { private void verifyDisplayedTbrPercentage() { scripter.verifyMenuIsDisplayed(MenuType.TBR_SET); - long displayedPercentage = readDisplayedTbrPercentage(); + long displayedPercentage = scripter.readBlinkingValue(Long.class, MenuAttribute.BASAL_RATE); if (displayedPercentage != percentage) { log.debug("Final displayed TBR percentage: " + displayedPercentage); throw new CommandException().message("Failed to set TBR percentage"); @@ -138,7 +136,8 @@ public class SetTbrCommandAlt extends BaseCommand { // check again to ensure the displayed value hasn't change due to due scrolling taking extremely long SystemClock.sleep(2000); - long refreshedDisplayedTbrPecentage = readDisplayedTbrPercentage(); + scripter.verifyMenuIsDisplayed(MenuType.TBR_SET); + long refreshedDisplayedTbrPecentage = scripter.readBlinkingValue(Long.class, MenuAttribute.BASAL_RATE); if (displayedPercentage != refreshedDisplayedTbrPecentage) { throw new CommandException().message("Failed to set TBR percentage: " + "percentage changed after input stopped from " @@ -146,21 +145,9 @@ public class SetTbrCommandAlt extends BaseCommand { } } - private long readDisplayedTbrPercentage() { - // TODO v2 add timeout? Currently the command execution timeout would trigger if exceeded - Object percentageObj = scripter.currentMenu.getAttribute(MenuAttribute.BASAL_RATE); - // this as a bit hacky, the display value is blinking, so we might catch that, so - // keep trying till we get the Double we want - while (!(percentageObj instanceof Double)) { - scripter.waitForMenuUpdate(); - percentageObj = scripter.currentMenu.getAttribute(MenuAttribute.BASAL_RATE); - } - return ((Double) percentageObj).longValue(); - } - private void inputTbrDuration() { scripter.verifyMenuIsDisplayed(MenuType.TBR_DURATION); - long currentDuration = readDisplayedTbrDuration(); + long currentDuration = scripter.readDisplayedDuration(); if (currentDuration % 15 != 0) { // The duration displayed is how long an active TBR will still run, // which might be something like 0:13, hence not in 15 minute steps. @@ -170,7 +157,7 @@ public class SetTbrCommandAlt extends BaseCommand { scripter.verifyMenuIsDisplayed(MenuType.TBR_DURATION); scripter.pressUpKey(); scripter.waitForMenuUpdate(); - currentDuration = readDisplayedTbrDuration(); + currentDuration = scripter.readDisplayedDuration(); } log.debug("Current TBR duration: " + currentDuration); long durationChange = duration - currentDuration; @@ -195,7 +182,7 @@ public class SetTbrCommandAlt extends BaseCommand { private void verifyDisplayedTbrDuration() { scripter.verifyMenuIsDisplayed(MenuType.TBR_DURATION); - long displayedDuration = readDisplayedTbrDuration(); + long displayedDuration = scripter.readDisplayedDuration(); if (displayedDuration != duration) { log.debug("Final displayed TBR duration: " + displayedDuration); throw new CommandException().message("Failed to set TBR duration"); @@ -203,7 +190,8 @@ public class SetTbrCommandAlt extends BaseCommand { // check again to ensure the displayed value hasn't change due to due scrolling taking extremely long SystemClock.sleep(2000); - long refreshedDisplayedTbrDuration = readDisplayedTbrDuration(); + scripter.verifyMenuIsDisplayed(MenuType.TBR_DURATION); + long refreshedDisplayedTbrDuration = scripter.readDisplayedDuration(); if (displayedDuration != refreshedDisplayedTbrDuration) { throw new CommandException().message("Failed to set TBR duration: " + "duration changed after input stopped from " @@ -211,19 +199,7 @@ public class SetTbrCommandAlt extends BaseCommand { } } - private long readDisplayedTbrDuration() { - // TODO v2 add timeout? Currently the command execution timeout would trigger if exceeded - scripter.verifyMenuIsDisplayed(MenuType.TBR_DURATION); - Object durationObj = scripter.currentMenu.getAttribute(MenuAttribute.RUNTIME); - // this as a bit hacky, the display value is blinking, so we might catch that, so - // keep trying till we get the Double we want - while (!(durationObj instanceof MenuTime)) { - scripter.waitForMenuUpdate(); - durationObj = scripter.currentMenu.getAttribute(MenuAttribute.RUNTIME); - } - MenuTime duration = (MenuTime) durationObj; - return duration.getHour() * 60 + duration.getMinute(); - } + private void cancelTbrAndConfirmCancellationWarning() { // confirm entered TBR