Format code.
This commit is contained in:
parent
d75a04c985
commit
ead8508760
2 changed files with 107 additions and 114 deletions
|
@ -60,8 +60,13 @@ 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 {
|
||||||
|
ruffyService.removeHandler(mHandler);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
if (newService != null) {
|
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
|
||||||
|
@ -69,7 +74,11 @@ public class RuffyScripter {
|
||||||
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);
|
||||||
|
@ -113,9 +122,7 @@ public class RuffyScripter {
|
||||||
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) {
|
||||||
|
@ -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,8 +212,7 @@ 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
|
||||||
|
@ -226,7 +233,11 @@ 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();
|
||||||
|
@ -440,8 +453,7 @@ public class RuffyScripter {
|
||||||
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,33 +464,26 @@ 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)
|
||||||
|
@ -500,6 +505,7 @@ public class RuffyScripter {
|
||||||
// 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++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,14 +58,13 @@ 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);
|
||||||
|
@ -79,16 +78,14 @@ public class SetTbrCommand implements Command {
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
@ -114,9 +111,9 @@ public class SetTbrCommand implements Command {
|
||||||
scripter.waitForScreenUpdate(1000);
|
scripter.waitForScreenUpdate(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
currentPercentage = -100;
|
currentPercentage = -100;
|
||||||
|
}
|
||||||
scripter.waitForScreenUpdate(1000);
|
scripter.waitForScreenUpdate(1000);
|
||||||
}
|
}
|
||||||
if (currentPercentage < 0 || retries < 0)
|
if (currentPercentage < 0 || retries < 0)
|
||||||
|
@ -132,10 +129,10 @@ public class SetTbrCommand implements Command {
|
||||||
|
|
||||||
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);
|
||||||
|
@ -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)
|
||||||
|
@ -206,8 +202,7 @@ 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);
|
||||||
|
@ -215,23 +210,20 @@ public class SetTbrCommand implements Command {
|
||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue