Use PumpEnactResult's fluent interface in Combo plugin.

This commit is contained in:
Johannes Mockenhaupt 2017-11-03 14:40:01 +01:00
parent 49885afb39
commit f9a7d6fe20
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1
2 changed files with 21 additions and 43 deletions

View file

@ -57,6 +57,11 @@ public class PumpEnactResult extends Object {
return this; return this;
} }
public PumpEnactResult percent(Integer percent) {
this.percent = percent;
return this;
}
public PumpEnactResult isPercent(boolean isPercent) { public PumpEnactResult isPercent(boolean isPercent) {
this.isPercent = isPercent; this.isPercent = isPercent;
return this; return this;

View file

@ -75,13 +75,8 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
return plugin; return plugin;
} }
private static PumpEnactResult OPERATION_NOT_SUPPORTED = new PumpEnactResult(); private static PumpEnactResult OPERATION_NOT_SUPPORTED = new PumpEnactResult()
.success(false).enacted(false).comment(MainApp.sResources.getString(R.string.combo_pump_unsupported_operation));
static {
OPERATION_NOT_SUPPORTED.success = false;
OPERATION_NOT_SUPPORTED.enacted = false;
OPERATION_NOT_SUPPORTED.comment = MainApp.sResources.getString(R.string.combo_pump_unsupported_operation);
}
private ComboPlugin() { private ComboPlugin() {
definePumpCapabilities(); definePumpCapabilities();
@ -334,32 +329,24 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
try { try {
if (detailedBolusInfo.insulin == 0 && detailedBolusInfo.carbs == 0) { if (detailedBolusInfo.insulin == 0 && detailedBolusInfo.carbs == 0) {
// neither carbs nor bolus requested // neither carbs nor bolus requested
PumpEnactResult pumpEnactResult = new PumpEnactResult();
pumpEnactResult.success = false;
pumpEnactResult.enacted = false;
pumpEnactResult.bolusDelivered = 0d;
pumpEnactResult.carbsDelivered = 0d;
pumpEnactResult.comment = MainApp.instance().getString(R.string.danar_invalidinput);
log.error("deliverTreatment: Invalid input"); log.error("deliverTreatment: Invalid input");
return pumpEnactResult; return new PumpEnactResult().success(false).enacted(false)
.bolusDelivered(0d).carbsDelivered(0d)
.comment(MainApp.instance().getString(R.string.danar_invalidinput));
} else if (detailedBolusInfo.insulin > 0) { } else if (detailedBolusInfo.insulin > 0) {
// bolus needed, ask pump to deliver it // bolus needed, ask pump to deliver it
return deliverBolus(detailedBolusInfo); return deliverBolus(detailedBolusInfo);
} else { } else {
// no bolus required, carb only treatment // no bolus required, carb only treatment
SystemClock.sleep(6000);
PumpEnactResult pumpEnactResult = new PumpEnactResult();
pumpEnactResult.success = true;
pumpEnactResult.enacted = true;
pumpEnactResult.bolusDelivered = 0d;
pumpEnactResult.carbsDelivered = detailedBolusInfo.carbs;
pumpEnactResult.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
MainApp.getConfigBuilder().addToHistoryTreatment(detailedBolusInfo); MainApp.getConfigBuilder().addToHistoryTreatment(detailedBolusInfo);
EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance();
bolusingEvent.percent = 100; bolusingEvent.percent = 100;
MainApp.bus().post(bolusingEvent); MainApp.bus().post(bolusingEvent);
return pumpEnactResult;
return new PumpEnactResult().success(true).enacted(true)
.bolusDelivered(0d).carbsDelivered(detailedBolusInfo.carbs)
.comment(MainApp.instance().getString(R.string.virtualpump_resultok));
} }
} finally { } finally {
cancelBolus = false; cancelBolus = false;
@ -399,15 +386,12 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
// TODO should we check against pump.lastBolus or rather the DB ... // TODO should we check against pump.lastBolus or rather the DB ...
if (!Objects.equals(pump.lastBolus, reservoirBolusResult.lastBolus)) { if (!Objects.equals(pump.lastBolus, reservoirBolusResult.lastBolus)) {
new Thread(this::checkPumpHistory).start(); new Thread(this::checkPumpHistory).start();
return new PumpEnactResult().success(false).enacted(false). return new PumpEnactResult().success(false).enacted(false)
comment(MainApp.sResources.getString(R.string.combo_pump_bolus_history_state_mismatch)); .comment(MainApp.sResources.getString(R.string.combo_pump_bolus_history_state_mismatch));
} }
if (cancelBolus) { if (cancelBolus) {
PumpEnactResult pumpEnactResult = new PumpEnactResult(); return new PumpEnactResult().success(true).enacted(false);
pumpEnactResult.success = true;
pumpEnactResult.enacted = false;
return pumpEnactResult;
} }
// start bolus delivery // start bolus delivery
@ -448,11 +432,8 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
detailedBolusInfo.insulin = lastPumpBolus.amount; detailedBolusInfo.insulin = lastPumpBolus.amount;
detailedBolusInfo.date = lastPumpBolus.timestamp; detailedBolusInfo.date = lastPumpBolus.timestamp;
MainApp.getConfigBuilder().addToHistoryTreatment(detailedBolusInfo); MainApp.getConfigBuilder().addToHistoryTreatment(detailedBolusInfo);
return new PumpEnactResult() return new PumpEnactResult().success(true).enacted(true)
.success(true) .bolusDelivered(lastPumpBolus.amount).carbsDelivered(detailedBolusInfo.carbs);
.enacted(true)
.bolusDelivered(lastPumpBolus.amount)
.carbsDelivered(detailedBolusInfo.carbs);
} else { } else {
return new PumpEnactResult().success(true).enacted(false); return new PumpEnactResult().success(true).enacted(false);
} }
@ -534,16 +515,8 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
MainApp.bus().post(new EventComboPumpUpdateGUI()); MainApp.bus().post(new EventComboPumpUpdateGUI());
} }
PumpEnactResult pumpEnactResult = new PumpEnactResult(); return new PumpEnactResult().success(true).enacted(true).isPercent(true)
pumpEnactResult.success = commandResult.success; .percent(state.tbrPercent).duration(state.tbrRemainingDuration);
pumpEnactResult.enacted = commandResult.enacted;
pumpEnactResult.isPercent = true;
// Combo would have bailed if this wasn't set properly. Maybe we should
// have the command return this anyways ...
pumpEnactResult.percent = adjustedPercent;
pumpEnactResult.duration = durationInMinutes;
return pumpEnactResult;
} }
@Override @Override