RuffyScripter.runCommand: Next shot at understanding threading better.
This commit is contained in:
parent
83e9ec743e
commit
2ddccd3c31
1 changed files with 29 additions and 33 deletions
|
@ -110,68 +110,64 @@ public class RuffyScripter {
|
||||||
public CommandResult runCommand(final Command cmd) {
|
public CommandResult runCommand(final Command cmd) {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
try {
|
try {
|
||||||
if (isPumpBusy()) {
|
|
||||||
return new CommandResult().message("Pump is busy");
|
|
||||||
}
|
|
||||||
ensureConnected();
|
|
||||||
|
|
||||||
// TODO reuse thread, scheduler ...
|
|
||||||
Thread cmdThread;
|
|
||||||
|
|
||||||
cmdResult = null;
|
|
||||||
activeCmd = cmd;
|
activeCmd = cmd;
|
||||||
|
cmdResult = null;
|
||||||
|
Thread cmdThread;
|
||||||
|
final RuffyScripter scripter = this;
|
||||||
|
cmdThread = new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
ensureConnected();
|
||||||
|
cmdResult = null;
|
||||||
// wait till pump is ready for input
|
// wait till pump is ready for input
|
||||||
waitForMenuUpdate();
|
waitForMenuUpdate();
|
||||||
// check if pump is an an error state
|
// check if pump is an an error state
|
||||||
if (currentMenu != null && currentMenu.getType() == MenuType.WARNING_OR_ERROR) {
|
if (currentMenu != null && currentMenu.getType() == MenuType.WARNING_OR_ERROR) {
|
||||||
try {
|
try {
|
||||||
PumpState pumpState = readPumpState();
|
PumpState pumpState = readPumpState();
|
||||||
return new CommandResult().message("Pump is in an error state: " + currentMenu.getAttribute(MenuAttribute.MESSAGE)).state(pumpState);
|
cmdResult = new CommandResult().message("Pump is in an error state: " + currentMenu.getAttribute(MenuAttribute.MESSAGE)).state(pumpState);
|
||||||
|
return;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return new CommandResult().message("Pump is in an error state, reading the error state resulted in the attached exception").exception(e);
|
cmdResult = new CommandResult().message("Pump is in an error state, reading the error state resulted in the attached exception").exception(e);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.debug("Cmd execution: connection ready, executing cmd " + cmd);
|
log.debug("Cmd execution: connection ready, executing cmd " + cmd);
|
||||||
final RuffyScripter scripter = this;
|
|
||||||
cmdThread = new Thread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
cmdResult = cmd.execute(scripter);
|
cmdResult = cmd.execute(scripter);
|
||||||
|
} catch (CommandException e) {
|
||||||
|
cmdResult = e.toCommandResult();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
cmdResult = new CommandResult().exception(e).message("Unexpected exception running cmd");
|
cmdResult = new CommandResult().exception(e).message("Unexpected exception running cmd");
|
||||||
} finally {
|
|
||||||
activeCmd = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
cmdThread.start();
|
cmdThread.start();
|
||||||
|
|
||||||
// TODO really?
|
|
||||||
long timeout = System.currentTimeMillis() + 90 * 1000;
|
long timeout = System.currentTimeMillis() + 90 * 1000;
|
||||||
while (activeCmd != null) {
|
while (cmdResult == null) {
|
||||||
SystemClock.sleep(500);
|
|
||||||
log.trace("Waiting for running command to complete");
|
log.trace("Waiting for running command to complete");
|
||||||
|
SystemClock.sleep(500);
|
||||||
if (System.currentTimeMillis() > timeout) {
|
if (System.currentTimeMillis() > timeout) {
|
||||||
log.error("Running command " + activeCmd + " timed out");
|
log.error("Running command " + activeCmd + " timed out");
|
||||||
cmdThread.interrupt();
|
cmdThread.interrupt();
|
||||||
activeCmd = null;
|
cmdResult = new CommandResult().success(false).enacted(false).message("Command timed out");
|
||||||
return new CommandResult().success(false).enacted(false).message("Command timed out");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cmdResult.state == null) {
|
CommandResult result = cmdResult;
|
||||||
cmdResult.state = readPumpState();
|
if (result.state == null) {
|
||||||
|
result.state = readPumpState();
|
||||||
}
|
}
|
||||||
log.debug("Command result: " + cmdResult);
|
log.debug("Command result: " + result);
|
||||||
return cmdResult;
|
return result;
|
||||||
} catch (CommandException e) {
|
} catch (CommandException e) {
|
||||||
return e.toCommandResult();
|
return e.toCommandResult();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return new CommandResult().exception(e).message("Unexpected exception communication with ruffy");
|
return new CommandResult().exception(e).message("Unexpected exception communication with ruffy");
|
||||||
} finally {
|
} finally {
|
||||||
activeCmd = null;
|
activeCmd = null;
|
||||||
|
cmdResult = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue