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:
parent
952aa0e8a2
commit
d0dabf34da
4 changed files with 71 additions and 47 deletions
|
@ -6,13 +6,17 @@ import android.os.SystemClock;
|
|||
import org.monkey.d.ruffy.ruffy.driver.IRTHandler;
|
||||
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.MenuAttribute;
|
||||
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.LoggerFactory;
|
||||
|
||||
import de.jotomo.ruffyscripter.commands.Command;
|
||||
import de.jotomo.ruffyscripter.commands.CommandException;
|
||||
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.
|
||||
// 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);
|
||||
CommandResult r = cmdResult;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
|
@ -9,6 +9,7 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
|
||||
import de.jotomo.ruffyscripter.RuffyScripter;
|
||||
|
||||
|
@ -197,12 +198,11 @@ public class SetTbrCommand implements Command {
|
|||
while (System.currentTimeMillis() < inTwoSeconds && !alertProcessed) {
|
||||
if (scripter.currentMenu.getType() == MenuType.WARNING_OR_ERROR) {
|
||||
// check the raised alarm is TBR CANCELLED
|
||||
int errorCode = (int) scripter.currentMenu.getAttribute(MenuAttribute.ERROR);
|
||||
String errorMsg = (String) scripter.currentMenu.getAttribute(MenuAttribute.MESSAGE);
|
||||
if (errorCode != 6 || errorMsg.equals("TBR CANCELLED")) {
|
||||
PumpAlert alert = scripter.readDisplayPumpAlert();
|
||||
if (alert.code != 6 || Objects.equals(alert.msg, "TBR CANCELLED")) {
|
||||
throw new CommandException().success(false).enacted(false)
|
||||
.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"
|
||||
scripter.pressCheckKey();
|
||||
|
|
|
@ -95,13 +95,7 @@ public class ComboPlugin implements PluginBase, PumpInterface {
|
|||
@Override
|
||||
public void onServiceConnected(ComponentName name, IBinder service) {
|
||||
ruffyScripter = new RuffyScripter(IRuffyService.Stub.asInterface(service));
|
||||
log.debug("ruffy serivce connected, fetching initial pump state");
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
readPumpState(false);
|
||||
}
|
||||
}).start();
|
||||
log.debug("ruffy serivce connected");
|
||||
}
|
||||
|
||||
@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() {
|
||||
pumpDescription.isBolusCapable = true;
|
||||
pumpDescription.bolusStep = 0.1d;
|
||||
|
@ -280,7 +263,7 @@ public class ComboPlugin implements PluginBase, PumpInterface {
|
|||
if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) {
|
||||
PumpEnactResult result = new PumpEnactResult();
|
||||
if (detailedBolusInfo.insulin > 0) {
|
||||
CommandResult bolusCmdResult = runCommand(new BolusCommand(detailedBolusInfo.insulin), false);
|
||||
CommandResult bolusCmdResult = runCommand(new BolusCommand(detailedBolusInfo.insulin));
|
||||
result.success = bolusCmdResult.success;
|
||||
result.enacted = bolusCmdResult.enacted;
|
||||
// TODO if no error occurred, the requested bolus is what the pump delievered,
|
||||
|
@ -316,22 +299,19 @@ 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
|
||||
// will be required when doing multiple commands in sequence.
|
||||
// Alternatively provide 'composite commands' to return everything needed in one go?
|
||||
try {
|
||||
long timeout = System.currentTimeMillis() + 10 * 1000;
|
||||
while (ruffyScripter.isPumpBusy()) {
|
||||
if (System.currentTimeMillis() < timeout) {
|
||||
return new CommandResult().success(false).enacted(false).message("Timeout waiting for the pump to be ready");
|
||||
}
|
||||
SystemClock.sleep(200);
|
||||
CommandResult commandResult = ruffyScripter.runCommand(command);
|
||||
if (commandResult.success && commandResult.state != null) {
|
||||
pumpState = commandResult.state;
|
||||
}
|
||||
return ruffyScripter.runCommand(command);
|
||||
return commandResult;
|
||||
} finally {
|
||||
lastCmdTime = new Date();
|
||||
if (!keepConnectionOpen) {
|
||||
ruffyScripter.disconnect();
|
||||
}
|
||||
ruffyScripter.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -351,13 +331,6 @@ public class ComboPlugin implements PluginBase, PumpInterface {
|
|||
if (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;
|
||||
if (activeTbrPercentage != -1 && Math.abs(activeTbrPercentage - roundedPercentage) <= 20) {
|
||||
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;
|
||||
}
|
||||
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) {
|
||||
TemporaryBasal tempStart = new TemporaryBasal(System.currentTimeMillis());
|
||||
tempStart.durationInMinutes = durationInMinutes;
|
||||
|
@ -398,8 +371,6 @@ public class ComboPlugin implements PluginBase, PumpInterface {
|
|||
treatmentsInterface.addToHistoryTempBasal(tempStart);
|
||||
}
|
||||
|
||||
readPumpState(false);
|
||||
|
||||
PumpEnactResult pumpEnactResult = new PumpEnactResult();
|
||||
pumpEnactResult.success = commandResult.success;
|
||||
pumpEnactResult.enacted = commandResult.enacted;
|
||||
|
@ -421,7 +392,7 @@ public class ComboPlugin implements PluginBase, PumpInterface {
|
|||
public PumpEnactResult cancelTempBasal() {
|
||||
log.debug("cancelTempBasal called");
|
||||
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) {
|
||||
TemporaryBasal tempStop = new TemporaryBasal(System.currentTimeMillis());
|
||||
tempStop.durationInMinutes = 0; // ending temp basal
|
||||
|
@ -430,8 +401,6 @@ public class ComboPlugin implements PluginBase, PumpInterface {
|
|||
treatmentsInterface.addToHistoryTempBasal(tempStop);
|
||||
}
|
||||
|
||||
readPumpState(false);
|
||||
|
||||
PumpEnactResult pumpEnactResult = new PumpEnactResult();
|
||||
pumpEnactResult.success = commandResult.success;
|
||||
pumpEnactResult.enacted = commandResult.enacted;
|
||||
|
|
Loading…
Reference in a new issue