From e526de68ea0257066e248e70423ba5c2cfe73608 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Wed, 19 Jul 2017 22:31:43 +0200 Subject: [PATCH] Some improvements around connection state. --- .../d/ruffy/ruffy/driver/IRuffyService.aidl | 1 + .../jotomo/ruffyscripter/RuffyScripter.java | 41 ++++++++++--------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/app/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/IRuffyService.aidl b/app/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/IRuffyService.aidl index 3baa4116e1..c093e855b8 100644 --- a/app/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/IRuffyService.aidl +++ b/app/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/IRuffyService.aidl @@ -11,4 +11,5 @@ interface IRuffyService { void doRTDisconnect(); void rtSendKey(byte keyCode, boolean changed); void resetPairing(); + boolean isConnected(); } diff --git a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java index caa157bfa3..1f14c20965 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java @@ -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,