Some improvements around connection state.

This commit is contained in:
Johannes Mockenhaupt 2017-07-19 22:31:43 +02:00
parent bf179cd751
commit e526de68ea
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1
2 changed files with 23 additions and 19 deletions

View file

@ -11,4 +11,5 @@ interface IRuffyService {
void doRTDisconnect();
void rtSendKey(byte keyCode, boolean changed);
void resetPairing();
boolean isConnected();
}

View file

@ -45,6 +45,8 @@ public class RuffyScripter {
private volatile long lastCmdExecutionTime;
private volatile Command activeCmd = null;
private volatile boolean connected = false;
public RuffyScripter(final IRuffyService ruffyService) {
this.ruffyService = ruffyService;
try {
@ -55,8 +57,6 @@ public class RuffyScripter {
}
}
private volatile boolean connected = false;
private Thread idleDisconnectMonitorThread = new Thread(new Runnable() {
@Override
public void run() {
@ -72,6 +72,7 @@ public class RuffyScripter {
log.debug("Disconnecting after " + (connectionTimeOutMs / 1000) + "s inactivity timeout");
lastDisconnect = now;
ruffyService.doRTDisconnect();
connected = false;
SystemClock.sleep(1000);
}
} catch (DeadObjectException doe) {
@ -133,6 +134,8 @@ public class RuffyScripter {
currentMenu = menu;
menuLastUpdated = System.currentTimeMillis();
connected = true;
// note that a WARNING_OR_ERROR menu can be a valid temporary state (cancelling TBR)
// of a running command
if (activeCmd == null && currentMenu.getType() == MenuType.WARNING_OR_ERROR) {
@ -279,25 +282,25 @@ public class RuffyScripter {
/** If there's an issue, this times out eventually and throws a CommandException */
private void ensureConnected() {
boolean menuUpdateRecentlyReceived = currentMenu != null && menuLastUpdated + 1000 > System.currentTimeMillis();
log.debug("ensureConnect, connected: " + connected + ", receiving menu updates: " + menuUpdateRecentlyReceived);
if (menuUpdateRecentlyReceived) {
log.debug("Pump is sending us menu updates, so we're connected");
return;
}
try {
boolean connectInitSuccessful = ruffyService.doRTConnect() == 0;
log.debug("Connect init successful: " + connectInitSuccessful);
while (currentMenu == null) {
log.debug("Waiting for first menu update to be sent");
// waitForMenuUpdate times out after 60s and throws a CommandException
waitForMenuUpdate();
}
} catch (RemoteException e) {
throw new CommandException().exception(e).message("Unexpected exception while initiating/restoring pump connection");
try {
boolean menuUpdateRecentlyReceived = currentMenu != null && menuLastUpdated + 1000 > System.currentTimeMillis();
log.debug("ensureConnect, connected: " + connected + ", receiving menu updates: " + menuUpdateRecentlyReceived);
if (menuUpdateRecentlyReceived) {
log.debug("Pump is sending us menu updates, so we're connected");
return;
}
boolean connectInitSuccessful = ruffyService.doRTConnect() == 0;
log.debug("Connect init successful: " + connectInitSuccessful);
while (currentMenu == null) {
log.debug("Waiting for first menu update to be sent");
// waitForMenuUpdate times out after 60s and throws a CommandException
waitForMenuUpdate();
}
} catch (RemoteException e) {
throw new CommandException().exception(e).message("Unexpected exception while initiating/restoring pump connection");
}
}
// below: methods to be used by commands
// TODO move into a new Operations(scripter) class commands can delegate to,