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,8 +60,13 @@ public class RuffyScripter {
public void start(IRuffyService newService) {
try {
if(ruffyService!=null)
try{ruffyService.removeHandler(mHandler);}catch(Exception e){};
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
@ -69,7 +74,11 @@ public class RuffyScripter {
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);
@ -113,9 +122,7 @@ public class RuffyScripter {
lastDisconnect = System.currentTimeMillis();
// don't attempt anything fancy in the next 10s, let the pump settle
SystemClock.sleep(10 * 1000);
}
else
{
} else {
canDisconnect = false;
}
} catch (Exception e) {
@ -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,8 +212,7 @@ public class RuffyScripter {
@Override
public void keySent(int sequence) throws RemoteException {
synchronized (keylock)
{
synchronized (keylock) {
if (keynotwait > 0)
keynotwait--;
else
@ -226,7 +233,11 @@ public class RuffyScripter {
public void unbind() {
if (ruffyService != null)
try{ruffyService.removeHandler(mHandler);}catch (Exception e){}
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();
@ -440,8 +453,7 @@ public class RuffyScripter {
log.debug("Releasing back key");
}
public boolean waitForScreenUpdate(long timeout)
{
public boolean waitForScreenUpdate(long timeout) {
synchronized (screenlock) {
try {
screenlock.wait(timeout);
@ -452,33 +464,26 @@ 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;
}
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)
return true;
if (currentMenu == null || currentMenu.getType() != startType)
@ -500,6 +505,7 @@ public class RuffyScripter {
// 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
*/
@ -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++;
}
}

View file

@ -58,14 +58,13 @@ 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");
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);
@ -79,16 +78,14 @@ public class SetTbrCommand implements Command {
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);
SystemClock.sleep(500);
if (scripter.currentMenu.getType() == TBR_SET)
break;
if(scripter.currentMenu.getType()== TBR_DURATION)
{
if (scripter.currentMenu.getType() == TBR_DURATION) {
scripter.pressMenuKey();
scripter.waitForScreenUpdate(1000);
}
@ -114,9 +111,9 @@ public class SetTbrCommand implements Command {
scripter.waitForScreenUpdate(1000);
}
}
else
} else {
currentPercentage = -100;
}
scripter.waitForScreenUpdate(1000);
}
if (currentPercentage < 0 || retries < 0)
@ -132,10 +129,10 @@ public class SetTbrCommand implements Command {
if (percentageObj != null && (percentageObj instanceof Double)) {
currentPercentage = ((Double) percentageObj).doubleValue();
}
else
} else {
scripter.waitForScreenUpdate(1000);
}
}
if (retries < 0 || currentPercentage != percentage)
throw new CommandException().message("Unable to set percentage. Requested: " + percentage + ", value displayed on pump: " + currentPercentage);
@ -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)
@ -206,8 +202,7 @@ 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))
{
while (retries >= 0 && (scripter.currentMenu.getType() == TBR_DURATION || scripter.currentMenu.getType() == TBR_SET)) {
retries--;
scripter.pressCheckKey();
scripter.waitForScreenUpdate(1000);
@ -215,23 +210,20 @@ public class SetTbrCommand implements Command {
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;
boolean cancelledError = true;
if (percentage == 100)
canceledError=false;
while(retries>=0 && scripter.currentMenu.getType()!=MAIN_MENU )
{
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;
}
}