Robustness for RuffyScripter.

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

View file

@ -58,7 +58,7 @@ public class RuffyScripter {
@Override @Override
public void run() { public void run() {
long lastDisconnect = System.currentTimeMillis(); long lastDisconnect = System.currentTimeMillis();
SystemClock.sleep(10_000); SystemClock.sleep(1000);
while (unrecoverableError == null) { while (unrecoverableError == null) {
try { try {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
@ -173,10 +173,16 @@ public class RuffyScripter {
cmdResult = null; 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
for (int i = 0; currentMenu == null && i < 20; i++) {
log.debug("Extra wait required before pump is ready");
SystemClock.sleep(50);
}
// 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 = null;
try { pumpState = readPumpState(); } catch (Exception e) {}
returnable.cmdResult = new CommandResult().message("Pump is in an error state: " + currentMenu.getAttribute(MenuAttribute.MESSAGE)).state(pumpState); returnable.cmdResult = new CommandResult().message("Pump is in an error state: " + currentMenu.getAttribute(MenuAttribute.MESSAGE)).state(pumpState);
return; return;
} catch (Exception e) { } catch (Exception e) {
@ -206,16 +212,22 @@ public class RuffyScripter {
log.error("Running command " + activeCmd + " timed out"); log.error("Running command " + activeCmd + " timed out");
cmdThread.interrupt(); cmdThread.interrupt();
SystemClock.sleep(5000); SystemClock.sleep(5000);
log.error("Thread dead yet? " + cmdThread.isAlive()); log.error("Timed out thread dead yet? " + cmdThread.isAlive());
return new CommandResult().success(false).enacted(false).message("Command timed out"); return new CommandResult().success(false).enacted(false).message("Command timed out");
} }
if (now > menuLastUpdated + 5000) { // TODO this is an extra safety measure when the user interrupts a command by using the pump
log.debug("Stopped received menu updates while running command, aborting the command"); // and the pump returns before the timeout, the command might continue to try, but will
cmdThread.interrupt(); // very likely safely fail, still ....
SystemClock.sleep(5000); // TODO if ruffyscripter ensures the meun is ready for a command: then
log.error("Thread dead yet? " + cmdThread.isAlive()); // operations of a command should all timeout after 5-10 seconds, right?
return new CommandResult().success(false).enacted(false).message("Pump stopped sending state updates"); // anything else can't be right. and the pump quits menus after what, 8 seconds? alright, twenty
} // if (currentMenu != null && now > menuLastUpdated + 5 * 1000) {
// log.debug("Stopped receiving menu updates while running command, aborting the command");
// cmdThread.interrupt();
// SystemClock.sleep(5000);
// log.error("Thread not receiving menu updates dead yet? " + cmdThread.isAlive());
// return new CommandResult().success(false).enacted(false).message("Pump stopped sending state updates");
// }
} }
if (returnable.cmdResult.state == null) { if (returnable.cmdResult.state == null) {
@ -226,7 +238,8 @@ public class RuffyScripter {
} 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"); log.error("Error in ruffyscripter/ruffy", e);
return new CommandResult().exception(e).message("Unexpected exception communication with ruffy: " + e.getMessage());
} finally { } finally {
activeCmd = null; activeCmd = null;
} }
@ -238,7 +251,7 @@ public class RuffyScripter {
// did we get a menu update from the pump in the last second? Then we're connected // did we get a menu update from the pump in the last second? Then we're connected
boolean recentMenuUpdate = currentMenu != null && menuLastUpdated + 1000 > System.currentTimeMillis(); boolean recentMenuUpdate = currentMenu != null && menuLastUpdated + 1000 > System.currentTimeMillis();
log.debug("ensureConnect, connected: " + connected + ", receiving menu updates: " + recentMenuUpdate); log.debug("ensureConnect, connected: " + connected + ", receiving menu updates: " + recentMenuUpdate);
if (currentMenu != null && menuLastUpdated + 1000 > System.currentTimeMillis()) { if (recentMenuUpdate) {
log.debug("Pump is sending us menu updating, so we're connected"); log.debug("Pump is sending us menu updating, so we're connected");
return; return;
} }
@ -248,7 +261,7 @@ public class RuffyScripter {
log.debug("Connect init successful: " + connectInitSuccessful); log.debug("Connect init successful: " + connectInitSuccessful);
while (currentMenu == null) { while (currentMenu == null) {
log.debug("Waiting for first menu update to be sent"); log.debug("Waiting for first menu update to be sent");
// waitForMenuUpdate times out after 90s and throws a CommandException // waitForMenuUpdate times out after 60s and throws a CommandException
waitForMenuUpdate(); waitForMenuUpdate();
} }
connected = true; connected = true;
@ -297,7 +310,7 @@ public class RuffyScripter {
* Wait until the menu update is in * Wait until the menu update is in
*/ */
public void waitForMenuUpdate() { public void waitForMenuUpdate() {
long timeoutExpired = System.currentTimeMillis() + 30 * 1000; long timeoutExpired = System.currentTimeMillis() + 60 * 1000;
long initialUpdateTime = menuLastUpdated; long initialUpdateTime = menuLastUpdated;
while (initialUpdateTime == menuLastUpdated) { while (initialUpdateTime == menuLastUpdated) {
if (System.currentTimeMillis() > timeoutExpired) { if (System.currentTimeMillis() > timeoutExpired) {
@ -367,6 +380,9 @@ public class RuffyScripter {
private PumpState readPumpState() { private PumpState readPumpState() {
PumpState state = new PumpState(); PumpState state = new PumpState();
Menu menu = currentMenu; Menu menu = currentMenu;
if (menu == null) {
return new PumpState().errorMsg("Menu is not available");
}
MenuType menuType = menu.getType(); MenuType menuType = menu.getType();
if (menuType == MenuType.MAIN_MENU) { if (menuType == MenuType.MAIN_MENU) {
Double tbrPercentage = (Double) menu.getAttribute(MenuAttribute.TBR); Double tbrPercentage = (Double) menu.getAttribute(MenuAttribute.TBR);