From 83d95f1e87a460dbdaf7c96d28ef88e306f879b5 Mon Sep 17 00:00:00 2001 From: Bart Sopers Date: Thu, 29 Aug 2019 21:06:33 +0200 Subject: [PATCH] Return PumpEnactResult instead of throwing Exceptions in OmnipodManager --- .../plugins/pump/omnipod/OmnipodManager.java | 238 +++++++++++++----- 1 file changed, 176 insertions(+), 62 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/OmnipodManager.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/OmnipodManager.java index 7786cd9004..8d49c07eb2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/OmnipodManager.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/OmnipodManager.java @@ -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.SetTempBasalService; 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.defs.DeliveryType; import info.nightscout.androidaps.plugins.pump.omnipod.defs.OmnipodCommunicationManagerInterface; @@ -63,36 +62,51 @@ public class OmnipodManager implements OmnipodCommunicationManagerInterface { @Override public PumpEnactResult insertCannula(Profile profile) { 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)) { - 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, - BasalScheduleMapper.mapProfileToBasalSchedule(profile))); + try { + communicationService.executeAction(new InsertCannulaAction(new InsertCannulaService(), podState, + BasalScheduleMapper.mapProfileToBasalSchedule(profile))); - executeDelayed(() -> { - StatusResponse delayedStatusResponse = communicationService.executeAction(new GetStatusAction(podState)); - InsertCannulaAction.updateCannulaInsertionStatus(podState, delayedStatusResponse); - }, OmnipodConst.POD_CANNULA_INSERTION_DURATION); + executeDelayed(() -> { + StatusResponse delayedStatusResponse = communicationService.executeAction(new GetStatusAction(podState)); + InsertCannulaAction.updateCannulaInsertionStatus(podState, delayedStatusResponse); + }, 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 } @Override public PumpEnactResult pairAndPrime() { - if (podState == null) { - podState = communicationService.executeAction(new PairAction(new PairService())); - } - if (podState.getSetupProgress().isBefore(SetupProgress.PRIMING_FINISHED)) { - communicationService.executeAction(new PrimeAction(new PrimeService(), podState)); + try { + if (podState == null) { + podState = communicationService.executeAction(new PairAction(new PairService())); + } + if (podState.getSetupProgress().isBefore(SetupProgress.PRIMING_FINISHED)) { + communicationService.executeAction(new PrimeAction(new PrimeService(), podState)); - executeDelayed(() -> { - StatusResponse delayedStatusResponse = communicationService.executeAction(new GetStatusAction(podState)); - PrimeAction.updatePrimingStatus(podState, delayedStatusResponse); - }, OmnipodConst.POD_PRIME_DURATION); - } else { - throw new IllegalStateException("Illegal setup state: " + podState.getSetupProgress().name()); + executeDelayed(() -> { + StatusResponse delayedStatusResponse = communicationService.executeAction(new GetStatusAction(podState)); + PrimeAction.updatePrimingStatus(podState, delayedStatusResponse); + }, OmnipodConst.POD_PRIME_DURATION); + } else { + // 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 @@ -101,9 +115,16 @@ public class OmnipodManager implements OmnipodCommunicationManagerInterface { @Override public PumpEnactResult cancelBolus() { 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 } @@ -111,20 +132,37 @@ public class OmnipodManager implements OmnipodCommunicationManagerInterface { @Override public PumpEnactResult getPodStatus() { 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 } @Override public PumpEnactResult deactivatePod() { 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 } @@ -132,11 +170,18 @@ public class OmnipodManager implements OmnipodCommunicationManagerInterface { @Override public PumpEnactResult setBasalProfile(Profile basalProfile) { 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 } @@ -152,9 +197,16 @@ public class OmnipodManager implements OmnipodCommunicationManagerInterface { @Override public PumpEnactResult bolus(Double units) { 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 } @@ -162,11 +214,18 @@ public class OmnipodManager implements OmnipodCommunicationManagerInterface { @Override public PumpEnactResult setTemporaryBasal(TempBasalPair tempBasalPair) { 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 } @@ -174,9 +233,16 @@ public class OmnipodManager implements OmnipodCommunicationManagerInterface { @Override public PumpEnactResult cancelTemporaryBasal() { 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 } @@ -184,53 +250,96 @@ public class OmnipodManager implements OmnipodCommunicationManagerInterface { @Override public PumpEnactResult acknowledgeAlerts() { 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 } // TODO should we add this to the OmnipodCommunicationManager interface? - public T getPodInfo(PodInfoType podInfoType) { + public PumpEnactResult getPodInfo(PodInfoType podInfoType) { 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? - public void suspendDelivery() { + public PumpEnactResult suspendDelivery() { 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? - public void resumeDelivery() { + public PumpEnactResult resumeDelivery() { 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? - public void setTime() { + public PumpEnactResult setTime() { 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 - DateTimeZone.setDefault(DateTimeZone.forTimeZone(TimeZone.getDefault())); - podState.setTimeZone(DateTimeZone.getDefault()); + try { + // Suspend delivery + communicationService.executeAction(new CancelDeliveryAction(podState, EnumSet.allOf(DeliveryType.class), false)); - // Resume delivery - communicationService.executeAction(new SetBasalScheduleAction(podState, podState.getBasalSchedule(), - true, podState.getScheduleOffset(), true)); + // Joda seems to cache the default time zone, so we use the JVM's + DateTimeZone.setDefault(DateTimeZone.forTimeZone(TimeZone.getDefault())); + 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() { @@ -253,4 +362,9 @@ public class OmnipodManager implements OmnipodCommunicationManagerInterface { ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1); 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"); + } } \ No newline at end of file