Initial take on bolus progress reporting.

This commit is contained in:
Johannes Mockenhaupt 2017-08-15 20:20:30 +02:00
parent a230501f74
commit c1ecad1ed6
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1
3 changed files with 62 additions and 4 deletions

View file

@ -14,12 +14,18 @@ import java.util.Locale;
import de.jotomo.ruffyscripter.PumpState;
import de.jotomo.ruffyscripter.RuffyScripter;
import static de.jotomo.ruffyscripter.commands.ProgressReportCallback.State.BOLUSING;
import static de.jotomo.ruffyscripter.commands.ProgressReportCallback.State.FINISHED;
import static de.jotomo.ruffyscripter.commands.ProgressReportCallback.State.PREPARING;
public class BolusCommand implements Command {
private static final Logger log = LoggerFactory.getLogger(BolusCommand.class);
private final double bolus;
private final ProgressReportCallback progressReportCallback;
public BolusCommand(double bolus) {
public BolusCommand(double bolus, ProgressReportCallback progressReportCallback) {
this.progressReportCallback = progressReportCallback;
this.bolus = bolus;
}
@ -36,6 +42,7 @@ public class BolusCommand implements Command {
@Override
public CommandResult execute(RuffyScripter scripter, PumpState initialPumpState) {
progressReportCallback.progress(PREPARING, 0, 0);
try {
enterBolusMenu(scripter);
@ -45,6 +52,7 @@ public class BolusCommand implements Command {
// confirm bolus
scripter.verifyMenuIsDisplayed(MenuType.BOLUS_ENTER);
scripter.pressCheckKey();
progressReportCallback.progress(BOLUSING, 0, 0);
// the pump displays the entered bolus and waits a bit to let user check and cancel
scripter.waitForMenuToBeLeft(MenuType.BOLUS_ENTER);
@ -56,9 +64,15 @@ public class BolusCommand implements Command {
// wait for bolus delivery to complete; the remaining units to deliver are counted
// down and are displayed on the main menu.
Double bolusRemaining = (Double) scripter.currentMenu.getAttribute(MenuAttribute.BOLUS_REMAINING);
double lastBolusReported = 0;
while (bolusRemaining != null) {
log.debug("Delivering bolus, remaining: " + bolusRemaining);
SystemClock.sleep(200);
if (lastBolusReported != bolusRemaining) {
log.debug("Delivering bolus, remaining: " + bolusRemaining);
int percentDelivered = (int) (100 - (bolusRemaining / bolus * 100));
progressReportCallback.progress(BOLUSING, percentDelivered, bolus - bolusRemaining);
lastBolusReported = bolusRemaining;
}
SystemClock.sleep(50);
bolusRemaining = (Double) scripter.currentMenu.getAttribute(MenuAttribute.BOLUS_REMAINING);
}
@ -70,6 +84,8 @@ public class BolusCommand implements Command {
"Bolus delivery did not complete as expected. "
+ "Check pump manually, the bolus might not have been delivered.");
progressReportCallback.progress(FINISHED, 100, bolus);
// read last bolus record; those menus display static data and therefore
// only a single menu update is sent
scripter.navigateToMenu(MenuType.MY_DATA_MENU);

View file

@ -0,0 +1,13 @@
package de.jotomo.ruffyscripter.commands;
public interface ProgressReportCallback {
enum State {
PREPARING,
BOLUSING,
CANCELLING,
FINISHED,
CANCELLED
}
void progress(State state, int percent, double delivered);
}

View file

@ -31,6 +31,7 @@ import de.jotomo.ruffyscripter.commands.BolusCommand;
import de.jotomo.ruffyscripter.commands.CancelTbrCommand;
import de.jotomo.ruffyscripter.commands.Command;
import de.jotomo.ruffyscripter.commands.CommandResult;
import de.jotomo.ruffyscripter.commands.ProgressReportCallback;
import de.jotomo.ruffyscripter.commands.DetermineCapabilitiesCommand;
import de.jotomo.ruffyscripter.commands.ReadPumpStateCommand;
import de.jotomo.ruffyscripter.commands.SetTbrCommand;
@ -48,6 +49,7 @@ import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PumpDescription;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.Overview.events.EventOverviewBolusProgress;
import info.nightscout.androidaps.plugins.PumpCombo.events.EventComboPumpUpdateGUI;
import info.nightscout.utils.DateUtil;
import info.nightscout.utils.SP;
@ -369,7 +371,34 @@ public class ComboPlugin implements PluginBase, PumpInterface {
if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) {
if (detailedBolusInfo.insulin > 0) {
// bolus needed, ask pump to deliver it
CommandResult bolusCmdResult = runCommand(new BolusCommand(detailedBolusInfo.insulin));
EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance();
MainApp.bus().post(bolusingEvent);
CommandResult bolusCmdResult = runCommand(new BolusCommand(detailedBolusInfo.insulin, new ProgressReportCallback() {
@Override
public void progress(State state, int percent, double delivered) {
EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance();
switch (state) {
// TODO move into enum as toString or so and make it translateb
case BOLUSING:
bolusingEvent.status = String.format(MainApp.sResources.getString(R.string.bolusdelivering), delivered);
break;
case PREPARING:
bolusingEvent.status = "Preparing pump for bolus";
break;
case FINISHED:
bolusingEvent.status = "Bolus delivery finished successfully";
break;
case CANCELLED:
bolusingEvent.status = "Bolus delivery was cancelled";
break;
case CANCELLING:
bolusingEvent.status = "Cancelling bolus delivery";
break;
}
bolusingEvent.percent = percent;
MainApp.bus().post(bolusingEvent);
}
}));
PumpEnactResult pumpEnactResult = new PumpEnactResult();
pumpEnactResult.success = bolusCmdResult.success;
pumpEnactResult.enacted = bolusCmdResult.enacted;