diff --git a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java index b89cf3c9c1..b7729d457e 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java @@ -60,16 +60,25 @@ public class RuffyScripter { public void start(IRuffyService newService) { try { - if(ruffyService!=null) - try{ruffyService.removeHandler(mHandler);}catch(Exception e){}; - if(newService!=null) { + if (ruffyService != null) { + try { + ruffyService.removeHandler(mHandler); + } catch (Exception e) { + // ignore + } + } + if (newService != null) { this.ruffyService = newService; // TODO this'll be done better in v2 via ConnectionManager if (idleDisconnectMonitorThread.getState() == Thread.State.NEW) { idleDisconnectMonitorThread.start(); } started = true; - try{newService.addHandler(mHandler);}catch (Exception e){} + try { + newService.addHandler(mHandler); + } catch (Exception e) { + // ignore + } } } catch (Exception e) { throw new RuntimeException(e); @@ -78,7 +87,7 @@ public class RuffyScripter { public void stop() { if (started) { - started=false; + started = false; // TODO ruffy removes dead handlers automatically by now. // still, check this when going through recovery logic /* try { @@ -107,16 +116,14 @@ public class RuffyScripter { && now > lastDisconnect + 15 * 1000) { log.debug("Disconnecting after " + (connectionTimeOutMs / 1000) + "s inactivity timeout"); lastDisconnect = now; - canDisconnect=true; + canDisconnect = true; ruffyService.doRTDisconnect(mHandler); connected = false; lastDisconnect = System.currentTimeMillis(); // don't attempt anything fancy in the next 10s, let the pump settle SystemClock.sleep(10 * 1000); - } - else - { - canDisconnect=false; + } else { + canDisconnect = false; } } catch (Exception e) { // TODO do we need to catch this exception somewhere else too? right now it's @@ -182,11 +189,12 @@ public class RuffyScripter { currentMenu = menu; menuLastUpdated = System.currentTimeMillis(); - synchronized (screenlock) - { + synchronized (screenlock) { screenlock.notifyAll(); } + // TODO v2 switch to using IRuffyService.isConnected, rather than guessing connectivity state + // passed on screen updates connected = true; // note that a WARNING_OR_ERROR menu can be a valid temporary state (cancelling TBR) @@ -204,9 +212,8 @@ public class RuffyScripter { @Override public void keySent(int sequence) throws RemoteException { - synchronized (keylock) - { - if(keynotwait>0) + synchronized (keylock) { + if (keynotwait > 0) keynotwait--; else keylock.notify(); @@ -225,8 +232,12 @@ public class RuffyScripter { } public void unbind() { - if(ruffyService!=null) - try{ruffyService.removeHandler(mHandler);}catch (Exception e){} + if (ruffyService != null) + try { + ruffyService.removeHandler(mHandler); + } catch (Exception e) { + // ignore + } this.ruffyService = null; } @@ -357,7 +368,9 @@ public class RuffyScripter { } } - /** If there's an issue, this times out eventually and throws a CommandException */ + /** + * If there's an issue, this times out eventually and throws a CommandException + */ private void ensureConnected() { try { boolean menuUpdateRecentlyReceived = currentMenu != null && menuLastUpdated + 1000 > System.currentTimeMillis(); @@ -377,7 +390,7 @@ public class RuffyScripter { SystemClock.sleep(10 * 1000); } - canDisconnect=false; + canDisconnect = false; boolean connectInitSuccessful = ruffyService.doRTConnect(mHandler) == 0; log.debug("Connect init successful: " + connectInitSuccessful); log.debug("Waiting for first menu update to be sent"); @@ -412,36 +425,35 @@ public class RuffyScripter { public void pressUpKey() { log.debug("Pressing up key"); - pressKey(Key.UP,2000); + pressKey(Key.UP, 2000); log.debug("Releasing up key"); } public void pressDownKey() { log.debug("Pressing down key"); - pressKey(Key.DOWN,2000); + pressKey(Key.DOWN, 2000); log.debug("Releasing down key"); } public void pressCheckKey() { log.debug("Pressing check key"); - pressKey(Key.CHECK,2000); + pressKey(Key.CHECK, 2000); log.debug("Releasing check key"); } public void pressMenuKey() { log.debug("Pressing menu key"); - pressKey(Key.MENU,2000); + pressKey(Key.MENU, 2000); log.debug("Releasing menu key"); } public void pressBackKey() { log.debug("Pressing back key"); - pressKey(Key.BACK,2000); + pressKey(Key.BACK, 2000); log.debug("Releasing back key"); } - public boolean waitForScreenUpdate(long timeout) - { + public boolean waitForScreenUpdate(long timeout) { synchronized (screenlock) { try { screenlock.wait(timeout); @@ -452,59 +464,53 @@ public class RuffyScripter { return true; } - public boolean goToMainTypeScreen(MenuType screen, long timeout) - { + 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) - { + 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()) - { + } else if (currentMenu != null && !currentMenu.getType().isMaintype()) { pressBackKey(); - } - else + } else pressMenuKey(); waitForScreenUpdate(250); } - return currentMenu != null && currentMenu.getType()==screen; + return currentMenu != null && currentMenu.getType() == screen; } - public boolean enterMenu(MenuType startType, MenuType targetType, byte key, long timeout) - { - if(currentMenu.getType()==targetType) + public boolean enterMenu(MenuType startType, MenuType targetType, byte key, long timeout) { + if (currentMenu.getType() == targetType) return true; - if(currentMenu==null || currentMenu.getType() != startType) + 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()) { + pressKey(key, 2000); + while ((currentMenu == null || currentMenu.getType() != targetType) && start + timeout > System.currentTimeMillis()) { waitForScreenUpdate(100); } - return currentMenu!=null && currentMenu.getType()==targetType; + 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); + 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 // then anything longer than a few seconds is an error; // 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 + /** * Wait until the menu update is in */ public void waitForMenuUpdate() { - waitForMenuUpdate(60); + waitForMenuUpdate(60); } public void waitForMenuUpdate(long timeoutInSeconds) { @@ -523,17 +529,12 @@ public class RuffyScripter { ruffyService.rtSendKey(key, true); //SystemClock.sleep(200); ruffyService.rtSendKey(Key.NO_KEY, true); - if(timeout > 0) - { - synchronized (keylock) - { + if (timeout > 0) { + synchronized (keylock) { keylock.wait(timeout); } - } - else - { - synchronized (keylock) - { + } else { + synchronized (keylock) { keynotwait++; } } 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 20a68ad026..137a1a8196 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java @@ -58,47 +58,44 @@ public class SetTbrCommand implements Command { return violations; } + @Override public CommandResult execute(RuffyScripter scripter, PumpState initialPumpState) { - try { - log.debug("1. going from "+scripter.currentMenu+" to TBR_MENU"); + log.debug("1. going from " + scripter.currentMenu + " to TBR_MENU"); int retries = 5; - while(!scripter.goToMainTypeScreen(TBR_MENU,3000)) - { + while (!scripter.goToMainTypeScreen(TBR_MENU, 3000)) { retries--; - if(retries==0) - throw new CommandException().message("not able to find TBR_MENU: stuck in "+scripter.currentMenu); + if (retries == 0) + throw new CommandException().message("not able to find TBR_MENU: stuck in " + scripter.currentMenu); SystemClock.sleep(500); - if(scripter.currentMenu.getType()== TBR_MENU) + if (scripter.currentMenu.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.currentMenu.getType() != TBR_MENU) + throw new CommandException().message("not able to find TBR_MENU: stuck in " + scripter.currentMenu); - log.debug("2. entering "+scripter.currentMenu); + log.debug("2. entering " + scripter.currentMenu); retries = 5; - while(!scripter.enterMenu(TBR_MENU,MenuType.TBR_SET, RuffyScripter.Key.CHECK,2000)) - { + 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); + if (retries == 0) + throw new CommandException().message("not able to find TBR_SET: stuck in " + scripter.currentMenu); SystemClock.sleep(500); - if(scripter.currentMenu.getType()== TBR_SET) + if (scripter.currentMenu.getType() == TBR_SET) break; - if(scripter.currentMenu.getType()== TBR_DURATION) - { + if (scripter.currentMenu.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.currentMenu); retries = 30; double currentPercentage = -100; - while(currentPercentage!=percentage && retries>=0) { + while (currentPercentage != percentage && retries >= 0) { retries--; Object percentageObj = scripter.currentMenu.getAttribute(MenuAttribute.BASAL_RATE); @@ -114,36 +111,36 @@ public class SetTbrCommand implements Command { scripter.waitForScreenUpdate(1000); } + } else { + currentPercentage = -100; } - else - currentPercentage=-100; scripter.waitForScreenUpdate(1000); } - if(currentPercentage<0 ||retries < 0) - throw new CommandException().message("unable to set basal percentage"); + 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.currentMenu); scripter.waitForScreenUpdate(1000); - currentPercentage= -1000; - retries=10; - while(currentPercentage<0 && retries>=0) { + 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 + } else { scripter.waitForScreenUpdate(1000); + } } - if(retries<0 ||currentPercentage!=percentage) + if (retries < 0 || currentPercentage != percentage) throw new CommandException().message("Unable to set percentage. Requested: " + percentage + ", value displayed on pump: " + currentPercentage); - if(currentPercentage!=100) { + if (currentPercentage != 100) { log.debug("5. change to TBR_DURATION from " + scripter.currentMenu); retries = 5; - while (retries >=0 && !scripter.enterMenu(TBR_SET, MenuType.TBR_DURATION, RuffyScripter.Key.MENU, 2000)) { + 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); @@ -196,8 +193,7 @@ public class SetTbrCommand implements Command { if (durationObj != null && durationObj instanceof MenuTime) { MenuTime time = (MenuTime) durationObj; currentDuration = (time.getHour() * 60) + time.getMinute(); - } - else + } else scripter.waitForScreenUpdate(1000); } if (retries < 0 || currentDuration != duration) @@ -205,62 +201,58 @@ public class SetTbrCommand implements Command { } log.debug("8. confirming TBR om " + scripter.currentMenu); - retries=5; - while(retries>= 0 && (scripter.currentMenu.getType()==TBR_DURATION ||scripter.currentMenu.getType()==TBR_SET)) - { + retries = 5; + while (retries >= 0 && (scripter.currentMenu.getType() == TBR_DURATION || scripter.currentMenu.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.currentMenu.getType() == TBR_DURATION || scripter.currentMenu.getType() == TBR_SET) throw new CommandException().message("failed setting basal!"); - retries=10; - boolean canceledError = true; - if(percentage==100) - canceledError=false; - while(retries>=0 && scripter.currentMenu.getType()!=MAIN_MENU ) - { + retries = 10; + boolean cancelledError = true; + if (percentage == 100) + cancelledError = false; + while (retries >= 0 && scripter.currentMenu.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.currentMenu.getType() == WARNING_OR_ERROR) { scripter.pressCheckKey(); retries++; - canceledError = true; + cancelledError = true; scripter.waitForScreenUpdate(1000); - } - else { + } else { retries--; - if (scripter.currentMenu.getType() == MAIN_MENU && canceledError) + if (scripter.currentMenu.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.currentMenu.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); - if(percentage==100) { - if (percentageObj != null || durationObj != null) + if (percentage == 100) { + if (percentageObj != null || durationObj != null) throw new CommandException().message("TBR cancelled, but main menu shows a running TBR"); return new CommandResult().success(true).enacted(true).message("TBR was cancelled"); } - if(percentageObj == null || !(percentageObj instanceof Double)) + if (percentageObj == null || !(percentageObj instanceof Double)) throw new CommandException().message("not percentage"); - if(((double)percentageObj)!=percentage) + if (((double) percentageObj) != percentage) throw new CommandException().message("wrong percentage set!"); - if(durationObj==null || !(durationObj instanceof MenuTime)) + if (durationObj == null || !(durationObj instanceof MenuTime)) throw new CommandException().message("not time"); MenuTime t = (MenuTime) durationObj; - if(t.getMinute()+(60*t.getHour())> duration || t.getMinute()+(60*t.getHour())< duration-5) + if (t.getMinute() + (60 * t.getHour()) > duration || t.getMinute() + (60 * t.getHour()) < duration - 5) throw new CommandException().message("wrong time set!");