Misc cleanups.

This commit is contained in:
Johannes Mockenhaupt 2017-08-23 22:10:19 +02:00
parent 5eea6d7b8d
commit eb74a24e54
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1
7 changed files with 67 additions and 74 deletions

View file

@ -36,7 +36,7 @@ public class RuffyScripter {
private IRuffyService ruffyService;
private String unrecoverableError = null;
public volatile Menu currentMenu;
private volatile Menu currentMenu;
private volatile long menuLastUpdated = 0;
private volatile long lastCmdExecutionTime;
@ -49,10 +49,6 @@ public class RuffyScripter {
private final Object screenlock = new Object();
public RuffyScripter() {
}
public void start(IRuffyService newService) {
try {
if (ruffyService != null) {
@ -72,23 +68,11 @@ public class RuffyScripter {
newService.addHandler(mHandler);
}
} catch (Exception e) {
log.error("Unhandled exception starting RuffyScripter", e);
throw new RuntimeException(e);
}
}
public void stop() {
if (started) {
started = false;
// TODO ruffy removes dead handlers automatically by now.
// still, check this when going through recovery logic
/* try {
ruffyService.removeHandler(mHandler);
} catch (RemoteException e) {
log.warn("Removing IRTHandler from Ruffy service failed, ignoring", e);
}*/
}
}
public boolean isRunning() {
return started;
}
@ -234,6 +218,10 @@ public class RuffyScripter {
this.ruffyService = null;
}
public Menu getCurrentMenu() {
return currentMenu;
}
private static class Returnable {
CommandResult cmdResult;
}

View file

@ -11,8 +11,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import de.jotomo.ruffyscripter.RuffyScripter;
public class BolusCommand extends BaseCommand {
private static final Logger log = LoggerFactory.getLogger(BolusCommand.class);
@ -54,11 +52,11 @@ public class BolusCommand extends BaseCommand {
// wait for bolus delivery to complete; the remaining units to deliver are counted
// down and are displayed on the main menu.
Double bolusRemaining = (Double) scripter.currentMenu.getAttribute(MenuAttribute.BOLUS_REMAINING);
Double bolusRemaining = (Double) scripter.getCurrentMenu().getAttribute(MenuAttribute.BOLUS_REMAINING);
while (bolusRemaining != null) {
log.debug("Delivering bolus, remaining: " + bolusRemaining);
SystemClock.sleep(200);
bolusRemaining = (Double) scripter.currentMenu.getAttribute(MenuAttribute.BOLUS_REMAINING);
bolusRemaining = (Double) scripter.getCurrentMenu().getAttribute(MenuAttribute.BOLUS_REMAINING);
}
// TODO what if we hit 'cartridge low' alert here? is it immediately displayed or after the bolus?
@ -74,16 +72,16 @@ public class BolusCommand extends BaseCommand {
scripter.navigateToMenu(MenuType.MY_DATA_MENU);
scripter.verifyMenuIsDisplayed(MenuType.MY_DATA_MENU);
scripter.pressCheckKey();
if (scripter.currentMenu.getType() != MenuType.BOLUS_DATA) {
if (scripter.getCurrentMenu().getType() != MenuType.BOLUS_DATA) {
scripter.waitForMenuUpdate();
}
if (!scripter.currentMenu.attributes().contains(MenuAttribute.BOLUS)) {
if (!scripter.getCurrentMenu().attributes().contains(MenuAttribute.BOLUS)) {
throw new CommandException().success(false).enacted(true)
.message("Bolus was delivered, but unable to confirm it with history record");
}
double lastBolusInHistory = (double) scripter.currentMenu.getAttribute(MenuAttribute.BOLUS);
double lastBolusInHistory = (double) scripter.getCurrentMenu().getAttribute(MenuAttribute.BOLUS);
if (Math.abs(bolus - lastBolusInHistory) > 0.05) {
throw new CommandException().success(false).enacted(true)
.message("Last bolus shows " + lastBolusInHistory

View file

@ -12,7 +12,6 @@ import java.util.Collections;
import java.util.List;
import de.jotomo.ruffyscripter.PumpCapabilities;
import de.jotomo.ruffyscripter.RuffyScripter;
public class DetermineCapabilitiesCommand extends BaseCommand {
@ -123,21 +122,21 @@ public class DetermineCapabilitiesCommand extends BaseCommand {
private long readDisplayedTbrPercentage() {
SystemClock.sleep(1000);
// TODO v2 add timeout? Currently the command execution timeout would trigger if exceeded
Object percentageObj = scripter.currentMenu.getAttribute(MenuAttribute.BASAL_RATE);
Object percentageObj = scripter.getCurrentMenu().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);
percentageObj = scripter.getCurrentMenu().getAttribute(MenuAttribute.BASAL_RATE);
}
return ((Double) percentageObj).longValue();
}
private int readDisplayedTbrDurationMainMenu() {
scripter.verifyMenuIsDisplayed(MenuType.MAIN_MENU);
if(scripter.currentMenu.attributes().contains(MenuAttribute.RUNTIME)){
if(scripter.getCurrentMenu().attributes().contains(MenuAttribute.RUNTIME)){
// TODO v2 add timeout? Currently the command execution timeout would trigger if exceeded
Object durationObj = scripter.currentMenu.getAttribute(MenuAttribute.RUNTIME);
Object durationObj = scripter.getCurrentMenu().getAttribute(MenuAttribute.RUNTIME);
MenuTime duration = (MenuTime) durationObj;
return duration.getHour() * 60 + duration.getMinute();
} else {
@ -147,8 +146,8 @@ public class DetermineCapabilitiesCommand extends BaseCommand {
private int readDisplayedTbrPercentageMainMenu() {
scripter.verifyMenuIsDisplayed(MenuType.MAIN_MENU);
if(scripter.currentMenu.attributes().contains(MenuAttribute.TBR)){
return (int)((Double) scripter.currentMenu.getAttribute(MenuAttribute.TBR)).doubleValue();
if(scripter.getCurrentMenu().attributes().contains(MenuAttribute.TBR)){
return (int)((Double) scripter.getCurrentMenu().getAttribute(MenuAttribute.TBR)).doubleValue();
} else {
return 100;
}

View file

@ -4,6 +4,7 @@ import org.monkey.d.ruffy.ruffy.driver.display.MenuType;
import java.util.Collections;
import java.util.List;
import java.util.TreeSet;
import de.jotomo.ruffyscripter.PumpState;
import de.jotomo.ruffyscripter.RuffyScripter;
@ -68,6 +69,7 @@ public class GetPumpStateCommand extends BaseCommand {
public StepBuilder cancel(Step step) {
return this;
}
public StepBuilder failure(Step step) { return this; }
public StepBuilder run() {
return this;
}
@ -106,6 +108,12 @@ public class GetPumpStateCommand extends BaseCommand {
.run();
new StepBuilder("Input bolus") // turn into a method createStep() or so, which has access to the scripter
.retries(5)
.failure(new Step() {
@Override
public void doStep() {
System.out.println("retry command");
}
})
.step(new Step() {
@Override
public void doStep() {

View file

@ -61,36 +61,36 @@ public class SetTbrCommand extends BaseCommand {
@Override
public CommandResult execute() {
try {
log.debug("1. going from " + scripter.currentMenu + " to TBR_MENU");
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.currentMenu);
throw new CommandException().message("not able to find TBR_MENU: stuck in " + scripter.getCurrentMenu());
SystemClock.sleep(500);
if (scripter.currentMenu.getType() == TBR_MENU)
if (scripter.getCurrentMenu().getType() == TBR_MENU)
break;
}
if (scripter.currentMenu.getType() != TBR_MENU)
throw new CommandException().message("not able to find TBR_MENU: stuck in " + scripter.currentMenu);
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.currentMenu);
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.currentMenu);
throw new CommandException().message("not able to find TBR_SET: stuck in " + scripter.getCurrentMenu());
SystemClock.sleep(500);
if (scripter.currentMenu.getType() == TBR_SET)
if (scripter.getCurrentMenu().getType() == TBR_SET)
break;
if (scripter.currentMenu.getType() == TBR_DURATION) {
if (scripter.getCurrentMenu().getType() == TBR_DURATION) {
scripter.pressMenuKey();
scripter.waitForScreenUpdate(1000);
}
}
log.debug("SetTbrCommand: 3. getting/setting basal percentage in " + scripter.currentMenu);
log.debug("SetTbrCommand: 3. getting/setting basal percentage in " + scripter.getCurrentMenu());
retries = 30;
double currentPercentage = scripter.readBlinkingValue(Double.class, MenuAttribute.BASAL_RATE);
@ -110,29 +110,29 @@ public class SetTbrCommand extends BaseCommand {
if (currentPercentage < 0 || retries < 0)
throw new CommandException().message("unable to set basal percentage");
log.debug("4. checking basal percentage in " + scripter.currentMenu);
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.currentMenu);
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.currentMenu);
throw new CommandException().message("not able to find TBR_SET: stuck in " + scripter.getCurrentMenu());
SystemClock.sleep(500);
if (scripter.currentMenu.getType() == TBR_DURATION)
if (scripter.getCurrentMenu().getType() == TBR_DURATION)
break;
if (scripter.currentMenu.getType() == TBR_SET) {
if (scripter.getCurrentMenu().getType() == TBR_SET) {
scripter.pressMenuKey();
scripter.waitForScreenUpdate(1000);
}
}
log.debug("6. getting/setting duration in " + scripter.currentMenu);
log.debug("6. getting/setting duration in " + scripter.getCurrentMenu());
retries = 30;
double currentDuration = scripter.readDisplayedDuration();
@ -156,30 +156,30 @@ public class SetTbrCommand extends BaseCommand {
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);
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.currentMenu);
log.debug("8. confirming TBR om " + scripter.getCurrentMenu());
retries = 5;
while (retries >= 0 && (scripter.currentMenu.getType() == TBR_DURATION || scripter.currentMenu.getType() == TBR_SET)) {
while (retries >= 0 && (scripter.getCurrentMenu().getType() == TBR_DURATION || scripter.getCurrentMenu().getType() == TBR_SET)) {
retries--;
scripter.pressCheckKey();
scripter.waitForScreenUpdate(1000);
}
if (retries < 0 || scripter.currentMenu.getType() == TBR_DURATION || scripter.currentMenu.getType() == TBR_SET)
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.currentMenu.getType() != MAIN_MENU) {
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.currentMenu.getType() == WARNING_OR_ERROR) {
if (percentage == 100 && scripter.getCurrentMenu().getType() == WARNING_OR_ERROR) {
// TODO extract method confirmAlert(alert)
scripter.pressCheckKey();
retries++;
@ -187,17 +187,17 @@ public class SetTbrCommand extends BaseCommand {
scripter.waitForScreenUpdate(1000);
} else {
retries--;
if (scripter.currentMenu.getType() == MAIN_MENU && cancelledError)
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.currentMenu.getType() != MAIN_MENU)
if (retries < 0 || scripter.getCurrentMenu().getType() != MAIN_MENU)
throw new CommandException().message("failed going to main!");
Object percentageObj = scripter.currentMenu.getAttribute(MenuAttribute.TBR);
Object durationObj = scripter.currentMenu.getAttribute(MenuAttribute.RUNTIME);
Object percentageObj = scripter.getCurrentMenu().getAttribute(MenuAttribute.TBR);
Object durationObj = scripter.getCurrentMenu().getAttribute(MenuAttribute.RUNTIME);
if (percentage == 100) {
if (durationObj != null)

View file

@ -214,14 +214,14 @@ public class SetTbrCommandAlt extends BaseCommand {
long inFiveSeconds = System.currentTimeMillis() + 5 * 1000;
boolean alertProcessed = false;
while (System.currentTimeMillis() < inFiveSeconds && !alertProcessed) {
if (scripter.currentMenu.getType() == MenuType.WARNING_OR_ERROR) {
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.currentMenu.getAttribute(MenuAttribute.MESSAGE);
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: "
@ -242,8 +242,8 @@ public class SetTbrCommandAlt extends BaseCommand {
private void verifyMainMenuShowsNoActiveTbr() {
scripter.verifyMenuIsDisplayed(MenuType.MAIN_MENU);
Double tbrPercentage = (Double) scripter.currentMenu.getAttribute(MenuAttribute.TBR);
boolean runtimeDisplayed = scripter.currentMenu.attributes().contains(MenuAttribute.RUNTIME);
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");
}
@ -252,12 +252,12 @@ public class SetTbrCommandAlt extends BaseCommand {
private void verifyMainMenuShowsExpectedTbrActive() {
scripter.verifyMenuIsDisplayed(MenuType.MAIN_MENU);
// new TBR set; percentage and duration must be displayed ...
if (!scripter.currentMenu.attributes().contains(MenuAttribute.TBR) ||
!scripter.currentMenu.attributes().contains(MenuAttribute.RUNTIME)) {
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.currentMenu.getAttribute(MenuAttribute.TBR);
MenuTime mmTbrDuration = (MenuTime) scripter.currentMenu.getAttribute(MenuAttribute.RUNTIME);
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

View file

@ -189,10 +189,11 @@ public class ComboPlugin implements PluginBase, PumpInterface {
// this must be the base package of the app (check package attribute in
// manifest element in the manifest file of the providing app)
"org.monkey.d.ruffy.ruffy",
// full path to the driver
// full path to the driver;
// in the logs this service is mentioned as (note the slash)
// "org.monkey.d.ruffy.ruffy/.driver.Ruffy"
//org.monkey.d.ruffy.ruffy is the base package identifier and /.driver.Ruffy the service within the package
// "org.monkey.d.ruffy.ruffy/.driver.Ruffy";
// org.monkey.d.ruffy.ruffy is the base package identifier
// and /.driver.Ruffy the service within the package
"org.monkey.d.ruffy.ruffy.driver.Ruffy"
));
context.startService(intent);
@ -208,9 +209,8 @@ public class ComboPlugin implements PluginBase, PumpInterface {
@Override
public void onServiceDisconnected(ComponentName name) {
ruffyScripter.stop();
log.debug("ruffy service disconnected");
// try to reconnect ruffy service unless unbind was explicitely requested
// try to reconnect ruffy service unless unbind was explicitly requested
// via unbindRuffyService
if(!keepUnbound) {
SystemClock.sleep(250);
@ -311,7 +311,7 @@ public class ComboPlugin implements PluginBase, PumpInterface {
@Override
public boolean isBusy() {
return ruffyScripter == null || ruffyScripter.isPumpBusy();
return ruffyScripter.isPumpBusy();
}
// TODO
@ -337,8 +337,8 @@ public class ComboPlugin implements PluginBase, PumpInterface {
log.debug("RefreshDataFromPump called");
// if Android is sluggish this might get called before ruffy is bound
if (ruffyScripter == null) {
log.warn("Rejecting call to RefreshDataFromPump: ruffy service not bound (yet)");
if (!ruffyScripter.isRunning()) {
log.warn("Rejecting call to RefreshDataFromPump: scripter not ready yet.");
return;
}