Refactor.

(cherry picked from commit 06a454d)
This commit is contained in:
Johannes Mockenhaupt 2017-09-01 10:45:06 +02:00
parent c96122cbed
commit 58757735f8
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1
3 changed files with 19 additions and 56 deletions

View file

@ -193,6 +193,16 @@ public class RuffyScripter {
this.ruffyService = null; 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 { private static class Returnable {
CommandResult cmdResult; CommandResult cmdResult;
} }
@ -540,49 +550,11 @@ public class RuffyScripter {
return true; 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 // TODO v2, rework these two methods: waitForMenuUpdate shoud only be used by commands
// then anything longer than a few seconds is an error; // then anything longer than a few seconds is an error;
// only ensureConnected() uses the method with the timeout parameter; inline that code, // 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 // 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? // TODO confirmAlarms? and report back which were cancelled?
/** Confirms and dismisses the given alert if it's raised before the timeout */ /** Confirms and dismisses the given alert if it's raised before the timeout */
@ -615,10 +587,7 @@ public class RuffyScripter {
return alertProcessed; return alertProcessed;
} }
/** /** Wait until the menu is updated */
* Wait until the menu update is in
*/
// TODO donn't use this in ensureConnected
public void waitForMenuUpdate() { public void waitForMenuUpdate() {
waitForMenuUpdate(60, "Timeout waiting for menu update"); 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) { public void waitForMenuToBeLeft(MenuType menuType) {
long timeout = System.currentTimeMillis() + 60 * 1000; long timeout = System.currentTimeMillis() + 60 * 1000;
while (currentMenu.getType() == menuType) { while (currentMenu.getType() == menuType) {

View file

@ -100,13 +100,7 @@ public class BolusCommand extends BaseCommand {
log.debug("Bolus record in history confirms delivered bolus"); log.debug("Bolus record in history confirms delivered bolus");
if(SP.getBoolean(R.string.key_combo_enable_experimental_features, false)) { if(SP.getBoolean(R.string.key_combo_enable_experimental_features, false)) {
// returning to main menu using the 'back' key should not cause a vibration scripter.returnToMainMenu();
// 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);
} else { } else {
// leave menu to go back to main menu // leave menu to go back to main menu
scripter.pressCheckKey(); scripter.pressCheckKey();

View file

@ -52,7 +52,7 @@ public class CancellableBolusCommand extends BolusCommand {
if (cancelRequested) { if (cancelRequested) {
progressReportCallback.report(STOPPING, 0, 0); progressReportCallback.report(STOPPING, 0, 0);
scripter.goToMainTypeScreen(MenuType.MAIN_MENU, 30 * 1000); scripter.returnToMainMenu();
progressReportCallback.report(STOPPED, 0, 0); progressReportCallback.report(STOPPED, 0, 0);
return new CommandResult().success(true).enacted(false) return new CommandResult().success(true).enacted(false)
.message("Bolus cancelled as per user request with no insulin delivered"); .message("Bolus cancelled as per user request with no insulin delivered");
@ -69,7 +69,7 @@ public class CancellableBolusCommand extends BolusCommand {
scripter.pressUpKey(); scripter.pressUpKey();
// wait up to 1s for a BOLUS_CANCELLED alert, if it doesn't happen we missed // 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 // 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) { if (alertWasCancelled) {
progressReportCallback.report(STOPPED, 0, 0); progressReportCallback.report(STOPPED, 0, 0);
return new CommandResult().success(true).enacted(false) return new CommandResult().success(true).enacted(false)
@ -120,7 +120,7 @@ public class CancellableBolusCommand extends BolusCommand {
String message = (String) scripter.getCurrentMenu().getAttribute(MenuAttribute.MESSAGE); String message = (String) scripter.getCurrentMenu().getAttribute(MenuAttribute.MESSAGE);
if (message.equals("LOW CARTRIDGE")) { if (message.equals("LOW CARTRIDGE")) {
lowCartdrigeAlarmTriggered = true; lowCartdrigeAlarmTriggered = true;
confirmAlert("LOW CARTRIDGE", 2000); scripter.confirmAlert("LOW CARTRIDGE", 2000);
} else { } else {
// any other alert // any other alert
break; break;
@ -176,7 +176,9 @@ public class CancellableBolusCommand extends BolusCommand {
} }
log.debug("Bolus record in history confirms delivered bolus"); 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) throw new CommandException().success(false).enacted(true)
.message("Bolus was correctly delivered and checked against history, but we " .message("Bolus was correctly delivered and checked against history, but we "
+ "did not return the main menu successfully."); + "did not return the main menu successfully.");