diff --git a/TODO-Combo.md b/TODO-Combo.md index 74792b375b..063138d1ea 100644 --- a/TODO-Combo.md +++ b/TODO-Combo.md @@ -1,7 +1,8 @@ - [ ] Bugs - [ ] Taking over benign warnings on connect doesn't work properly (Notification raised but not confirmed?) - - [ ] Reading full history multiple times duplicates entries shown in Stats/TDD dialog + - [x] Optimization reading full history doesn't seem to work + - [x] Reading full history multiple times duplicates entries shown in Stats/TDD dialog - [x] ruffy: Accessing the quick info menu yields noMenu when cartridge is low - [x] ruffy: Multi-digit error codes in error history aren't supported - [-] No connection can be established anymore; ruffy issue i can't solve diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboErrorHistoryDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboErrorHistoryDialog.java index 1099f9fd76..051e70d9c4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboErrorHistoryDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboErrorHistoryDialog.java @@ -21,7 +21,7 @@ public class ComboErrorHistoryDialog extends DialogFragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View layout = inflater.inflate(R.layout.combo_error_history_fragment, container, false); TextView text = (TextView) layout.findViewById(R.id.combo_error_history_text); - List errors = ComboPlugin.getPlugin().getPump().history.pumpErrorHistory; + List errors = ComboPlugin.getPlugin().getPump().errorHistory; StringBuilder sb = new StringBuilder(); // TODO i18n SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM. HH:mm"); 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 1651e33ab8..2f5b0ab867 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 @@ -868,9 +868,25 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf return false; } - updateDbFromPumpHistory(historyResult.history); - CommandResult reservoirBolusResult = runCommand(null, 3, ruffyScripter::readReservoirLevelAndLastBolus); - return historyResult.success && reservoirBolusResult.success; + // update local cache + PumpHistory history = historyResult.history; + if (!history.bolusHistory.isEmpty()) { + pump.lastHistoryBolusTime = history.bolusHistory.get(0).timestamp; + } + if (!history.tbrHistory.isEmpty()) { + pump.lastHistoryTbrTime = history.tbrHistory.get(0).timestamp; + } + + if (!history.pumpErrorHistory.isEmpty()) { + pump.errorHistory = history.pumpErrorHistory; + } + if (!history.tddHistory.isEmpty()) { + pump.tddHistory = history.tddHistory; + } + + updateDbFromPumpHistory(history); + + return historyResult.success; } private synchronized void updateDbFromPumpHistory(@NonNull PumpHistory history) { @@ -905,34 +921,27 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf MainApp.getConfigBuilder().addToHistoryTempBasal(temporaryBasal); } } - - // errors (not persisted in DB, read from pump on start up and cached in plugin) - for (PumpError pumpError : history.pumpErrorHistory) { - if (!pump.history.pumpErrorHistory.contains(pumpError)) { - pump.history.pumpErrorHistory.add(pumpError); - } - } - - // TDDs (not persisted in DB, read from pump on start up and cached in plugin) - for (Tdd tdd : history.tddHistory) { - if (!pump.history.tddHistory.contains(tdd)) { - pump.history.tddHistory.add(tdd); - } - } } - // TODO, opt doesn't seem to work void readAllPumpData() { readHistory(new PumpHistoryRequest() - .bolusHistory(pump.history.bolusHistory.isEmpty() ? PumpHistoryRequest.FULL : pump.history.bolusHistory.get(0).timestamp) - .tbrHistory(pump.history.tbrHistory.isEmpty() ? PumpHistoryRequest.FULL : pump.history.tbrHistory.get(0).timestamp) - .pumpErrorHistory(pump.history.pumpErrorHistory.isEmpty() ? PumpHistoryRequest.FULL : pump.history.pumpErrorHistory.get(0).timestamp) - .tddHistory(pump.history.tddHistory.isEmpty() ? PumpHistoryRequest.FULL : pump.history.tddHistory.get(0).timestamp)); - CommandResult commandResult = runCommand("Reading basal profile", 2, - ruffyScripter::readBasalProfile); - if (commandResult.success) { - pump.basalProfile = commandResult.basalProfile; + .bolusHistory(pump.lastHistoryBolusTime) + .tbrHistory(pump.lastHistoryTbrTime) + .pumpErrorHistory(PumpHistoryRequest.FULL) + .tddHistory(PumpHistoryRequest.FULL)); + + CommandResult reservoirResult = runCommand("Checking reservoir level", 2, + ruffyScripter::readReservoirLevelAndLastBolus); + if (!reservoirResult.success) { + return; } + + CommandResult basalResult = runCommand("Reading basal profile", 2, ruffyScripter::readBasalProfile); + if (!basalResult.success) { + return; + } + + pump.basalProfile = basalResult.basalProfile; } @Override 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 be862e318e..fd23cdd49d 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,11 +3,17 @@ package info.nightscout.androidaps.plugins.PumpCombo; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import java.util.ArrayList; +import java.util.List; + import de.jotomo.ruffy.spi.BasalProfile; import de.jotomo.ruffy.spi.CommandResult; import de.jotomo.ruffy.spi.PumpState; import de.jotomo.ruffy.spi.history.Bolus; +import de.jotomo.ruffy.spi.history.PumpError; import de.jotomo.ruffy.spi.history.PumpHistory; +import de.jotomo.ruffy.spi.history.PumpHistoryRequest; +import de.jotomo.ruffy.spi.history.Tdd; class ComboPump { boolean initialized = false; @@ -22,8 +28,15 @@ class ComboPump { volatile Bolus lastBolus = null; @NonNull volatile BasalProfile basalProfile = new BasalProfile(); - @NonNull - volatile PumpHistory history = new PumpHistory(); + /** Time the active TBR was set (if any). Needed to calculate remaining time in fragment */ long tbrSetTime; + + // Last known history record times to skip over old ones when reading history + long lastHistoryBolusTime = PumpHistoryRequest.FULL; + long lastHistoryTbrTime = PumpHistoryRequest.FULL; + + // Alert and TDD histories are not stored in DB, but are read on demand and just cached here + List errorHistory = new ArrayList<>(0); + List tddHistory = new ArrayList<>(0); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboTddHistoryDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboTddHistoryDialog.java index edb28cc98f..c69fb5bfe5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboTddHistoryDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboTddHistoryDialog.java @@ -21,7 +21,7 @@ public class ComboTddHistoryDialog extends DialogFragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View layout = inflater.inflate(R.layout.combo_tdd_history_fragment, container, false); TextView text = (TextView) layout.findViewById(R.id.combo_tdd_history_text); - List tdds = ComboPlugin.getPlugin().getPump().history.tddHistory; + List tdds = ComboPlugin.getPlugin().getPump().tddHistory; StringBuilder sb = new StringBuilder(); // TODO i18n SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM."); 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 675f8e93ad..72d3380946 100644 --- a/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/commands/ReadHistoryCommand.java +++ b/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/commands/ReadHistoryCommand.java @@ -138,11 +138,11 @@ public class ReadHistoryCommand extends BaseCommand { int record = (int) scripter.getCurrentMenu().getAttribute(MenuAttribute.CURRENT_RECORD); int totalRecords = (int) scripter.getCurrentMenu().getAttribute(MenuAttribute.TOTAL_RECORD); while (true) { - log.debug("Reading TDD record #" + record + "/" + totalRecords); Tdd tdd = readTddRecord(); if (requestedTime != PumpHistoryRequest.FULL && tdd.timestamp <= requestedTime) { break; } + log.debug("Read TDD record #" + record + "/" + totalRecords); history.tddHistory.add(tdd); log.debug("Parsed " + scripter.getCurrentMenu().toString() + " => " + tdd); if (record == totalRecords) { @@ -174,11 +174,11 @@ public class ReadHistoryCommand extends BaseCommand { int record = (int) scripter.getCurrentMenu().getAttribute(MenuAttribute.CURRENT_RECORD); int totalRecords = (int) scripter.getCurrentMenu().getAttribute(MenuAttribute.TOTAL_RECORD); while (true) { - log.debug("Reading TBR record #" + record + "/" + totalRecords); Tbr tbr = readTbrRecord(); if (requestedTime != PumpHistoryRequest.FULL && tbr.timestamp <= requestedTime) { break; } + log.debug("Read TBR record #" + record + "/" + totalRecords); history.tbrHistory.add(tbr); log.debug("Parsed " + scripter.getCurrentMenu().toString() + " => " + tbr); if (record == totalRecords) { @@ -204,11 +204,11 @@ public class ReadHistoryCommand extends BaseCommand { int record = (int) scripter.getCurrentMenu().getAttribute(MenuAttribute.CURRENT_RECORD); int totalRecords = (int) scripter.getCurrentMenu().getAttribute(MenuAttribute.TOTAL_RECORD); while (true) { - log.debug("Reading bolus record #" + record + "/" + totalRecords); Bolus bolus = readBolusRecord(); if (requestedTime != PumpHistoryRequest.FULL && bolus.timestamp <= requestedTime) { break; } + log.debug("Read bolus record #" + record + "/" + totalRecords); history.bolusHistory.add(bolus); log.debug("Parsed " + scripter.getCurrentMenu().toString() + " => " + bolus); if (record == totalRecords) { @@ -234,11 +234,11 @@ public class ReadHistoryCommand extends BaseCommand { int record = (int) scripter.getCurrentMenu().getAttribute(MenuAttribute.CURRENT_RECORD); int totalRecords = (int) scripter.getCurrentMenu().getAttribute(MenuAttribute.TOTAL_RECORD); while (true) { - log.debug("Reading error record #" + record + "/" + totalRecords); PumpError error = readErrorRecord(); if (requestedTime != PumpHistoryRequest.FULL && error.timestamp <= requestedTime) { break; } + log.debug("Read error record #" + record + "/" + totalRecords); history.pumpErrorHistory.add(error); log.debug("Parsed " + scripter.getCurrentMenu().toString() + " => " + error); if (record == totalRecords) {