Add basic bolus progress indication
This commit is contained in:
parent
8c07e747d4
commit
342765c5a2
3 changed files with 54 additions and 15 deletions
|
@ -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);
|
||||
}
|
|
@ -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<StatusResponse> bolus(Double units) {
|
||||
public Single<StatusResponse> 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() {
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue