Cleanups.

This commit is contained in:
Johannes Mockenhaupt 2017-07-16 20:48:20 +02:00
parent 20a8350c67
commit 5fad9b3a85
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1

View file

@ -39,8 +39,6 @@ public class RuffyScripter {
private volatile long lastCmdExecutionTime; private volatile long lastCmdExecutionTime;
private volatile Command activeCmd = null; private volatile Command activeCmd = null;
private volatile CommandResult cmdResult;
public RuffyScripter(final IRuffyService ruffyService) { public RuffyScripter(final IRuffyService ruffyService) {
this.ruffyService = ruffyService; this.ruffyService = ruffyService;
@ -77,6 +75,7 @@ public class RuffyScripter {
// converted into a command failure, but it's not classified as unrecoverable; // converted into a command failure, but it's not classified as unrecoverable;
// eventually we might try to recover ... check docs, there's also another // eventually we might try to recover ... check docs, there's also another
// execption we should watch interacting with a remote service. // execption we should watch interacting with a remote service.
// SecurityException was the other, when there's an AIDL mismatch;
unrecoverableError = "Ruffy service went away"; unrecoverableError = "Ruffy service went away";
} catch (RemoteException e) { } catch (RemoteException e) {
log.debug("Exception in idle disconnect monitor thread, carrying on", e); log.debug("Exception in idle disconnect monitor thread, carrying on", e);
@ -148,8 +147,6 @@ public class RuffyScripter {
return activeCmd != null; return activeCmd != null;
} }
// TODO still needed?
// problem was some timing issue something when disconnectin from ruffy and immediately reconnecting
private static class Returnable { private static class Returnable {
CommandResult cmdResult; CommandResult cmdResult;
} }
@ -161,16 +158,14 @@ public class RuffyScripter {
} }
synchronized (RuffyScripter.class) { synchronized (RuffyScripter.class) {
try { try {
activeCmd = cmd;
cmdResult = null;
final RuffyScripter scripter = this; final RuffyScripter scripter = this;
activeCmd = cmd;
final Returnable returnable = new Returnable(); final Returnable returnable = new Returnable();
Thread cmdThread = new Thread(new Runnable() { Thread cmdThread = new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
ensureConnected(); ensureConnected();
cmdResult = null;
// wait till pump is ready for input // wait till pump is ready for input
waitForMenuUpdate(); waitForMenuUpdate();
// wait a bit longer to make extra sure we have a valid menu to work with // wait a bit longer to make extra sure we have a valid menu to work with
@ -247,12 +242,10 @@ public class RuffyScripter {
} }
private void ensureConnected() { private void ensureConnected() {
// TODO cleanup/simplify boolean menuUpdateRecentlyReceived = currentMenu != null && menuLastUpdated + 1000 > System.currentTimeMillis();
// did we get a menu update from the pump in the last second? Then we're connected log.debug("ensureConnect, connected: " + connected + ", receiving menu updates: " + menuUpdateRecentlyReceived);
boolean recentMenuUpdate = currentMenu != null && menuLastUpdated + 1000 > System.currentTimeMillis(); if (menuUpdateRecentlyReceived) {
log.debug("ensureConnect, connected: " + connected + ", receiving menu updates: " + recentMenuUpdate); log.debug("Pump is sending us menu updates, so we're connected");
if (recentMenuUpdate) {
log.debug("Pump is sending us menu updating, so we're connected");
return; return;
} }
@ -271,8 +264,9 @@ public class RuffyScripter {
} }
// below: methods to be used by commands // below: methods to be used by commands
// TODO move into a new Operations(scripter) class commands can delegate to // TODO move into a new Operations(scripter) class commands can delegate to,
// while refactoring, move everything thats not a command out of the commands package // so this class can focus on providing a connection to run commands
// (or maybe reconsider putting it into a base class)
private static class Key { private static class Key {
static byte NO_KEY = (byte) 0x00; static byte NO_KEY = (byte) 0x00;