From 2a80111034fe19fc6b14e2da8d0340b20d7d3069 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sun, 27 Aug 2017 14:12:47 +0200 Subject: [PATCH] Retry with current ruffy. --- .../d/ruffy/ruffy/driver/IRTHandler.aidl | 9 +++- .../d/ruffy/ruffy/driver/IRuffyService.aidl | 10 +++-- .../d/ruffy/ruffy/driver/package-info.java | 1 - .../jotomo/ruffyscripter/RuffyScripter.java | 42 +++++++++++++++---- 4 files changed, 47 insertions(+), 15 deletions(-) delete mode 100644 app/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/package-info.java diff --git a/app/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/IRTHandler.aidl b/app/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/IRTHandler.aidl index 996b10b666..b6a07226b8 100644 --- a/app/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/IRTHandler.aidl +++ b/app/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/IRTHandler.aidl @@ -10,12 +10,17 @@ interface IRTHandler { void fail(String message); void requestBluetooth(); + boolean canDisconnect(); void rtStopped(); void rtStarted(); void rtClearDisplay(); void rtUpdateDisplay(in byte[] quarter, int which); - void rtDisplayHandleMenu(in Menu menu); - void rtDisplayHandleNoMenu(); + void rtDisplayHandleMenu(in Menu menu, in int sequence); + void rtDisplayHandleNoMenu(in int sequence); + + void keySent(in int sequence); + + String getServiceIdentifier(); } 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 6c988aa038..f4879445b7 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 @@ -6,18 +6,22 @@ import org.monkey.d.ruffy.ruffy.driver.IRTHandler; interface IRuffyService { - void setHandler(IRTHandler handler); + void addHandler(IRTHandler handler); + void removeHandler(IRTHandler handler); /** Connect to the pump * * @return 0 if successful, -1 otherwise */ - int doRTConnect(); + int doRTConnect(IRTHandler handler); /** Disconnect from the pump */ - void doRTDisconnect(); + void doRTDisconnect(IRTHandler handler); + /*What's the meaning of 'changed'? + * changed means if a button state has been changed, like btton pressed is a change and button release another*/ void rtSendKey(byte keyCode, boolean changed); void resetPairing(); boolean isConnected(); + boolean isBoundToPump(); } diff --git a/app/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/package-info.java b/app/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/package-info.java deleted file mode 100644 index ab1def7407..0000000000 --- a/app/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/package-info.java +++ /dev/null @@ -1 +0,0 @@ -//b916a900c0899ef58ad58c7427d1c30d3c8731f4 \ No newline at end of file diff --git a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java index 7630907160..4546aa90c6 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java @@ -46,9 +46,11 @@ public class RuffyScripter { private final Object screenlock = new Object(); + private final Object keylock = new Object(); + private int keynotwait = 0; + public void start(IRuffyService newService) { try { -/* if (ruffyService != null) { try { ruffyService.removeHandler(mHandler); @@ -56,7 +58,6 @@ public class RuffyScripter { // ignore } } -*/ if (newService != null) { this.ruffyService = newService; // TODO this'll be done better in v2 via ConnectionManager @@ -64,7 +65,7 @@ public class RuffyScripter { idleDisconnectMonitorThread.start(); } started = true; - newService.setHandler(mHandler); + newService.addHandler(mHandler); } } catch (Exception e) { log.error("Unhandled exception starting RuffyScripter", e); @@ -77,6 +78,7 @@ public class RuffyScripter { } private volatile boolean connected = false; + private volatile boolean canDisconnect = false; private volatile long lastDisconnected = 0; private Thread idleDisconnectMonitorThread = new Thread(new Runnable() { @@ -92,7 +94,8 @@ public class RuffyScripter { && now > lastDisconnected + 15 * 1000) { log.debug("Disconnecting after " + (connectionTimeOutMs / 1000) + "s inactivity timeout"); lastDisconnected = now; - ruffyService.doRTDisconnect(); + canDisconnect = true; + ruffyService.doRTDisconnect(mHandler); connected = false; // don't attempt anything fancy in the next 10s, let the pump settle SystemClock.sleep(10 * 1000); @@ -127,6 +130,11 @@ public class RuffyScripter { log.trace("Ruffy invoked requestBluetooth callback"); } + @Override + public boolean canDisconnect() throws RemoteException { + return canDisconnect; + } + @Override public void rtStopped() throws RemoteException { log.debug("rtStopped callback invoked"); @@ -149,7 +157,7 @@ public class RuffyScripter { } @Override - public void rtDisplayHandleMenu(Menu menu) throws RemoteException { + public void rtDisplayHandleMenu(Menu menu, int sequence) throws RemoteException { // method is called every ~500ms log.debug("rtDisplayHandleMenu: " + menu); @@ -172,9 +180,26 @@ public class RuffyScripter { } @Override - public void rtDisplayHandleNoMenu() throws RemoteException { + public void rtDisplayHandleNoMenu(int sequence) throws RemoteException { log.debug("rtDisplayHandleNoMenu callback invoked"); } + + + @Override + public void keySent(int sequence) throws RemoteException { + synchronized (keylock) { + if (keynotwait > 0) + keynotwait--; + else + keylock.notify(); + } + } + + @Override + public String getServiceIdentifier() throws RemoteException { + return this.toString(); + } + }; public boolean isPumpBusy() { @@ -182,14 +207,12 @@ public class RuffyScripter { } public void unbind() { - /* if (ruffyService != null) try { ruffyService.removeHandler(mHandler); } catch (Exception e) { // ignore } - */ this.ruffyService = null; } @@ -352,7 +375,8 @@ public class RuffyScripter { SystemClock.sleep(10 * 1000); } - boolean connectInitSuccessful = ruffyService.doRTConnect() == 0; + canDisconnect = false; + boolean connectInitSuccessful = ruffyService.doRTConnect(mHandler) == 0; log.debug("Connect init successful: " + connectInitSuccessful); log.debug("Waiting for first menu update to be sent"); // Note: there was an 'if(currentMenu == null)' around the next call, since