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 doRTDisconnect();
void rtSendKey(byte keyCode, boolean changed); void rtSendKey(byte keyCode, boolean changed);
void resetPairing(); void resetPairing();
boolean isConnected();
} }

View file

@ -45,6 +45,8 @@ public class RuffyScripter {
private volatile long lastCmdExecutionTime; private volatile long lastCmdExecutionTime;
private volatile Command activeCmd = null; private volatile Command activeCmd = null;
private volatile boolean connected = false;
public RuffyScripter(final IRuffyService ruffyService) { public RuffyScripter(final IRuffyService ruffyService) {
this.ruffyService = ruffyService; this.ruffyService = ruffyService;
try { try {
@ -55,8 +57,6 @@ public class RuffyScripter {
} }
} }
private volatile boolean connected = false;
private Thread idleDisconnectMonitorThread = new Thread(new Runnable() { private Thread idleDisconnectMonitorThread = new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -72,6 +72,7 @@ public class RuffyScripter {
log.debug("Disconnecting after " + (connectionTimeOutMs / 1000) + "s inactivity timeout"); log.debug("Disconnecting after " + (connectionTimeOutMs / 1000) + "s inactivity timeout");
lastDisconnect = now; lastDisconnect = now;
ruffyService.doRTDisconnect(); ruffyService.doRTDisconnect();
connected = false;
SystemClock.sleep(1000); SystemClock.sleep(1000);
} }
} catch (DeadObjectException doe) { } catch (DeadObjectException doe) {
@ -133,6 +134,8 @@ public class RuffyScripter {
currentMenu = menu; currentMenu = menu;
menuLastUpdated = System.currentTimeMillis(); menuLastUpdated = System.currentTimeMillis();
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)
// of a running command // of a running command
if (activeCmd == null && currentMenu.getType() == MenuType.WARNING_OR_ERROR) { if (activeCmd == null && currentMenu.getType() == MenuType.WARNING_OR_ERROR) {
@ -279,6 +282,7 @@ 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 {
boolean menuUpdateRecentlyReceived = currentMenu != null && menuLastUpdated + 1000 > System.currentTimeMillis(); boolean menuUpdateRecentlyReceived = currentMenu != null && menuLastUpdated + 1000 > System.currentTimeMillis();
log.debug("ensureConnect, connected: " + connected + ", receiving menu updates: " + menuUpdateRecentlyReceived); log.debug("ensureConnect, connected: " + connected + ", receiving menu updates: " + menuUpdateRecentlyReceived);
if (menuUpdateRecentlyReceived) { if (menuUpdateRecentlyReceived) {
@ -286,7 +290,6 @@ public class RuffyScripter {
return; return;
} }
try {
boolean connectInitSuccessful = ruffyService.doRTConnect() == 0; boolean connectInitSuccessful = ruffyService.doRTConnect() == 0;
log.debug("Connect init successful: " + connectInitSuccessful); log.debug("Connect init successful: " + connectInitSuccessful);
while (currentMenu == null) { while (currentMenu == null) {