Format code.

This commit is contained in:
Johannes Mockenhaupt 2017-08-12 16:00:05 +02:00
parent d75a04c985
commit ead8508760
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1
2 changed files with 107 additions and 114 deletions

View file

@ -60,16 +60,25 @@ public class RuffyScripter {
public void start(IRuffyService newService) { public void start(IRuffyService newService) {
try { try {
if(ruffyService!=null) if (ruffyService != null) {
try{ruffyService.removeHandler(mHandler);}catch(Exception e){}; try {
if(newService!=null) { ruffyService.removeHandler(mHandler);
} catch (Exception e) {
// ignore
}
}
if (newService != null) {
this.ruffyService = newService; this.ruffyService = newService;
// TODO this'll be done better in v2 via ConnectionManager // TODO this'll be done better in v2 via ConnectionManager
if (idleDisconnectMonitorThread.getState() == Thread.State.NEW) { if (idleDisconnectMonitorThread.getState() == Thread.State.NEW) {
idleDisconnectMonitorThread.start(); idleDisconnectMonitorThread.start();
} }
started = true; started = true;
try{newService.addHandler(mHandler);}catch (Exception e){} try {
newService.addHandler(mHandler);
} catch (Exception e) {
// ignore
}
} }
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@ -78,7 +87,7 @@ public class RuffyScripter {
public void stop() { public void stop() {
if (started) { if (started) {
started=false; started = false;
// TODO ruffy removes dead handlers automatically by now. // TODO ruffy removes dead handlers automatically by now.
// still, check this when going through recovery logic // still, check this when going through recovery logic
/* try { /* try {
@ -107,16 +116,14 @@ public class RuffyScripter {
&& now > lastDisconnect + 15 * 1000) { && now > lastDisconnect + 15 * 1000) {
log.debug("Disconnecting after " + (connectionTimeOutMs / 1000) + "s inactivity timeout"); log.debug("Disconnecting after " + (connectionTimeOutMs / 1000) + "s inactivity timeout");
lastDisconnect = now; lastDisconnect = now;
canDisconnect=true; canDisconnect = true;
ruffyService.doRTDisconnect(mHandler); ruffyService.doRTDisconnect(mHandler);
connected = false; connected = false;
lastDisconnect = System.currentTimeMillis(); lastDisconnect = System.currentTimeMillis();
// don't attempt anything fancy in the next 10s, let the pump settle // don't attempt anything fancy in the next 10s, let the pump settle
SystemClock.sleep(10 * 1000); SystemClock.sleep(10 * 1000);
} } else {
else canDisconnect = false;
{
canDisconnect=false;
} }
} catch (Exception e) { } catch (Exception e) {
// TODO do we need to catch this exception somewhere else too? right now it's // TODO do we need to catch this exception somewhere else too? right now it's
@ -182,11 +189,12 @@ public class RuffyScripter {
currentMenu = menu; currentMenu = menu;
menuLastUpdated = System.currentTimeMillis(); menuLastUpdated = System.currentTimeMillis();
synchronized (screenlock) synchronized (screenlock) {
{
screenlock.notifyAll(); screenlock.notifyAll();
} }
// TODO v2 switch to using IRuffyService.isConnected, rather than guessing connectivity state
// passed on screen updates
connected = true; connected = true;
// note that a WARNING_OR_ERROR menu can be a valid temporary state (cancelling TBR) // note that a WARNING_OR_ERROR menu can be a valid temporary state (cancelling TBR)
@ -204,9 +212,8 @@ public class RuffyScripter {
@Override @Override
public void keySent(int sequence) throws RemoteException { public void keySent(int sequence) throws RemoteException {
synchronized (keylock) synchronized (keylock) {
{ if (keynotwait > 0)
if(keynotwait>0)
keynotwait--; keynotwait--;
else else
keylock.notify(); keylock.notify();
@ -225,8 +232,12 @@ public class RuffyScripter {
} }
public void unbind() { public void unbind() {
if(ruffyService!=null) if (ruffyService != null)
try{ruffyService.removeHandler(mHandler);}catch (Exception e){} try {
ruffyService.removeHandler(mHandler);
} catch (Exception e) {
// ignore
}
this.ruffyService = null; 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() { private void ensureConnected() {
try { try {
boolean menuUpdateRecentlyReceived = currentMenu != null && menuLastUpdated + 1000 > System.currentTimeMillis(); boolean menuUpdateRecentlyReceived = currentMenu != null && menuLastUpdated + 1000 > System.currentTimeMillis();
@ -377,7 +390,7 @@ public class RuffyScripter {
SystemClock.sleep(10 * 1000); SystemClock.sleep(10 * 1000);
} }
canDisconnect=false; canDisconnect = false;
boolean connectInitSuccessful = ruffyService.doRTConnect(mHandler) == 0; boolean connectInitSuccessful = ruffyService.doRTConnect(mHandler) == 0;
log.debug("Connect init successful: " + connectInitSuccessful); log.debug("Connect init successful: " + connectInitSuccessful);
log.debug("Waiting for first menu update to be sent"); log.debug("Waiting for first menu update to be sent");
@ -412,36 +425,35 @@ public class RuffyScripter {
public void pressUpKey() { public void pressUpKey() {
log.debug("Pressing up key"); log.debug("Pressing up key");
pressKey(Key.UP,2000); pressKey(Key.UP, 2000);
log.debug("Releasing up key"); log.debug("Releasing up key");
} }
public void pressDownKey() { public void pressDownKey() {
log.debug("Pressing down key"); log.debug("Pressing down key");
pressKey(Key.DOWN,2000); pressKey(Key.DOWN, 2000);
log.debug("Releasing down key"); log.debug("Releasing down key");
} }
public void pressCheckKey() { public void pressCheckKey() {
log.debug("Pressing check key"); log.debug("Pressing check key");
pressKey(Key.CHECK,2000); pressKey(Key.CHECK, 2000);
log.debug("Releasing check key"); log.debug("Releasing check key");
} }
public void pressMenuKey() { public void pressMenuKey() {
log.debug("Pressing menu key"); log.debug("Pressing menu key");
pressKey(Key.MENU,2000); pressKey(Key.MENU, 2000);
log.debug("Releasing menu key"); log.debug("Releasing menu key");
} }
public void pressBackKey() { public void pressBackKey() {
log.debug("Pressing back key"); log.debug("Pressing back key");
pressKey(Key.BACK,2000); pressKey(Key.BACK, 2000);
log.debug("Releasing back key"); log.debug("Releasing back key");
} }
public boolean waitForScreenUpdate(long timeout) public boolean waitForScreenUpdate(long timeout) {
{
synchronized (screenlock) { synchronized (screenlock) {
try { try {
screenlock.wait(timeout); screenlock.wait(timeout);
@ -452,54 +464,48 @@ public class RuffyScripter {
return true; return true;
} }
public boolean goToMainTypeScreen(MenuType screen, long timeout) public boolean goToMainTypeScreen(MenuType screen, long timeout) {
{
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
while((currentMenu == null || currentMenu.getType()!=screen) && start+timeout>System.currentTimeMillis()) while ((currentMenu == null || currentMenu.getType() != screen) && start + timeout > System.currentTimeMillis()) {
{ if (currentMenu != null && currentMenu.getType() == MenuType.WARNING_OR_ERROR) {
if(currentMenu!=null && currentMenu.getType()==MenuType.WARNING_OR_ERROR)
{
throw new CommandException().message("Warning/errors raised by pump, please check pump"); 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 // since warnings and errors can occur at any time, they should be dealt with in
// a more general way, see the handleMenuUpdate callback above // a more general way, see the handleMenuUpdate callback above
//FIXME bad thing to do :D //FIXME bad thing to do :D
// yup, commenting this out since I don't want an occlusionn alert to hidden by this :-) // yup, commenting this out since I don't want an occlusionn alert to hidden by this :-)
//pressCheckKey(); //pressCheckKey();
} } else if (currentMenu != null && !currentMenu.getType().isMaintype()) {
else if(currentMenu!=null && !currentMenu.getType().isMaintype())
{
pressBackKey(); pressBackKey();
} } else
else
pressMenuKey(); pressMenuKey();
waitForScreenUpdate(250); 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) public boolean enterMenu(MenuType startType, MenuType targetType, byte key, long timeout) {
{ if (currentMenu.getType() == targetType)
if(currentMenu.getType()==targetType)
return true; return true;
if(currentMenu==null || currentMenu.getType() != startType) if (currentMenu == null || currentMenu.getType() != startType)
return false; return false;
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
pressKey(key,2000); pressKey(key, 2000);
while((currentMenu == null || currentMenu.getType()!=targetType) && start+timeout>System.currentTimeMillis()) { while ((currentMenu == null || currentMenu.getType() != targetType) && start + timeout > System.currentTimeMillis()) {
waitForScreenUpdate(100); waitForScreenUpdate(100);
} }
return currentMenu!=null && currentMenu.getType()==targetType; return currentMenu != null && currentMenu.getType() == targetType;
} }
public void step(int steps, byte key, long timeout) { public void step(int steps, byte key, long timeout) {
for(int i = 0; i < Math.abs(steps);i++) for (int i = 0; i < Math.abs(steps); i++)
pressKey(key,timeout); pressKey(key, timeout);
} }
// TODO v2, rework these two methods: waitForMenuUpdate shoud only be used by commands // TODO v2, rework these two methods: waitForMenuUpdate shoud only be used by commands
// then anything longer than a few seconds is an error; // then anything longer than a few seconds is an error;
// only ensureConnected() uses the method with the timeout parameter; inline that code, // only ensureConnected() uses the method with the timeout parameter; inline that code,
// so we can use a custom timeout and give a better error message in case of failure // so we can use a custom timeout and give a better error message in case of failure
/** /**
* Wait until the menu update is in * Wait until the menu update is in
*/ */
@ -523,17 +529,12 @@ public class RuffyScripter {
ruffyService.rtSendKey(key, true); ruffyService.rtSendKey(key, true);
//SystemClock.sleep(200); //SystemClock.sleep(200);
ruffyService.rtSendKey(Key.NO_KEY, true); ruffyService.rtSendKey(Key.NO_KEY, true);
if(timeout > 0) if (timeout > 0) {
{ synchronized (keylock) {
synchronized (keylock)
{
keylock.wait(timeout); keylock.wait(timeout);
} }
} } else {
else synchronized (keylock) {
{
synchronized (keylock)
{
keynotwait++; keynotwait++;
} }
} }

View file

@ -58,47 +58,44 @@ public class SetTbrCommand implements Command {
return violations; return violations;
} }
@Override @Override
public CommandResult execute(RuffyScripter scripter, PumpState initialPumpState) { public CommandResult execute(RuffyScripter scripter, PumpState initialPumpState) {
try { try {
log.debug("1. going from "+scripter.currentMenu+" to TBR_MENU"); log.debug("1. going from " + scripter.currentMenu + " to TBR_MENU");
int retries = 5; int retries = 5;
while(!scripter.goToMainTypeScreen(TBR_MENU,3000)) while (!scripter.goToMainTypeScreen(TBR_MENU, 3000)) {
{
retries--; retries--;
if(retries==0) if (retries == 0)
throw new CommandException().message("not able to find TBR_MENU: stuck in "+scripter.currentMenu); throw new CommandException().message("not able to find TBR_MENU: stuck in " + scripter.currentMenu);
SystemClock.sleep(500); SystemClock.sleep(500);
if(scripter.currentMenu.getType()== TBR_MENU) if (scripter.currentMenu.getType() == TBR_MENU)
break; break;
} }
if(scripter.currentMenu.getType()!=TBR_MENU) if (scripter.currentMenu.getType() != TBR_MENU)
throw new CommandException().message("not able to find TBR_MENU: stuck in "+scripter.currentMenu); throw new CommandException().message("not able to find TBR_MENU: stuck in " + scripter.currentMenu);
log.debug("2. entering "+scripter.currentMenu); log.debug("2. entering " + scripter.currentMenu);
retries = 5; 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--; retries--;
if(retries==0) if (retries == 0)
throw new CommandException().message("not able to find TBR_SET: stuck in "+scripter.currentMenu); throw new CommandException().message("not able to find TBR_SET: stuck in " + scripter.currentMenu);
SystemClock.sleep(500); SystemClock.sleep(500);
if(scripter.currentMenu.getType()== TBR_SET) if (scripter.currentMenu.getType() == TBR_SET)
break; break;
if(scripter.currentMenu.getType()== TBR_DURATION) if (scripter.currentMenu.getType() == TBR_DURATION) {
{
scripter.pressMenuKey(); scripter.pressMenuKey();
scripter.waitForScreenUpdate(1000); 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; retries = 30;
double currentPercentage = -100; double currentPercentage = -100;
while(currentPercentage!=percentage && retries>=0) { while (currentPercentage != percentage && retries >= 0) {
retries--; retries--;
Object percentageObj = scripter.currentMenu.getAttribute(MenuAttribute.BASAL_RATE); Object percentageObj = scripter.currentMenu.getAttribute(MenuAttribute.BASAL_RATE);
@ -114,36 +111,36 @@ public class SetTbrCommand implements Command {
scripter.waitForScreenUpdate(1000); scripter.waitForScreenUpdate(1000);
} }
} else {
currentPercentage = -100;
} }
else
currentPercentage=-100;
scripter.waitForScreenUpdate(1000); scripter.waitForScreenUpdate(1000);
} }
if(currentPercentage<0 ||retries < 0) if (currentPercentage < 0 || retries < 0)
throw new CommandException().message("unable to set basal percentage"); 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); scripter.waitForScreenUpdate(1000);
currentPercentage= -1000; currentPercentage = -1000;
retries=10; retries = 10;
while(currentPercentage<0 && retries>=0) { while (currentPercentage < 0 && retries >= 0) {
retries--; retries--;
Object percentageObj = scripter.currentMenu.getAttribute(MenuAttribute.BASAL_RATE); Object percentageObj = scripter.currentMenu.getAttribute(MenuAttribute.BASAL_RATE);
if (percentageObj != null && (percentageObj instanceof Double)) { if (percentageObj != null && (percentageObj instanceof Double)) {
currentPercentage = ((Double) percentageObj).doubleValue(); currentPercentage = ((Double) percentageObj).doubleValue();
} } else {
else
scripter.waitForScreenUpdate(1000); 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); 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); log.debug("5. change to TBR_DURATION from " + scripter.currentMenu);
retries = 5; 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--; retries--;
if (retries == 0) if (retries == 0)
throw new CommandException().message("not able to find TBR_SET: stuck in " + scripter.currentMenu); throw new CommandException().message("not able to find TBR_SET: stuck in " + scripter.currentMenu);
@ -196,8 +193,7 @@ public class SetTbrCommand implements Command {
if (durationObj != null && durationObj instanceof MenuTime) { if (durationObj != null && durationObj instanceof MenuTime) {
MenuTime time = (MenuTime) durationObj; MenuTime time = (MenuTime) durationObj;
currentDuration = (time.getHour() * 60) + time.getMinute(); currentDuration = (time.getHour() * 60) + time.getMinute();
} } else
else
scripter.waitForScreenUpdate(1000); scripter.waitForScreenUpdate(1000);
} }
if (retries < 0 || currentDuration != duration) if (retries < 0 || currentDuration != duration)
@ -205,62 +201,58 @@ public class SetTbrCommand implements Command {
} }
log.debug("8. confirming TBR om " + scripter.currentMenu); log.debug("8. confirming TBR om " + scripter.currentMenu);
retries=5; retries = 5;
while(retries>= 0 && (scripter.currentMenu.getType()==TBR_DURATION ||scripter.currentMenu.getType()==TBR_SET)) while (retries >= 0 && (scripter.currentMenu.getType() == TBR_DURATION || scripter.currentMenu.getType() == TBR_SET)) {
{
retries--; retries--;
scripter.pressCheckKey(); scripter.pressCheckKey();
scripter.waitForScreenUpdate(1000); 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!"); throw new CommandException().message("failed setting basal!");
retries=10; retries = 10;
boolean canceledError = true; boolean cancelledError = true;
if(percentage==100) if (percentage == 100)
canceledError=false; cancelledError = false;
while(retries>=0 && scripter.currentMenu.getType()!=MAIN_MENU ) while (retries >= 0 && scripter.currentMenu.getType() != MAIN_MENU) {
{
// TODO how probable is it, that a totally unrelated error (like occlusion alert) // 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? // 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(); scripter.pressCheckKey();
retries++; retries++;
canceledError = true; cancelledError = true;
scripter.waitForScreenUpdate(1000); scripter.waitForScreenUpdate(1000);
} } else {
else {
retries--; retries--;
if (scripter.currentMenu.getType() == MAIN_MENU && canceledError) if (scripter.currentMenu.getType() == MAIN_MENU && cancelledError)
break; break;
} }
} }
log.debug("9. verifying the main menu display the TBR we just set/cancelled"); 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!"); throw new CommandException().message("failed going to main!");
Object percentageObj = scripter.currentMenu.getAttribute(MenuAttribute.TBR); Object percentageObj = scripter.currentMenu.getAttribute(MenuAttribute.TBR);
Object durationObj = scripter.currentMenu.getAttribute(MenuAttribute.RUNTIME); Object durationObj = scripter.currentMenu.getAttribute(MenuAttribute.RUNTIME);
if(percentage==100) { if (percentage == 100) {
if (percentageObj != null || durationObj != null) if (percentageObj != null || durationObj != null)
throw new CommandException().message("TBR cancelled, but main menu shows a running TBR"); throw new CommandException().message("TBR cancelled, but main menu shows a running TBR");
return new CommandResult().success(true).enacted(true).message("TBR was cancelled"); 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"); throw new CommandException().message("not percentage");
if(((double)percentageObj)!=percentage) if (((double) percentageObj) != percentage)
throw new CommandException().message("wrong percentage set!"); 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"); throw new CommandException().message("not time");
MenuTime t = (MenuTime) durationObj; 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!"); throw new CommandException().message("wrong time set!");