Fix handling TBR CANCELLED alert, remove ill-advised attempts to fake a command queue and have all commands return a state object.

This commit is contained in:
Johannes Mockenhaupt 2017-07-15 15:51:48 +02:00
parent 952aa0e8a2
commit d0dabf34da
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1
4 changed files with 71 additions and 47 deletions

View file

@ -6,13 +6,17 @@ import android.os.SystemClock;
import org.monkey.d.ruffy.ruffy.driver.IRTHandler; import org.monkey.d.ruffy.ruffy.driver.IRTHandler;
import org.monkey.d.ruffy.ruffy.driver.IRuffyService; import org.monkey.d.ruffy.ruffy.driver.IRuffyService;
import org.monkey.d.ruffy.ruffy.driver.display.Menu; import org.monkey.d.ruffy.ruffy.driver.display.Menu;
import org.monkey.d.ruffy.ruffy.driver.display.MenuAttribute;
import org.monkey.d.ruffy.ruffy.driver.display.MenuType; import org.monkey.d.ruffy.ruffy.driver.display.MenuType;
import org.monkey.d.ruffy.ruffy.driver.display.menu.MenuTime;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import de.jotomo.ruffyscripter.commands.Command; import de.jotomo.ruffyscripter.commands.Command;
import de.jotomo.ruffyscripter.commands.CommandException; import de.jotomo.ruffyscripter.commands.CommandException;
import de.jotomo.ruffyscripter.commands.CommandResult; import de.jotomo.ruffyscripter.commands.CommandResult;
import de.jotomo.ruffyscripter.commands.PumpAlert;
import de.jotomo.ruffyscripter.commands.PumpState;
// TODO regularly read "My data" history (boluses, TBR) to double check all commands ran successfully. // TODO regularly read "My data" history (boluses, TBR) to double check all commands ran successfully.
// Automatically compare against AAPS db, or log all requests in the PumpInterface (maybe Milos // Automatically compare against AAPS db, or log all requests in the PumpInterface (maybe Milos
@ -156,6 +160,9 @@ public class RuffyScripter {
} }
} }
if (cmdResult.state == null) {
cmdResult.state = readPumpState();
}
log.debug("Command result: " + cmdResult); log.debug("Command result: " + cmdResult);
CommandResult r = cmdResult; CommandResult r = cmdResult;
cmdResult = null; cmdResult = null;
@ -314,4 +321,33 @@ public class RuffyScripter {
} }
} }
} }
private PumpState readPumpState() {
verifyMenuIsDisplayed(MenuType.MAIN_MENU);
PumpState state = new PumpState();
Double tbrPercentage = (Double) currentMenu.getAttribute(MenuAttribute.TBR);
if (tbrPercentage != 100) {
state.tbrActive = true;
Double displayedTbr = (Double) currentMenu.getAttribute(MenuAttribute.TBR);
state.tbrPercent = displayedTbr.intValue();
MenuTime durationMenuTime = ((MenuTime) currentMenu.getAttribute(MenuAttribute.RUNTIME));
state.tbrRemainingDuration = durationMenuTime.getHour() * 60 + durationMenuTime.getMinute();
}
return state;
}
public PumpAlert readDisplayPumpAlert() {
Object errorObj = currentMenu.getAttribute(MenuAttribute.ERROR);
Object errorMsgObj = currentMenu.getAttribute(MenuAttribute.MESSAGE);
while (errorObj == null) {
SystemClock.sleep(10);
errorObj = currentMenu.getAttribute(MenuAttribute.ERROR);
errorMsgObj = currentMenu.getAttribute(MenuAttribute.MESSAGE);
}
while (errorMsgObj == null) {
SystemClock.sleep(10);
errorMsgObj = currentMenu.getAttribute(MenuAttribute.MESSAGE);
}
return new PumpAlert((int) errorObj, (String) errorMsgObj);
}
} }

View file

@ -0,0 +1,19 @@
package de.jotomo.ruffyscripter.commands;
public class PumpAlert {
public final int code;
public final String msg;
public PumpAlert(int code, String msg) {
this.code = code;
this.msg = msg;
}
@Override
public String toString() {
return "PumpAlert{" +
"code=" + code +
", msg='" + msg + '\'' +
'}';
}
}

View file

@ -9,6 +9,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.Locale; import java.util.Locale;
import java.util.Objects;
import de.jotomo.ruffyscripter.RuffyScripter; import de.jotomo.ruffyscripter.RuffyScripter;
@ -197,12 +198,11 @@ public class SetTbrCommand implements Command {
while (System.currentTimeMillis() < inTwoSeconds && !alertProcessed) { while (System.currentTimeMillis() < inTwoSeconds && !alertProcessed) {
if (scripter.currentMenu.getType() == MenuType.WARNING_OR_ERROR) { if (scripter.currentMenu.getType() == MenuType.WARNING_OR_ERROR) {
// check the raised alarm is TBR CANCELLED // check the raised alarm is TBR CANCELLED
int errorCode = (int) scripter.currentMenu.getAttribute(MenuAttribute.ERROR); PumpAlert alert = scripter.readDisplayPumpAlert();
String errorMsg = (String) scripter.currentMenu.getAttribute(MenuAttribute.MESSAGE); if (alert.code != 6 || Objects.equals(alert.msg, "TBR CANCELLED")) {
if (errorCode != 6 || errorMsg.equals("TBR CANCELLED")) {
throw new CommandException().success(false).enacted(false) throw new CommandException().success(false).enacted(false)
.message("An alert other than the expected TBR CANCELLED was raised by the pump: " .message("An alert other than the expected TBR CANCELLED was raised by the pump: "
+ errorMsg + "(" + errorCode + "). Please check the pump."); + alert.code + "(" + alert.msg + "). Please check the pump.");
} }
// confirm "TBR CANCELLED alert" // confirm "TBR CANCELLED alert"
scripter.pressCheckKey(); scripter.pressCheckKey();

View file

@ -95,13 +95,7 @@ public class ComboPlugin implements PluginBase, PumpInterface {
@Override @Override
public void onServiceConnected(ComponentName name, IBinder service) { public void onServiceConnected(ComponentName name, IBinder service) {
ruffyScripter = new RuffyScripter(IRuffyService.Stub.asInterface(service)); ruffyScripter = new RuffyScripter(IRuffyService.Stub.asInterface(service));
log.debug("ruffy serivce connected, fetching initial pump state"); log.debug("ruffy serivce connected");
new Thread(new Runnable() {
@Override
public void run() {
readPumpState(false);
}
}).start();
} }
@Override @Override
@ -116,17 +110,6 @@ public class ComboPlugin implements PluginBase, PumpInterface {
} }
} }
private CommandResult readPumpState(boolean keepConnectionOpen) {
CommandResult commandResult = runCommand(new ReadStateCommand(), keepConnectionOpen);
if (commandResult.success) {
pumpState = commandResult.state;
log.debug("Pump state: " + commandResult.state);
} else {
log.warn("Reading pump status failed: " + commandResult.message);
}
return commandResult;
}
private void definePumpCapabilities() { private void definePumpCapabilities() {
pumpDescription.isBolusCapable = true; pumpDescription.isBolusCapable = true;
pumpDescription.bolusStep = 0.1d; pumpDescription.bolusStep = 0.1d;
@ -280,7 +263,7 @@ public class ComboPlugin implements PluginBase, PumpInterface {
if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) { if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) {
PumpEnactResult result = new PumpEnactResult(); PumpEnactResult result = new PumpEnactResult();
if (detailedBolusInfo.insulin > 0) { if (detailedBolusInfo.insulin > 0) {
CommandResult bolusCmdResult = runCommand(new BolusCommand(detailedBolusInfo.insulin), false); CommandResult bolusCmdResult = runCommand(new BolusCommand(detailedBolusInfo.insulin));
result.success = bolusCmdResult.success; result.success = bolusCmdResult.success;
result.enacted = bolusCmdResult.enacted; result.enacted = bolusCmdResult.enacted;
// TODO if no error occurred, the requested bolus is what the pump delievered, // TODO if no error occurred, the requested bolus is what the pump delievered,
@ -316,24 +299,21 @@ public class ComboPlugin implements PluginBase, PumpInterface {
} }
} }
private CommandResult runCommand(Command command, boolean keepConnectionOpen) { private CommandResult runCommand(Command command) {
// TODO use this to dispatch methods to a service thread, like DanaRs executionService // TODO use this to dispatch methods to a service thread, like DanaRs executionService
// will be required when doing multiple commands in sequence.
// Alternatively provide 'composite commands' to return everything needed in one go?
try { try {
long timeout = System.currentTimeMillis() + 10 * 1000; CommandResult commandResult = ruffyScripter.runCommand(command);
while (ruffyScripter.isPumpBusy()) { if (commandResult.success && commandResult.state != null) {
if (System.currentTimeMillis() < timeout) { pumpState = commandResult.state;
return new CommandResult().success(false).enacted(false).message("Timeout waiting for the pump to be ready");
} }
SystemClock.sleep(200); return commandResult;
}
return ruffyScripter.runCommand(command);
} finally { } finally {
lastCmdTime = new Date(); lastCmdTime = new Date();
if (!keepConnectionOpen) {
ruffyScripter.disconnect(); ruffyScripter.disconnect();
} }
} }
}
@Override @Override
public void stopBolusDelivering() { public void stopBolusDelivering() {
@ -351,13 +331,6 @@ public class ComboPlugin implements PluginBase, PumpInterface {
if (unroundedPercentage != roundedPercentage) { if (unroundedPercentage != roundedPercentage) {
log.debug("Rounded requested rate " + unroundedPercentage + "% -> " + roundedPercentage + "%"); log.debug("Rounded requested rate " + unroundedPercentage + "% -> " + roundedPercentage + "%");
} }
CommandResult readStateCmdResult = readPumpState(true);
if (!readStateCmdResult.success) {
PumpEnactResult pumpEnactResult = new PumpEnactResult();
pumpEnactResult.success = false;
pumpEnactResult.enacted = false;
pumpEnactResult.comment = "Failed to read pump state";
}
int activeTbrPercentage = pumpState != null ? pumpState.tbrPercent : 100; int activeTbrPercentage = pumpState != null ? pumpState.tbrPercent : 100;
if (activeTbrPercentage != -1 && Math.abs(activeTbrPercentage - roundedPercentage) <= 20) { if (activeTbrPercentage != -1 && Math.abs(activeTbrPercentage - roundedPercentage) <= 20) {
log.debug("Not bothering the pump for a small TBR change from " + activeTbrPercentage + "% -> " + roundedPercentage + "%"); log.debug("Not bothering the pump for a small TBR change from " + activeTbrPercentage + "% -> " + roundedPercentage + "%");
@ -387,7 +360,7 @@ public class ComboPlugin implements PluginBase, PumpInterface {
percent = rounded; percent = rounded;
} }
MainApp.bus().post(new EventPumpStatusChanged(MainApp.sResources.getString(R.string.settingtempbasal))); MainApp.bus().post(new EventPumpStatusChanged(MainApp.sResources.getString(R.string.settingtempbasal)));
CommandResult commandResult = runCommand(new SetTbrCommand(percent, durationInMinutes), true); CommandResult commandResult = runCommand(new SetTbrCommand(percent, durationInMinutes));
if (commandResult.enacted) { if (commandResult.enacted) {
TemporaryBasal tempStart = new TemporaryBasal(System.currentTimeMillis()); TemporaryBasal tempStart = new TemporaryBasal(System.currentTimeMillis());
tempStart.durationInMinutes = durationInMinutes; tempStart.durationInMinutes = durationInMinutes;
@ -398,8 +371,6 @@ public class ComboPlugin implements PluginBase, PumpInterface {
treatmentsInterface.addToHistoryTempBasal(tempStart); treatmentsInterface.addToHistoryTempBasal(tempStart);
} }
readPumpState(false);
PumpEnactResult pumpEnactResult = new PumpEnactResult(); PumpEnactResult pumpEnactResult = new PumpEnactResult();
pumpEnactResult.success = commandResult.success; pumpEnactResult.success = commandResult.success;
pumpEnactResult.enacted = commandResult.enacted; pumpEnactResult.enacted = commandResult.enacted;
@ -421,7 +392,7 @@ public class ComboPlugin implements PluginBase, PumpInterface {
public PumpEnactResult cancelTempBasal() { public PumpEnactResult cancelTempBasal() {
log.debug("cancelTempBasal called"); log.debug("cancelTempBasal called");
MainApp.bus().post(new EventPumpStatusChanged(MainApp.sResources.getString(R.string.stoppingtempbasal))); MainApp.bus().post(new EventPumpStatusChanged(MainApp.sResources.getString(R.string.stoppingtempbasal)));
CommandResult commandResult = runCommand(new CancelTbrCommand(), true); CommandResult commandResult = runCommand(new CancelTbrCommand());
if (commandResult.enacted) { if (commandResult.enacted) {
TemporaryBasal tempStop = new TemporaryBasal(System.currentTimeMillis()); TemporaryBasal tempStop = new TemporaryBasal(System.currentTimeMillis());
tempStop.durationInMinutes = 0; // ending temp basal tempStop.durationInMinutes = 0; // ending temp basal
@ -430,8 +401,6 @@ public class ComboPlugin implements PluginBase, PumpInterface {
treatmentsInterface.addToHistoryTempBasal(tempStop); treatmentsInterface.addToHistoryTempBasal(tempStop);
} }
readPumpState(false);
PumpEnactResult pumpEnactResult = new PumpEnactResult(); PumpEnactResult pumpEnactResult = new PumpEnactResult();
pumpEnactResult.success = commandResult.success; pumpEnactResult.success = commandResult.success;
pumpEnactResult.enacted = commandResult.enacted; pumpEnactResult.enacted = commandResult.enacted;