When checking for changed pump history, compare the last two records.

This ensures changes are also detected if a bolus was added on the
pump within the same minute of the previous record.
This commit is contained in:
Johannes Mockenhaupt 2018-02-03 20:03:30 +01:00
parent 1e8e2a59fd
commit 183edfcc09
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1

View file

@ -11,8 +11,10 @@ import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.List;
import info.nightscout.androidaps.BuildConfig; import info.nightscout.androidaps.BuildConfig;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
@ -99,6 +101,7 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
private volatile boolean bolusInProgress; private volatile boolean bolusInProgress;
private volatile boolean cancelBolus; private volatile boolean cancelBolus;
/** Used to reject boluses with the same amount requested within two minutes */
private Bolus lastRequestedBolus; private Bolus lastRequestedBolus;
/** /**
@ -106,10 +109,12 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
* records on the pump have been found. This effectively blocks high temps and boluses * records on the pump have been found. This effectively blocks high temps and boluses
* till the queue is empty and the connection is shut down. The next reconnect will * till the queue is empty and the connection is shut down. The next reconnect will
* then reset this flag. This might cause some grief when attempting to bolus again within * then reset this flag. This might cause some grief when attempting to bolus again within
* the 5s of idling it takes before the connecting is shut down. * the 5s of idling it takes before the connecting is shut down. Or if the queue is very
* large, giving the user more time to input boluses. I don't have a good solution for this
* at the moment, but this is edge-casey to not address this at this point.
*/ */
private volatile boolean pumpHistoryChanged = false; private volatile boolean pumpHistoryChanged = false;
private volatile long timestampOfLastKnownPumpBolusRecord; private volatile List<Bolus> recentBoluses = new ArrayList<>(0);
public static ComboPlugin getPlugin() { public static ComboPlugin getPlugin() {
if (plugin == null) if (plugin == null)
@ -568,7 +573,7 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
// history below to see what was actually delivered // history below to see what was actually delivered
// get last bolus from pump history for verification // get last bolus from pump history for verification
CommandResult postBolusStateResult = runCommand(null, 3, () -> ruffyScripter.readQuickInfo(1)); CommandResult postBolusStateResult = runCommand(null, 3, () -> ruffyScripter.readQuickInfo(2));
if (!postBolusStateResult.success) { if (!postBolusStateResult.success) {
return new PumpEnactResult().success(false).enacted(false) return new PumpEnactResult().success(false).enacted(false)
.comment(MainApp.gs(R.string.combo_error_bolus_verification_failed)); .comment(MainApp.gs(R.string.combo_error_bolus_verification_failed));
@ -594,7 +599,7 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
return new PumpEnactResult().success(false).enacted(true) return new PumpEnactResult().success(false).enacted(true)
.comment(MainApp.gs(R.string.combo_error_updating_treatment_record)); .comment(MainApp.gs(R.string.combo_error_updating_treatment_record));
timestampOfLastKnownPumpBolusRecord = lastPumpBolus.timestamp; recentBoluses = postBolusStateResult.history.bolusHistory;
// only a partial bolus was delivered // only a partial bolus was delivered
if (Math.abs(lastPumpBolus.amount - detailedBolusInfo.insulin) > 0.01) { if (Math.abs(lastPumpBolus.amount - detailedBolusInfo.insulin) > 0.01) {
@ -1154,26 +1159,36 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
* @return null on success or the failed command result * @return null on success or the failed command result
*/ */
private CommandResult checkHistory() { private CommandResult checkHistory() {
CommandResult quickInfoResult = runCommand(MainApp.gs(R.string.combo_activity_checking_for_history_changes), 3, ruffyScripter::readQuickInfo); CommandResult quickInfoResult = runCommand(MainApp.gs(R.string.combo_activity_checking_for_history_changes), 3, () -> ruffyScripter.readQuickInfo(2));
if (quickInfoResult.history != null && !quickInfoResult.history.bolusHistory.isEmpty()
&& quickInfoResult.history.bolusHistory.get(0).timestamp == timestampOfLastKnownPumpBolusRecord) { // no history, nothing to check or complain about
if (quickInfoResult.history == null || quickInfoResult.history.bolusHistory.isEmpty()) {
return null; return null;
} }
// OPTIMIZE this reads the entire history on start, so this could be optimized by persisting // compare recent records
// `timestampOfLastKnownPumpBolusRecord`, though this should be thought through, to make sure List<Bolus> existingbolusHistory = quickInfoResult.history.bolusHistory;
// all scenarios are covered if (existingbolusHistory.size() == 1 && recentBoluses.size() == 1
&& quickInfoResult.history.bolusHistory.get(0).equals(recentBoluses.get(0))) {
return null;
} else if (existingbolusHistory.size() == 2 && recentBoluses.size() == 2
&& quickInfoResult.history.bolusHistory.get(0).equals(recentBoluses.get(0))
&& quickInfoResult.history.bolusHistory.get(1).equals(recentBoluses.get(1))) {
return null;
}
// fetch new records
CommandResult historyResult = runCommand(MainApp.gs(R.string.combo_activity_reading_pump_history), 3, () -> CommandResult historyResult = runCommand(MainApp.gs(R.string.combo_activity_reading_pump_history), 3, () ->
ruffyScripter.readHistory(new PumpHistoryRequest() ruffyScripter.readHistory(new PumpHistoryRequest().bolusHistory(existingbolusHistory.get(0).timestamp)));
.bolusHistory(timestampOfLastKnownPumpBolusRecord)));
if (!historyResult.success) { if (!historyResult.success) {
return historyResult; return historyResult;
} }
pumpHistoryChanged = updateDbFromPumpHistory(historyResult.history); pumpHistoryChanged = updateDbFromPumpHistory(historyResult.history);
if (!historyResult.history.bolusHistory.isEmpty()) { List<Bolus> updatedBolusHistory = historyResult.history.bolusHistory;
timestampOfLastKnownPumpBolusRecord = historyResult.history.bolusHistory.get(0).timestamp; if (!updatedBolusHistory.isEmpty()) {
recentBoluses = updatedBolusHistory.subList(0, Math.min(updatedBolusHistory.size(), 2));
} }
return null; return null;