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.exception.OmnipodException;
|
||||||
import info.nightscout.androidaps.plugins.pump.omnipod.util.OmnipodConst;
|
import info.nightscout.androidaps.plugins.pump.omnipod.util.OmnipodConst;
|
||||||
import info.nightscout.androidaps.utils.SP;
|
import info.nightscout.androidaps.utils.SP;
|
||||||
|
import io.reactivex.Flowable;
|
||||||
|
import io.reactivex.Observable;
|
||||||
import io.reactivex.Single;
|
import io.reactivex.Single;
|
||||||
|
import io.reactivex.disposables.CompositeDisposable;
|
||||||
|
|
||||||
public class OmnipodManager {
|
public class OmnipodManager {
|
||||||
private static final int ACTION_VERIFICATION_TRIES = 3;
|
private static final int ACTION_VERIFICATION_TRIES = 3;
|
||||||
|
@ -64,7 +67,7 @@ public class OmnipodManager {
|
||||||
throw new IllegalArgumentException("Communication service cannot be null");
|
throw new IllegalArgumentException("Communication service cannot be null");
|
||||||
}
|
}
|
||||||
this.communicationService = communicationService;
|
this.communicationService = communicationService;
|
||||||
if(podState != null) {
|
if (podState != null) {
|
||||||
podState.setStateChangedHandler(podStateChangedHandler);
|
podState.setStateChangedHandler(podStateChangedHandler);
|
||||||
}
|
}
|
||||||
this.podState = podState;
|
this.podState = podState;
|
||||||
|
@ -150,7 +153,7 @@ public class OmnipodManager {
|
||||||
communicationService.executeAction(new CancelDeliveryAction(podState, DeliveryType.TEMP_BASAL, true));
|
communicationService.executeAction(new CancelDeliveryAction(podState, DeliveryType.TEMP_BASAL, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Single<StatusResponse> bolus(Double units) {
|
public Single<StatusResponse> bolus(Double units, BolusProgressIndicationConsumer progressIndicationConsumer) {
|
||||||
assertReadyForDelivery();
|
assertReadyForDelivery();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -179,18 +182,39 @@ public class OmnipodManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Single.create(emitter -> executeDelayed(() -> {
|
CompositeDisposable disposables = new CompositeDisposable();
|
||||||
try {
|
Duration bolusDuration = calculateBolusDuration(units, OmnipodConst.POD_BOLUS_DELIVERY_RATE);
|
||||||
StatusResponse statusResponse = getPodStatus();
|
|
||||||
if (statusResponse.getDeliveryStatus().isBolusing()) {
|
if (progressIndicationConsumer != null) {
|
||||||
emitter.onError(new IllegalDeliveryStatusException(DeliveryStatus.NORMAL, statusResponse.getDeliveryStatus()));
|
int numberOfProgressReports = 20;
|
||||||
} else {
|
long progressReportInterval = bolusDuration.getMillis() / numberOfProgressReports;
|
||||||
emitter.onSuccess(statusResponse);
|
|
||||||
|
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) {
|
}, bolusDuration);
|
||||||
emitter.onError(ex);
|
});
|
||||||
}
|
|
||||||
}, calculateBolusDuration(units, OmnipodConst.POD_BOLUS_DELIVERY_RATE)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void cancelBolus() {
|
public void cancelBolus() {
|
||||||
|
|
|
@ -13,6 +13,8 @@ import info.nightscout.androidaps.R;
|
||||||
import info.nightscout.androidaps.data.Profile;
|
import info.nightscout.androidaps.data.Profile;
|
||||||
import info.nightscout.androidaps.data.PumpEnactResult;
|
import info.nightscout.androidaps.data.PumpEnactResult;
|
||||||
import info.nightscout.androidaps.logging.L;
|
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.common.data.TempBasalPair;
|
||||||
import info.nightscout.androidaps.plugins.pump.omnipod.comm.OmnipodCommunicationService;
|
import info.nightscout.androidaps.plugins.pump.omnipod.comm.OmnipodCommunicationService;
|
||||||
import info.nightscout.androidaps.plugins.pump.omnipod.comm.OmnipodManager;
|
import info.nightscout.androidaps.plugins.pump.omnipod.comm.OmnipodManager;
|
||||||
|
@ -146,9 +148,15 @@ public class AapsOmnipodManager implements OmnipodCommunicationManagerInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PumpEnactResult setBolus(Double amount) {
|
public PumpEnactResult setBolus(Double units) {
|
||||||
try {
|
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
|
// At this point, we know that the bolus command has been succesfully sent
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue