From 342765c5a2b01002b058fb25c96a22648570904c Mon Sep 17 00:00:00 2001 From: Bart Sopers Date: Wed, 4 Dec 2019 22:46:27 +0100 Subject: [PATCH] Add basic bolus progress indication --- .../comm/BolusProgressIndicationConsumer.java | 7 +++ .../pump/omnipod/comm/OmnipodManager.java | 50 ++++++++++++++----- .../driver/comm/AapsOmnipodManager.java | 12 ++++- 3 files changed, 54 insertions(+), 15 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/BolusProgressIndicationConsumer.java diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/BolusProgressIndicationConsumer.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/BolusProgressIndicationConsumer.java new file mode 100644 index 0000000000..35d07e3622 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/BolusProgressIndicationConsumer.java @@ -0,0 +1,7 @@ +package info.nightscout.androidaps.plugins.pump.omnipod.comm; + +// TODO replace with Consumer when our min API level >= 24 +@FunctionalInterface +public interface BolusProgressIndicationConsumer { + void accept(double estimatedUnitsDelivered, int percentage); +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/OmnipodManager.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/OmnipodManager.java index c31fe83c18..e842a4b196 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/OmnipodManager.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/OmnipodManager.java @@ -47,7 +47,10 @@ import info.nightscout.androidaps.plugins.pump.omnipod.exception.NonceOutOfSyncE import info.nightscout.androidaps.plugins.pump.omnipod.exception.OmnipodException; import info.nightscout.androidaps.plugins.pump.omnipod.util.OmnipodConst; import info.nightscout.androidaps.utils.SP; +import io.reactivex.Flowable; +import io.reactivex.Observable; import io.reactivex.Single; +import io.reactivex.disposables.CompositeDisposable; public class OmnipodManager { private static final int ACTION_VERIFICATION_TRIES = 3; @@ -64,7 +67,7 @@ public class OmnipodManager { throw new IllegalArgumentException("Communication service cannot be null"); } this.communicationService = communicationService; - if(podState != null) { + if (podState != null) { podState.setStateChangedHandler(podStateChangedHandler); } this.podState = podState; @@ -150,7 +153,7 @@ public class OmnipodManager { communicationService.executeAction(new CancelDeliveryAction(podState, DeliveryType.TEMP_BASAL, true)); } - public Single bolus(Double units) { + public Single bolus(Double units, BolusProgressIndicationConsumer progressIndicationConsumer) { assertReadyForDelivery(); try { @@ -179,18 +182,39 @@ public class OmnipodManager { } } - return Single.create(emitter -> executeDelayed(() -> { - try { - StatusResponse statusResponse = getPodStatus(); - if (statusResponse.getDeliveryStatus().isBolusing()) { - emitter.onError(new IllegalDeliveryStatusException(DeliveryStatus.NORMAL, statusResponse.getDeliveryStatus())); - } else { - emitter.onSuccess(statusResponse); + CompositeDisposable disposables = new CompositeDisposable(); + Duration bolusDuration = calculateBolusDuration(units, OmnipodConst.POD_BOLUS_DELIVERY_RATE); + + if (progressIndicationConsumer != null) { + int numberOfProgressReports = 20; + long progressReportInterval = bolusDuration.getMillis() / numberOfProgressReports; + + disposables.add(Flowable.intervalRange(0, numberOfProgressReports, 0, progressReportInterval, TimeUnit.MILLISECONDS) // + .subscribe(count -> { + // TODO needs improvement + // take (average) radio communication time into account + double factor = (double)count / numberOfProgressReports; + // Round estimated unites delivered to pod pulse size 0.05 + int roundingDivisor = (int) (1 / OmnipodConst.POD_PULSE_SIZE); + double estimatedUnitsDelivered = Math.round(factor * units * roundingDivisor) / roundingDivisor; + progressIndicationConsumer.accept(estimatedUnitsDelivered, (int) (factor * 100)); + })); + } + + return Single.create(emitter -> { + executeDelayed(() -> { + try { + StatusResponse statusResponse = getPodStatus(); + if (statusResponse.getDeliveryStatus().isBolusing()) { + emitter.onError(new IllegalDeliveryStatusException(DeliveryStatus.NORMAL, statusResponse.getDeliveryStatus())); + } else { + emitter.onSuccess(statusResponse); + } + } catch (Exception ex) { + emitter.onError(ex); } - } catch (Exception ex) { - emitter.onError(ex); - } - }, calculateBolusDuration(units, OmnipodConst.POD_BOLUS_DELIVERY_RATE))); + }, bolusDuration); + }); } public void cancelBolus() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/comm/AapsOmnipodManager.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/comm/AapsOmnipodManager.java index 20d24bd034..9f82675249 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/comm/AapsOmnipodManager.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/comm/AapsOmnipodManager.java @@ -13,6 +13,8 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; +import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress; import info.nightscout.androidaps.plugins.pump.common.data.TempBasalPair; import info.nightscout.androidaps.plugins.pump.omnipod.comm.OmnipodCommunicationService; import info.nightscout.androidaps.plugins.pump.omnipod.comm.OmnipodManager; @@ -146,9 +148,15 @@ public class AapsOmnipodManager implements OmnipodCommunicationManagerInterface } @Override - public PumpEnactResult setBolus(Double amount) { + public PumpEnactResult setBolus(Double units) { try { - Single responseObserver = delegate.bolus(amount); + Single responseObserver = delegate.bolus(units, + (estimatedUnitsDelivered, percentage) -> { + EventOverviewBolusProgress progressUpdateEvent = EventOverviewBolusProgress.INSTANCE; + progressUpdateEvent.setStatus(getStringResource(R.string.bolusdelivering, units)); + progressUpdateEvent.setPercent(percentage); + RxBus.INSTANCE.send(progressUpdateEvent); + }); // At this point, we know that the bolus command has been succesfully sent