From 6dba4e4956cd6aa4958e4d77167fc66559bdca7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandra=20Ke=C3=9Fler?= Date: Mon, 24 Jul 2017 18:39:43 +0200 Subject: [PATCH 01/28] fix >500% basal setting --- .../nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java index 02dfd805ee..046e94dea8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java @@ -502,6 +502,7 @@ public class ComboPlugin implements PluginBase, PumpInterface { } CommandResult commandResult = runCommand(new SetTbrCommand(adjustedPercent, durationInMinutes)); + if (commandResult.enacted) { TemporaryBasal tempStart = new TemporaryBasal(commandResult.completionTime); // TODO commandResult.state.tbrRemainingDuration might already display 29 if 30 was set, since 29:59 is shown as 29 ... From 089295188f50e3327b3d81cada0c4e91b67fa78d Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sun, 6 Aug 2017 21:47:17 +0200 Subject: [PATCH 02/28] Revert merge which was committed due to a misunderstanding. --- .../androidaps/plugins/PumpCombo/ComboPlugin.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java index 046e94dea8..85aea8011d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java @@ -533,7 +533,7 @@ public class ComboPlugin implements PluginBase, PumpInterface { } @Override - public PumpEnactResult cancelTempBasal(boolean userRequested) { + public PumpEnactResult cancelTempBasal() { log.debug("cancelTempBasal called"); CommandResult commandResult = null; @@ -578,14 +578,14 @@ public class ComboPlugin implements PluginBase, PumpInterface { if (tempBasal != null) { ConfigBuilderPlugin treatmentsInterface = MainApp.getConfigBuilder(); - treatmentsInterface.addToHistoryTempBasal(tempBasal); + treatmentsInterface.addToHistoryTempBasal(tempStop); } - if (commandResult != null) { - pumpEnactResult.success = commandResult.success; - pumpEnactResult.enacted = commandResult.enacted; - pumpEnactResult.comment = commandResult.message; - } + PumpEnactResult pumpEnactResult = new PumpEnactResult(); + pumpEnactResult.success = commandResult.success; + pumpEnactResult.enacted = commandResult.enacted; + pumpEnactResult.comment = commandResult.message; + pumpEnactResult.isTempCancel = true; return pumpEnactResult; } From b1a7083371116db77340c0f02aeba2d6d749b2a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandra=20Ke=C3=9Fler?= Date: Sat, 5 Aug 2017 15:33:28 +0200 Subject: [PATCH 03/28] add Logs to setTbrCommand --- .../ruffyscripter/commands/SetTbrCommand.java | 195 ++++++++++++++++++ 1 file changed, 195 insertions(+) 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 4ffd154acf..0fabcdc53b 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java @@ -1,6 +1,7 @@ package de.jotomo.ruffyscripter.commands; import android.os.SystemClock; +import android.util.Log; import org.monkey.d.ruffy.ruffy.driver.display.MenuAttribute; import org.monkey.d.ruffy.ruffy.driver.display.MenuType; @@ -95,6 +96,7 @@ public class SetTbrCommand implements Command { return e.toCommandResult(); } } +<<<<<<< HEAD private void enterTbrMenu(RuffyScripter scripter) { scripter.verifyMenuIsDisplayed(MenuType.MAIN_MENU); @@ -115,6 +117,199 @@ public class SetTbrCommand implements Command { if (percentageSteps < 0) { increasePercentage = false; percentageSteps = Math.abs(percentageSteps); +======= + private MenuType lastMenu; + private int retries = 0; + private void tick() + { + Log.v("SetTbrCommand:tick",state+": "+scripter.currentMenu); + switch (state) + { + case BEFORE: + if(scripter.currentMenu!=null && scripter.currentMenu.getType()==MenuType.MAIN_MENU) + { + updateState(MAIN,120); + lastMenu = MenuType.MAIN_MENU; + log.debug("found MAIN_MENU -> state:MAIN"); + retries=3; + } + break; + case MAIN: + if(retries>0) + if(scripter.goToMainMenuScreen(MenuType.TBR_MENU,30000)) + { + updateState(TBR,30); + retries=0; + } + else + retries--; + else + updateState(ERROR,30); + break; + case TBR: + if(scripter.enterMenu(MenuType.TBR_MENU,MenuType.TBR_SET, RuffyScripter.Key.CHECK,20000)) + { + updateState(SET_TBR,60); + retries = 10; + } + else + updateState(ERROR,30); + break; + case SET_TBR: + if(scripter.currentMenu!=null && scripter.currentMenu.getType()==MenuType.TBR_SET) + { + Object percentageObj = scripter.currentMenu.getAttribute(MenuAttribute.BASAL_RATE); + Log.v("SetTbrCommand:tick",state+": requested basal: "+percentage+" actual percetage: "+percentageObj); + if(percentageObj != null && percentageObj instanceof Double) + { + double currentPercentage = ((Double) percentageObj).doubleValue(); + + if(currentPercentage!=percentage) + { + if(retries>0) { + retries--; + int steps = (int) ((percentage - currentPercentage) / 10.0); + scripter.step(steps,(steps<0? RuffyScripter.Key.DOWN: RuffyScripter.Key.UP), 3000); + Log.v("SetTbrCommand:tick",state+": adjusting basal with "+steps+" steps and "+retries+" retries left"); + } + else + { + Log.v("SetTbrCommand:tick",state+": no retries left for adjusting basals"); + updateState(ERROR,30); + } + } + else + { + if(percentage==100) + { + Log.v("SetTbrCommand:tick",state+": percentage 100 going to SET"); + scripter.pressCheckKey(); + updateState(SET,30); + } + else { + Log.v("SetTbrCommand:tick",state+": basal set, going to TIME"); + updateState(TIME, 30); + } + retries=10; + } + } + } + else + { + Log.v("SetTbrCommand:tick",state+": not in correct menu: "+scripter.currentMenu+" expected "+MenuType.TBR_SET); + updateState(ERROR,30); + } + break; + case TIME: + if((scripter.currentMenu!=null && scripter.currentMenu.getType()==MenuType.TBR_DURATION) || scripter.enterMenu(MenuType.TBR_SET,MenuType.TBR_DURATION, RuffyScripter.Key.MENU,20000)) + { + Log.v("SetTbrCommand:tick",state+": going to SET_TIME"); + updateState(SET_TIME,60); + retries = 10; + } + else if(retries==0) + { + Log.v("SetTbrCommand:tick",state+": no retries left"); + updateState(ERROR,30); + } + else + { + Log.v("SetTbrCommand:tick",state+": next retry "+retries+"left"); + retries--; + } + break; + case SET_TIME: + if(scripter.currentMenu!=null && scripter.currentMenu.getType()==MenuType.TBR_DURATION) + { + Object durationObj = scripter.currentMenu.getAttribute(MenuAttribute.RUNTIME); + Log.v("SetTbrCommand:tick",state+": requested time: "+duration+" actual time: "+durationObj); + if(durationObj != null && durationObj instanceof MenuTime) + { + MenuTime time = (MenuTime) durationObj; + double currentDuration = (time.getHour()*60)+time.getMinute(); + if(currentDuration!=duration) + { + if(retries>0) { + retries--; + int steps = (int)((currentDuration - duration)/15.0); + if(currentDuration+(steps*15)duration) + steps--; + scripter.step(steps,(steps>0? RuffyScripter.Key.UP: RuffyScripter.Key.DOWN), 3000); + Log.v("SetTbrCommand:tick",state+": adjusting duration with "+steps+" steps and "+retries+" retries left"); + } + else + { + Log.v("SetTbrCommand:tick",state+": no retry left"); + updateState(ERROR,30); + } + } + else { + Log.v("SetTbrCommand:tick",state+": setting and going to SET"); + scripter.pressCheckKey(); + updateState(SET, 30); + } + } + } + else + { + Log.v("SetTbrCommand:tick",state+": not in "+ MenuType.TBR_DURATION+" but in "+scripter.currentMenu); + updateState(ERROR,60); + } + break; + case SET: + if(scripter.currentMenu!=null && scripter.currentMenu.getType()==MenuType.WARNING_OR_ERROR) + { + Log.v("SetTbrCommand:tick",state+": in ERROR_WARNING acking"); + lastMenu = scripter.currentMenu.getType(); + scripter.pressCheckKey(); + updateState(SET, 30); + } + else if(scripter.currentMenu!=null && scripter.currentMenu.getType()==MenuType.MAIN_MENU) { + Object setPercentage = scripter.currentMenu.getAttribute(MenuAttribute.TBR); + Object setDuration = scripter.currentMenu.getAttribute(MenuAttribute.RUNTIME); + Log.v("SetTbrCommand:tick",state+": got percentage: "+setPercentage+" requestes: "+percentage+" duration: "+setDuration+" request: "+duration); + + if (setPercentage== null ||setDuration==null) { + if(percentage!=100) + { + Log.v("SetTbrCommand:tick",state+": fail because nothign set"); + updateState(ERROR,10); + } + else + { + Log.v("SetTbrCommand:tick",state+": finished"); + if(lastMenu==MenuType.WARNING_OR_ERROR) + updateState(AFTER,10); + else + updateState(SET,10); + } + } + else { + double mmTbrPercentage = (Double) setPercentage; + MenuTime mmTbrDuration = (MenuTime) setDuration; + // ... 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) { + Log.v("SetTbrCommand:tick",state+": finished, correctly set"); + updateState(AFTER, 10); + } else { + Log.v("SetTbrCommand:tick",state+": failed"); + updateState(ERROR, 10); + } + } + } + break; + case ERROR: + case AFTER: + synchronized(SetTbrCommand.this) { + SetTbrCommand.this.notify(); + } + break; +>>>>>>> add Logs to setTbrCommand } log.debug("Pressing " + (increasePercentage ? "up" : "down") + " " + percentageSteps + " times"); for (int i = 0; i < percentageSteps; i++) { From 29b5ad969690ab9bc526e1917f322b4ee6692df8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandra=20Ke=C3=9Fler?= Date: Sat, 5 Aug 2017 15:33:52 +0200 Subject: [PATCH 04/28] modify Waits/timeouts to better Set tar with short Menu timeouts --- .../de/jotomo/ruffyscripter/commands/SetTbrCommand.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 0fabcdc53b..1e190de12e 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java @@ -169,8 +169,9 @@ public class SetTbrCommand implements Command { if(retries>0) { retries--; int steps = (int) ((percentage - currentPercentage) / 10.0); - scripter.step(steps,(steps<0? RuffyScripter.Key.DOWN: RuffyScripter.Key.UP), 3000); Log.v("SetTbrCommand:tick",state+": adjusting basal with "+steps+" steps and "+retries+" retries left"); + scripter.step(steps,(steps<0? RuffyScripter.Key.DOWN: RuffyScripter.Key.UP), 500); + scripter.waitScreen(1000); } else { @@ -236,8 +237,9 @@ public class SetTbrCommand implements Command { steps++; else if(currentDuration+(steps*15)>duration) steps--; - scripter.step(steps,(steps>0? RuffyScripter.Key.UP: RuffyScripter.Key.DOWN), 3000); Log.v("SetTbrCommand:tick",state+": adjusting duration with "+steps+" steps and "+retries+" retries left"); + scripter.step(steps,(steps>0? RuffyScripter.Key.UP: RuffyScripter.Key.DOWN), 500); + scripter.waitScreen(1000); } else { From e20084c97f9c9a3da98f6a94ef62aa7c2df0e455 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandra=20Ke=C3=9Fler?= Date: Sun, 6 Aug 2017 00:26:05 +0200 Subject: [PATCH 05/28] change step calculation and fix time setting (got the actual vs requested time wrong) --- .../ruffyscripter/commands/SetTbrCommand.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) 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 1e190de12e..f610e691cf 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java @@ -168,8 +168,10 @@ public class SetTbrCommand implements Command { { if(retries>0) { retries--; - int steps = (int) ((percentage - currentPercentage) / 10.0); - Log.v("SetTbrCommand:tick",state+": adjusting basal with "+steps+" steps and "+retries+" retries left"); + int requestedPercentage = (int) percentage; + int actualPercentage = (int) currentPercentage; + int steps = (requestedPercentage - actualPercentage) / 10; + Log.v("SetTbrCommand:tick",state+": adjusting basal("+requestedPercentage+"/"+actualPercentage+") with "+steps+" steps and "+retries+" retries left"); scripter.step(steps,(steps<0? RuffyScripter.Key.DOWN: RuffyScripter.Key.UP), 500); scripter.waitScreen(1000); } @@ -232,12 +234,14 @@ public class SetTbrCommand implements Command { { if(retries>0) { retries--; - int steps = (int)((currentDuration - duration)/15.0); - if(currentDuration+(steps*15)duration) + else if(currentDuration+(steps*15)>requestedDuration) steps--; - Log.v("SetTbrCommand:tick",state+": adjusting duration with "+steps+" steps and "+retries+" retries left"); + Log.v("SetTbrCommand:tick",state+": adjusting duration("+requestedDuration+"/"+actualDuration+") with "+steps+" steps and "+retries+" retries left"); scripter.step(steps,(steps>0? RuffyScripter.Key.UP: RuffyScripter.Key.DOWN), 500); scripter.waitScreen(1000); } From bd0a2981681c56b3edf00521786dfa50884cfe35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandra=20Ke=C3=9Fler?= Date: Sun, 6 Aug 2017 18:18:13 +0200 Subject: [PATCH 06/28] fix maybe long duration before setting tbr --- .../ruffyscripter/commands/SetTbrCommand.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) 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 f610e691cf..38eab80161 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java @@ -252,9 +252,24 @@ public class SetTbrCommand implements Command { } } else { - Log.v("SetTbrCommand:tick",state+": setting and going to SET"); + Log.v("SetTbrCommand:tick",state+": setting"); scripter.pressCheckKey(); - updateState(SET, 30); + scripter.waitScreen(300); + if(scripter.currentMenu.getType()==MenuType.MAIN_MENU || scripter.currentMenu.getType()!=MenuType.WARNING_OR_ERROR) + { + Log.v("SetTbrCommand:tick",state+": set and going to SET"); + updateState(SET, 30); + } + else if(retries>0) + { + Log.v("SetTbrCommand:tick",state+": not set, wait"); + retries--; + } + else + { + Log.v("SetTbrCommand:tick",state+": no retry left"); + updateState(ERROR,30); + } } } } From 6acc25978b7aa975ccf79934deccb6210b88afb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandra=20Ke=C3=9Fler?= Date: Sun, 6 Aug 2017 22:02:34 +0200 Subject: [PATCH 07/28] Small fix for slow Set error --- .../java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 38eab80161..7f0b73949f 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java @@ -254,8 +254,8 @@ public class SetTbrCommand implements Command { else { Log.v("SetTbrCommand:tick",state+": setting"); scripter.pressCheckKey(); - scripter.waitScreen(300); - if(scripter.currentMenu.getType()==MenuType.MAIN_MENU || scripter.currentMenu.getType()!=MenuType.WARNING_OR_ERROR) + scripter.waitScreen(500); + if(scripter.currentMenu.getType()==MenuType.MAIN_MENU || scripter.currentMenu.getType()==MenuType.WARNING_OR_ERROR) { Log.v("SetTbrCommand:tick",state+": set and going to SET"); updateState(SET, 30); From 6a8b39ca4f650b20f6f9873630bb4796f40d91a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandra=20Ke=C3=9Fler?= Date: Sun, 6 Aug 2017 23:19:32 +0200 Subject: [PATCH 08/28] forgot in last commit --- .../java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java | 5 +++++ 1 file changed, 5 insertions(+) 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 7f0b73949f..e6e6db0d37 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java @@ -273,6 +273,11 @@ public class SetTbrCommand implements Command { } } } + else if(scripter.currentMenu.getType()==MenuType.MAIN_MENU || scripter.currentMenu.getType()==MenuType.WARNING_OR_ERROR) + { + Log.v("SetTbrCommand:tick",state+": set and going to SET"); + updateState(SET, 30); + } else { Log.v("SetTbrCommand:tick",state+": not in "+ MenuType.TBR_DURATION+" but in "+scripter.currentMenu); From 92b342b89a2c3f66ae3d59f3648a424acb3b303b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandra=20Ke=C3=9Fler?= Date: Sun, 6 Aug 2017 23:20:01 +0200 Subject: [PATCH 09/28] add an additional wait --- .../java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java | 1 + 1 file changed, 1 insertion(+) 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 e6e6db0d37..fe4b46c5f5 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java @@ -244,6 +244,7 @@ public class SetTbrCommand implements Command { Log.v("SetTbrCommand:tick",state+": adjusting duration("+requestedDuration+"/"+actualDuration+") with "+steps+" steps and "+retries+" retries left"); scripter.step(steps,(steps>0? RuffyScripter.Key.UP: RuffyScripter.Key.DOWN), 500); scripter.waitScreen(1000); + scripter.waitScreen(1000); } else { From e2b09f9e42ba7f25e4da58dcf239b312a5501c24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandra=20Ke=C3=9Fler?= Date: Fri, 11 Aug 2017 14:19:16 +0200 Subject: [PATCH 10/28] recovered merge/unmerge damage --- app/build.gradle | 2 +- .../RuffyScripterInstrumentedTest.java | 4 +- .../d/ruffy/ruffy/driver/IRTHandler.aidl | 9 +- .../d/ruffy/ruffy/driver/IRuffyService.aidl | 4 +- .../jotomo/ruffyscripter/RuffyScripter.java | 187 +++++++++-- .../commands/GetBasalCommand.java | 191 +++++++++++ .../ruffyscripter/commands/SetTbrCommand.java | 309 +++++------------- .../plugins/PumpCombo/ComboPlugin.java | 46 ++- .../d/ruffy/ruffy/driver/display/Menu.java | 56 ++-- .../ruffy/ruffy/driver/display/MenuType.java | 77 +++-- 10 files changed, 545 insertions(+), 340 deletions(-) create mode 100644 app/src/main/java/de/jotomo/ruffyscripter/commands/GetBasalCommand.java diff --git a/app/build.gradle b/app/build.gradle index 24f609f8a9..c8e5636b6b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -44,7 +44,7 @@ android { minSdkVersion 21 targetSdkVersion 23 versionCode 1500 - version "1.5g" + version "1.5g-combo-sandratest" buildConfigField "String", "VERSION", '"' + version + '"' buildConfigField "String", "BUILDVERSION", generateGitBuild() } diff --git a/app/src/androidTest/java/ruffyscripter/RuffyScripterInstrumentedTest.java b/app/src/androidTest/java/ruffyscripter/RuffyScripterInstrumentedTest.java index 4374de8bc0..afc17ff868 100644 --- a/app/src/androidTest/java/ruffyscripter/RuffyScripterInstrumentedTest.java +++ b/app/src/androidTest/java/ruffyscripter/RuffyScripterInstrumentedTest.java @@ -44,7 +44,7 @@ public class RuffyScripterInstrumentedTest { private static Context appContext = InstrumentationRegistry.getTargetContext(); private static ServiceConnection mRuffyServiceConnection; - private static RuffyScripter ruffyScripter; + private static RuffyScripter ruffyScripter = new RuffyScripter(); @BeforeClass public static void bindRuffy() { @@ -64,7 +64,7 @@ public class RuffyScripterInstrumentedTest { @Override public void onServiceConnected(ComponentName name, IBinder service) { - ruffyScripter = new RuffyScripter(IRuffyService.Stub.asInterface(service)); + ruffyScripter.start(IRuffyService.Stub.asInterface(service)); log.debug("ruffy serivce connected"); } diff --git a/app/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/IRTHandler.aidl b/app/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/IRTHandler.aidl index 996b10b666..b6a07226b8 100644 --- a/app/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/IRTHandler.aidl +++ b/app/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/IRTHandler.aidl @@ -10,12 +10,17 @@ interface IRTHandler { void fail(String message); void requestBluetooth(); + boolean canDisconnect(); void rtStopped(); void rtStarted(); void rtClearDisplay(); void rtUpdateDisplay(in byte[] quarter, int which); - void rtDisplayHandleMenu(in Menu menu); - void rtDisplayHandleNoMenu(); + void rtDisplayHandleMenu(in Menu menu, in int sequence); + void rtDisplayHandleNoMenu(in int sequence); + + void keySent(in int sequence); + + String getServiceIdentifier(); } diff --git a/app/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/IRuffyService.aidl b/app/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/IRuffyService.aidl index ded119c7b4..f4879445b7 100644 --- a/app/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/IRuffyService.aidl +++ b/app/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/IRuffyService.aidl @@ -13,10 +13,10 @@ interface IRuffyService { * * @return 0 if successful, -1 otherwise */ - int doRTConnect(); + int doRTConnect(IRTHandler handler); /** Disconnect from the pump */ - void doRTDisconnect(); + void doRTDisconnect(IRTHandler handler); /*What's the meaning of 'changed'? * changed means if a button state has been changed, like btton pressed is a change and button release another*/ diff --git a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java index 5a29c46985..761d8aa312 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java @@ -1,6 +1,5 @@ package de.jotomo.ruffyscripter; -import android.os.DeadObjectException; import android.os.RemoteException; import android.os.SystemClock; @@ -35,7 +34,7 @@ public class RuffyScripter { private static final Logger log = LoggerFactory.getLogger(RuffyScripter.class); - private final IRuffyService ruffyService; + private IRuffyService ruffyService; private final long connectionTimeOutMs = 5000; private String unrecoverableError = null; @@ -50,27 +49,41 @@ public class RuffyScripter { private boolean started = false; - public RuffyScripter(final IRuffyService ruffyService) { - this.ruffyService = ruffyService; + private final Object keylock = new Object(); + private int keynotwait = 0; + + private final Object screenlock = new Object(); + + public RuffyScripter() { + } - public void start() { + public void start(IRuffyService newService) { try { - ruffyService.addHandler(mHandler); - idleDisconnectMonitorThread.start(); - started = true; - } catch (RemoteException e) { + 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){} + } + } catch (Exception e) { throw new RuntimeException(e); } } public void stop() { if (started) { - try { + 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); - } + }*/ } } @@ -78,6 +91,7 @@ public class RuffyScripter { return started; } + private boolean canDisconnect = false; private Thread idleDisconnectMonitorThread = new Thread(new Runnable() { @Override public void run() { @@ -91,20 +105,24 @@ public class RuffyScripter { && now > lastDisconnect + 15 * 1000) { log.debug("Disconnecting after " + (connectionTimeOutMs / 1000) + "s inactivity timeout"); lastDisconnect = now; - ruffyService.doRTDisconnect(); + 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); } - } catch (DeadObjectException doe) { + else + { + canDisconnect=false; + } + } catch (Exception e) { // TODO do we need to catch this exception somewhere else too? right now it's // converted into a command failure, but it's not classified as unrecoverable; // eventually we might try to recover ... check docs, there's also another // execption we should watch interacting with a remote service. // SecurityException was the other, when there's an AIDL mismatch; - unrecoverableError = "Ruffy service went away"; - } catch (RemoteException e) { + //unrecoverableError = "Ruffy service went away"; log.debug("Exception in idle disconnect monitor thread, carrying on", e); } SystemClock.sleep(1000); @@ -128,6 +146,11 @@ public class RuffyScripter { log.trace("Ruffy invoked requestBluetooth callback"); } + @Override + public boolean canDisconnect() throws RemoteException { + return canDisconnect; + } + @Override public void rtStopped() throws RemoteException { log.debug("rtStopped callback invoked"); @@ -150,13 +173,18 @@ public class RuffyScripter { } @Override - public void rtDisplayHandleMenu(Menu menu) throws RemoteException { + public void rtDisplayHandleMenu(Menu menu, int sequence) throws RemoteException { // method is called every ~500ms log.debug("rtDisplayHandleMenu: " + menu.getType()); currentMenu = menu; menuLastUpdated = System.currentTimeMillis(); + synchronized (screenlock) + { + screenlock.notifyAll(); + } + connected = true; // note that a WARNING_OR_ERROR menu can be a valid temporary state (cancelling TBR) @@ -167,16 +195,39 @@ public class RuffyScripter { } @Override - public void rtDisplayHandleNoMenu() throws RemoteException { + public void rtDisplayHandleNoMenu(int sequence) throws RemoteException { log.debug("rtDisplayHandleNoMenu callback invoked"); } + + @Override + public void keySent(int sequence) throws RemoteException { + synchronized (keylock) + { + if(keynotwait>0) + keynotwait--; + else + keylock.notify(); + } + } + + @Override + public String getServiceIdentifier() throws RemoteException { + return this.toString(); + } + }; public boolean isPumpBusy() { return activeCmd != null; } + public void unbind() { + if(ruffyService!=null) + try{ruffyService.removeHandler(mHandler);}catch (Exception e){} + this.ruffyService = null; + } + private static class Returnable { CommandResult cmdResult; } @@ -324,7 +375,8 @@ public class RuffyScripter { SystemClock.sleep(10 * 1000); } - boolean connectInitSuccessful = ruffyService.doRTConnect() == 0; + canDisconnect=false; + boolean connectInitSuccessful = ruffyService.doRTConnect(mHandler) == 0; log.debug("Connect init successful: " + connectInitSuccessful); log.debug("Waiting for first menu update to be sent"); // Note: there was an 'if(currentMenu == null)' around the next call, since @@ -337,7 +389,7 @@ public class RuffyScripter { // before a connection is possible. In that case, it takes 45s before the // connection comes up. waitForMenuUpdate(90); - } catch (RemoteException e) { + } catch (Exception e) { throw new CommandException().exception(e).message("Unexpected exception while initiating/restoring pump connection"); } } @@ -347,38 +399,95 @@ public class RuffyScripter { // so this class can focus on providing a connection to run commands // (or maybe reconsider putting it into a base class) - private static class Key { - static byte NO_KEY = (byte) 0x00; - static byte MENU = (byte) 0x03; - static byte CHECK = (byte) 0x0C; - static byte UP = (byte) 0x30; - static byte DOWN = (byte) 0xC0; + public static class Key { + public static byte NO_KEY = (byte) 0x00; + public static byte MENU = (byte) 0x03; + public static byte CHECK = (byte) 0x0C; + public static byte UP = (byte) 0x30; + public static byte DOWN = (byte) 0xC0; + public static byte BACK = (byte) 0x33; } public void pressUpKey() { log.debug("Pressing up key"); - pressKey(Key.UP); + pressKey(Key.UP,2000); log.debug("Releasing up key"); } public void pressDownKey() { log.debug("Pressing down key"); - pressKey(Key.DOWN); + pressKey(Key.DOWN,2000); log.debug("Releasing down key"); } public void pressCheckKey() { log.debug("Pressing check key"); - pressKey(Key.CHECK); + pressKey(Key.CHECK,2000); log.debug("Releasing check key"); } public void pressMenuKey() { log.debug("Pressing menu key"); - pressKey(Key.MENU); + pressKey(Key.MENU,2000); log.debug("Releasing menu key"); } + public void pressBackKey() { + log.debug("Pressing back key"); + pressKey(Key.BACK,2000); + log.debug("Releasing back key"); + } + + public boolean waitScreen(long timeout) + { + synchronized (screenlock) { + try { + screenlock.wait(timeout); + } catch (Exception e) { + return false; + } + } + return true; + } + + public boolean goToMainMenuScreen(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) + { + //FIXME bad thing to do :D + pressCheckKey(); + } + else if(currentMenu!=null && !currentMenu.getType().isMaintype()) + { + pressBackKey(); + } + else + pressMenuKey(); + waitScreen(250); + } + return currentMenu != null && currentMenu.getType()==screen; + } + + public boolean enterMenu(MenuType startType, MenuType targetType, byte key, long timeout) + { + 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()) { + waitScreen(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 // then anything longer than a few seconds is an error; // only ensureConnected() uses the method with the timeout parameter; inline that code, @@ -401,12 +510,26 @@ public class RuffyScripter { } } - private void pressKey(final byte key) { + private void pressKey(final byte key, long timeout) { try { ruffyService.rtSendKey(key, true); - SystemClock.sleep(100); + //SystemClock.sleep(200); ruffyService.rtSendKey(Key.NO_KEY, true); - } catch (RemoteException e) { + if(timeout > 0) + { + synchronized (keylock) + { + keylock.wait(timeout); + } + } + else + { + synchronized (keylock) + { + keynotwait++; + } + } + } catch (Exception e) { throw new CommandException().exception(e).message("Error while pressing buttons"); } } diff --git a/app/src/main/java/de/jotomo/ruffyscripter/commands/GetBasalCommand.java b/app/src/main/java/de/jotomo/ruffyscripter/commands/GetBasalCommand.java new file mode 100644 index 0000000000..8305e72031 --- /dev/null +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/GetBasalCommand.java @@ -0,0 +1,191 @@ +package de.jotomo.ruffyscripter.commands; + +import android.util.Log; + +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.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Objects; + +import de.jotomo.ruffyscripter.PumpState; +import de.jotomo.ruffyscripter.RuffyScripter; + +import static de.jotomo.ruffyscripter.commands.GetBasalCommand.State.BEFORE; +import static de.jotomo.ruffyscripter.commands.GetBasalCommand.State.MAIN; +import static de.jotomo.ruffyscripter.commands.GetBasalCommand.State.BASAL_1_MENU; +import static de.jotomo.ruffyscripter.commands.GetBasalCommand.State.BASAL_OVERVIEW; +import static de.jotomo.ruffyscripter.commands.GetBasalCommand.State.READ_BASAL; +import static de.jotomo.ruffyscripter.commands.GetBasalCommand.State.ERROR; +import static de.jotomo.ruffyscripter.commands.GetBasalCommand.State.AFTER; + +public class GetBasalCommand implements Command { + private static final Logger log = LoggerFactory.getLogger(GetBasalCommand.class); + + private RuffyScripter scripter; + + public GetBasalCommand() {} + + @Override + public List validateArguments() { + List violations = new ArrayList<>(); + + return violations; + } + + enum State { + BEFORE, + MAIN, + BASAL_1_MENU, + BASAL_OVERVIEW, + READ_BASAL, + ERROR, + AFTER, + }; + private State lastState,state; + private long last; + private long timeout; + private Thread timeoutThread = new Thread() + { + @Override + public void run() { + while(state != ERROR && state!=AFTER) { + if (timeout + last < System.currentTimeMillis()) { + lastState = state; + state = ERROR; + log.debug("timeout reached -> state:ERROR"); + } + tick(); + try { + Thread.sleep(100); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + tick(); + } + }; + private void updateState(State newState,long timeoutSec) + { + lastState = state; + state = newState; + last = System.currentTimeMillis(); + timeout = timeoutSec*1000; + } + private MenuType lastMenu; + private int retries = 0; + private double basalTotal = 0; + private Map rate = new HashMap<>(); + + private void tick() + { + switch (state) + { + case BEFORE: + if(scripter.currentMenu!=null && scripter.currentMenu.getType()==MenuType.MAIN_MENU) + { + updateState(MAIN,120); + lastMenu = MenuType.MAIN_MENU; + log.debug("found MAIN_MENU -> state:MAIN"); + retries=3; + } + break; + case MAIN: + if(retries>0) + if(scripter.goToMainMenuScreen(MenuType.BASAL_1_MENU,30000)) + { + if(scripter.enterMenu(MenuType.BASAL_1_MENU,MenuType.BASAL_TOTAL, RuffyScripter.Key.CHECK,2000)) + { + updateState(BASAL_OVERVIEW, 30); + retries=0; + } + } + else + retries--; + else + updateState(ERROR,30); + break; + case BASAL_OVERVIEW: + if(scripter.currentMenu.getType()==MenuType.BASAL_TOTAL && scripter.currentMenu.getAttribute(MenuAttribute.BASAL_TOTAL)!=null && (Integer)scripter.currentMenu.getAttribute(MenuAttribute.BASAL_SELECTED)==1) + { + basalTotal = (Double)scripter.currentMenu.getAttribute(MenuAttribute.BASAL_TOTAL); + if(scripter.enterMenu(MenuType.BASAL_TOTAL,MenuType.BASAL_SET, RuffyScripter.Key.MENU,3000)) + { + updateState(READ_BASAL,30); + retries = 96; + } + } + break; + case READ_BASAL: + if(scripter.currentMenu.getType()==MenuType.BASAL_SET && scripter.currentMenu.getAttribute(MenuAttribute.BASAL_START)!=null) { + Object rateObj = scripter.currentMenu.getAttribute(MenuAttribute.BASAL_RATE); + MenuTime time = (MenuTime) scripter.currentMenu.getAttribute(MenuAttribute.BASAL_START); + if (rateObj instanceof Double) { + rate.put(time.getHour(), (Double) rateObj); + } + boolean complete = true; + for (int t = 0; t < 24; t++) { + if (rate.get(t) == null) + complete = false; + } + if (retries > 0) { + if (complete) { + scripter.pressBackKey(); + updateState(AFTER, 30); + } else { + retries--; + scripter.pressMenuKey(); + scripter.waitScreen(250); + } + } else { + updateState(ERROR, 30); + } + } + break; + case ERROR: + case AFTER: + scripter.goToMainMenuScreen(MenuType.MAIN_MENU,2000); + synchronized(GetBasalCommand.this) { + GetBasalCommand.this.notify(); + } + break; + } + } + @Override + public CommandResult execute(RuffyScripter scripter, PumpState initialPumpState) { + state = BEFORE; + this.scripter = scripter; + updateState(BEFORE,120); + timeoutThread.start(); + + try { + synchronized (this) { + this.wait(); + } + } catch (InterruptedException e) { + e.printStackTrace(); + return new CommandResult().success(false).message("failed to wait: "+e.getMessage()); + } + if(state==AFTER) + { + for(int i = 0; i < 24;i++) + { + Log.v("BASAL_RATE","BASAL_RATE from "+String.format("%02d",i)+":00 = "+rate.get(i)); + } + return new CommandResult().success(true).enacted(true).message("Basal Rate was read"); + } + return new CommandResult().success(false).message("failed with state: "+state+" from: "+lastState); + } + + @Override + public String toString() { + return "GetTbrCommand{}"; + } +} 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 fe4b46c5f5..670d51411a 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java @@ -1,6 +1,5 @@ package de.jotomo.ruffyscripter.commands; -import android.os.SystemClock; import android.util.Log; import org.monkey.d.ruffy.ruffy.driver.display.MenuAttribute; @@ -16,11 +15,22 @@ import java.util.Locale; import de.jotomo.ruffyscripter.PumpState; import de.jotomo.ruffyscripter.RuffyScripter; +import static de.jotomo.ruffyscripter.commands.SetTbrCommand.State.AFTER; +import static de.jotomo.ruffyscripter.commands.SetTbrCommand.State.BEFORE; +import static de.jotomo.ruffyscripter.commands.SetTbrCommand.State.ERROR; +import static de.jotomo.ruffyscripter.commands.SetTbrCommand.State.MAIN; +import static de.jotomo.ruffyscripter.commands.SetTbrCommand.State.SET; +import static de.jotomo.ruffyscripter.commands.SetTbrCommand.State.SET_TBR; +import static de.jotomo.ruffyscripter.commands.SetTbrCommand.State.SET_TIME; +import static de.jotomo.ruffyscripter.commands.SetTbrCommand.State.TBR; +import static de.jotomo.ruffyscripter.commands.SetTbrCommand.State.TIME; + public class SetTbrCommand implements Command { private static final Logger log = LoggerFactory.getLogger(SetTbrCommand.class); private final long percentage; private final long duration; + private RuffyScripter scripter; public SetTbrCommand(long percentage, long duration) { this.percentage = percentage; @@ -54,70 +64,47 @@ public class SetTbrCommand implements Command { return violations; } - @Override - public CommandResult execute(RuffyScripter scripter, PumpState initialPumpState) { - try { - enterTbrMenu(scripter); - inputTbrPercentage(scripter); - verifyDisplayedTbrPercentage(scripter); - - if (percentage == 100) { - cancelTbrAndConfirmCancellationWarning(scripter); - } 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(scripter); - verifyDisplayedTbrDuration(scripter); - - // confirm TBR - scripter.pressCheckKey(); - scripter.waitForMenuToBeLeft(MenuType.TBR_DURATION); + enum State { + BEFORE, + MAIN, + TBR, + SET_TBR, + TIME, + SET_TIME, + SET, + AFTER, + ERROR + }; + private State lastState,state; + private long last; + private long timeout; + private Thread timeoutThread = new Thread() + { + @Override + public void run() { + while(state != ERROR && state!=AFTER) { + if (timeout + last < System.currentTimeMillis()) { + lastState = state; + state = ERROR; + log.debug("timeout reached -> state:ERROR"); + } + tick(); + try { + Thread.sleep(100); + } catch (InterruptedException e) { + e.printStackTrace(); + } } - - 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(scripter); - return new CommandResult().success(true).enacted(true).message("TBR was cancelled"); - } else { - verifyMainMenuShowsExpectedTbrActive(scripter); - 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(); + tick(); } + }; + private void updateState(State newState,long timeoutSec) + { + lastState = state; + state = newState; + last = System.currentTimeMillis(); + timeout = timeoutSec*1000; } -<<<<<<< HEAD - - private void enterTbrMenu(RuffyScripter scripter) { - 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(RuffyScripter scripter) { - scripter.verifyMenuIsDisplayed(MenuType.TBR_SET); - long currentPercent = readDisplayedTbrPercentage(scripter); - 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); -======= private MenuType lastMenu; private int retries = 0; private void tick() @@ -144,7 +131,10 @@ public class SetTbrCommand implements Command { else retries--; else + { + Log.v("SetTbrCommand:tick",state+": no retries left for adjusting basals"); updateState(ERROR,30); + } break; case TBR: if(scripter.enterMenu(MenuType.TBR_MENU,MenuType.TBR_SET, RuffyScripter.Key.CHECK,20000)) @@ -153,7 +143,10 @@ public class SetTbrCommand implements Command { retries = 10; } else + { + Log.v("SetTbrCommand:tick",state+": no retries left for adjusting basals"); updateState(ERROR,30); + } break; case SET_TBR: if(scripter.currentMenu!=null && scripter.currentMenu.getType()==MenuType.TBR_SET) @@ -217,8 +210,8 @@ public class SetTbrCommand implements Command { } else { - Log.v("SetTbrCommand:tick",state+": next retry "+retries+"left"); retries--; + Log.v("SetTbrCommand:tick",state+": next retry "+retries+"left"); } break; case SET_TIME: @@ -271,6 +264,7 @@ public class SetTbrCommand implements Command { Log.v("SetTbrCommand:tick",state+": no retry left"); updateState(ERROR,30); } + } } } @@ -297,7 +291,6 @@ public class SetTbrCommand implements Command { Object setPercentage = scripter.currentMenu.getAttribute(MenuAttribute.TBR); Object setDuration = scripter.currentMenu.getAttribute(MenuAttribute.RUNTIME); Log.v("SetTbrCommand:tick",state+": got percentage: "+setPercentage+" requestes: "+percentage+" duration: "+setDuration+" request: "+duration); - if (setPercentage== null ||setDuration==null) { if(percentage!=100) { @@ -336,182 +329,32 @@ public class SetTbrCommand implements Command { SetTbrCommand.this.notify(); } break; ->>>>>>> add Logs to setTbrCommand - } - log.debug("Pressing " + (increasePercentage ? "up" : "down") + " " + percentageSteps + " times"); - for (int i = 0; i < percentageSteps; i++) { - scripter.verifyMenuIsDisplayed(MenuType.TBR_SET); - 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(RuffyScripter scripter) { - scripter.verifyMenuIsDisplayed(MenuType.TBR_SET); - long displayedPercentage = readDisplayedTbrPercentage(scripter); - if (displayedPercentage != percentage) { - log.debug("Final displayed TBR percentage: " + displayedPercentage); - throw new CommandException().message("Failed to set TBR percentage"); - } - - // check again to ensure the displayed value hasn't change due to due scrolling taking extremely long - SystemClock.sleep(2000); - long refreshedDisplayedTbrPecentage = readDisplayedTbrPercentage(scripter); - if (displayedPercentage != refreshedDisplayedTbrPecentage) { - throw new CommandException().message("Failed to set TBR percentage: " + - "percentage changed after input stopped from " - + displayedPercentage + " -> " + refreshedDisplayedTbrPecentage); } } + @Override + public CommandResult execute(RuffyScripter scripter, PumpState initialPumpState) { + state = BEFORE; + this.scripter = scripter; + updateState(BEFORE,120); + timeoutThread.start(); - private long readDisplayedTbrPercentage(RuffyScripter scripter) { - // 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(RuffyScripter scripter) { - scripter.verifyMenuIsDisplayed(MenuType.TBR_DURATION); - long currentDuration = readDisplayedTbrDuration(scripter); - 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 = readDisplayedTbrDuration(scripter); - } - 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(RuffyScripter scripter) { - scripter.verifyMenuIsDisplayed(MenuType.TBR_DURATION); - long displayedDuration = readDisplayedTbrDuration(scripter); - 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); - long refreshedDisplayedTbrDuration = readDisplayedTbrDuration(scripter); - if (displayedDuration != refreshedDisplayedTbrDuration) { - throw new CommandException().message("Failed to set TBR duration: " + - "duration changed after input stopped from " - + displayedDuration + " -> " + refreshedDisplayedTbrDuration); - } - } - - private long readDisplayedTbrDuration(RuffyScripter scripter) { - // 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(RuffyScripter scripter) { - // 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.currentMenu.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); - 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; + try { + synchronized (this) { + this.wait(); } - SystemClock.sleep(10); + } catch (InterruptedException e) { + e.printStackTrace(); + return new CommandResult().success(false).message("failed to wait: "+e.getMessage()); } - } + if(state==AFTER) + { + if(percentage==100) + return new CommandResult().success(true).enacted(true).message("TBR was cancelled"); - private void verifyMainMenuShowsNoActiveTbr(RuffyScripter scripter) { - scripter.verifyMenuIsDisplayed(MenuType.MAIN_MENU); - Double tbrPercentage = (Double) scripter.currentMenu.getAttribute(MenuAttribute.TBR); - boolean runtimeDisplayed = scripter.currentMenu.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(RuffyScripter scripter) { - 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)) { - 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); - // ... 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"); + return new CommandResult().success(true).enacted(true).message( + String.format(Locale.US, "TBR set to %d%% for %d min", percentage, duration)); } + return new CommandResult().success(false).message("failed with state: "+state+" from: "+lastState); } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java index 85aea8011d..b276c23594 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java @@ -91,6 +91,7 @@ public class ComboPlugin implements PluginBase, PumpInterface { MainApp.bus().register(this); bindRuffyService(); startAlerter(); + ruffyScripter = new RuffyScripter(); } private void definePumpCapabilities() { @@ -163,7 +164,10 @@ public class ComboPlugin implements PluginBase, PumpInterface { mgr.notify(id, notificationBuilder.build()); lastAlarmTime = now; } else { + // TODO would it be useful to have a 'last error' field in the ui showing the most recent + // failed command? the next command that runs successful with will override this error log.warn("Pump still in error state, but alarm raised recently, so not triggering again: " + localLastCmdResult.message); + refreshDataFromPump("from Error Recovery"); } } SystemClock.sleep(5 * 1000); @@ -172,7 +176,8 @@ public class ComboPlugin implements PluginBase, PumpInterface { }, "combo-alerter").start(); } - private void bindRuffyService() { + private boolean bindRuffyService() { + Context context = MainApp.instance().getApplicationContext(); boolean boundSucceeded = false; @@ -185,6 +190,7 @@ public class ComboPlugin implements PluginBase, PumpInterface { // 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" )); context.startService(intent); @@ -193,16 +199,22 @@ public class ComboPlugin implements PluginBase, PumpInterface { @Override public void onServiceConnected(ComponentName name, IBinder service) { - ruffyScripter = new RuffyScripter(IRuffyService.Stub.asInterface(service)); - ruffyScripter.start(); + keepUnbound=false; + ruffyScripter.start(IRuffyService.Stub.asInterface(service)); log.debug("ruffy serivce connected"); } @Override public void onServiceDisconnected(ComponentName name) { ruffyScripter.stop(); - ruffyScripter = null; log.debug("ruffy service disconnected"); + if(!keepUnbound) { + try { + Thread.sleep(250); + } catch (Exception e) { + } + bindRuffyService(); + } } }; boundSucceeded = context.bindService(intent, mRuffyServiceConnection, Context.BIND_AUTO_CREATE); @@ -213,9 +225,13 @@ public class ComboPlugin implements PluginBase, PumpInterface { if (!boundSucceeded) { statusSummary = "No connection to ruffy. Pump control not available."; } + return true; } + private boolean keepUnbound = false; private void unbindRuffyService() { + keepUnbound = true; + ruffyScripter.unbind(); MainApp.instance().getApplicationContext().unbindService(mRuffyServiceConnection); } @@ -414,6 +430,7 @@ public class ComboPlugin implements PluginBase, PumpInterface { MainApp.bus().post(new EventComboPumpUpdateGUI()); CommandResult commandResult = ruffyScripter.runCommand(command); + // TODO extract this into a recovery method and check the logic of restarting and rebinding ruffy if (!commandResult.success && commandResult.exception != null) { log.error("CommandResult has exception, rebinding ruffy service", commandResult.exception); @@ -421,6 +438,12 @@ public class ComboPlugin implements PluginBase, PumpInterface { try { unbindRuffyService(); SystemClock.sleep(5000); + } catch (Exception e) { + /*String msg = "No connection to ruffy. Pump control not available."; + statusSummary = msg; + return new CommandResult().message(msg);*/ + } + try { bindRuffyService(); SystemClock.sleep(5000); } catch (Exception e) { @@ -533,7 +556,7 @@ public class ComboPlugin implements PluginBase, PumpInterface { } @Override - public PumpEnactResult cancelTempBasal() { + public PumpEnactResult cancelTempBasal(boolean userRequested) { log.debug("cancelTempBasal called"); CommandResult commandResult = null; @@ -543,6 +566,7 @@ public class ComboPlugin implements PluginBase, PumpInterface { final TemporaryBasal activeTemp = MainApp.getConfigBuilder().getTempBasalFromHistory(System.currentTimeMillis()); if (activeTemp == null || userRequested) { + /* v1 compatibility to sync DB to pump if they diverged (activeTemp == null) */ log.debug("cancelTempBasal: hard-cancelling TBR since user requested"); commandResult = runCommand(new CancelTbrCommand()); if (commandResult.enacted) { @@ -578,14 +602,14 @@ public class ComboPlugin implements PluginBase, PumpInterface { if (tempBasal != null) { ConfigBuilderPlugin treatmentsInterface = MainApp.getConfigBuilder(); - treatmentsInterface.addToHistoryTempBasal(tempStop); + treatmentsInterface.addToHistoryTempBasal(tempBasal); } - PumpEnactResult pumpEnactResult = new PumpEnactResult(); - pumpEnactResult.success = commandResult.success; - pumpEnactResult.enacted = commandResult.enacted; - pumpEnactResult.comment = commandResult.message; - pumpEnactResult.isTempCancel = true; + if (commandResult != null) { + pumpEnactResult.success = commandResult.success; + pumpEnactResult.enacted = commandResult.enacted; + pumpEnactResult.comment = commandResult.message; + } return pumpEnactResult; } diff --git a/app/src/main/java/org/monkey/d/ruffy/ruffy/driver/display/Menu.java b/app/src/main/java/org/monkey/d/ruffy/ruffy/driver/display/Menu.java index 20e74c7805..24a57b7d15 100644 --- a/app/src/main/java/org/monkey/d/ruffy/ruffy/driver/display/Menu.java +++ b/app/src/main/java/org/monkey/d/ruffy/ruffy/driver/display/Menu.java @@ -35,30 +35,32 @@ public class Menu implements Parcelable{ String clas = in.readString(); String value = in.readString(); - MenuAttribute a = MenuAttribute.valueOf(attr); - Object o = null; - if (Integer.class.toString().equals(clas)) { - o = new Integer(value); - } else if (Double.class.toString().equals(clas)) { - o = new Double(value); - } else if (Boolean.class.toString().equals(clas)) { - o = new Boolean(value); - } else if (MenuDate.class.toString().equals(clas)) { - o = new MenuDate(value); - } else if (MenuTime.class.toString().equals(clas)) { - o = new MenuTime(value); - } else if (MenuBlink.class.toString().equals(clas)) { - o = new MenuBlink(); - } else if (BolusType.class.toString().equals(clas)) { - o = BolusType.valueOf(value); - } else if (String.class.toString().equals(clas)) { - o = new String(value); - } + if(attr!=null && clas!=null && value!=null) { + MenuAttribute a = MenuAttribute.valueOf(attr); + Object o = null; + if (Integer.class.toString().equals(clas)) { + o = new Integer(value); + } else if (Double.class.toString().equals(clas)) { + o = new Double(value); + } else if (Boolean.class.toString().equals(clas)) { + o = new Boolean(value); + } else if (MenuDate.class.toString().equals(clas)) { + o = new MenuDate(value); + } else if (MenuTime.class.toString().equals(clas)) { + o = new MenuTime(value); + } else if (MenuBlink.class.toString().equals(clas)) { + o = new MenuBlink(); + } else if (BolusType.class.toString().equals(clas)) { + o = BolusType.valueOf(value); + } else if (String.class.toString().equals(clas)) { + o = new String(value); + } - if (o != null) { - attributes.put(a, o); - } else { - Log.e("MenuIn", "failed to parse: " + attr + " / " + clas + " / " + value); + if (o != null) { + attributes.put(a, o); + } else { + Log.e("MenuIn", "failed to parse: " + attr + " / " + clas + " / " + value); + } } }catch(Exception e) { @@ -120,4 +122,12 @@ public class Menu implements Parcelable{ return new Menu[size]; } }; + + @Override + public String toString() { + String to = "Menu: "+getType()+" atr:"; + for(MenuAttribute atr : attributes()) + to+=atr.toString()+"="+getAttribute(atr)+";"; + return to; + } } diff --git a/app/src/main/java/org/monkey/d/ruffy/ruffy/driver/display/MenuType.java b/app/src/main/java/org/monkey/d/ruffy/ruffy/driver/display/MenuType.java index 4d00ea9155..2fb7a53f67 100644 --- a/app/src/main/java/org/monkey/d/ruffy/ruffy/driver/display/MenuType.java +++ b/app/src/main/java/org/monkey/d/ruffy/ruffy/driver/display/MenuType.java @@ -5,38 +5,47 @@ package org.monkey.d.ruffy.ruffy.driver.display; */ public enum MenuType { - MAIN_MENU, - STOP_MENU, - BOLUS_MENU, - BOLUS_ENTER, - EXTENDED_BOLUS_MENU, - BOLUS_DURATION, - MULTIWAVE_BOLUS_MENU, - IMMEDIATE_BOLUS, - TBR_MENU, - MY_DATA_MENU, - BASAL_MENU, - BASAL_1_MENU, - BASAL_2_MENU, - BASAL_3_MENU, - BASAL_4_MENU, - BASAL_5_MENU, - DATE_AND_TIME_MENU, - ALARM_MENU, - MENU_SETTINGS_MENU, - BLUETOOTH_MENU, - THERAPY_MENU, - PUMP_MENU, - QUICK_INFO, - BOLUS_DATA, - DAILY_DATA, - TBR_DATA, - ERROR_DATA, - TBR_SET, - TBR_DURATION, - STOP, - START_MENU, - BASAL_TOTAL, - BASAL_SET, - WARNING_OR_ERROR, + MAIN_MENU(true), + STOP_MENU(true), + BOLUS_MENU(true), + BOLUS_ENTER(false), + EXTENDED_BOLUS_MENU(true), + BOLUS_DURATION(false), + MULTIWAVE_BOLUS_MENU(true), + IMMEDIATE_BOLUS(false), + TBR_MENU(true), + MY_DATA_MENU(true), + BASAL_MENU(true), + BASAL_1_MENU(true), + BASAL_2_MENU(true), + BASAL_3_MENU(true), + BASAL_4_MENU(true), + BASAL_5_MENU(true), + DATE_AND_TIME_MENU(true), + ALARM_MENU(true), + MENU_SETTINGS_MENU(true), + BLUETOOTH_MENU(true), + THERAPY_MENU(true), + PUMP_MENU(true), + QUICK_INFO(false), + BOLUS_DATA(false), + DAILY_DATA(false), + TBR_DATA(false), + ERROR_DATA(false), + TBR_SET(false), + TBR_DURATION(false), + STOP(false), + START_MENU(false), + BASAL_TOTAL(false), + BASAL_SET(false), + WARNING_OR_ERROR(false),; + + private boolean maintype = false; + MenuType(boolean b) { + maintype=b; + } + + public boolean isMaintype() { + return maintype; + } } From 1c1ea14b3c1dd48ebe756810926e013e12c49acc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandra=20Ke=C3=9Fler?= Date: Fri, 11 Aug 2017 23:58:45 +0200 Subject: [PATCH 11/28] remove old handler if it was set --- app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java index 761d8aa312..1afbac3b9f 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java @@ -60,6 +60,8 @@ public class RuffyScripter { public void start(IRuffyService newService) { try { + if(ruffyService!=null) + try{ruffyService.removeHandler(mHandler);}catch(Exception e){}; if(newService!=null) { this.ruffyService = newService; // TODO this'll be done better in v2 via ConnectionManager From abe4c1ff3a3b08e632840610007652bc4b502269 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandra=20Ke=C3=9Fler?= Date: Fri, 11 Aug 2017 23:59:05 +0200 Subject: [PATCH 12/28] return immidiatly if we are in correct menu --- app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java index 1afbac3b9f..849b1a92ed 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java @@ -475,6 +475,8 @@ public class RuffyScripter { 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(); From 3fd6884d03401f551239cf1692d49277bec8e4d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandra=20Ke=C3=9Fler?= Date: Sat, 12 Aug 2017 00:12:11 +0200 Subject: [PATCH 13/28] just rewrote SetTbrCommand to be a scripted command, maybe fixed some errors along the way, Runs very smooth on my shitty pump --- .../ruffyscripter/commands/SetTbrCommand.java | 489 +++++++----------- 1 file changed, 199 insertions(+), 290 deletions(-) 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 670d51411a..15013764d3 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java @@ -15,22 +15,17 @@ import java.util.Locale; import de.jotomo.ruffyscripter.PumpState; import de.jotomo.ruffyscripter.RuffyScripter; -import static de.jotomo.ruffyscripter.commands.SetTbrCommand.State.AFTER; -import static de.jotomo.ruffyscripter.commands.SetTbrCommand.State.BEFORE; -import static de.jotomo.ruffyscripter.commands.SetTbrCommand.State.ERROR; -import static de.jotomo.ruffyscripter.commands.SetTbrCommand.State.MAIN; -import static de.jotomo.ruffyscripter.commands.SetTbrCommand.State.SET; -import static de.jotomo.ruffyscripter.commands.SetTbrCommand.State.SET_TBR; -import static de.jotomo.ruffyscripter.commands.SetTbrCommand.State.SET_TIME; -import static de.jotomo.ruffyscripter.commands.SetTbrCommand.State.TBR; -import static de.jotomo.ruffyscripter.commands.SetTbrCommand.State.TIME; +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 implements Command { private static final Logger log = LoggerFactory.getLogger(SetTbrCommand.class); private final long percentage; private final long duration; - private RuffyScripter scripter; public SetTbrCommand(long percentage, long duration) { this.percentage = percentage; @@ -64,297 +59,211 @@ public class SetTbrCommand implements Command { return violations; } - enum State { - BEFORE, - MAIN, - TBR, - SET_TBR, - TIME, - SET_TIME, - SET, - AFTER, - ERROR - }; - private State lastState,state; - private long last; - private long timeout; - private Thread timeoutThread = new Thread() - { - @Override - public void run() { - while(state != ERROR && state!=AFTER) { - if (timeout + last < System.currentTimeMillis()) { - lastState = state; - state = ERROR; - log.debug("timeout reached -> state:ERROR"); - } - tick(); - try { - Thread.sleep(100); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - tick(); - } - }; - private void updateState(State newState,long timeoutSec) - { - lastState = state; - state = newState; - last = System.currentTimeMillis(); - timeout = timeoutSec*1000; - } - private MenuType lastMenu; - private int retries = 0; - private void tick() - { - Log.v("SetTbrCommand:tick",state+": "+scripter.currentMenu); - switch (state) - { - case BEFORE: - if(scripter.currentMenu!=null && scripter.currentMenu.getType()==MenuType.MAIN_MENU) - { - updateState(MAIN,120); - lastMenu = MenuType.MAIN_MENU; - log.debug("found MAIN_MENU -> state:MAIN"); - retries=3; - } - break; - case MAIN: - if(retries>0) - if(scripter.goToMainMenuScreen(MenuType.TBR_MENU,30000)) - { - updateState(TBR,30); - retries=0; - } - else - retries--; - else - { - Log.v("SetTbrCommand:tick",state+": no retries left for adjusting basals"); - updateState(ERROR,30); - } - break; - case TBR: - if(scripter.enterMenu(MenuType.TBR_MENU,MenuType.TBR_SET, RuffyScripter.Key.CHECK,20000)) - { - updateState(SET_TBR,60); - retries = 10; - } - else - { - Log.v("SetTbrCommand:tick",state+": no retries left for adjusting basals"); - updateState(ERROR,30); - } - break; - case SET_TBR: - if(scripter.currentMenu!=null && scripter.currentMenu.getType()==MenuType.TBR_SET) - { - Object percentageObj = scripter.currentMenu.getAttribute(MenuAttribute.BASAL_RATE); - Log.v("SetTbrCommand:tick",state+": requested basal: "+percentage+" actual percetage: "+percentageObj); - if(percentageObj != null && percentageObj instanceof Double) - { - double currentPercentage = ((Double) percentageObj).doubleValue(); - - if(currentPercentage!=percentage) - { - if(retries>0) { - retries--; - int requestedPercentage = (int) percentage; - int actualPercentage = (int) currentPercentage; - int steps = (requestedPercentage - actualPercentage) / 10; - Log.v("SetTbrCommand:tick",state+": adjusting basal("+requestedPercentage+"/"+actualPercentage+") with "+steps+" steps and "+retries+" retries left"); - scripter.step(steps,(steps<0? RuffyScripter.Key.DOWN: RuffyScripter.Key.UP), 500); - scripter.waitScreen(1000); - } - else - { - Log.v("SetTbrCommand:tick",state+": no retries left for adjusting basals"); - updateState(ERROR,30); - } - } - else - { - if(percentage==100) - { - Log.v("SetTbrCommand:tick",state+": percentage 100 going to SET"); - scripter.pressCheckKey(); - updateState(SET,30); - } - else { - Log.v("SetTbrCommand:tick",state+": basal set, going to TIME"); - updateState(TIME, 30); - } - retries=10; - } - } - } - else - { - Log.v("SetTbrCommand:tick",state+": not in correct menu: "+scripter.currentMenu+" expected "+MenuType.TBR_SET); - updateState(ERROR,30); - } - break; - case TIME: - if((scripter.currentMenu!=null && scripter.currentMenu.getType()==MenuType.TBR_DURATION) || scripter.enterMenu(MenuType.TBR_SET,MenuType.TBR_DURATION, RuffyScripter.Key.MENU,20000)) - { - Log.v("SetTbrCommand:tick",state+": going to SET_TIME"); - updateState(SET_TIME,60); - retries = 10; - } - else if(retries==0) - { - Log.v("SetTbrCommand:tick",state+": no retries left"); - updateState(ERROR,30); - } - else - { - retries--; - Log.v("SetTbrCommand:tick",state+": next retry "+retries+"left"); - } - break; - case SET_TIME: - if(scripter.currentMenu!=null && scripter.currentMenu.getType()==MenuType.TBR_DURATION) - { - Object durationObj = scripter.currentMenu.getAttribute(MenuAttribute.RUNTIME); - Log.v("SetTbrCommand:tick",state+": requested time: "+duration+" actual time: "+durationObj); - if(durationObj != null && durationObj instanceof MenuTime) - { - MenuTime time = (MenuTime) durationObj; - double currentDuration = (time.getHour()*60)+time.getMinute(); - if(currentDuration!=duration) - { - if(retries>0) { - retries--; - int requestedDuration = (int) duration; - int actualDuration = (int) currentDuration; - int steps = (requestedDuration - actualDuration)/15; - if(currentDuration+(steps*15)requestedDuration) - steps--; - Log.v("SetTbrCommand:tick",state+": adjusting duration("+requestedDuration+"/"+actualDuration+") with "+steps+" steps and "+retries+" retries left"); - scripter.step(steps,(steps>0? RuffyScripter.Key.UP: RuffyScripter.Key.DOWN), 500); - scripter.waitScreen(1000); - scripter.waitScreen(1000); - } - else - { - Log.v("SetTbrCommand:tick",state+": no retry left"); - updateState(ERROR,30); - } - } - else { - Log.v("SetTbrCommand:tick",state+": setting"); - scripter.pressCheckKey(); - scripter.waitScreen(500); - if(scripter.currentMenu.getType()==MenuType.MAIN_MENU || scripter.currentMenu.getType()==MenuType.WARNING_OR_ERROR) - { - Log.v("SetTbrCommand:tick",state+": set and going to SET"); - updateState(SET, 30); - } - else if(retries>0) - { - Log.v("SetTbrCommand:tick",state+": not set, wait"); - retries--; - } - else - { - Log.v("SetTbrCommand:tick",state+": no retry left"); - updateState(ERROR,30); - } - - } - } - } - else if(scripter.currentMenu.getType()==MenuType.MAIN_MENU || scripter.currentMenu.getType()==MenuType.WARNING_OR_ERROR) - { - Log.v("SetTbrCommand:tick",state+": set and going to SET"); - updateState(SET, 30); - } - else - { - Log.v("SetTbrCommand:tick",state+": not in "+ MenuType.TBR_DURATION+" but in "+scripter.currentMenu); - updateState(ERROR,60); - } - break; - case SET: - if(scripter.currentMenu!=null && scripter.currentMenu.getType()==MenuType.WARNING_OR_ERROR) - { - Log.v("SetTbrCommand:tick",state+": in ERROR_WARNING acking"); - lastMenu = scripter.currentMenu.getType(); - scripter.pressCheckKey(); - updateState(SET, 30); - } - else if(scripter.currentMenu!=null && scripter.currentMenu.getType()==MenuType.MAIN_MENU) { - Object setPercentage = scripter.currentMenu.getAttribute(MenuAttribute.TBR); - Object setDuration = scripter.currentMenu.getAttribute(MenuAttribute.RUNTIME); - Log.v("SetTbrCommand:tick",state+": got percentage: "+setPercentage+" requestes: "+percentage+" duration: "+setDuration+" request: "+duration); - if (setPercentage== null ||setDuration==null) { - if(percentage!=100) - { - Log.v("SetTbrCommand:tick",state+": fail because nothign set"); - updateState(ERROR,10); - } - else - { - Log.v("SetTbrCommand:tick",state+": finished"); - if(lastMenu==MenuType.WARNING_OR_ERROR) - updateState(AFTER,10); - else - updateState(SET,10); - } - } - else { - double mmTbrPercentage = (Double) setPercentage; - MenuTime mmTbrDuration = (MenuTime) setDuration; - // ... 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) { - Log.v("SetTbrCommand:tick",state+": finished, correctly set"); - updateState(AFTER, 10); - } else { - Log.v("SetTbrCommand:tick",state+": failed"); - updateState(ERROR, 10); - } - } - } - break; - case ERROR: - case AFTER: - synchronized(SetTbrCommand.this) { - SetTbrCommand.this.notify(); - } - break; - } - } @Override public CommandResult execute(RuffyScripter scripter, PumpState initialPumpState) { - state = BEFORE; - this.scripter = scripter; - updateState(BEFORE,120); - timeoutThread.start(); try { - synchronized (this) { - this.wait(); + Log.v("SetTbrCommand","1. going from "+scripter.currentMenu+" to TBR_MENU"); + int retries = 5; + while(!scripter.goToMainMenuScreen(TBR_MENU,3000)) + { + retries--; + if(retries==0) + throw new Exception("not able to find TBR_MENU: stuck in "+scripter.currentMenu); + Thread.sleep(500); + if(scripter.currentMenu.getType()== TBR_MENU) + break; } - } catch (InterruptedException e) { - e.printStackTrace(); - return new CommandResult().success(false).message("failed to wait: "+e.getMessage()); - } - if(state==AFTER) - { + + if(scripter.currentMenu.getType()!=TBR_MENU) + throw new Exception("not able to find TBR_MENU: stuck in "+scripter.currentMenu); + + Log.v("SetTbrCommand","2. entering "+scripter.currentMenu); + retries = 5; + while(!scripter.enterMenu(TBR_MENU,MenuType.TBR_SET, RuffyScripter.Key.CHECK,2000)) + { + retries--; + if(retries==0) + throw new Exception("not able to find TBR_SET: stuck in "+scripter.currentMenu); + Thread.sleep(500); + if(scripter.currentMenu.getType()== TBR_SET) + break; + if(scripter.currentMenu.getType()== TBR_DURATION) + { + scripter.pressMenuKey(); + scripter.waitScreen(1000); + } + } + + Log.v("SetTbrCommand","3. getting/setting basal in "+scripter.currentMenu); + retries = 30; + + double currentPercentage = -100; + 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.v("SetTbrCommand", "Adjusting basal(" + requestedPercentage + "/" + actualPercentage + ") with " + steps + " steps and " + retries + " retries left"); + scripter.step(steps, (steps < 0 ? RuffyScripter.Key.DOWN : RuffyScripter.Key.UP), 500); + scripter.waitScreen(1000); + } + + } + else + currentPercentage=-100; + scripter.waitScreen(1000); + } + if(currentPercentage<0 ||retries < 0) + throw new Exception("unable to set basalrate"); + + Log.v("SetTbrCommand","4. checking basal in "+scripter.currentMenu); + scripter.waitScreen(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.waitScreen(1000); + } + + if(retries<0 ||currentPercentage!=percentage) + throw new Exception("wrong rate!"); + + if(currentPercentage!=100) { + Log.v("SetTbrCommand", "5. change to TBR_DURATION from " + scripter.currentMenu); + retries = 5; + while (retries >=0 && !scripter.enterMenu(TBR_SET, MenuType.TBR_DURATION, RuffyScripter.Key.MENU, 2000)) { + retries--; + if (retries == 0) + throw new Exception("not able to find TBR_SET: stuck in " + scripter.currentMenu); + Thread.sleep(500); + if (scripter.currentMenu.getType() == TBR_DURATION) + break; + if (scripter.currentMenu.getType() == TBR_SET) { + scripter.pressMenuKey(); + scripter.waitScreen(1000); + } + } + + Log.v("SetTbrCommand", "6. getting/setting duration in " + scripter.currentMenu); + retries = 30; + + double currentDuration = -100; + while (currentDuration != duration && retries >= 0) { + retries--; + Object durationObj = scripter.currentMenu.getAttribute(MenuAttribute.RUNTIME); + Log.v("SetTbrCommand", "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.v("SetTbrCommand", "Adjusting duration(" + requestedDuration + "/" + actualDuration + ") with " + steps + " steps and " + retries + " retries left"); + scripter.step(steps, (steps > 0 ? RuffyScripter.Key.UP : RuffyScripter.Key.DOWN), 500); + scripter.waitScreen(1000); + } else { + break; + } + } + else + scripter.waitScreen(1000); + } + if (currentDuration < 0 || retries < 0) + throw new Exception("unable to set duration"); + + Log.v("SetTbrCommand", "7. checking time in " + scripter.currentMenu); + scripter.waitScreen(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.waitScreen(1000); + } + if (retries < 0 || currentDuration != duration) + throw new Exception("wrong time!"); + } + + Log.v("SetTbrCommand", "8. setting from " + scripter.currentMenu); + retries=5; + while(retries>= 0 && (scripter.currentMenu.getType()==TBR_DURATION ||scripter.currentMenu.getType()==TBR_SET)) + { + retries--; + scripter.pressCheckKey(); + scripter.waitScreen(1000); + } + if(retries<0 || scripter.currentMenu.getType()==TBR_DURATION ||scripter.currentMenu.getType()==TBR_SET) + throw new Exception("failed setting basal!"); + retries=10; + boolean canceledError = true; + if(percentage==100) + canceledError=false; + while(retries>=0 && scripter.currentMenu.getType()!=MAIN_MENU ) + { + if(percentage==100 && scripter.currentMenu.getType()==WARNING_OR_ERROR) + { + scripter.pressCheckKey(); + retries++; + canceledError = true; + } + else { + retries--; + if (scripter.currentMenu.getType() == MAIN_MENU && canceledError) + break; + } + } + if(retries<0 || scripter.currentMenu.getType()!=MAIN_MENU ) + throw new Exception("failed going to main!"); + + Object percentageObj = scripter.currentMenu.getAttribute(MenuAttribute.TBR); + Object durationObj = scripter.currentMenu.getAttribute(MenuAttribute.RUNTIME); + + if(percentageObj == null || !(percentageObj instanceof Double)) + throw new Exception("not percentage"); + + if(((double)percentageObj)!=percentage) + throw new Exception("wrong percentage set!"); + if(percentage==100) return new CommandResult().success(true).enacted(true).message("TBR was cancelled"); + if(durationObj==null || !(durationObj instanceof MenuTime)) + throw new Exception("not time"); + + MenuTime t = (MenuTime) durationObj; + if(t.getMinute()+(60*t.getHour())> duration || t.getMinute()+(60*t.getHour())< duration-5) + throw new Exception("wrong time set!"); + 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.e("SetTbrCommand", "got exception: ", e); + return new CommandResult().success(false).message("failed to wait: " + e.getMessage()); } - return new CommandResult().success(false).message("failed with state: "+state+" from: "+lastState); } @Override From 92ca9aaedecfbf770daebd7b29b9b46ae7fc6b19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandra=20Ke=C3=9Fler?= Date: Sat, 12 Aug 2017 00:27:30 +0200 Subject: [PATCH 14/28] some Small improvements --- .../de/jotomo/ruffyscripter/commands/SetTbrCommand.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) 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 15013764d3..77bff247c8 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java @@ -121,7 +121,7 @@ public class SetTbrCommand implements Command { scripter.waitScreen(1000); } if(currentPercentage<0 ||retries < 0) - throw new Exception("unable to set basalrate"); + throw new Exception("unable to set basalrate"); Log.v("SetTbrCommand","4. checking basal in "+scripter.currentMenu); scripter.waitScreen(1000); @@ -179,12 +179,9 @@ public class SetTbrCommand implements Command { Log.v("SetTbrCommand", "Adjusting duration(" + requestedDuration + "/" + actualDuration + ") with " + steps + " steps and " + retries + " retries left"); scripter.step(steps, (steps > 0 ? RuffyScripter.Key.UP : RuffyScripter.Key.DOWN), 500); scripter.waitScreen(1000); - } else { - break; } } - else - scripter.waitScreen(1000); + scripter.waitScreen(1000); } if (currentDuration < 0 || retries < 0) throw new Exception("unable to set duration"); @@ -229,6 +226,7 @@ public class SetTbrCommand implements Command { scripter.pressCheckKey(); retries++; canceledError = true; + scripter.waitScreen(1000); } else { retries--; From 59ee67a3534e67a04c09a270d0c12e260fc360b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandra=20Ke=C3=9Fler?= Date: Sat, 12 Aug 2017 09:29:07 +0200 Subject: [PATCH 15/28] add Logger logs --- .../ruffyscripter/commands/SetTbrCommand.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) 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 77bff247c8..d1b43384b8 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java @@ -58,12 +58,12 @@ public class SetTbrCommand implements Command { return violations; } - @Override public CommandResult execute(RuffyScripter scripter, PumpState initialPumpState) { try { Log.v("SetTbrCommand","1. going from "+scripter.currentMenu+" to TBR_MENU"); + log.debug("1. going from "+scripter.currentMenu+" to TBR_MENU"); int retries = 5; while(!scripter.goToMainMenuScreen(TBR_MENU,3000)) { @@ -79,6 +79,7 @@ public class SetTbrCommand implements Command { throw new Exception("not able to find TBR_MENU: stuck in "+scripter.currentMenu); Log.v("SetTbrCommand","2. entering "+scripter.currentMenu); + log.debug("2. entering "+scripter.currentMenu); retries = 5; while(!scripter.enterMenu(TBR_MENU,MenuType.TBR_SET, RuffyScripter.Key.CHECK,2000)) { @@ -96,6 +97,7 @@ public class SetTbrCommand implements Command { } Log.v("SetTbrCommand","3. getting/setting basal in "+scripter.currentMenu); + log.debug("SetTbrCommand: 3. getting/setting basal in "+scripter.currentMenu); retries = 30; double currentPercentage = -100; @@ -111,6 +113,7 @@ public class SetTbrCommand implements Command { int actualPercentage = (int) currentPercentage; int steps = (requestedPercentage - actualPercentage) / 10; Log.v("SetTbrCommand", "Adjusting basal(" + requestedPercentage + "/" + actualPercentage + ") with " + steps + " steps and " + retries + " retries left"); + 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.waitScreen(1000); } @@ -124,6 +127,7 @@ public class SetTbrCommand implements Command { throw new Exception("unable to set basalrate"); Log.v("SetTbrCommand","4. checking basal in "+scripter.currentMenu); + log.debug("4. checking basal in "+scripter.currentMenu); scripter.waitScreen(1000); currentPercentage= -1000; retries=10; @@ -143,6 +147,7 @@ public class SetTbrCommand implements Command { if(currentPercentage!=100) { Log.v("SetTbrCommand", "5. change to TBR_DURATION from " + scripter.currentMenu); + 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)) { retries--; @@ -158,6 +163,7 @@ public class SetTbrCommand implements Command { } Log.v("SetTbrCommand", "6. getting/setting duration in " + scripter.currentMenu); + log.debug("6. getting/setting duration in " + scripter.currentMenu); retries = 30; double currentDuration = -100; @@ -165,6 +171,7 @@ public class SetTbrCommand implements Command { retries--; Object durationObj = scripter.currentMenu.getAttribute(MenuAttribute.RUNTIME); Log.v("SetTbrCommand", "Requested time: " + duration + " actual time: " + durationObj); + log.debug("Requested time: " + duration + " actual time: " + durationObj); if (durationObj != null && durationObj instanceof MenuTime) { MenuTime time = (MenuTime) durationObj; currentDuration = (time.getHour() * 60) + time.getMinute(); @@ -177,6 +184,7 @@ public class SetTbrCommand implements Command { else if (currentDuration + (steps * 15) > requestedDuration) steps--; Log.v("SetTbrCommand", "Adjusting duration(" + requestedDuration + "/" + actualDuration + ") with " + steps + " steps and " + retries + " retries left"); + 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.waitScreen(1000); } @@ -187,6 +195,7 @@ public class SetTbrCommand implements Command { throw new Exception("unable to set duration"); Log.v("SetTbrCommand", "7. checking time in " + scripter.currentMenu); + log.debug("7. checking time in " + scripter.currentMenu); scripter.waitScreen(1000); currentDuration = -1000; retries = 10; @@ -206,6 +215,7 @@ public class SetTbrCommand implements Command { } Log.v("SetTbrCommand", "8. setting from " + scripter.currentMenu); + log.debug("8. setting from " + scripter.currentMenu); retries=5; while(retries>= 0 && (scripter.currentMenu.getType()==TBR_DURATION ||scripter.currentMenu.getType()==TBR_SET)) { @@ -260,6 +270,7 @@ public class SetTbrCommand implements Command { String.format(Locale.US, "TBR set to %d%% for %d min", percentage, duration)); } catch (Exception e) { Log.e("SetTbrCommand", "got exception: ", e); + log.error("got exception: ", e); return new CommandResult().success(false).message("failed to wait: " + e.getMessage()); } } From d89b914bc272c5c5dcf8a93d62d18891b0b7d368 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandra=20Ke=C3=9Fler?= Date: Sat, 12 Aug 2017 13:31:47 +0200 Subject: [PATCH 16/28] removed Log from SetTbrCommand --- .../ruffyscripter/commands/SetTbrCommand.java | 14 -------------- 1 file changed, 14 deletions(-) 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 d1b43384b8..f94a74aee7 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java @@ -1,7 +1,5 @@ package de.jotomo.ruffyscripter.commands; -import android.util.Log; - 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; @@ -62,7 +60,6 @@ public class SetTbrCommand implements Command { public CommandResult execute(RuffyScripter scripter, PumpState initialPumpState) { try { - Log.v("SetTbrCommand","1. going from "+scripter.currentMenu+" to TBR_MENU"); log.debug("1. going from "+scripter.currentMenu+" to TBR_MENU"); int retries = 5; while(!scripter.goToMainMenuScreen(TBR_MENU,3000)) @@ -78,7 +75,6 @@ public class SetTbrCommand implements Command { if(scripter.currentMenu.getType()!=TBR_MENU) throw new Exception("not able to find TBR_MENU: stuck in "+scripter.currentMenu); - Log.v("SetTbrCommand","2. entering "+scripter.currentMenu); log.debug("2. entering "+scripter.currentMenu); retries = 5; while(!scripter.enterMenu(TBR_MENU,MenuType.TBR_SET, RuffyScripter.Key.CHECK,2000)) @@ -96,7 +92,6 @@ public class SetTbrCommand implements Command { } } - Log.v("SetTbrCommand","3. getting/setting basal in "+scripter.currentMenu); log.debug("SetTbrCommand: 3. getting/setting basal in "+scripter.currentMenu); retries = 30; @@ -112,7 +107,6 @@ public class SetTbrCommand implements Command { int requestedPercentage = (int) percentage; int actualPercentage = (int) currentPercentage; int steps = (requestedPercentage - actualPercentage) / 10; - Log.v("SetTbrCommand", "Adjusting basal(" + requestedPercentage + "/" + actualPercentage + ") with " + steps + " steps and " + retries + " retries left"); 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.waitScreen(1000); @@ -126,7 +120,6 @@ public class SetTbrCommand implements Command { if(currentPercentage<0 ||retries < 0) throw new Exception("unable to set basalrate"); - Log.v("SetTbrCommand","4. checking basal in "+scripter.currentMenu); log.debug("4. checking basal in "+scripter.currentMenu); scripter.waitScreen(1000); currentPercentage= -1000; @@ -146,7 +139,6 @@ public class SetTbrCommand implements Command { throw new Exception("wrong rate!"); if(currentPercentage!=100) { - Log.v("SetTbrCommand", "5. change to TBR_DURATION from " + scripter.currentMenu); 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)) { @@ -162,7 +154,6 @@ public class SetTbrCommand implements Command { } } - Log.v("SetTbrCommand", "6. getting/setting duration in " + scripter.currentMenu); log.debug("6. getting/setting duration in " + scripter.currentMenu); retries = 30; @@ -170,7 +161,6 @@ public class SetTbrCommand implements Command { while (currentDuration != duration && retries >= 0) { retries--; Object durationObj = scripter.currentMenu.getAttribute(MenuAttribute.RUNTIME); - Log.v("SetTbrCommand", "Requested time: " + duration + " actual time: " + durationObj); log.debug("Requested time: " + duration + " actual time: " + durationObj); if (durationObj != null && durationObj instanceof MenuTime) { MenuTime time = (MenuTime) durationObj; @@ -183,7 +173,6 @@ public class SetTbrCommand implements Command { steps++; else if (currentDuration + (steps * 15) > requestedDuration) steps--; - Log.v("SetTbrCommand", "Adjusting duration(" + requestedDuration + "/" + actualDuration + ") with " + steps + " steps and " + retries + " retries left"); 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.waitScreen(1000); @@ -194,7 +183,6 @@ public class SetTbrCommand implements Command { if (currentDuration < 0 || retries < 0) throw new Exception("unable to set duration"); - Log.v("SetTbrCommand", "7. checking time in " + scripter.currentMenu); log.debug("7. checking time in " + scripter.currentMenu); scripter.waitScreen(1000); currentDuration = -1000; @@ -214,7 +202,6 @@ public class SetTbrCommand implements Command { throw new Exception("wrong time!"); } - Log.v("SetTbrCommand", "8. setting from " + scripter.currentMenu); log.debug("8. setting from " + scripter.currentMenu); retries=5; while(retries>= 0 && (scripter.currentMenu.getType()==TBR_DURATION ||scripter.currentMenu.getType()==TBR_SET)) @@ -269,7 +256,6 @@ public class SetTbrCommand implements Command { 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.e("SetTbrCommand", "got exception: ", e); log.error("got exception: ", e); return new CommandResult().success(false).message("failed to wait: " + e.getMessage()); } From 389eae93c583d687798e521c3b364cbd3f5269e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandra=20Ke=C3=9Fler?= Date: Sat, 12 Aug 2017 14:15:04 +0200 Subject: [PATCH 17/28] renamed goToMainMenuScreen to gotToMainTypeMenu to make it clevrer that Menu.isMaintype is meant, deactivates getBasal for now --- .../jotomo/ruffyscripter/RuffyScripter.java | 2 +- .../commands/GetBasalCommand.java | 233 +++++++----------- .../ruffyscripter/commands/SetTbrCommand.java | 2 +- 3 files changed, 85 insertions(+), 152 deletions(-) diff --git a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java index 849b1a92ed..7a11e9fbb0 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java @@ -452,7 +452,7 @@ public class RuffyScripter { return true; } - public boolean goToMainMenuScreen(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()) diff --git a/app/src/main/java/de/jotomo/ruffyscripter/commands/GetBasalCommand.java b/app/src/main/java/de/jotomo/ruffyscripter/commands/GetBasalCommand.java index 8305e72031..196edeb734 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/GetBasalCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/GetBasalCommand.java @@ -2,30 +2,17 @@ package de.jotomo.ruffyscripter.commands; import android.util.Log; -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.HashMap; import java.util.List; -import java.util.Locale; import java.util.Map; -import java.util.Objects; import de.jotomo.ruffyscripter.PumpState; import de.jotomo.ruffyscripter.RuffyScripter; -import static de.jotomo.ruffyscripter.commands.GetBasalCommand.State.BEFORE; -import static de.jotomo.ruffyscripter.commands.GetBasalCommand.State.MAIN; -import static de.jotomo.ruffyscripter.commands.GetBasalCommand.State.BASAL_1_MENU; -import static de.jotomo.ruffyscripter.commands.GetBasalCommand.State.BASAL_OVERVIEW; -import static de.jotomo.ruffyscripter.commands.GetBasalCommand.State.READ_BASAL; -import static de.jotomo.ruffyscripter.commands.GetBasalCommand.State.ERROR; -import static de.jotomo.ruffyscripter.commands.GetBasalCommand.State.AFTER; - public class GetBasalCommand implements Command { private static final Logger log = LoggerFactory.getLogger(GetBasalCommand.class); @@ -40,148 +27,94 @@ public class GetBasalCommand implements Command { return violations; } - enum State { - BEFORE, - MAIN, - BASAL_1_MENU, - BASAL_OVERVIEW, - READ_BASAL, - ERROR, - AFTER, - }; - private State lastState,state; - private long last; - private long timeout; - private Thread timeoutThread = new Thread() - { - @Override - public void run() { - while(state != ERROR && state!=AFTER) { - if (timeout + last < System.currentTimeMillis()) { - lastState = state; - state = ERROR; - log.debug("timeout reached -> state:ERROR"); - } - tick(); - try { - Thread.sleep(100); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - tick(); - } - }; - private void updateState(State newState,long timeoutSec) - { - lastState = state; - state = newState; - last = System.currentTimeMillis(); - timeout = timeoutSec*1000; - } - private MenuType lastMenu; - private int retries = 0; - private double basalTotal = 0; - private Map rate = new HashMap<>(); - - private void tick() - { - switch (state) - { - case BEFORE: - if(scripter.currentMenu!=null && scripter.currentMenu.getType()==MenuType.MAIN_MENU) - { - updateState(MAIN,120); - lastMenu = MenuType.MAIN_MENU; - log.debug("found MAIN_MENU -> state:MAIN"); - retries=3; - } - break; - case MAIN: - if(retries>0) - if(scripter.goToMainMenuScreen(MenuType.BASAL_1_MENU,30000)) - { - if(scripter.enterMenu(MenuType.BASAL_1_MENU,MenuType.BASAL_TOTAL, RuffyScripter.Key.CHECK,2000)) - { - updateState(BASAL_OVERVIEW, 30); - retries=0; - } - } - else - retries--; - else - updateState(ERROR,30); - break; - case BASAL_OVERVIEW: - if(scripter.currentMenu.getType()==MenuType.BASAL_TOTAL && scripter.currentMenu.getAttribute(MenuAttribute.BASAL_TOTAL)!=null && (Integer)scripter.currentMenu.getAttribute(MenuAttribute.BASAL_SELECTED)==1) - { - basalTotal = (Double)scripter.currentMenu.getAttribute(MenuAttribute.BASAL_TOTAL); - if(scripter.enterMenu(MenuType.BASAL_TOTAL,MenuType.BASAL_SET, RuffyScripter.Key.MENU,3000)) - { - updateState(READ_BASAL,30); - retries = 96; - } - } - break; - case READ_BASAL: - if(scripter.currentMenu.getType()==MenuType.BASAL_SET && scripter.currentMenu.getAttribute(MenuAttribute.BASAL_START)!=null) { - Object rateObj = scripter.currentMenu.getAttribute(MenuAttribute.BASAL_RATE); - MenuTime time = (MenuTime) scripter.currentMenu.getAttribute(MenuAttribute.BASAL_START); - if (rateObj instanceof Double) { - rate.put(time.getHour(), (Double) rateObj); - } - boolean complete = true; - for (int t = 0; t < 24; t++) { - if (rate.get(t) == null) - complete = false; - } - if (retries > 0) { - if (complete) { - scripter.pressBackKey(); - updateState(AFTER, 30); - } else { - retries--; - scripter.pressMenuKey(); - scripter.waitScreen(250); - } - } else { - updateState(ERROR, 30); - } - } - break; - case ERROR: - case AFTER: - scripter.goToMainMenuScreen(MenuType.MAIN_MENU,2000); - synchronized(GetBasalCommand.this) { - GetBasalCommand.this.notify(); - } - break; - } - } +// private void tick() +// { +// switch (state) +// { +// case BEFORE: +// if(scripter.currentMenu!=null && scripter.currentMenu.getType()==MenuType.MAIN_MENU) +// { +// updateState(MAIN,120); +// lastMenu = MenuType.MAIN_MENU; +// log.debug("found MAIN_MENU -> state:MAIN"); +// retries=3; +// } +// break; +// case MAIN: +// if(retries>0) +// if(scripter.goToMainTypeScreen(MenuType.BASAL_1_MENU,30000)) +// { +// if(scripter.enterMenu(MenuType.BASAL_1_MENU,MenuType.BASAL_TOTAL, RuffyScripter.Key.CHECK,2000)) +// { +// updateState(BASAL_OVERVIEW, 30); +// retries=0; +// } +// } +// else +// retries--; +// else +// updateState(ERROR,30); +// break; +// case BASAL_OVERVIEW: +// if(scripter.currentMenu.getType()==MenuType.BASAL_TOTAL && scripter.currentMenu.getAttribute(MenuAttribute.BASAL_TOTAL)!=null && (Integer)scripter.currentMenu.getAttribute(MenuAttribute.BASAL_SELECTED)==1) +// { +// basalTotal = (Double)scripter.currentMenu.getAttribute(MenuAttribute.BASAL_TOTAL); +// if(scripter.enterMenu(MenuType.BASAL_TOTAL,MenuType.BASAL_SET, RuffyScripter.Key.MENU,3000)) +// { +// updateState(READ_BASAL,30); +// retries = 96; +// } +// } +// break; +// case READ_BASAL: +// if(scripter.currentMenu.getType()==MenuType.BASAL_SET && scripter.currentMenu.getAttribute(MenuAttribute.BASAL_START)!=null) { +// Object rateObj = scripter.currentMenu.getAttribute(MenuAttribute.BASAL_RATE); +// MenuTime time = (MenuTime) scripter.currentMenu.getAttribute(MenuAttribute.BASAL_START); +// if (rateObj instanceof Double) { +// rate.put(time.getHour(), (Double) rateObj); +// } +// boolean complete = true; +// for (int t = 0; t < 24; t++) { +// if (rate.get(t) == null) +// complete = false; +// } +// if (retries > 0) { +// if (complete) { +// scripter.pressBackKey(); +// updateState(AFTER, 30); +// } else { +// retries--; +// scripter.pressMenuKey(); +// scripter.waitScreen(250); +// } +// } else { +// updateState(ERROR, 30); +// } +// } +// break; +// case ERROR: +// case AFTER: +// scripter.goToMainMenuScreen(MenuType.MAIN_MENU,2000); +// synchronized(GetBasalCommand.this) { +// GetBasalCommand.this.notify(); +// } +// break; +// } +// } @Override public CommandResult execute(RuffyScripter scripter, PumpState initialPumpState) { - state = BEFORE; - this.scripter = scripter; - updateState(BEFORE,120); - timeoutThread.start(); + try { + Map rate = new HashMap<>(); - try { - synchronized (this) { - this.wait(); - } - } catch (InterruptedException e) { - e.printStackTrace(); - return new CommandResult().success(false).message("failed to wait: "+e.getMessage()); - } - if(state==AFTER) + for(int i = 0; i < 24;i++) { - for(int i = 0; i < 24;i++) - { - Log.v("BASAL_RATE","BASAL_RATE from "+String.format("%02d",i)+":00 = "+rate.get(i)); - } - return new CommandResult().success(true).enacted(true).message("Basal Rate was read"); + Log.v("BASAL_RATE","BASAL_RATE from "+String.format("%02d",i)+":00 = "+rate.get(i)); } - return new CommandResult().success(false).message("failed with state: "+state+" from: "+lastState); + } catch (Exception e) { + log.error("failed to get basal",e); + return new CommandResult().success(false).message("failed to get basal: "+e.getMessage()); + } + return new CommandResult().success(true).enacted(true).message("Basal Rate was read"); } @Override 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 f94a74aee7..c5aa0d184e 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java @@ -62,7 +62,7 @@ public class SetTbrCommand implements Command { try { log.debug("1. going from "+scripter.currentMenu+" to TBR_MENU"); int retries = 5; - while(!scripter.goToMainMenuScreen(TBR_MENU,3000)) + while(!scripter.goToMainTypeScreen(TBR_MENU,3000)) { retries--; if(retries==0) From bdbf151efea6e4bd0cd776b90409c861e714ade8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandra=20Ke=C3=9Fler?= Date: Sat, 12 Aug 2017 15:10:14 +0200 Subject: [PATCH 18/28] add full Menu Output to debug --- app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java index 7a11e9fbb0..6fc1a9d470 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java @@ -177,7 +177,7 @@ public class RuffyScripter { @Override public void rtDisplayHandleMenu(Menu menu, int sequence) throws RemoteException { // method is called every ~500ms - log.debug("rtDisplayHandleMenu: " + menu.getType()); + log.debug("rtDisplayHandleMenu: " + menu); currentMenu = menu; menuLastUpdated = System.currentTimeMillis(); From 306a2787d0cdb210a4b75903c16b7cfd9ce15d51 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sat, 12 Aug 2017 13:26:05 +0200 Subject: [PATCH 19/28] Throw CommandException instead of Exception. --- .../ruffyscripter/commands/SetTbrCommand.java | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) 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 c5aa0d184e..b50b8c85f7 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java @@ -66,14 +66,14 @@ public class SetTbrCommand implements Command { { retries--; if(retries==0) - throw new Exception("not able to find TBR_MENU: stuck in "+scripter.currentMenu); + throw new CommandException().message("not able to find TBR_MENU: stuck in "+scripter.currentMenu); Thread.sleep(500); if(scripter.currentMenu.getType()== TBR_MENU) break; } if(scripter.currentMenu.getType()!=TBR_MENU) - throw new Exception("not able to find TBR_MENU: stuck in "+scripter.currentMenu); + throw new CommandException().message("not able to find TBR_MENU: stuck in "+scripter.currentMenu); log.debug("2. entering "+scripter.currentMenu); retries = 5; @@ -81,7 +81,7 @@ public class SetTbrCommand implements Command { { retries--; if(retries==0) - throw new Exception("not able to find TBR_SET: stuck in "+scripter.currentMenu); + throw new CommandException().message("not able to find TBR_SET: stuck in "+scripter.currentMenu); Thread.sleep(500); if(scripter.currentMenu.getType()== TBR_SET) break; @@ -118,7 +118,7 @@ public class SetTbrCommand implements Command { scripter.waitScreen(1000); } if(currentPercentage<0 ||retries < 0) - throw new Exception("unable to set basalrate"); + throw new CommandException().message("unable to set basalrate"); log.debug("4. checking basal in "+scripter.currentMenu); scripter.waitScreen(1000); @@ -136,7 +136,7 @@ public class SetTbrCommand implements Command { } if(retries<0 ||currentPercentage!=percentage) - throw new Exception("wrong rate!"); + throw new CommandException().message("Unable to set percentage. Desired: " + percentage + ", value displayed on pump: " + currentPercentage); if(currentPercentage!=100) { log.debug("5. change to TBR_DURATION from " + scripter.currentMenu); @@ -144,7 +144,7 @@ public class SetTbrCommand implements Command { while (retries >=0 && !scripter.enterMenu(TBR_SET, MenuType.TBR_DURATION, RuffyScripter.Key.MENU, 2000)) { retries--; if (retries == 0) - throw new Exception("not able to find TBR_SET: stuck in " + scripter.currentMenu); + throw new CommandException().message("not able to find TBR_SET: stuck in " + scripter.currentMenu); Thread.sleep(500); if (scripter.currentMenu.getType() == TBR_DURATION) break; @@ -181,7 +181,7 @@ public class SetTbrCommand implements Command { scripter.waitScreen(1000); } if (currentDuration < 0 || retries < 0) - throw new Exception("unable to set duration"); + throw new CommandException().message("unable to set duration"); log.debug("7. checking time in " + scripter.currentMenu); scripter.waitScreen(1000); @@ -199,7 +199,7 @@ public class SetTbrCommand implements Command { scripter.waitScreen(1000); } if (retries < 0 || currentDuration != duration) - throw new Exception("wrong time!"); + throw new CommandException().message("wrong time!"); } log.debug("8. setting from " + scripter.currentMenu); @@ -211,7 +211,7 @@ public class SetTbrCommand implements Command { scripter.waitScreen(1000); } if(retries<0 || scripter.currentMenu.getType()==TBR_DURATION ||scripter.currentMenu.getType()==TBR_SET) - throw new Exception("failed setting basal!"); + throw new CommandException().message("failed setting basal!"); retries=10; boolean canceledError = true; if(percentage==100) @@ -232,26 +232,26 @@ public class SetTbrCommand implements Command { } } if(retries<0 || scripter.currentMenu.getType()!=MAIN_MENU ) - throw new Exception("failed going to main!"); + throw new CommandException().message("failed going to main!"); Object percentageObj = scripter.currentMenu.getAttribute(MenuAttribute.TBR); Object durationObj = scripter.currentMenu.getAttribute(MenuAttribute.RUNTIME); if(percentageObj == null || !(percentageObj instanceof Double)) - throw new Exception("not percentage"); + throw new CommandException().message("not percentage"); if(((double)percentageObj)!=percentage) - throw new Exception("wrong percentage set!"); + throw new CommandException().message("wrong percentage set!"); if(percentage==100) return new CommandResult().success(true).enacted(true).message("TBR was cancelled"); if(durationObj==null || !(durationObj instanceof MenuTime)) - throw new Exception("not time"); + throw new CommandException().message("not time"); MenuTime t = (MenuTime) durationObj; if(t.getMinute()+(60*t.getHour())> duration || t.getMinute()+(60*t.getHour())< duration-5) - throw new Exception("wrong time set!"); + throw new CommandException().message("wrong time set!"); return new CommandResult().success(true).enacted(true).message( String.format(Locale.US, "TBR set to %d%% for %d min", percentage, duration)); From 2f3d362240bb1edebb0183b206147d551b9b8133 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sat, 12 Aug 2017 13:36:09 +0200 Subject: [PATCH 20/28] Use SystemClock.sleep() instead of Thread.sleep() throughout. --- .../de/jotomo/ruffyscripter/commands/SetTbrCommand.java | 8 +++++--- .../androidaps/plugins/PumpCombo/ComboPlugin.java | 5 +---- 2 files changed, 6 insertions(+), 7 deletions(-) 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 b50b8c85f7..f737abbfbd 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java @@ -1,5 +1,7 @@ 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; @@ -67,7 +69,7 @@ public class SetTbrCommand implements Command { retries--; if(retries==0) throw new CommandException().message("not able to find TBR_MENU: stuck in "+scripter.currentMenu); - Thread.sleep(500); + SystemClock.sleep(500); if(scripter.currentMenu.getType()== TBR_MENU) break; } @@ -82,7 +84,7 @@ public class SetTbrCommand implements Command { retries--; if(retries==0) throw new CommandException().message("not able to find TBR_SET: stuck in "+scripter.currentMenu); - Thread.sleep(500); + SystemClock.sleep(500); if(scripter.currentMenu.getType()== TBR_SET) break; if(scripter.currentMenu.getType()== TBR_DURATION) @@ -145,7 +147,7 @@ public class SetTbrCommand implements Command { retries--; if (retries == 0) throw new CommandException().message("not able to find TBR_SET: stuck in " + scripter.currentMenu); - Thread.sleep(500); + SystemClock.sleep(500); if (scripter.currentMenu.getType() == TBR_DURATION) break; if (scripter.currentMenu.getType() == TBR_SET) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java index b276c23594..696c87ba2d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java @@ -209,10 +209,7 @@ public class ComboPlugin implements PluginBase, PumpInterface { ruffyScripter.stop(); log.debug("ruffy service disconnected"); if(!keepUnbound) { - try { - Thread.sleep(250); - } catch (Exception e) { - } + SystemClock.sleep(250); bindRuffyService(); } } From d3058f60636d4b7e6e7f6da596a97893081260d9 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sat, 12 Aug 2017 14:01:57 +0200 Subject: [PATCH 21/28] Let an unexpected warning/error escalate rather than hide it. --- .../main/java/de/jotomo/ruffyscripter/RuffyScripter.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java index 6fc1a9d470..3c12acf697 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java @@ -459,8 +459,12 @@ public class RuffyScripter { { 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 - pressCheckKey(); + // yup, commenting this out since I don't want an occlusionn alert to hidden by this :-) + //pressCheckKey(); } else if(currentMenu!=null && !currentMenu.getType().isMaintype()) { From f763eb246b894941834f5bf2bf0c9708468c50d2 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sat, 12 Aug 2017 14:14:22 +0200 Subject: [PATCH 22/28] Use 1.5g-combo-dev as version. --- app/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index c8e5636b6b..dd040d8e39 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -44,7 +44,7 @@ android { minSdkVersion 21 targetSdkVersion 23 versionCode 1500 - version "1.5g-combo-sandratest" + version "1.5g-combo-dev" buildConfigField "String", "VERSION", '"' + version + '"' buildConfigField "String", "BUILDVERSION", generateGitBuild() } From fc5b4cd93b1bb3f511b5fedca1ffe93bedf437b7 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sat, 12 Aug 2017 14:14:48 +0200 Subject: [PATCH 23/28] Some logging. --- .../java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 f737abbfbd..7d6a9a43f4 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java @@ -183,7 +183,7 @@ public class SetTbrCommand implements Command { scripter.waitScreen(1000); } if (currentDuration < 0 || retries < 0) - throw new CommandException().message("unable to set duration"); + throw new CommandException().message("unable to set duration, requested:" + duration + ", displayed on pump: " + currentDuration); log.debug("7. checking time in " + scripter.currentMenu); scripter.waitScreen(1000); @@ -233,6 +233,8 @@ public class SetTbrCommand implements Command { break; } } + + log.debug("9. verifying the main menu display the TBR we just set/cancelled"); if(retries<0 || scripter.currentMenu.getType()!=MAIN_MENU ) throw new CommandException().message("failed going to main!"); From 5a1d2d495d2299242ff202bd61ec2437e5bcd619 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sat, 12 Aug 2017 14:15:23 +0200 Subject: [PATCH 24/28] Rename RuffyScripter.waitScreen -> waitForScreenUpdate. --- .../jotomo/ruffyscripter/RuffyScripter.java | 6 ++--- .../commands/GetBasalCommand.java | 2 +- .../ruffyscripter/commands/SetTbrCommand.java | 24 +++++++++---------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java index 3c12acf697..b89cf3c9c1 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java @@ -440,7 +440,7 @@ public class RuffyScripter { log.debug("Releasing back key"); } - public boolean waitScreen(long timeout) + public boolean waitForScreenUpdate(long timeout) { synchronized (screenlock) { try { @@ -472,7 +472,7 @@ public class RuffyScripter { } else pressMenuKey(); - waitScreen(250); + waitForScreenUpdate(250); } return currentMenu != null && currentMenu.getType()==screen; } @@ -486,7 +486,7 @@ public class RuffyScripter { long start = System.currentTimeMillis(); pressKey(key,2000); while((currentMenu == null || currentMenu.getType()!=targetType) && start+timeout>System.currentTimeMillis()) { - waitScreen(100); + waitForScreenUpdate(100); } return currentMenu!=null && currentMenu.getType()==targetType; } diff --git a/app/src/main/java/de/jotomo/ruffyscripter/commands/GetBasalCommand.java b/app/src/main/java/de/jotomo/ruffyscripter/commands/GetBasalCommand.java index 196edeb734..09958f6bf1 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/GetBasalCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/GetBasalCommand.java @@ -85,7 +85,7 @@ public class GetBasalCommand implements Command { // } else { // retries--; // scripter.pressMenuKey(); -// scripter.waitScreen(250); +// scripter.waitForScreenUpdate(250); // } // } else { // updateState(ERROR, 30); 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 7d6a9a43f4..ec96b8e03d 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java @@ -90,7 +90,7 @@ public class SetTbrCommand implements Command { if(scripter.currentMenu.getType()== TBR_DURATION) { scripter.pressMenuKey(); - scripter.waitScreen(1000); + scripter.waitForScreenUpdate(1000); } } @@ -111,19 +111,19 @@ public class SetTbrCommand implements Command { 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.waitScreen(1000); + scripter.waitForScreenUpdate(1000); } } else currentPercentage=-100; - scripter.waitScreen(1000); + scripter.waitForScreenUpdate(1000); } if(currentPercentage<0 ||retries < 0) throw new CommandException().message("unable to set basalrate"); log.debug("4. checking basal in "+scripter.currentMenu); - scripter.waitScreen(1000); + scripter.waitForScreenUpdate(1000); currentPercentage= -1000; retries=10; while(currentPercentage<0 && retries>=0) { @@ -134,7 +134,7 @@ public class SetTbrCommand implements Command { currentPercentage = ((Double) percentageObj).doubleValue(); } else - scripter.waitScreen(1000); + scripter.waitForScreenUpdate(1000); } if(retries<0 ||currentPercentage!=percentage) @@ -152,7 +152,7 @@ public class SetTbrCommand implements Command { break; if (scripter.currentMenu.getType() == TBR_SET) { scripter.pressMenuKey(); - scripter.waitScreen(1000); + scripter.waitForScreenUpdate(1000); } } @@ -177,16 +177,16 @@ public class SetTbrCommand implements Command { 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.waitScreen(1000); + scripter.waitForScreenUpdate(1000); } } - scripter.waitScreen(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 time in " + scripter.currentMenu); - scripter.waitScreen(1000); + scripter.waitForScreenUpdate(1000); currentDuration = -1000; retries = 10; while (currentDuration < 0 && retries >= 0) { @@ -198,7 +198,7 @@ public class SetTbrCommand implements Command { currentDuration = (time.getHour() * 60) + time.getMinute(); } else - scripter.waitScreen(1000); + scripter.waitForScreenUpdate(1000); } if (retries < 0 || currentDuration != duration) throw new CommandException().message("wrong time!"); @@ -210,7 +210,7 @@ public class SetTbrCommand implements Command { { retries--; scripter.pressCheckKey(); - scripter.waitScreen(1000); + scripter.waitForScreenUpdate(1000); } if(retries<0 || scripter.currentMenu.getType()==TBR_DURATION ||scripter.currentMenu.getType()==TBR_SET) throw new CommandException().message("failed setting basal!"); @@ -225,7 +225,7 @@ public class SetTbrCommand implements Command { scripter.pressCheckKey(); retries++; canceledError = true; - scripter.waitScreen(1000); + scripter.waitForScreenUpdate(1000); } else { retries--; From 49aa63bd315a0746c3e1d9a66ae59940c976946f Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sat, 12 Aug 2017 15:06:30 +0200 Subject: [PATCH 25/28] More verbose logging, some notes added. --- .../ruffyscripter/commands/SetTbrCommand.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) 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 ec96b8e03d..a42ec84285 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java @@ -94,7 +94,7 @@ public class SetTbrCommand implements Command { } } - log.debug("SetTbrCommand: 3. getting/setting basal in "+scripter.currentMenu); + log.debug("SetTbrCommand: 3. getting/setting basal percentage in "+scripter.currentMenu); retries = 30; double currentPercentage = -100; @@ -120,9 +120,9 @@ public class SetTbrCommand implements Command { scripter.waitForScreenUpdate(1000); } if(currentPercentage<0 ||retries < 0) - throw new CommandException().message("unable to set basalrate"); + throw new CommandException().message("unable to set basal percentage"); - log.debug("4. checking basal in "+scripter.currentMenu); + log.debug("4. checking basal percentage in "+scripter.currentMenu); scripter.waitForScreenUpdate(1000); currentPercentage= -1000; retries=10; @@ -138,7 +138,7 @@ public class SetTbrCommand implements Command { } if(retries<0 ||currentPercentage!=percentage) - throw new CommandException().message("Unable to set percentage. Desired: " + percentage + ", value displayed on pump: " + currentPercentage); + 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); @@ -185,7 +185,7 @@ public class SetTbrCommand implements Command { if (currentDuration < 0 || retries < 0) throw new CommandException().message("unable to set duration, requested:" + duration + ", displayed on pump: " + currentDuration); - log.debug("7. checking time in " + scripter.currentMenu); + log.debug("7. checking duration in " + scripter.currentMenu); scripter.waitForScreenUpdate(1000); currentDuration = -1000; retries = 10; @@ -201,10 +201,10 @@ public class SetTbrCommand implements Command { scripter.waitForScreenUpdate(1000); } if (retries < 0 || currentDuration != duration) - throw new CommandException().message("wrong time!"); + throw new CommandException().message("wrong duration! Requested: " + duration + ", displayed on pump: " + currentDuration); } - log.debug("8. setting from " + scripter.currentMenu); + log.debug("8. confirming TBR om " + scripter.currentMenu); retries=5; while(retries>= 0 && (scripter.currentMenu.getType()==TBR_DURATION ||scripter.currentMenu.getType()==TBR_SET)) { @@ -220,6 +220,8 @@ public class SetTbrCommand implements Command { canceledError=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) { scripter.pressCheckKey(); From d75a04c985f215bd7cc100e975021b16d94e8eff Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sat, 12 Aug 2017 15:11:19 +0200 Subject: [PATCH 26/28] After setting/cancelling TBR also check main menu state after cancel. --- .../jotomo/ruffyscripter/commands/SetTbrCommand.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) 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 a42ec84285..20a68ad026 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java @@ -243,15 +243,19 @@ public class SetTbrCommand implements Command { Object percentageObj = scripter.currentMenu.getAttribute(MenuAttribute.TBR); Object durationObj = scripter.currentMenu.getAttribute(MenuAttribute.RUNTIME); + 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)) throw new CommandException().message("not percentage"); if(((double)percentageObj)!=percentage) throw new CommandException().message("wrong percentage set!"); - if(percentage==100) - return new CommandResult().success(true).enacted(true).message("TBR was cancelled"); - if(durationObj==null || !(durationObj instanceof MenuTime)) throw new CommandException().message("not time"); @@ -259,6 +263,7 @@ public class SetTbrCommand implements Command { if(t.getMinute()+(60*t.getHour())> duration || t.getMinute()+(60*t.getHour())< duration-5) throw new CommandException().message("wrong time set!"); + return new CommandResult().success(true).enacted(true).message( String.format(Locale.US, "TBR set to %d%% for %d min", percentage, duration)); } catch (Exception e) { From ead85087606ccf87f1b4c104bf60f67754d6f986 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sat, 12 Aug 2017 16:00:05 +0200 Subject: [PATCH 27/28] Format code. --- .../jotomo/ruffyscripter/RuffyScripter.java | 115 +++++++++--------- .../ruffyscripter/commands/SetTbrCommand.java | 106 ++++++++-------- 2 files changed, 107 insertions(+), 114 deletions(-) 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!"); From bb0bea98c379e3667af62a02ba8150976275a216 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sat, 12 Aug 2017 16:01:05 +0200 Subject: [PATCH 28/28] Don't ignore exceptions when binding the ruffy service. --- .../main/java/de/jotomo/ruffyscripter/RuffyScripter.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java index b7729d457e..f329a71df3 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java @@ -74,11 +74,7 @@ public class RuffyScripter { idleDisconnectMonitorThread.start(); } started = true; - try { - newService.addHandler(mHandler); - } catch (Exception e) { - // ignore - } + newService.addHandler(mHandler); } } catch (Exception e) { throw new RuntimeException(e);