From 8b8a13580480415400523ad51d3671a0567d0cad Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Fri, 20 Oct 2017 00:57:14 +0200 Subject: [PATCH] Add (back) last bolus to ComboFragment. --- .../plugins/PumpCombo/ComboFragment.java | 23 +++++++--- .../plugins/PumpCombo/ComboPlugin.java | 12 ++++- .../plugins/PumpCombo/ComboPump.java | 5 ++- .../main/res/layout/combopump_fragment.xml | 45 +++++++++++++++++++ .../ruffyscripter/commands/BolusCommand.java | 8 +++- .../commands/ReadHistoryCommand.java | 34 +++++++++----- .../ruffy/driver/display/menu/MenuDate.java | 8 ++++ 7 files changed, 112 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboFragment.java index 95d887690d..10d45a19c5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboFragment.java @@ -4,7 +4,6 @@ package info.nightscout.androidaps.plugins.PumpCombo; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; -import android.os.SystemClock; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -16,14 +15,11 @@ import com.squareup.otto.Subscribe; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Date; - import de.jotomo.ruffy.spi.CommandResult; import de.jotomo.ruffy.spi.PumpState; import de.jotomo.ruffy.spi.history.Bolus; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.plugins.Common.SubscriberFragment; import info.nightscout.androidaps.plugins.PumpCombo.events.EventComboPumpUpdateGUI; import info.nightscout.utils.DateUtil; @@ -36,6 +32,7 @@ public class ComboFragment extends SubscriberFragment implements View.OnClickLis private TextView batteryView; private TextView reservoirView; private TextView lastConnectionView; + private TextView lastBolusView; private TextView tempBasalText; private Button refresh; @@ -49,6 +46,7 @@ public class ComboFragment extends SubscriberFragment implements View.OnClickLis batteryView = (TextView) view.findViewById(R.id.combo_pumpstate_battery); reservoirView = (TextView) view.findViewById(R.id.combo_insulinstate); lastConnectionView = (TextView) view.findViewById(R.id.combo_lastconnection); + lastBolusView = (TextView) view.findViewById(R.id.combo_last_bolus); tempBasalText = (TextView) view.findViewById(R.id.combo_temp_basal); refresh = (Button) view.findViewById(R.id.combo_refresh); @@ -111,7 +109,7 @@ public class ComboFragment extends SubscriberFragment implements View.OnClickLis } if (plugin.getPump().state.errorMsg != null) { statusView.setTextColor(Color.RED); - } else if (plugin.getPump().state.suspended ) { + } else if (plugin.getPump().state.suspended) { statusView.setTextColor(Color.YELLOW); } else { statusView.setTextColor(Color.WHITE); @@ -140,7 +138,7 @@ public class ComboFragment extends SubscriberFragment implements View.OnClickLis } else { reservoirView.setTextColor(Color.WHITE); } - int reservoirLevel = plugin.getPump().history.reservoirLevel; + int reservoirLevel = plugin.getPump().reservoirLevel; reservoirView.setText(reservoirLevel == -1 ? "" : "" + reservoirLevel + " U"); // last connection @@ -148,6 +146,19 @@ public class ComboFragment extends SubscriberFragment implements View.OnClickLis String time = DateUtil.timeString(lastCmdResult.completionTime); lastConnectionView.setText("" + minAgo + " (" + time + ")"); + // last bolus + plugin.getPump().history.bolusHistory.add(new Bolus(System.currentTimeMillis() - 7 * 60 * 1000, 12.8d)); + Bolus bolus = plugin.getPump().lastBolus; + if (bolus == null || bolus.timestamp + 6 * 60 * 60 * 1000 < System.currentTimeMillis()) { + lastBolusView.setText(""); + } else { + long agoMsc = System.currentTimeMillis() - bolus.timestamp; + double agoHours = agoMsc / 60d / 60d / 1000d; + lastBolusView.setText(DecimalFormatter.to2Decimal(bolus.amount) + " U " + + "(" + DecimalFormatter.to1Decimal(agoHours) + " " + MainApp.sResources.getString(R.string.hoursago) + ", " + + DateUtil.timeString(bolus.timestamp) + ") "); + } + // TBR boolean tbrActive = ps.tbrPercent != -1 && ps.tbrPercent != 100; if (tbrActive) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java index b284e1d140..195217eed6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java @@ -19,6 +19,7 @@ import de.jotomo.ruffy.spi.BolusProgressReporter; import de.jotomo.ruffy.spi.CommandResult; import de.jotomo.ruffy.spi.PumpState; import de.jotomo.ruffy.spi.RuffyCommands; +import de.jotomo.ruffy.spi.history.Bolus; import de.jotomo.ruffy.spi.history.PumpHistoryRequest; import de.jotomo.ruffyscripter.RuffyCommandsV1Impl; import info.nightscout.androidaps.BuildConfig; @@ -562,11 +563,18 @@ public class ComboPlugin implements PluginBase, PumpInterface { // TODO handle running into WARNING_OR_ERROR ... or scripter? purge it CommandResult commandResult = commandExecution.execute(); pump.lastCmdResult = commandResult; - pump.lastCmdResult.completionTime = System.currentTimeMillis(); // todo pump.state = commandResult.state; // TOOD - if (commandResult.history != null) + if (commandResult.history != null) { + if (commandResult.history.reservoirLevel != -1) { + pump.reservoirLevel = commandResult.history.reservoirLevel; + } pump.history = commandResult.history; + if (pump.history.bolusHistory.size() > 0) { + pump.lastBolus = pump.history.bolusHistory.get(0); + } + } + MainApp.bus().post(new EventComboPumpUpdateGUI()); return commandResult; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPump.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPump.java index 2a40fd66fa..1b6c29a008 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPump.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPump.java @@ -3,10 +3,10 @@ package info.nightscout.androidaps.plugins.PumpCombo; import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import java.util.Date; import de.jotomo.ruffy.spi.PumpState; import de.jotomo.ruffy.spi.CommandResult; +import de.jotomo.ruffy.spi.history.Bolus; import de.jotomo.ruffy.spi.history.PumpHistory; class ComboPump { @@ -16,4 +16,7 @@ class ComboPump { volatile PumpState state = new PumpState(); @NonNull volatile PumpHistory history = new PumpHistory(); + @Nullable + volatile Bolus lastBolus; + volatile int reservoirLevel = -1; } diff --git a/app/src/main/res/layout/combopump_fragment.xml b/app/src/main/res/layout/combopump_fragment.xml index 30249f37ea..a637a48c41 100644 --- a/app/src/main/res/layout/combopump_fragment.xml +++ b/app/src/main/res/layout/combopump_fragment.xml @@ -190,6 +190,51 @@ + + + + + + + + + + + + boluses = new ArrayList<>(); + boluses.add(new Bolus(System.currentTimeMillis(), bolus)); return new CommandResult().success(true).enacted(true) - .message(String.format(Locale.US, "Delivered %02.1f U", bolus)); + .message(String.format(Locale.US, "Delivered %02.1f U", bolus)) + .history(new PumpHistory().bolusHistory(boluses)); } catch (CommandException e) { return e.toCommandResult(); } diff --git a/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/commands/ReadHistoryCommand.java b/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/commands/ReadHistoryCommand.java index 92ab430821..6621b66548 100644 --- a/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/commands/ReadHistoryCommand.java +++ b/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/commands/ReadHistoryCommand.java @@ -7,6 +7,7 @@ import org.monkey.d.ruffy.ruffy.driver.display.menu.MenuDate; import org.monkey.d.ruffy.ruffy.driver.display.menu.MenuTime; import java.util.Collections; +import java.util.Date; import java.util.List; import de.jotomo.ruffy.spi.CommandResult; @@ -36,25 +37,33 @@ public class ReadHistoryCommand extends BaseCommand { scripter.pressCheckKey(); scripter.verifyMenuIsDisplayed(MenuType.BOLUS_DATA); if (request.bolusHistory != PumpHistoryRequest.SKIP) { - // Could also be extended, multiwave: - BolusType bolusType = (BolusType) scripter.getCurrentMenu().getAttribute(MenuAttribute.BOLUS_TYPE); - if (!bolusType.equals(BolusType.NORMAL)) { - throw new CommandException().success(false).enacted(false).message("Unsupported bolus type encountered: " + bolusType); - } - Double bolus = (Double) scripter.getCurrentMenu().getAttribute(MenuAttribute.BOLUS); - MenuDate date = (MenuDate) scripter.getCurrentMenu().getAttribute(MenuAttribute.DATE); - MenuTime time = (MenuTime) scripter.getCurrentMenu().getAttribute(MenuAttribute.TIME); + if (request.bolusHistory == PumpHistoryRequest.LAST) { + // Could also be extended, multiwave: + BolusType bolusType = (BolusType) scripter.getCurrentMenu().getAttribute(MenuAttribute.BOLUS_TYPE); + if (!bolusType.equals(BolusType.NORMAL)) { + throw new CommandException().success(false).enacted(false).message("Unsupported bolus type encountered: " + bolusType); + } + Double bolus = (Double) scripter.getCurrentMenu().getAttribute(MenuAttribute.BOLUS); + MenuDate date = (MenuDate) scripter.getCurrentMenu().getAttribute(MenuAttribute.DATE); + MenuTime time = (MenuTime) scripter.getCurrentMenu().getAttribute(MenuAttribute.TIME); + // TODO handle year changes; if current month == 1 and record date == 12, use $YEAR-1 + int currentMonth = new Date().getMonth() + 1; + int currentYear = new Date().getYear() + 1900; + if (currentMonth == 1 && date.getMonth() == 12) { + currentYear -= 1; + } + long recordDate = new Date(currentYear - 1900, date.getMonth() - 1, date.getDay(), time.getHour(), time.getMinute()).getTime(); + history.bolusHistory.add(new Bolus(recordDate, bolus)); - history.bolusHistory.add(new Bolus(0, bolus)); - - int record = (int) scripter.getCurrentMenu().getAttribute(MenuAttribute.CURRENT_RECORD); - int totalRecords = (int) scripter.getCurrentMenu().getAttribute(MenuAttribute.TOTAL_RECORD); +// int record = (int) scripter.getCurrentMenu().getAttribute(MenuAttribute.CURRENT_RECORD); +// int totalRecords = (int) scripter.getCurrentMenu().getAttribute(MenuAttribute.TOTAL_RECORD); /* read displayed date, bolus, add to history while data > last known, press up to go through history */ + } } scripter.pressMenuKey(); scripter.verifyMenuIsDisplayed(MenuType.ERROR_DATA); @@ -81,6 +90,7 @@ public class ReadHistoryCommand extends BaseCommand { // TODO start or end time? MenuTime time = (MenuTime) scripter.getCurrentMenu().getAttribute(MenuAttribute.TIME); } + scripter.pressBackKey(); scripter.returnToRootMenu(); } scripter.verifyRootMenuIsDisplayed(); diff --git a/ruffyscripter/src/main/java/org/monkey/d/ruffy/ruffy/driver/display/menu/MenuDate.java b/ruffyscripter/src/main/java/org/monkey/d/ruffy/ruffy/driver/display/menu/MenuDate.java index 3a1dc5fd10..51b5d2b9b1 100644 --- a/ruffyscripter/src/main/java/org/monkey/d/ruffy/ruffy/driver/display/menu/MenuDate.java +++ b/ruffyscripter/src/main/java/org/monkey/d/ruffy/ruffy/driver/display/menu/MenuDate.java @@ -20,6 +20,14 @@ public class MenuDate { month = Integer.parseInt(p[1]); } + public int getDay() { + return day; + } + + public int getMonth() { + return month; + } + @Override public String toString() { return day+"."+ String.format("%02d",month)+".";