Clean up bolus delivery code.

This commit is contained in:
Johannes Mockenhaupt 2018-01-29 13:18:49 +01:00
parent 038ea06abc
commit 3adda9bf6e
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1
2 changed files with 14 additions and 17 deletions

View file

@ -493,18 +493,16 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
} }
lastRequestedBolus = new Bolus(System.currentTimeMillis(), detailedBolusInfo.insulin, true); lastRequestedBolus = new Bolus(System.currentTimeMillis(), detailedBolusInfo.insulin, true);
// check pump is ready and all pump bolus records are known
CommandResult stateResult = runCommand(null, 2, ruffyScripter::readReservoirLevelAndLastBolus); CommandResult stateResult = runCommand(null, 2, ruffyScripter::readReservoirLevelAndLastBolus);
long pumpTimeWhenBolusWasRequested = stateResult .state.pumpTime; if (!stateResult.success) {
if (!stateResult.success || pumpTimeWhenBolusWasRequested == 0) {
return new PumpEnactResult().success(false).enacted(false) return new PumpEnactResult().success(false).enacted(false)
.comment(MainApp.gs(R.string.combo_error_no_bolus_delivered)); .comment(MainApp.gs(R.string.combo_error_no_connection_no_bolus_delivered));
} }
if (stateResult.reservoirLevel != -1 && stateResult.reservoirLevel - 0.5 < detailedBolusInfo.insulin) { if (stateResult.reservoirLevel != -1 && stateResult.reservoirLevel - 0.5 < detailedBolusInfo.insulin) {
return new PumpEnactResult().success(false).enacted(false) return new PumpEnactResult().success(false).enacted(false)
.comment(MainApp.gs(R.string.combo_reservoir_level_insufficient_for_bolus)); .comment(MainApp.gs(R.string.combo_reservoir_level_insufficient_for_bolus));
} }
// the commands above ensured a connection was made, which updated this field // the commands above ensured a connection was made, which updated this field
if (pumpHistoryChanged) { if (pumpHistoryChanged) {
return new PumpEnactResult().success(false).enacted(false) return new PumpEnactResult().success(false).enacted(false)
@ -529,32 +527,30 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
() -> ruffyScripter.deliverBolus(detailedBolusInfo.insulin, progressReporter)); () -> ruffyScripter.deliverBolus(detailedBolusInfo.insulin, progressReporter));
bolusInProgress = false; bolusInProgress = false;
// Note that the result of the issues the bolus command is not checked. If there was // Note that the result of the issued bolus command is not checked. If there was
// a connection problem, ruffyscripter tried to recover and we can just check the // a connection problem, ruffyscripter tried to recover and we can just check the
// history below // history below to see what was actually delivered
CommandResult postBolusStateResult = runCommand(null, 3, ruffyScripter::readReservoirLevelAndLastBolus); CommandResult postBolusStateResult = runCommand(null, 3, ruffyScripter::readReservoirLevelAndLastBolus);
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));
} }
Bolus lastPumpBolus = postBolusStateResult.lastBolus;
Bolus lastBolus = postBolusStateResult.lastBolus; if (lastPumpBolus == null || lastPumpBolus.equals(previousBolus)) {
if (lastBolus == null || lastBolus.equals(previousBolus)) {
return new PumpEnactResult().success(false).enacted(false) return new PumpEnactResult().success(false).enacted(false)
.comment(MainApp.gs(R.string.combo_error_no_bolus_delivered)); .comment(MainApp.gs(R.string.combo_error_no_bolus_delivered));
} }
if (Math.abs(lastPumpBolus.amount - detailedBolusInfo.insulin) > 0.01) {
if (Math.abs(lastBolus.amount - detailedBolusInfo.insulin) > 0.01) {
return new PumpEnactResult().success(false).enacted(true) return new PumpEnactResult().success(false).enacted(true)
.comment(MainApp.gs(R.string.combo_error_partial_bolus_delivered)); .comment(MainApp.gs(R.string.combo_error_partial_bolus_delivered));
} }
// seems we actually made it this far, let's add a treatment record // seems we actually made it this far, let's add a treatment record
detailedBolusInfo.date = calculateFakeBolusDate(lastBolus); detailedBolusInfo.date = calculateFakeBolusDate(lastPumpBolus);
detailedBolusInfo.pumpId = calculateFakeBolusDate(lastBolus); detailedBolusInfo.pumpId = calculateFakeBolusDate(lastPumpBolus);
detailedBolusInfo.source = Source.PUMP; detailedBolusInfo.source = Source.PUMP;
detailedBolusInfo.insulin = lastBolus.amount; detailedBolusInfo.insulin = lastPumpBolus.amount;
try { try {
boolean treatmentCreated = MainApp.getConfigBuilder().addToHistoryTreatment(detailedBolusInfo); boolean treatmentCreated = MainApp.getConfigBuilder().addToHistoryTreatment(detailedBolusInfo);
if (!treatmentCreated) { if (!treatmentCreated) {
@ -579,8 +575,8 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
return new PumpEnactResult() return new PumpEnactResult()
.success(true) .success(true)
.enacted(lastBolus.amount > 0) .enacted(lastPumpBolus.amount > 0)
.bolusDelivered(lastBolus.amount) .bolusDelivered(lastPumpBolus.amount)
.carbsDelivered(detailedBolusInfo.carbs); .carbsDelivered(detailedBolusInfo.carbs);
} finally { } finally {
pump.activity = null; pump.activity = null;

View file

@ -858,6 +858,7 @@
<string name="combo_read_full_history_warning">This will read the full history and state of the pump. Everything in \"My Data\" and the basal rate. Boluses and TBRs will be added to Treatments if they don\'t already exist. This can cause entries to be duplicated because the pump\'s time is imprecise. Using this when normally looping with the pump is highly discouraged and reserved for special circumstances. If you still want to do this, long press this button again.\n\nWARNING: this can trigger a bug which causes the pump to reject all connection attempts and requires pressing a button on the pump to recover and should therefore be avoided.</string> <string name="combo_read_full_history_warning">This will read the full history and state of the pump. Everything in \"My Data\" and the basal rate. Boluses and TBRs will be added to Treatments if they don\'t already exist. This can cause entries to be duplicated because the pump\'s time is imprecise. Using this when normally looping with the pump is highly discouraged and reserved for special circumstances. If you still want to do this, long press this button again.\n\nWARNING: this can trigger a bug which causes the pump to reject all connection attempts and requires pressing a button on the pump to recover and should therefore be avoided.</string>
<string name="combo_read_full_history_confirmation">Are you really sure you want to read all pump data and take the consequences of this action?</string> <string name="combo_read_full_history_confirmation">Are you really sure you want to read all pump data and take the consequences of this action?</string>
<string name="combo_pump_tbr_cancelled_warrning">TBR CANCELLED warning was confirmed</string> <string name="combo_pump_tbr_cancelled_warrning">TBR CANCELLED warning was confirmed</string>
<string name="combo_error_no_connection_no_bolus_delivered">The pump could\'nt be reached. No bolus was given</string>
<string name="combo_error_no_bolus_delivered">Bolus delivery failed. It appears no bolus was delivered. To be sure, please check the pump to avoid a double bolus and then bolus again. To guard against bugs, boluses are not automatically retried.</string> <string name="combo_error_no_bolus_delivered">Bolus delivery failed. It appears no bolus was delivered. To be sure, please check the pump to avoid a double bolus and then bolus again. To guard against bugs, boluses are not automatically retried.</string>
<string name="combo_error_partial_bolus_delivered">Only %.2f U of the requested bolus of %.2f U was delivered due to an error. Please check the pump to verify this and take appropriate actions.</string> <string name="combo_error_partial_bolus_delivered">Only %.2f U of the requested bolus of %.2f U was delivered due to an error. Please check the pump to verify this and take appropriate actions.</string>
<string name="combo_error_bolus_verification_failed">Delivering the bolus and verifying the pump\'s history failed, please check the pump and manually create a bolus record using the Careportal tab if a bolus was delivered.</string> <string name="combo_error_bolus_verification_failed">Delivering the bolus and verifying the pump\'s history failed, please check the pump and manually create a bolus record using the Careportal tab if a bolus was delivered.</string>