Return PumpEnactResult instead of throwing Exceptions in OmnipodManager

This commit is contained in:
Bart Sopers 2019-08-29 21:06:33 +02:00
parent ed52070a4c
commit 83d95f1e87

View file

@ -30,7 +30,6 @@ import info.nightscout.androidaps.plugins.pump.omnipod.comm.action.service.PairS
import info.nightscout.androidaps.plugins.pump.omnipod.comm.action.service.PrimeService; import info.nightscout.androidaps.plugins.pump.omnipod.comm.action.service.PrimeService;
import info.nightscout.androidaps.plugins.pump.omnipod.comm.action.service.SetTempBasalService; import info.nightscout.androidaps.plugins.pump.omnipod.comm.action.service.SetTempBasalService;
import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.response.StatusResponse; import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.response.StatusResponse;
import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.response.podinfo.PodInfo;
import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.response.podinfo.PodInfoResponse; import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.response.podinfo.PodInfoResponse;
import info.nightscout.androidaps.plugins.pump.omnipod.defs.DeliveryType; import info.nightscout.androidaps.plugins.pump.omnipod.defs.DeliveryType;
import info.nightscout.androidaps.plugins.pump.omnipod.defs.OmnipodCommunicationManagerInterface; import info.nightscout.androidaps.plugins.pump.omnipod.defs.OmnipodCommunicationManagerInterface;
@ -63,36 +62,51 @@ public class OmnipodManager implements OmnipodCommunicationManagerInterface {
@Override @Override
public PumpEnactResult insertCannula(Profile profile) { public PumpEnactResult insertCannula(Profile profile) {
if (podState == null || podState.getSetupProgress().isBefore(SetupProgress.PRIMING_FINISHED)) { if (podState == null || podState.getSetupProgress().isBefore(SetupProgress.PRIMING_FINISHED)) {
throw new IllegalArgumentException("Pod should be paired and primed first"); // TODO use string resource
return new PumpEnactResult().success(false).enacted(false).comment("Pod should be paired and primed first");
} else if (podState.getSetupProgress().isAfter(SetupProgress.CANNULA_INSERTING)) { } else if (podState.getSetupProgress().isAfter(SetupProgress.CANNULA_INSERTING)) {
throw new IllegalStateException("Illegal setup state: " + podState.getSetupProgress().name()); // TODO use string resource
return new PumpEnactResult().success(false).enacted(false).comment("Illegal setup state: " + podState.getSetupProgress().name());
} }
communicationService.executeAction(new InsertCannulaAction(new InsertCannulaService(), podState, try {
BasalScheduleMapper.mapProfileToBasalSchedule(profile))); communicationService.executeAction(new InsertCannulaAction(new InsertCannulaService(), podState,
BasalScheduleMapper.mapProfileToBasalSchedule(profile)));
executeDelayed(() -> { executeDelayed(() -> {
StatusResponse delayedStatusResponse = communicationService.executeAction(new GetStatusAction(podState)); StatusResponse delayedStatusResponse = communicationService.executeAction(new GetStatusAction(podState));
InsertCannulaAction.updateCannulaInsertionStatus(podState, delayedStatusResponse); InsertCannulaAction.updateCannulaInsertionStatus(podState, delayedStatusResponse);
}, OmnipodConst.POD_CANNULA_INSERTION_DURATION); }, OmnipodConst.POD_CANNULA_INSERTION_DURATION);
} catch (Exception ex) {
// TODO distinguish between certain and uncertain failures
// TODO user friendly error messages (string resources)
return new PumpEnactResult().success(false).enacted(false);
}
return null; // TODO return null; // TODO
} }
@Override @Override
public PumpEnactResult pairAndPrime() { public PumpEnactResult pairAndPrime() {
if (podState == null) { try {
podState = communicationService.executeAction(new PairAction(new PairService())); if (podState == null) {
} podState = communicationService.executeAction(new PairAction(new PairService()));
if (podState.getSetupProgress().isBefore(SetupProgress.PRIMING_FINISHED)) { }
communicationService.executeAction(new PrimeAction(new PrimeService(), podState)); if (podState.getSetupProgress().isBefore(SetupProgress.PRIMING_FINISHED)) {
communicationService.executeAction(new PrimeAction(new PrimeService(), podState));
executeDelayed(() -> { executeDelayed(() -> {
StatusResponse delayedStatusResponse = communicationService.executeAction(new GetStatusAction(podState)); StatusResponse delayedStatusResponse = communicationService.executeAction(new GetStatusAction(podState));
PrimeAction.updatePrimingStatus(podState, delayedStatusResponse); PrimeAction.updatePrimingStatus(podState, delayedStatusResponse);
}, OmnipodConst.POD_PRIME_DURATION); }, OmnipodConst.POD_PRIME_DURATION);
} else { } else {
throw new IllegalStateException("Illegal setup state: " + podState.getSetupProgress().name()); // TODO use string resource
return new PumpEnactResult().success(false).enacted(false).comment("Illegal setup state: " + podState.getSetupProgress().name());
}
} catch (Exception ex) {
// TODO distinguish between certain and uncertain failures
// TODO user friendly error messages (string resources)
return new PumpEnactResult().success(false).enacted(false);
} }
return null; // TODO return null; // TODO
@ -101,9 +115,16 @@ public class OmnipodManager implements OmnipodCommunicationManagerInterface {
@Override @Override
public PumpEnactResult cancelBolus() { public PumpEnactResult cancelBolus() {
if (!isInitialized()) { if (!isInitialized()) {
throw new IllegalStateException("Pod should be initialized first"); return createNotInitializedResult();
}
try {
communicationService.executeAction(new CancelDeliveryAction(podState, DeliveryType.BOLUS, true));
} catch (Exception ex) {
// TODO distinguish between certain and uncertain failures
// TODO user friendly error messages (string resources)
return new PumpEnactResult().success(false).enacted(false);
} }
communicationService.executeAction(new CancelDeliveryAction(podState, DeliveryType.BOLUS, true));
return null; // TODO return null; // TODO
} }
@ -111,20 +132,37 @@ public class OmnipodManager implements OmnipodCommunicationManagerInterface {
@Override @Override
public PumpEnactResult getPodStatus() { public PumpEnactResult getPodStatus() {
if (podState == null) { if (podState == null) {
throw new IllegalStateException("Pod should be paired first"); // TODO use string resource
return new PumpEnactResult().success(false).enacted(false).comment("Pod should be paired and primed first");
}
try {
// TODO how can we return the status response? Also refer to TODO in interface
StatusResponse statusResponse = communicationService.executeAction(new GetStatusAction(podState));
} catch (Exception ex) {
// TODO distinguish between certain and uncertain failures
// TODO user friendly error messages (string resources)
return new PumpEnactResult().success(false).enacted(false);
} }
// return communicationService.executeAction(new GetStatusAction(podState));
return null; // TODO return null; // TODO
} }
@Override @Override
public PumpEnactResult deactivatePod() { public PumpEnactResult deactivatePod() {
if (podState == null) { if (podState == null) {
throw new IllegalStateException("Pod should be paired first"); // TODO use string resource
return new PumpEnactResult().success(false).enacted(false).comment("Pod should be paired and primed first");
}
try {
communicationService.executeAction(new DeactivatePodAction(podState, true));
resetPodState();
} catch (Exception ex) {
// TODO distinguish between certain and uncertain failures
// TODO user friendly error messages (string resources)
return new PumpEnactResult().success(false).enacted(false);
} }
communicationService.executeAction(new DeactivatePodAction(podState, true));
resetPodState();
return null; // TODO return null; // TODO
} }
@ -132,11 +170,18 @@ public class OmnipodManager implements OmnipodCommunicationManagerInterface {
@Override @Override
public PumpEnactResult setBasalProfile(Profile basalProfile) { public PumpEnactResult setBasalProfile(Profile basalProfile) {
if (!isInitialized()) { if (!isInitialized()) {
throw new IllegalStateException("Pod should be initialized first"); return createNotInitializedResult();
}
try {
communicationService.executeAction(new SetBasalScheduleAction(podState,
BasalScheduleMapper.mapProfileToBasalSchedule(basalProfile),
false, podState.getScheduleOffset(), true));
} catch (Exception ex) {
// TODO distinguish between certain and uncertain failures
// TODO user friendly error messages (string resources)
return new PumpEnactResult().success(false).enacted(false);
} }
communicationService.executeAction(new SetBasalScheduleAction(podState,
BasalScheduleMapper.mapProfileToBasalSchedule(basalProfile),
false, podState.getScheduleOffset(), true));
return null; // TODO return null; // TODO
} }
@ -152,9 +197,16 @@ public class OmnipodManager implements OmnipodCommunicationManagerInterface {
@Override @Override
public PumpEnactResult bolus(Double units) { public PumpEnactResult bolus(Double units) {
if (!isInitialized()) { if (!isInitialized()) {
throw new IllegalStateException("Pod should be initialized first"); return createNotInitializedResult();
}
try {
communicationService.executeAction(new BolusAction(podState, units, true, true));
} catch (Exception ex) {
// TODO distinguish between certain and uncertain failures
// TODO user friendly error messages (string resources)
return new PumpEnactResult().success(false).enacted(false);
} }
communicationService.executeAction(new BolusAction(podState, units, true, true));
return null; // TODO return null; // TODO
} }
@ -162,11 +214,18 @@ public class OmnipodManager implements OmnipodCommunicationManagerInterface {
@Override @Override
public PumpEnactResult setTemporaryBasal(TempBasalPair tempBasalPair) { public PumpEnactResult setTemporaryBasal(TempBasalPair tempBasalPair) {
if (!isInitialized()) { if (!isInitialized()) {
throw new IllegalStateException("Pod should be initialized first"); return createNotInitializedResult();
}
try {
communicationService.executeAction(new SetTempBasalAction(new SetTempBasalService(),
podState, tempBasalPair.getInsulinRate(), Duration.standardMinutes(tempBasalPair.getDurationMinutes()),
true, true));
} catch (Exception ex) {
// TODO distinguish between certain and uncertain failures
// TODO user friendly error messages (string resources)
return new PumpEnactResult().success(false).enacted(false);
} }
communicationService.executeAction(new SetTempBasalAction(new SetTempBasalService(),
podState, tempBasalPair.getInsulinRate(), Duration.standardMinutes(tempBasalPair.getDurationMinutes()),
true, true));
return null; // TODO return null; // TODO
} }
@ -174,9 +233,16 @@ public class OmnipodManager implements OmnipodCommunicationManagerInterface {
@Override @Override
public PumpEnactResult cancelTemporaryBasal() { public PumpEnactResult cancelTemporaryBasal() {
if (!isInitialized()) { if (!isInitialized()) {
throw new IllegalStateException("Pod should be initialized first"); return createNotInitializedResult();
}
try {
communicationService.executeAction(new CancelDeliveryAction(podState, DeliveryType.TEMP_BASAL, true));
} catch (Exception ex) {
// TODO distinguish between certain and uncertain failures
// TODO user friendly error messages (string resources)
return new PumpEnactResult().success(false).enacted(false);
} }
communicationService.executeAction(new CancelDeliveryAction(podState, DeliveryType.TEMP_BASAL, true));
return null; // TODO return null; // TODO
} }
@ -184,53 +250,96 @@ public class OmnipodManager implements OmnipodCommunicationManagerInterface {
@Override @Override
public PumpEnactResult acknowledgeAlerts() { public PumpEnactResult acknowledgeAlerts() {
if (!isInitialized()) { if (!isInitialized()) {
throw new IllegalStateException("Pod should be initialized first"); return createNotInitializedResult();
}
try {
communicationService.executeAction(new AcknowledgeAlertsAction(podState, podState.getActiveAlerts()));
} catch (Exception ex) {
// TODO distinguish between certain and uncertain failures
// TODO user friendly error messages (string resources)
return new PumpEnactResult().success(false).enacted(false);
} }
communicationService.executeAction(new AcknowledgeAlertsAction(podState, podState.getActiveAlerts()));
return null; // TODO return null; // TODO
} }
// TODO should we add this to the OmnipodCommunicationManager interface? // TODO should we add this to the OmnipodCommunicationManager interface?
public <T extends PodInfo> T getPodInfo(PodInfoType podInfoType) { public PumpEnactResult getPodInfo(PodInfoType podInfoType) {
if (!isInitialized()) { if (!isInitialized()) {
throw new IllegalStateException("Pod should be initialized first"); return createNotInitializedResult();
} }
PodInfoResponse podInfoResponse = communicationService.executeAction(new GetPodInfoAction(podState, podInfoType));
return podInfoResponse.getPodInfo(); try {
// TODO how can we return the PodInfo response?
PodInfoResponse podInfoResponse = communicationService.executeAction(new GetPodInfoAction(podState, podInfoType));
} catch (Exception ex) {
// TODO distinguish between certain and uncertain failures
// TODO user friendly error messages (string resources)
return new PumpEnactResult().success(false).enacted(false);
}
return null; // TODO
} }
// TODO should we add this to the OmnipodCommunicationManager interface? // TODO should we add this to the OmnipodCommunicationManager interface?
public void suspendDelivery() { public PumpEnactResult suspendDelivery() {
if (!isInitialized()) { if (!isInitialized()) {
throw new IllegalStateException("Pod should be initialized first"); return createNotInitializedResult();
} }
communicationService.executeAction(new CancelDeliveryAction(podState, EnumSet.allOf(DeliveryType.class), true));
try {
communicationService.executeAction(new CancelDeliveryAction(podState, EnumSet.allOf(DeliveryType.class), true));
} catch (Exception ex) {
// TODO distinguish between certain and uncertain failures
// TODO user friendly error messages (string resources)
return new PumpEnactResult().success(false).enacted(false);
}
return null; // TODO
} }
// TODO should we add this to the OmnipodCommunicationManager interface? // TODO should we add this to the OmnipodCommunicationManager interface?
public void resumeDelivery() { public PumpEnactResult resumeDelivery() {
if (!isInitialized()) { if (!isInitialized()) {
throw new IllegalStateException("Pod should be initialized first"); return createNotInitializedResult();
} }
communicationService.executeAction(new SetBasalScheduleAction(podState, podState.getBasalSchedule(),
true, podState.getScheduleOffset(), true)); try {
communicationService.executeAction(new SetBasalScheduleAction(podState, podState.getBasalSchedule(),
true, podState.getScheduleOffset(), true));
} catch (Exception ex) {
// TODO distinguish between certain and uncertain failures
// TODO user friendly error messages (string resources)
return new PumpEnactResult().success(false).enacted(false);
}
return null; // TODO
} }
// TODO should we add this to the OmnipodCommunicationManager interface? // TODO should we add this to the OmnipodCommunicationManager interface?
public void setTime() { public PumpEnactResult setTime() {
if (!isInitialized()) { if (!isInitialized()) {
throw new IllegalStateException("Pod should be initialized first"); return createNotInitializedResult();
} }
// Suspend delivery
communicationService.executeAction(new CancelDeliveryAction(podState, EnumSet.allOf(DeliveryType.class), false));
// Joda seems to cache the default time zone, so we use the JVM's try {
DateTimeZone.setDefault(DateTimeZone.forTimeZone(TimeZone.getDefault())); // Suspend delivery
podState.setTimeZone(DateTimeZone.getDefault()); communicationService.executeAction(new CancelDeliveryAction(podState, EnumSet.allOf(DeliveryType.class), false));
// Resume delivery // Joda seems to cache the default time zone, so we use the JVM's
communicationService.executeAction(new SetBasalScheduleAction(podState, podState.getBasalSchedule(), DateTimeZone.setDefault(DateTimeZone.forTimeZone(TimeZone.getDefault()));
true, podState.getScheduleOffset(), true)); podState.setTimeZone(DateTimeZone.getDefault());
// Resume delivery
communicationService.executeAction(new SetBasalScheduleAction(podState, podState.getBasalSchedule(),
true, podState.getScheduleOffset(), true));
} catch (Exception ex) {
// TODO distinguish between certain and uncertain failures
// TODO user friendly error messages (string resources)
return new PumpEnactResult().success(false).enacted(false);
}
return null; // TODO
} }
public OmnipodCommunicationService getCommunicationService() { public OmnipodCommunicationService getCommunicationService() {
@ -253,4 +362,9 @@ public class OmnipodManager implements OmnipodCommunicationManagerInterface {
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1); ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
scheduledExecutorService.schedule(r, timeout.getMillis(), TimeUnit.MILLISECONDS); scheduledExecutorService.schedule(r, timeout.getMillis(), TimeUnit.MILLISECONDS);
} }
private PumpEnactResult createNotInitializedResult() {
// TODO use string resource
return new PumpEnactResult().success(false).enacted(false).comment("Pod should be initialized first");
}
} }