Add basic bolus progress indication

This commit is contained in:
Bart Sopers 2019-12-04 22:46:27 +01:00
parent 8c07e747d4
commit 342765c5a2
3 changed files with 54 additions and 15 deletions

View file

@ -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);
}

View file

@ -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;
@ -150,7 +153,7 @@ public class OmnipodManager {
communicationService.executeAction(new CancelDeliveryAction(podState, DeliveryType.TEMP_BASAL, true));
}
public Single<StatusResponse> bolus(Double units) {
public Single<StatusResponse> bolus(Double units, BolusProgressIndicationConsumer progressIndicationConsumer) {
assertReadyForDelivery();
try {
@ -179,7 +182,27 @@ public class OmnipodManager {
}
}
return Single.create(emitter -> executeDelayed(() -> {
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()) {
@ -190,7 +213,8 @@ public class OmnipodManager {
} catch (Exception ex) {
emitter.onError(ex);
}
}, calculateBolusDuration(units, OmnipodConst.POD_BOLUS_DELIVERY_RATE)));
}, bolusDuration);
});
}
public void cancelBolus() {

View file

@ -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<StatusResponse> responseObserver = delegate.bolus(amount);
Single<StatusResponse> 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