RuffyScripter.runCommand: Next shot at understanding threading better.
This commit is contained in:
parent
83e9ec743e
commit
2ddccd3c31
|
@ -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;
|
||||||
// wait till pump is ready for input
|
cmdResult = null;
|
||||||
waitForMenuUpdate();
|
Thread cmdThread;
|
||||||
// check if pump is an an error state
|
|
||||||
if (currentMenu != null && currentMenu.getType() == MenuType.WARNING_OR_ERROR) {
|
|
||||||
try {
|
|
||||||
PumpState pumpState = readPumpState();
|
|
||||||
return new CommandResult().message("Pump is in an error state: " + currentMenu.getAttribute(MenuAttribute.MESSAGE)).state(pumpState);
|
|
||||||
} catch (Exception e) {
|
|
||||||
return new CommandResult().message("Pump is in an error state, reading the error state resulted in the attached exception").exception(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
log.debug("Cmd execution: connection ready, executing cmd " + cmd);
|
|
||||||
final RuffyScripter scripter = this;
|
final RuffyScripter scripter = this;
|
||||||
cmdThread = new Thread(new Runnable() {
|
cmdThread = new Thread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
|
ensureConnected();
|
||||||
|
cmdResult = null;
|
||||||
|
// wait till pump is ready for input
|
||||||
|
waitForMenuUpdate();
|
||||||
|
// check if pump is an an error state
|
||||||
|
if (currentMenu != null && currentMenu.getType() == MenuType.WARNING_OR_ERROR) {
|
||||||
|
try {
|
||||||
|
PumpState pumpState = readPumpState();
|
||||||
|
cmdResult = new CommandResult().message("Pump is in an error state: " + currentMenu.getAttribute(MenuAttribute.MESSAGE)).state(pumpState);
|
||||||
|
return;
|
||||||
|
} catch (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);
|
||||||
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