Cleanup, fix merge issues.
This commit is contained in:
parent
4d2fa19ad4
commit
fce696b51d
|
@ -12,14 +12,6 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import de.jotomo.ruffyscripter.RuffyScripter;
|
||||
|
||||
import static org.monkey.d.ruffy.ruffy.driver.display.MenuType.MAIN_MENU;
|
||||
import static org.monkey.d.ruffy.ruffy.driver.display.MenuType.TBR_DURATION;
|
||||
import static org.monkey.d.ruffy.ruffy.driver.display.MenuType.TBR_MENU;
|
||||
import static org.monkey.d.ruffy.ruffy.driver.display.MenuType.TBR_SET;
|
||||
import static org.monkey.d.ruffy.ruffy.driver.display.MenuType.WARNING_OR_ERROR;
|
||||
|
||||
public class SetTbrCommand extends BaseCommand {
|
||||
private static final Logger log = LoggerFactory.getLogger(SetTbrCommand.class);
|
||||
|
||||
|
@ -61,170 +53,217 @@ public class SetTbrCommand extends BaseCommand {
|
|||
@Override
|
||||
public CommandResult execute() {
|
||||
try {
|
||||
log.debug("1. going from " + scripter.getCurrentMenu() + " to TBR_MENU");
|
||||
int retries = 5;
|
||||
while (!scripter.goToMainTypeScreen(TBR_MENU, 3000)) {
|
||||
retries--;
|
||||
if (retries == 0)
|
||||
throw new CommandException().message("not able to find TBR_MENU: stuck in " + scripter.getCurrentMenu());
|
||||
SystemClock.sleep(500);
|
||||
if (scripter.getCurrentMenu().getType() == TBR_MENU)
|
||||
break;
|
||||
}
|
||||
|
||||
if (scripter.getCurrentMenu().getType() != TBR_MENU)
|
||||
throw new CommandException().message("not able to find TBR_MENU: stuck in " + scripter.getCurrentMenu());
|
||||
|
||||
log.debug("2. entering " + scripter.getCurrentMenu());
|
||||
retries = 5;
|
||||
while (!scripter.enterMenu(TBR_MENU, MenuType.TBR_SET, RuffyScripter.Key.CHECK, 2000)) {
|
||||
retries--;
|
||||
if (retries == 0)
|
||||
throw new CommandException().message("not able to find TBR_SET: stuck in " + scripter.getCurrentMenu());
|
||||
SystemClock.sleep(500);
|
||||
if (scripter.getCurrentMenu().getType() == TBR_SET)
|
||||
break;
|
||||
if (scripter.getCurrentMenu().getType() == TBR_DURATION) {
|
||||
scripter.pressMenuKey();
|
||||
scripter.waitForScreenUpdate(1000);
|
||||
}
|
||||
}
|
||||
|
||||
log.debug("SetTbrCommand: 3. getting/setting basal percentage in " + scripter.getCurrentMenu());
|
||||
retries = 30;
|
||||
|
||||
double currentPercentage = scripter.readBlinkingValue(Double.class, MenuAttribute.BASAL_RATE);
|
||||
while (currentPercentage != percentage && retries >= 0) {
|
||||
retries--;
|
||||
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);
|
||||
}
|
||||
if (currentPercentage < 0 || retries < 0)
|
||||
throw new CommandException().message("unable to set basal percentage");
|
||||
|
||||
log.debug("4. checking basal percentage in " + scripter.getCurrentMenu());
|
||||
scripter.waitForScreenUpdate(1000);
|
||||
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) {
|
||||
log.debug("5. change to TBR_DURATION from " + scripter.getCurrentMenu());
|
||||
retries = 5;
|
||||
while (retries >= 0 && !scripter.enterMenu(TBR_SET, MenuType.TBR_DURATION, RuffyScripter.Key.MENU, 2000)) {
|
||||
retries--;
|
||||
if (retries == 0)
|
||||
throw new CommandException().message("not able to find TBR_SET: stuck in " + scripter.getCurrentMenu());
|
||||
SystemClock.sleep(500);
|
||||
if (scripter.getCurrentMenu().getType() == TBR_DURATION)
|
||||
break;
|
||||
if (scripter.getCurrentMenu().getType() == TBR_SET) {
|
||||
scripter.pressMenuKey();
|
||||
scripter.waitForScreenUpdate(1000);
|
||||
}
|
||||
}
|
||||
|
||||
log.debug("6. getting/setting duration in " + scripter.getCurrentMenu());
|
||||
retries = 30;
|
||||
|
||||
double currentDuration = scripter.readDisplayedDuration();
|
||||
while (currentDuration != duration && retries >= 0) {
|
||||
retries--;
|
||||
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);
|
||||
}
|
||||
}
|
||||
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.getCurrentMenu());
|
||||
scripter.waitForScreenUpdate(1000);
|
||||
currentDuration = scripter.readDisplayedDuration();
|
||||
if (currentDuration != duration)
|
||||
throw new CommandException().message("wrong duration! Requested: " + duration + ", displayed on pump: " + currentDuration);
|
||||
}
|
||||
|
||||
log.debug("8. confirming TBR om " + scripter.getCurrentMenu());
|
||||
retries = 5;
|
||||
while (retries >= 0 && (scripter.getCurrentMenu().getType() == TBR_DURATION || scripter.getCurrentMenu().getType() == TBR_SET)) {
|
||||
retries--;
|
||||
scripter.pressCheckKey();
|
||||
scripter.waitForScreenUpdate(1000);
|
||||
}
|
||||
if (retries < 0 || scripter.getCurrentMenu().getType() == TBR_DURATION || scripter.getCurrentMenu().getType() == TBR_SET)
|
||||
throw new CommandException().message("failed setting basal!");
|
||||
retries = 10;
|
||||
boolean cancelledError = true;
|
||||
if (percentage == 100)
|
||||
cancelledError = false;
|
||||
while (retries >= 0 && scripter.getCurrentMenu().getType() != MAIN_MENU) {
|
||||
// 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.getCurrentMenu().getType() == WARNING_OR_ERROR) {
|
||||
// TODO extract method confirmAlert(alert)
|
||||
scripter.pressCheckKey();
|
||||
retries++;
|
||||
cancelledError = true;
|
||||
scripter.waitForScreenUpdate(1000);
|
||||
} else {
|
||||
retries--;
|
||||
if (scripter.getCurrentMenu().getType() == MAIN_MENU && cancelledError)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
log.debug("9. verifying the main menu display the TBR we just set/cancelled");
|
||||
if (retries < 0 || scripter.getCurrentMenu().getType() != MAIN_MENU)
|
||||
throw new CommandException().message("failed going to main!");
|
||||
|
||||
Object percentageObj = scripter.getCurrentMenu().getAttribute(MenuAttribute.TBR);
|
||||
Object durationObj = scripter.getCurrentMenu().getAttribute(MenuAttribute.RUNTIME);
|
||||
enterTbrMenu();
|
||||
inputTbrPercentage();
|
||||
verifyDisplayedTbrPercentage();
|
||||
|
||||
if (percentage == 100) {
|
||||
if (durationObj != null)
|
||||
throw new CommandException().message("TBR cancelled, but main menu shows a running TBR");
|
||||
cancelTbrAndConfirmCancellationWarning();
|
||||
} else {
|
||||
// switch to TBR_DURATION menu by pressing menu key
|
||||
scripter.verifyMenuIsDisplayed(MenuType.TBR_SET);
|
||||
scripter.pressMenuKey();
|
||||
scripter.waitForMenuUpdate();
|
||||
scripter.verifyMenuIsDisplayed(MenuType.TBR_DURATION);
|
||||
|
||||
return new CommandResult().success(true).enacted(true).message("TBR was cancelled");
|
||||
inputTbrDuration();
|
||||
verifyDisplayedTbrDuration();
|
||||
|
||||
// confirm TBR
|
||||
scripter.pressCheckKey();
|
||||
scripter.waitForMenuToBeLeft(MenuType.TBR_DURATION);
|
||||
}
|
||||
|
||||
if (percentageObj == null || !(percentageObj instanceof Double))
|
||||
throw new CommandException().message("not percentage");
|
||||
scripter.verifyMenuIsDisplayed(MenuType.MAIN_MENU,
|
||||
"Pump did not return to MAIN_MEU after setting TBR. " +
|
||||
"Check pump manually, the TBR might not have been set/cancelled.");
|
||||
|
||||
if (((double) percentageObj) != percentage)
|
||||
throw new CommandException().message("wrong percentage set!");
|
||||
// check main menu shows the same values we just set
|
||||
if (percentage == 100) {
|
||||
verifyMainMenuShowsNoActiveTbr();
|
||||
return new CommandResult().success(true).enacted(true).message("TBR was cancelled");
|
||||
} else {
|
||||
verifyMainMenuShowsExpectedTbrActive();
|
||||
return new CommandResult().success(true).enacted(true).message(
|
||||
String.format(Locale.US, "TBR set to %d%% for %d min", percentage, duration));
|
||||
}
|
||||
|
||||
if (durationObj == null || !(durationObj instanceof MenuTime))
|
||||
throw new CommandException().message("not time");
|
||||
} catch (CommandException e) {
|
||||
return e.toCommandResult();
|
||||
}
|
||||
}
|
||||
|
||||
MenuTime t = (MenuTime) durationObj;
|
||||
if (t.getMinute() + (60 * t.getHour()) > duration || t.getMinute() + (60 * t.getHour()) < duration - 5)
|
||||
throw new CommandException().message("wrong time set!");
|
||||
private void enterTbrMenu() {
|
||||
scripter.verifyMenuIsDisplayed(MenuType.MAIN_MENU);
|
||||
scripter.navigateToMenu(MenuType.TBR_MENU);
|
||||
scripter.verifyMenuIsDisplayed(MenuType.TBR_MENU);
|
||||
scripter.pressCheckKey();
|
||||
scripter.waitForMenuUpdate();
|
||||
scripter.verifyMenuIsDisplayed(MenuType.TBR_SET);
|
||||
}
|
||||
|
||||
private void inputTbrPercentage() {
|
||||
scripter.verifyMenuIsDisplayed(MenuType.TBR_SET);
|
||||
long currentPercent = scripter.readBlinkingValue(Double.class, MenuAttribute.BASAL_RATE).longValue();
|
||||
log.debug("Current TBR %: " + currentPercent);
|
||||
long percentageChange = percentage - currentPercent;
|
||||
long percentageSteps = percentageChange / 10;
|
||||
boolean increasePercentage = true;
|
||||
if (percentageSteps < 0) {
|
||||
increasePercentage = false;
|
||||
percentageSteps = Math.abs(percentageSteps);
|
||||
}
|
||||
log.debug("Pressing " + (increasePercentage ? "up" : "down") + " " + percentageSteps + " times");
|
||||
for (int i = 0; i < percentageSteps; i++) {
|
||||
scripter.verifyMenuIsDisplayed(MenuType.TBR_SET);
|
||||
log.debug("Push #" + (i + 1));
|
||||
if (increasePercentage) scripter.pressUpKey();
|
||||
else scripter.pressDownKey();
|
||||
SystemClock.sleep(100);
|
||||
}
|
||||
// Give the pump time to finish any scrolling that might still be going on, can take
|
||||
// up to 1100ms. Plus some extra time to be sure
|
||||
SystemClock.sleep(2000);
|
||||
}
|
||||
|
||||
private void verifyDisplayedTbrPercentage() {
|
||||
scripter.verifyMenuIsDisplayed(MenuType.TBR_SET);
|
||||
long displayedPercentage = scripter.readBlinkingValue(Double.class, MenuAttribute.BASAL_RATE).longValue();
|
||||
if (displayedPercentage != percentage) {
|
||||
log.debug("Final displayed TBR percentage: " + displayedPercentage);
|
||||
throw new CommandException().message("Failed to set TBR percentage, requested: " + percentage + ", actual: " + displayedPercentage);
|
||||
}
|
||||
|
||||
// check again to ensure the displayed value hasn't change due to due scrolling taking extremely long
|
||||
SystemClock.sleep(2000);
|
||||
scripter.verifyMenuIsDisplayed(MenuType.TBR_SET);
|
||||
long refreshedDisplayedTbrPecentage = scripter.readBlinkingValue(Double.class, MenuAttribute.BASAL_RATE).longValue();
|
||||
if (displayedPercentage != refreshedDisplayedTbrPecentage) {
|
||||
throw new CommandException().message("Failed to set TBR percentage: " +
|
||||
"percentage changed after input stopped from "
|
||||
+ displayedPercentage + " -> " + refreshedDisplayedTbrPecentage);
|
||||
}
|
||||
}
|
||||
|
||||
private void inputTbrDuration() {
|
||||
scripter.verifyMenuIsDisplayed(MenuType.TBR_DURATION);
|
||||
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.
|
||||
// Pressing up will go to the next higher 15 minute step.
|
||||
// Don't press down, from 0:13 it can't go down, so press up.
|
||||
// Pressing up from 23:59 works to go to 24:00.
|
||||
scripter.verifyMenuIsDisplayed(MenuType.TBR_DURATION);
|
||||
scripter.pressUpKey();
|
||||
scripter.waitForMenuUpdate();
|
||||
currentDuration = scripter.readDisplayedDuration();
|
||||
}
|
||||
log.debug("Current TBR duration: " + currentDuration);
|
||||
long durationChange = duration - currentDuration;
|
||||
long durationSteps = durationChange / 15;
|
||||
boolean increaseDuration = true;
|
||||
if (durationSteps < 0) {
|
||||
increaseDuration = false;
|
||||
durationSteps = Math.abs(durationSteps);
|
||||
}
|
||||
log.debug("Pressing " + (increaseDuration ? "up" : "down") + " " + durationSteps + " times");
|
||||
for (int i = 0; i < durationSteps; i++) {
|
||||
scripter.verifyMenuIsDisplayed(MenuType.TBR_DURATION);
|
||||
if (increaseDuration) scripter.pressUpKey();
|
||||
else scripter.pressDownKey();
|
||||
SystemClock.sleep(100);
|
||||
log.debug("Push #" + (i + 1));
|
||||
}
|
||||
// Give the pump time to finish any scrolling that might still be going on, can take
|
||||
// up to 1100ms. Plus some extra time to be sure
|
||||
SystemClock.sleep(2000);
|
||||
}
|
||||
|
||||
private void verifyDisplayedTbrDuration() {
|
||||
scripter.verifyMenuIsDisplayed(MenuType.TBR_DURATION);
|
||||
long displayedDuration = scripter.readDisplayedDuration();
|
||||
if (displayedDuration != duration) {
|
||||
log.debug("Final displayed TBR duration: " + displayedDuration);
|
||||
throw new CommandException().message("Failed to set TBR duration");
|
||||
}
|
||||
|
||||
// check again to ensure the displayed value hasn't change due to due scrolling taking extremely long
|
||||
SystemClock.sleep(2000);
|
||||
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 "
|
||||
+ displayedDuration + " -> " + refreshedDisplayedTbrDuration);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return new CommandResult().success(true).enacted(true).message(
|
||||
String.format(Locale.US, "TBR set to %d%% for %d min", percentage, duration));
|
||||
} catch (Exception e) {
|
||||
log.error("got exception: ", e);
|
||||
return new CommandResult().success(false).message(e.getMessage()).exception(e);
|
||||
|
||||
private void cancelTbrAndConfirmCancellationWarning() {
|
||||
// confirm entered TBR
|
||||
scripter.verifyMenuIsDisplayed(MenuType.TBR_SET);
|
||||
scripter.pressCheckKey();
|
||||
|
||||
// A "TBR CANCELLED alert" is only raised by the pump when the remaining time is
|
||||
// greater than 60s (displayed as 0:01, the pump goes from there to finished.
|
||||
// We could read the remaining duration from MAIN_MENU, but by the time we're here,
|
||||
// the pumup could have moved from 0:02 to 0:01, so instead, check if a "TBR CANCELLED" alert
|
||||
// is raised and if so dismiss it
|
||||
long inFiveSeconds = System.currentTimeMillis() + 5 * 1000;
|
||||
boolean alertProcessed = false;
|
||||
while (System.currentTimeMillis() < inFiveSeconds && !alertProcessed) {
|
||||
if (scripter.getCurrentMenu().getType() == MenuType.WARNING_OR_ERROR) {
|
||||
// Check the raised alarm is TBR CANCELLED, so we're not accidentally cancelling
|
||||
// a different alarm that might be raised at the same time.
|
||||
// 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 v2 this only works when the pump's language is English
|
||||
String errorMsg = (String) scripter.getCurrentMenu().getAttribute(MenuAttribute.MESSAGE);
|
||||
if (!errorMsg.equals("TBR CANCELLED")) {
|
||||
throw new CommandException().success(false).enacted(false)
|
||||
.message("An alert other than the expected TBR CANCELLED was raised by the pump: "
|
||||
+ errorMsg + ". Please check the pump.");
|
||||
}
|
||||
// confirm "TBR CANCELLED" alert
|
||||
scripter.verifyMenuIsDisplayed(MenuType.WARNING_OR_ERROR);
|
||||
scripter.pressCheckKey();
|
||||
// dismiss "TBR CANCELLED" alert
|
||||
scripter.verifyMenuIsDisplayed(MenuType.WARNING_OR_ERROR);
|
||||
scripter.pressCheckKey();
|
||||
scripter.waitForMenuToBeLeft(MenuType.WARNING_OR_ERROR);
|
||||
alertProcessed = true;
|
||||
}
|
||||
SystemClock.sleep(10);
|
||||
}
|
||||
}
|
||||
|
||||
private void verifyMainMenuShowsNoActiveTbr() {
|
||||
scripter.verifyMenuIsDisplayed(MenuType.MAIN_MENU);
|
||||
Double tbrPercentage = (Double) scripter.getCurrentMenu().getAttribute(MenuAttribute.TBR);
|
||||
boolean runtimeDisplayed = scripter.getCurrentMenu().attributes().contains(MenuAttribute.RUNTIME);
|
||||
if (tbrPercentage != 100 || runtimeDisplayed) {
|
||||
throw new CommandException().message("Cancelling TBR failed, TBR is still set according to MAIN_MENU");
|
||||
}
|
||||
}
|
||||
|
||||
private void verifyMainMenuShowsExpectedTbrActive() {
|
||||
scripter.verifyMenuIsDisplayed(MenuType.MAIN_MENU);
|
||||
// new TBR set; percentage and duration must be displayed ...
|
||||
if (!scripter.getCurrentMenu().attributes().contains(MenuAttribute.TBR) ||
|
||||
!scripter.getCurrentMenu().attributes().contains(MenuAttribute.RUNTIME)) {
|
||||
throw new CommandException().message("Setting TBR failed, according to MAIN_MENU no TBR is active");
|
||||
}
|
||||
Double mmTbrPercentage = (Double) scripter.getCurrentMenu().getAttribute(MenuAttribute.TBR);
|
||||
MenuTime mmTbrDuration = (MenuTime) scripter.getCurrentMenu().getAttribute(MenuAttribute.RUNTIME);
|
||||
// ... and be the same as what we set
|
||||
// note that displayed duration might have already counted down, e.g. from 30 minutes to
|
||||
// 29 minutes and 59 seconds, so that 29 minutes are displayed
|
||||
int mmTbrDurationInMinutes = mmTbrDuration.getHour() * 60 + mmTbrDuration.getMinute();
|
||||
if (mmTbrPercentage != percentage || (mmTbrDurationInMinutes != duration && mmTbrDurationInMinutes + 1 != duration)) {
|
||||
throw new CommandException().message("Setting TBR failed, TBR in MAIN_MENU differs from expected");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,281 +0,0 @@
|
|||
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 org.monkey.d.ruffy.ruffy.driver.display.menu.MenuTime;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class SetTbrCommandAlt extends BaseCommand {
|
||||
private static final Logger log = LoggerFactory.getLogger(SetTbrCommand.class);
|
||||
|
||||
private final long percentage;
|
||||
private final long duration;
|
||||
|
||||
public SetTbrCommandAlt(long percentage, long duration) {
|
||||
this.percentage = percentage;
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> validateArguments() {
|
||||
List<String> violations = new ArrayList<>();
|
||||
|
||||
if (percentage % 10 != 0) {
|
||||
violations.add("TBR percentage must be set in 10% steps");
|
||||
}
|
||||
if (percentage < 0 || percentage > 500) {
|
||||
violations.add("TBR percentage must be within 0-500%");
|
||||
}
|
||||
|
||||
if (percentage != 100) {
|
||||
if (duration % 15 != 0) {
|
||||
violations.add("TBR duration can only be set in 15 minute steps");
|
||||
}
|
||||
if (duration > 60 * 24) {
|
||||
violations.add("Maximum TBR duration is 24 hours");
|
||||
}
|
||||
}
|
||||
|
||||
if (percentage == 0 && duration > 120) {
|
||||
violations.add("Max allowed zero-temp duration is 2h");
|
||||
}
|
||||
|
||||
return violations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandResult execute() {
|
||||
try {
|
||||
enterTbrMenu();
|
||||
inputTbrPercentage();
|
||||
verifyDisplayedTbrPercentage();
|
||||
|
||||
if (percentage == 100) {
|
||||
cancelTbrAndConfirmCancellationWarning();
|
||||
} else {
|
||||
// switch to TBR_DURATION menu by pressing menu key
|
||||
scripter.verifyMenuIsDisplayed(MenuType.TBR_SET);
|
||||
scripter.pressMenuKey();
|
||||
scripter.waitForMenuUpdate();
|
||||
scripter.verifyMenuIsDisplayed(MenuType.TBR_DURATION);
|
||||
|
||||
inputTbrDuration();
|
||||
verifyDisplayedTbrDuration();
|
||||
|
||||
// confirm TBR
|
||||
scripter.pressCheckKey();
|
||||
scripter.waitForMenuToBeLeft(MenuType.TBR_DURATION);
|
||||
}
|
||||
|
||||
scripter.verifyMenuIsDisplayed(MenuType.MAIN_MENU,
|
||||
"Pump did not return to MAIN_MEU after setting TBR. " +
|
||||
"Check pump manually, the TBR might not have been set/cancelled.");
|
||||
|
||||
// check main menu shows the same values we just set
|
||||
if (percentage == 100) {
|
||||
verifyMainMenuShowsNoActiveTbr();
|
||||
return new CommandResult().success(true).enacted(true).message("TBR was cancelled");
|
||||
} else {
|
||||
verifyMainMenuShowsExpectedTbrActive();
|
||||
return new CommandResult().success(true).enacted(true).message(
|
||||
String.format(Locale.US, "TBR set to %d%% for %d min", percentage, duration));
|
||||
}
|
||||
|
||||
} catch (CommandException e) {
|
||||
return e.toCommandResult();
|
||||
}
|
||||
}
|
||||
|
||||
private void enterTbrMenu() {
|
||||
scripter.verifyMenuIsDisplayed(MenuType.MAIN_MENU);
|
||||
scripter.navigateToMenu(MenuType.TBR_MENU);
|
||||
scripter.verifyMenuIsDisplayed(MenuType.TBR_MENU);
|
||||
scripter.pressCheckKey();
|
||||
scripter.waitForMenuUpdate();
|
||||
scripter.verifyMenuIsDisplayed(MenuType.TBR_SET);
|
||||
}
|
||||
|
||||
private void inputTbrPercentage() {
|
||||
scripter.verifyMenuIsDisplayed(MenuType.TBR_SET);
|
||||
long currentPercent = scripter.readBlinkingValue(Double.class, MenuAttribute.BASAL_RATE).longValue();
|
||||
log.debug("Current TBR %: " + currentPercent);
|
||||
long percentageChange = percentage - currentPercent;
|
||||
long percentageSteps = percentageChange / 10;
|
||||
boolean increasePercentage = true;
|
||||
if (percentageSteps < 0) {
|
||||
increasePercentage = false;
|
||||
percentageSteps = Math.abs(percentageSteps);
|
||||
}
|
||||
log.debug("Pressing " + (increasePercentage ? "up" : "down") + " " + percentageSteps + " times");
|
||||
for (int i = 0; i < percentageSteps; i++) {
|
||||
scripter.verifyMenuIsDisplayed(MenuType.TBR_SET);
|
||||
log.debug("Push #" + (i + 1));
|
||||
if (increasePercentage) scripter.pressUpKey();
|
||||
else scripter.pressDownKey();
|
||||
SystemClock.sleep(100);
|
||||
if (increasePercentage) scripter.pressUpKey();
|
||||
else scripter.pressDownKey();
|
||||
SystemClock.sleep(100);
|
||||
log.debug("Push #" + (i + 1));
|
||||
}
|
||||
// Give the pump time to finish any scrolling that might still be going on, can take
|
||||
// up to 1100ms. Plus some extra time to be sure
|
||||
SystemClock.sleep(2000);
|
||||
}
|
||||
|
||||
private void verifyDisplayedTbrPercentage() {
|
||||
scripter.verifyMenuIsDisplayed(MenuType.TBR_SET);
|
||||
long displayedPercentage = scripter.readBlinkingValue(Double.class, MenuAttribute.BASAL_RATE).longValue();
|
||||
if (displayedPercentage != percentage) {
|
||||
log.debug("Final displayed TBR percentage: " + displayedPercentage);
|
||||
throw new CommandException().message("Failed to set TBR percentage, requested: " + percentage + ", actual: " + displayedPercentage);
|
||||
}
|
||||
|
||||
// check again to ensure the displayed value hasn't change due to due scrolling taking extremely long
|
||||
SystemClock.sleep(2000);
|
||||
scripter.verifyMenuIsDisplayed(MenuType.TBR_SET);
|
||||
long refreshedDisplayedTbrPecentage = scripter.readBlinkingValue(Double.class, MenuAttribute.BASAL_RATE).longValue();
|
||||
if (displayedPercentage != refreshedDisplayedTbrPecentage) {
|
||||
throw new CommandException().message("Failed to set TBR percentage: " +
|
||||
"percentage changed after input stopped from "
|
||||
+ displayedPercentage + " -> " + refreshedDisplayedTbrPecentage);
|
||||
}
|
||||
}
|
||||
|
||||
private void inputTbrDuration() {
|
||||
scripter.verifyMenuIsDisplayed(MenuType.TBR_DURATION);
|
||||
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.
|
||||
// Pressing up will go to the next higher 15 minute step.
|
||||
// Don't press down, from 0:13 it can't go down, so press up.
|
||||
// Pressing up from 23:59 works to go to 24:00.
|
||||
scripter.verifyMenuIsDisplayed(MenuType.TBR_DURATION);
|
||||
scripter.pressUpKey();
|
||||
scripter.waitForMenuUpdate();
|
||||
currentDuration = scripter.readDisplayedDuration();
|
||||
}
|
||||
log.debug("Current TBR duration: " + currentDuration);
|
||||
long durationChange = duration - currentDuration;
|
||||
long durationSteps = durationChange / 15;
|
||||
boolean increaseDuration = true;
|
||||
if (durationSteps < 0) {
|
||||
increaseDuration = false;
|
||||
durationSteps = Math.abs(durationSteps);
|
||||
}
|
||||
log.debug("Pressing " + (increaseDuration ? "up" : "down") + " " + durationSteps + " times");
|
||||
for (int i = 0; i < durationSteps; i++) {
|
||||
scripter.verifyMenuIsDisplayed(MenuType.TBR_DURATION);
|
||||
if (increaseDuration) scripter.pressUpKey();
|
||||
else scripter.pressDownKey();
|
||||
SystemClock.sleep(100);
|
||||
log.debug("Push #" + (i + 1));
|
||||
}
|
||||
// Give the pump time to finish any scrolling that might still be going on, can take
|
||||
// up to 1100ms. Plus some extra time to be sure
|
||||
SystemClock.sleep(2000);
|
||||
}
|
||||
|
||||
private void verifyDisplayedTbrDuration() {
|
||||
scripter.verifyMenuIsDisplayed(MenuType.TBR_DURATION);
|
||||
long displayedDuration = scripter.readDisplayedDuration();
|
||||
if (displayedDuration != duration) {
|
||||
log.debug("Final displayed TBR duration: " + displayedDuration);
|
||||
throw new CommandException().message("Failed to set TBR duration");
|
||||
}
|
||||
|
||||
// check again to ensure the displayed value hasn't change due to due scrolling taking extremely long
|
||||
SystemClock.sleep(2000);
|
||||
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 "
|
||||
+ displayedDuration + " -> " + refreshedDisplayedTbrDuration);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void cancelTbrAndConfirmCancellationWarning() {
|
||||
// confirm entered TBR
|
||||
scripter.verifyMenuIsDisplayed(MenuType.TBR_SET);
|
||||
scripter.pressCheckKey();
|
||||
|
||||
// A "TBR CANCELLED alert" is only raised by the pump when the remaining time is
|
||||
// greater than 60s (displayed as 0:01, the pump goes from there to finished.
|
||||
// We could read the remaining duration from MAIN_MENU, but by the time we're here,
|
||||
// the pumup could have moved from 0:02 to 0:01, so instead, check if a "TBR CANCELLED" alert
|
||||
// is raised and if so dismiss it
|
||||
long inFiveSeconds = System.currentTimeMillis() + 5 * 1000;
|
||||
boolean alertProcessed = false;
|
||||
while (System.currentTimeMillis() < inFiveSeconds && !alertProcessed) {
|
||||
if (scripter.getCurrentMenu().getType() == MenuType.WARNING_OR_ERROR) {
|
||||
// Check the raised alarm is TBR CANCELLED, so we're not accidentally cancelling
|
||||
// a different alarm that might be raised at the same time.
|
||||
// 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 v2 this only works when the pump's language is English
|
||||
String errorMsg = (String) scripter.getCurrentMenu().getAttribute(MenuAttribute.MESSAGE);
|
||||
if (!errorMsg.equals("TBR CANCELLED")) {
|
||||
throw new CommandException().success(false).enacted(false)
|
||||
.message("An alert other than the expected TBR CANCELLED was raised by the pump: "
|
||||
+ errorMsg + ". Please check the pump.");
|
||||
}
|
||||
// confirm "TBR CANCELLED" alert
|
||||
scripter.verifyMenuIsDisplayed(MenuType.WARNING_OR_ERROR);
|
||||
scripter.pressCheckKey();
|
||||
// dismiss "TBR CANCELLED" alert
|
||||
scripter.verifyMenuIsDisplayed(MenuType.WARNING_OR_ERROR);
|
||||
scripter.pressCheckKey();
|
||||
scripter.waitForMenuToBeLeft(MenuType.WARNING_OR_ERROR);
|
||||
alertProcessed = true;
|
||||
}
|
||||
SystemClock.sleep(10);
|
||||
}
|
||||
}
|
||||
|
||||
private void verifyMainMenuShowsNoActiveTbr() {
|
||||
scripter.verifyMenuIsDisplayed(MenuType.MAIN_MENU);
|
||||
Double tbrPercentage = (Double) scripter.getCurrentMenu().getAttribute(MenuAttribute.TBR);
|
||||
boolean runtimeDisplayed = scripter.getCurrentMenu().attributes().contains(MenuAttribute.RUNTIME);
|
||||
if (tbrPercentage != 100 || runtimeDisplayed) {
|
||||
throw new CommandException().message("Cancelling TBR failed, TBR is still set according to MAIN_MENU");
|
||||
}
|
||||
}
|
||||
|
||||
private void verifyMainMenuShowsExpectedTbrActive() {
|
||||
scripter.verifyMenuIsDisplayed(MenuType.MAIN_MENU);
|
||||
// new TBR set; percentage and duration must be displayed ...
|
||||
if (!scripter.getCurrentMenu().attributes().contains(MenuAttribute.TBR) ||
|
||||
!scripter.getCurrentMenu().attributes().contains(MenuAttribute.RUNTIME)) {
|
||||
throw new CommandException().message("Setting TBR failed, according to MAIN_MENU no TBR is active");
|
||||
}
|
||||
Double mmTbrPercentage = (Double) scripter.getCurrentMenu().getAttribute(MenuAttribute.TBR);
|
||||
MenuTime mmTbrDuration = (MenuTime) scripter.getCurrentMenu().getAttribute(MenuAttribute.RUNTIME);
|
||||
// ... and be the same as what we set
|
||||
// note that displayed duration might have already counted down, e.g. from 30 minutes to
|
||||
// 29 minutes and 59 seconds, so that 29 minutes are displayed
|
||||
int mmTbrDurationInMinutes = mmTbrDuration.getHour() * 60 + mmTbrDuration.getMinute();
|
||||
if (mmTbrPercentage != percentage || (mmTbrDurationInMinutes != duration && mmTbrDurationInMinutes + 1 != duration)) {
|
||||
throw new CommandException().message("Setting TBR failed, TBR in MAIN_MENU differs from expected");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SetTbrCommand{" +
|
||||
"percentage=" + percentage +
|
||||
", duration=" + duration +
|
||||
'}';
|
||||
}
|
||||
}
|
|
@ -47,10 +47,4 @@ public class Config {
|
|||
public static final boolean logDanaBTComm = true;
|
||||
public static final boolean logDanaMessageDetail = true;
|
||||
public static final boolean logDanaSerialEngine = true;
|
||||
|
||||
// Combo specific
|
||||
/** use alternate SetTbrCommand (uses the initial implementation) */
|
||||
public static final boolean comboUseAlternateSetTbrCommand = true;
|
||||
/** very quick hack to split up bolus into 2 U parts, spaced roughly 45s apart */
|
||||
public static final boolean comboSplitBoluses = false;
|
||||
}
|
||||
|
|
|
@ -12,7 +12,6 @@ import android.net.Uri;
|
|||
import android.os.IBinder;
|
||||
import android.os.SystemClock;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
|
||||
|
@ -213,6 +212,7 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
|
|||
|
||||
@Override
|
||||
public void onServiceDisconnected(ComponentName name) {
|
||||
// TODO stop?
|
||||
log.debug("ruffy service disconnected");
|
||||
// try to reconnect ruffy service unless unbind was explicitly requested
|
||||
// via unbindRuffyService
|
||||
|
|
Loading…
Reference in a new issue