Combo: only reject bolus requests if the requested bolus was delivered

recently

Since history is being read now (also just before starting to bolus),
it's safe to only reject boluses if the previous request actually
lead to insulin delivery.

Fixes #700.
This commit is contained in:
Johannes Mockenhaupt 2018-03-18 00:07:49 +01:00
parent 17219c08d1
commit a2ac21ea67
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1

View file

@ -115,12 +115,6 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
* will reset this flag. */ * will reset this flag. */
private volatile boolean cancelBolus; private volatile boolean cancelBolus;
/** Used to reject boluses with the same amount requested within two minutes.
* Used solely by {@link #deliverBolus(DetailedBolusInfo)}. This is independent of the
* pump history and is meant as a safety feature to block multiple requests due to an
* application bug. Whether the requested bolus was delivered once is not taken into account. */
private Bolus lastRequestedBolus;
/** /**
* This is set (in {@link #checkHistory()} whenever a connection to the pump is made and * This is set (in {@link #checkHistory()} whenever a connection to the pump is made and
* indicates if new history records on the pump have been found. This effectively blocks * indicates if new history records on the pump have been found. This effectively blocks
@ -538,18 +532,6 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
pump.activity = MainApp.gs(R.string.combo_pump_action_bolusing, detailedBolusInfo.insulin); pump.activity = MainApp.gs(R.string.combo_pump_action_bolusing, detailedBolusInfo.insulin);
MainApp.bus().post(new EventComboPumpUpdateGUI()); MainApp.bus().post(new EventComboPumpUpdateGUI());
// Guard against boluses issued multiple times within two minutes.
// Two minutes, so that the resulting timestamp and bolus are different with the Combo
// history records which only store with minute-precision
if (lastRequestedBolus != null
&& Math.abs(lastRequestedBolus.amount - detailedBolusInfo.insulin) < 0.01
&& lastRequestedBolus.timestamp + 120 * 1000 > System.currentTimeMillis()) {
log.error("Bolus request rejected, same bolus requested recently: " + lastRequestedBolus);
return new PumpEnactResult().success(false).enacted(false)
.comment(MainApp.gs(R.string.bolus_frequency_exceeded));
}
lastRequestedBolus = new Bolus(System.currentTimeMillis(), detailedBolusInfo.insulin, true);
// check pump is ready and all pump bolus records are known // check pump is ready and all pump bolus records are known
CommandResult stateResult = runCommand(null, 2, () -> ruffyScripter.readQuickInfo(1)); CommandResult stateResult = runCommand(null, 2, () -> ruffyScripter.readQuickInfo(1));
if (!stateResult.success) { if (!stateResult.success) {
@ -570,6 +552,15 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
? stateResult.history.bolusHistory.get(0) ? stateResult.history.bolusHistory.get(0)
: new Bolus(0, 0, false); : new Bolus(0, 0, false);
// reject a bolus if one with the exact same size was successfully delivered
// within the last 1-2 minutes
if (Math.abs(previousBolus.amount - detailedBolusInfo.insulin) < 0.01
&& previousBolus.timestamp + 60 * 1000 > System.currentTimeMillis()) {
log.debug("Bolu request rejected, same bolus was successfully delivered very recently");
return new PumpEnactResult().success(false).enacted(false)
.comment(MainApp.gs(R.string.bolus_frequency_exceeded));
}
// if the last bolus was given in the current minute, wait till the pump clock moves // if the last bolus was given in the current minute, wait till the pump clock moves
// to the next minute to ensure timestamps are unique and can be imported // to the next minute to ensure timestamps are unique and can be imported
CommandResult timeCheckResult = stateResult; CommandResult timeCheckResult = stateResult;