From 24cd0c771246422dfee146c85b67bf2398720ef9 Mon Sep 17 00:00:00 2001 From: Bart Sopers Date: Fri, 29 Nov 2019 23:10:39 +0100 Subject: [PATCH] Refactor exceptions in Omnipod driver to prepare for adding failure certainty --- .../comm/OmnipodCommunicationService.java | 42 ++++++++++++------- .../comm/action/AcknowledgeAlertsAction.java | 7 ++-- .../pump/omnipod/comm/action/BolusAction.java | 5 ++- .../comm/action/CancelDeliveryAction.java | 5 ++- .../comm/action/ConfigureAlertsAction.java | 5 ++- .../comm/action/DeactivatePodAction.java | 3 +- .../omnipod/comm/action/GetPodInfoAction.java | 5 ++- .../omnipod/comm/action/GetStatusAction.java | 3 +- .../comm/action/InsertCannulaAction.java | 12 +++--- .../pump/omnipod/comm/action/PairAction.java | 3 +- .../pump/omnipod/comm/action/PrimeAction.java | 10 +++-- .../comm/action/SetBasalScheduleAction.java | 7 ++-- .../comm/action/SetTempBasalAction.java | 11 ++--- .../comm/action/service/PairService.java | 4 +- .../omnipod/comm/message/OmnipodMessage.java | 16 ++++--- .../omnipod/comm/message/OmnipodPacket.java | 9 ++-- .../message/command/BolusExtraCommand.java | 5 ++- .../command/SetInsulinScheduleCommand.java | 7 ++-- .../command/TempBasalExtraCommand.java | 7 ++-- .../ActionInitializationException.java | 7 ++++ .../CommandInitializationException.java | 7 ++++ .../exception/CommunicationException.java | 34 +++++++++++++++ .../exception/CrcMismatchException.java | 19 +++++++-- .../IllegalDeliveryStatusException.java | 25 +++++++++++ .../exception/IllegalPacketTypeException.java | 25 +++++++++++ .../IllegalPodProgressException.java | 24 +++++++++++ .../exception/IllegalResponseException.java | 25 +++++++++++ .../IllegalSetupProgressException.java | 24 +++++++++++ .../exception/MessageDecodingException.java | 11 +++++ .../exception/NonceOutOfSyncException.java | 13 ------ .../exception/NonceResyncException.java | 7 ++++ .../exception/NotEnoughDataException.java | 13 ++++-- .../omnipod/exception/OmnipodException.java | 2 +- .../omnipod/exception/PodFaultException.java | 19 +-------- .../PodReturnedErrorResponseException.java | 13 +----- 35 files changed, 312 insertions(+), 122 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/ActionInitializationException.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/CommandInitializationException.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/CommunicationException.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/IllegalDeliveryStatusException.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/IllegalPacketTypeException.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/IllegalPodProgressException.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/IllegalResponseException.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/IllegalSetupProgressException.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/MessageDecodingException.java delete mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/NonceOutOfSyncException.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/NonceResyncException.java diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/OmnipodCommunicationService.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/OmnipodCommunicationService.java index ec244c988c..66602e2296 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/OmnipodCommunicationService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/OmnipodCommunicationService.java @@ -26,10 +26,14 @@ import info.nightscout.androidaps.plugins.pump.omnipod.defs.MessageBlockType; import info.nightscout.androidaps.plugins.pump.omnipod.defs.PacketType; import info.nightscout.androidaps.plugins.pump.omnipod.defs.PodInfoType; import info.nightscout.androidaps.plugins.pump.omnipod.defs.state.PodState; +import info.nightscout.androidaps.plugins.pump.omnipod.exception.CommunicationException; +import info.nightscout.androidaps.plugins.pump.omnipod.exception.NonceResyncException; import info.nightscout.androidaps.plugins.pump.omnipod.exception.NotEnoughDataException; import info.nightscout.androidaps.plugins.pump.omnipod.exception.OmnipodException; import info.nightscout.androidaps.plugins.pump.omnipod.exception.PodFaultException; import info.nightscout.androidaps.plugins.pump.omnipod.exception.PodReturnedErrorResponseException; +import info.nightscout.androidaps.plugins.pump.omnipod.exception.IllegalPacketTypeException; +import info.nightscout.androidaps.plugins.pump.omnipod.exception.IllegalResponseException; /** * Created by andy on 6/29/18. @@ -112,12 +116,12 @@ public class OmnipodCommunicationService extends RileyLinkCommunicationManager { podState.setFaultEvent(faultEvent); throw new PodFaultException(faultEvent); } else { - throw new OmnipodException("Unexpected response type: " + responseMessageBlock.toString()); + throw new IllegalResponseException(responseClass.getSimpleName(), responseMessageBlock.getType()); } } } - throw new OmnipodException("Nonce resync failed"); + throw new NonceResyncException(); } private MessageBlock transportMessages(PodState podState, OmnipodMessage message, Integer addressOverride, Integer ackAddressOverride) { @@ -139,15 +143,17 @@ public class OmnipodCommunicationService extends RileyLinkCommunicationManager { firstPacket = false; try { response = exchangePackets(podState, packet); + } catch (OmnipodException ex) { + throw ex; } catch (Exception ex) { - throw new OmnipodException("Failed to exchange packets", ex); + throw new CommunicationException(CommunicationException.Type.UNEXPECTED_EXCEPTION, ex); } //We actually ignore (ack) responses if it is not last packet to send } if (response.getPacketType() == PacketType.ACK) { podState.increasePacketNumber(1); - throw new OmnipodException("Received ack instead of real response"); + throw new IllegalPacketTypeException(null, PacketType.ACK); } OmnipodMessage receivedMessage = null; @@ -158,15 +164,19 @@ public class OmnipodCommunicationService extends RileyLinkCommunicationManager { } catch (NotEnoughDataException ex) { // Message is (probably) not complete yet OmnipodPacket ackForCon = createAckPacket(podState, packetAddress, ackAddressOverride); + try { OmnipodPacket conPacket = exchangePackets(podState, ackForCon, 3, 40); if (conPacket.getPacketType() != PacketType.CON) { - throw new OmnipodException("Received a non-con packet type: " + conPacket.getPacketType()); + throw new IllegalPacketTypeException(PacketType.CON, conPacket.getPacketType()); } receivedMessageData = ByteUtil.concat(receivedMessageData, conPacket.getEncodedMessage()); - } catch (RileyLinkCommunicationException ex2) { - throw new OmnipodException("RileyLink communication failed", ex2); + } catch (OmnipodException ex2) { + throw ex2; + } catch (Exception ex2) { + throw new CommunicationException(CommunicationException.Type.UNEXPECTED_EXCEPTION, ex2); } + } } @@ -177,7 +187,7 @@ public class OmnipodCommunicationService extends RileyLinkCommunicationManager { List messageBlocks = receivedMessage.getMessageBlocks(); if (messageBlocks.size() == 0) { - throw new OmnipodException("Not enough data"); + throw new NotEnoughDataException(receivedMessageData); } else if (messageBlocks.size() > 1) { LOG.error("received more than one message block: " + messageBlocks.toString()); } @@ -206,32 +216,34 @@ public class OmnipodCommunicationService extends RileyLinkCommunicationManager { if (RileyLinkBLEError.Timeout.equals(ex.getErrorCode())) { quiet = true; } else { - LOG.debug("Ignoring exception in ackUntilQuiet: " + ex.getClass().getSimpleName() + ": " + ex.getMessage()); + LOG.debug("Ignoring exception in ackUntilQuiet: " + ex.getClass().getSimpleName() + ": " + ex.getErrorCode() + ": " + ex.getMessage()); } } catch (Exception ex) { - LOG.debug("Ignoring exception in ackUntilQuiet: " + ex.getClass().getSimpleName() + ": " + ex.getMessage()); + throw new CommunicationException(CommunicationException.Type.UNEXPECTED_EXCEPTION, ex); } podState.increasePacketNumber(1); } - private OmnipodPacket exchangePackets(PodState podState, OmnipodPacket packet) throws RileyLinkCommunicationException { + private OmnipodPacket exchangePackets(PodState podState, OmnipodPacket packet) { return exchangePackets(podState, packet, 0, 333, 9000, 127); } - private OmnipodPacket exchangePackets(PodState podState, OmnipodPacket packet, int repeatCount, int preambleExtensionMilliseconds) throws RileyLinkCommunicationException { + private OmnipodPacket exchangePackets(PodState podState, OmnipodPacket packet, int repeatCount, int preambleExtensionMilliseconds) { return exchangePackets(podState, packet, repeatCount, 333, 9000, preambleExtensionMilliseconds); } - private OmnipodPacket exchangePackets(PodState podState, OmnipodPacket packet, int repeatCount, int responseTimeoutMilliseconds, int exchangeTimeoutMilliseconds, int preambleExtensionMilliseconds) throws RileyLinkCommunicationException { + private OmnipodPacket exchangePackets(PodState podState, OmnipodPacket packet, int repeatCount, int responseTimeoutMilliseconds, int exchangeTimeoutMilliseconds, int preambleExtensionMilliseconds) { long timeoutTime = System.currentTimeMillis() + exchangeTimeoutMilliseconds; while (System.currentTimeMillis() < timeoutTime) { OmnipodPacket response = null; try { response = sendAndListen(packet, responseTimeoutMilliseconds, repeatCount, 9, preambleExtensionMilliseconds, OmnipodPacket.class); - } catch (Exception ex) { + } catch (RileyLinkCommunicationException ex) { LOG.debug("Ignoring exception in exchangePackets: " + ex.getClass().getSimpleName() + ": " + ex.getMessage()); + } catch (Exception ex) { + throw new CommunicationException(CommunicationException.Type.UNEXPECTED_EXCEPTION, ex); } if (response == null || !response.isValid()) { continue; @@ -246,6 +258,6 @@ public class OmnipodCommunicationService extends RileyLinkCommunicationManager { podState.increasePacketNumber(2); return response; } - throw new OmnipodException("Timeout when trying to exchange packets"); + throw new CommunicationException(CommunicationException.Type.TIMEOUT); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/AcknowledgeAlertsAction.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/AcknowledgeAlertsAction.java index f049bc882e..70cea8da0a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/AcknowledgeAlertsAction.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/AcknowledgeAlertsAction.java @@ -8,6 +8,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.response.Sta import info.nightscout.androidaps.plugins.pump.omnipod.defs.AlertSet; import info.nightscout.androidaps.plugins.pump.omnipod.defs.AlertSlot; import info.nightscout.androidaps.plugins.pump.omnipod.defs.state.PodSessionState; +import info.nightscout.androidaps.plugins.pump.omnipod.exception.ActionInitializationException; public class AcknowledgeAlertsAction implements OmnipodAction { private final PodSessionState podState; @@ -15,12 +16,12 @@ public class AcknowledgeAlertsAction implements OmnipodAction { public AcknowledgeAlertsAction(PodSessionState podState, AlertSet alerts) { if (podState == null) { - throw new IllegalArgumentException("Pod state cannot be null"); + throw new ActionInitializationException("Pod state cannot be null"); } if (alerts == null) { - throw new IllegalArgumentException("Alert set can not be null"); + throw new ActionInitializationException("Alert set can not be null"); } else if (alerts.size() == 0) { - throw new IllegalArgumentException("Alert set can not be empty"); + throw new ActionInitializationException("Alert set can not be empty"); } this.podState = podState; this.alerts = alerts; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/BolusAction.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/BolusAction.java index 7dd9cedcbb..49bff8bff0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/BolusAction.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/BolusAction.java @@ -11,6 +11,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.command.SetI import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.response.StatusResponse; import info.nightscout.androidaps.plugins.pump.omnipod.defs.schedule.BolusDeliverySchedule; import info.nightscout.androidaps.plugins.pump.omnipod.defs.state.PodSessionState; +import info.nightscout.androidaps.plugins.pump.omnipod.exception.ActionInitializationException; public class BolusAction implements OmnipodAction { private final PodSessionState podState; @@ -22,10 +23,10 @@ public class BolusAction implements OmnipodAction { public BolusAction(PodSessionState podState, double units, Duration timeBetweenPulses, boolean acknowledgementBeep, boolean completionBeep) { if (podState == null) { - throw new IllegalArgumentException("Pod state cannot be null"); + throw new ActionInitializationException("Pod state cannot be null"); } if (timeBetweenPulses == null) { - throw new IllegalArgumentException("Time between pulses cannot be null"); + throw new ActionInitializationException("Time between pulses cannot be null"); } this.podState = podState; this.units = units; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/CancelDeliveryAction.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/CancelDeliveryAction.java index 197d65f826..929928cd2b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/CancelDeliveryAction.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/CancelDeliveryAction.java @@ -8,6 +8,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.response.Sta import info.nightscout.androidaps.plugins.pump.omnipod.defs.BeepType; import info.nightscout.androidaps.plugins.pump.omnipod.defs.DeliveryType; import info.nightscout.androidaps.plugins.pump.omnipod.defs.state.PodSessionState; +import info.nightscout.androidaps.plugins.pump.omnipod.exception.ActionInitializationException; public class CancelDeliveryAction implements OmnipodAction { private final PodSessionState podState; @@ -17,10 +18,10 @@ public class CancelDeliveryAction implements OmnipodAction { public CancelDeliveryAction(PodSessionState podState, EnumSet deliveryTypes, boolean acknowledgementBeep) { if (podState == null) { - throw new IllegalArgumentException("Pod state cannot be null"); + throw new ActionInitializationException("Pod state cannot be null"); } if (deliveryTypes == null) { - throw new IllegalArgumentException("Delivery types cannot be null"); + throw new ActionInitializationException("Delivery types cannot be null"); } this.podState = podState; this.deliveryTypes = deliveryTypes; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/ConfigureAlertsAction.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/ConfigureAlertsAction.java index 3cbae137a1..b89588b72b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/ConfigureAlertsAction.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/ConfigureAlertsAction.java @@ -7,6 +7,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.command.Conf import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.response.StatusResponse; import info.nightscout.androidaps.plugins.pump.omnipod.defs.AlertConfiguration; import info.nightscout.androidaps.plugins.pump.omnipod.defs.state.PodSessionState; +import info.nightscout.androidaps.plugins.pump.omnipod.exception.ActionInitializationException; public class ConfigureAlertsAction implements OmnipodAction { private final PodSessionState podState; @@ -14,10 +15,10 @@ public class ConfigureAlertsAction implements OmnipodAction { public ConfigureAlertsAction(PodSessionState podState, List alertConfigurations) { if (podState == null) { - throw new IllegalArgumentException("Pod state cannot be null"); + throw new ActionInitializationException("Pod state cannot be null"); } if (alertConfigurations == null) { - throw new IllegalArgumentException("Alert configurations cannot be null"); + throw new ActionInitializationException("Alert configurations cannot be null"); } this.podState = podState; this.alertConfigurations = alertConfigurations; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/DeactivatePodAction.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/DeactivatePodAction.java index 6f9b08ffbd..059ed0645d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/DeactivatePodAction.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/DeactivatePodAction.java @@ -7,6 +7,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.command.Deac import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.response.StatusResponse; import info.nightscout.androidaps.plugins.pump.omnipod.defs.DeliveryType; import info.nightscout.androidaps.plugins.pump.omnipod.defs.state.PodSessionState; +import info.nightscout.androidaps.plugins.pump.omnipod.exception.ActionInitializationException; public class DeactivatePodAction implements OmnipodAction { private final PodSessionState podState; @@ -14,7 +15,7 @@ public class DeactivatePodAction implements OmnipodAction { public DeactivatePodAction(PodSessionState podState, boolean acknowledgementBeep) { if (podState == null) { - throw new IllegalArgumentException("Pod state cannot be null"); + throw new ActionInitializationException("Pod state cannot be null"); } this.podState = podState; this.acknowledgementBeep = acknowledgementBeep; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/GetPodInfoAction.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/GetPodInfoAction.java index 618046d38e..a120bc02d9 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/GetPodInfoAction.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/GetPodInfoAction.java @@ -5,6 +5,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.command.GetS import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.response.podinfo.PodInfoResponse; import info.nightscout.androidaps.plugins.pump.omnipod.defs.PodInfoType; import info.nightscout.androidaps.plugins.pump.omnipod.defs.state.PodSessionState; +import info.nightscout.androidaps.plugins.pump.omnipod.exception.ActionInitializationException; public class GetPodInfoAction implements OmnipodAction { private final PodSessionState podState; @@ -12,10 +13,10 @@ public class GetPodInfoAction implements OmnipodAction { public GetPodInfoAction(PodSessionState podState, PodInfoType podInfoType) { if (podState == null) { - throw new IllegalArgumentException("Pod state cannot be null"); + throw new ActionInitializationException("Pod state cannot be null"); } if (podInfoType == null) { - throw new IllegalArgumentException("Pod info type cannot be null"); + throw new ActionInitializationException("Pod info type cannot be null"); } this.podState = podState; this.podInfoType = podInfoType; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/GetStatusAction.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/GetStatusAction.java index 302fa55577..a76f7cd3fa 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/GetStatusAction.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/GetStatusAction.java @@ -5,13 +5,14 @@ import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.command.GetS import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.response.StatusResponse; import info.nightscout.androidaps.plugins.pump.omnipod.defs.PodInfoType; import info.nightscout.androidaps.plugins.pump.omnipod.defs.state.PodSessionState; +import info.nightscout.androidaps.plugins.pump.omnipod.exception.ActionInitializationException; public class GetStatusAction implements OmnipodAction { private final PodSessionState podState; public GetStatusAction(PodSessionState podState) { if (podState == null) { - throw new IllegalArgumentException("Pod state cannot be null"); + throw new ActionInitializationException("Pod state cannot be null"); } this.podState = podState; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/InsertCannulaAction.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/InsertCannulaAction.java index b1739503e9..f2cc3328d3 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/InsertCannulaAction.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/InsertCannulaAction.java @@ -9,6 +9,8 @@ import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.response.Sta import info.nightscout.androidaps.plugins.pump.omnipod.defs.SetupProgress; import info.nightscout.androidaps.plugins.pump.omnipod.defs.schedule.BasalSchedule; import info.nightscout.androidaps.plugins.pump.omnipod.defs.state.PodSessionState; +import info.nightscout.androidaps.plugins.pump.omnipod.exception.ActionInitializationException; +import info.nightscout.androidaps.plugins.pump.omnipod.exception.IllegalSetupProgressException; public class InsertCannulaAction implements OmnipodAction { private static final Logger LOG = LoggerFactory.getLogger(InsertCannulaAction.class); @@ -19,13 +21,13 @@ public class InsertCannulaAction implements OmnipodAction { public InsertCannulaAction(InsertCannulaService insertCannulaService, PodSessionState podState, BasalSchedule initialBasalSchedule) { if (insertCannulaService == null) { - throw new IllegalArgumentException("Insert cannula service cannot be null"); + throw new ActionInitializationException("Insert cannula service cannot be null"); } if (podState == null) { - throw new IllegalArgumentException("Pod state cannot be null"); + throw new ActionInitializationException("Pod state cannot be null"); } if (initialBasalSchedule == null) { - throw new IllegalArgumentException("Initial basal schedule cannot be null"); + throw new ActionInitializationException("Initial basal schedule cannot be null"); } this.service = insertCannulaService; this.podState = podState; @@ -45,7 +47,7 @@ public class InsertCannulaAction implements OmnipodAction { @Override public StatusResponse execute(OmnipodCommunicationService communicationService) { if (podState.getSetupProgress().isBefore(SetupProgress.PRIMING_FINISHED)) { - throw new IllegalStateException("Pod should be primed first"); + throw new IllegalSetupProgressException(SetupProgress.PRIMING_FINISHED, podState.getSetupProgress()); } if (podState.getSetupProgress().isBefore(SetupProgress.INITIAL_BASAL_SCHEDULE_SET)) { @@ -67,7 +69,7 @@ public class InsertCannulaAction implements OmnipodAction { updateCannulaInsertionStatus(podState, statusResponse); return statusResponse; } else { - throw new IllegalStateException("Illegal setup progress: " + podState.getSetupProgress().name()); + throw new IllegalSetupProgressException(null, podState.getSetupProgress()); } } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/PairAction.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/PairAction.java index 6b271dd98b..4d98e34809 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/PairAction.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/PairAction.java @@ -11,6 +11,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.response.Ver import info.nightscout.androidaps.plugins.pump.omnipod.defs.SetupProgress; import info.nightscout.androidaps.plugins.pump.omnipod.defs.state.PodSessionState; import info.nightscout.androidaps.plugins.pump.omnipod.defs.state.PodSetupState; +import info.nightscout.androidaps.plugins.pump.omnipod.exception.ActionInitializationException; public class PairAction implements OmnipodAction { private final PairService service; @@ -18,7 +19,7 @@ public class PairAction implements OmnipodAction { public PairAction(PairService pairService, int address) { if (pairService == null) { - throw new IllegalArgumentException("Pair service cannot be null"); + throw new ActionInitializationException("Pair service cannot be null"); } this.service = pairService; this.address = address; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/PrimeAction.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/PrimeAction.java index e0b3f15187..57eff83694 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/PrimeAction.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/PrimeAction.java @@ -9,6 +9,8 @@ import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.response.Sta import info.nightscout.androidaps.plugins.pump.omnipod.defs.PodProgressStatus; import info.nightscout.androidaps.plugins.pump.omnipod.defs.SetupProgress; import info.nightscout.androidaps.plugins.pump.omnipod.defs.state.PodSessionState; +import info.nightscout.androidaps.plugins.pump.omnipod.exception.ActionInitializationException; +import info.nightscout.androidaps.plugins.pump.omnipod.exception.IllegalSetupProgressException; public class PrimeAction implements OmnipodAction { private static final Logger LOG = LoggerFactory.getLogger(PrimeAction.class); @@ -18,10 +20,10 @@ public class PrimeAction implements OmnipodAction { public PrimeAction(PrimeService primeService, PodSessionState podState) { if (primeService == null) { - throw new IllegalArgumentException("Prime service cannot be null"); + throw new ActionInitializationException("Prime service cannot be null"); } if (podState == null) { - throw new IllegalArgumentException("Pod state cannot be null"); + throw new ActionInitializationException("Pod state cannot be null"); } this.service = primeService; this.podState = podState; @@ -39,7 +41,7 @@ public class PrimeAction implements OmnipodAction { @Override public StatusResponse execute(OmnipodCommunicationService communicationService) { if (podState.getSetupProgress().isBefore(SetupProgress.POD_CONFIGURED)) { - throw new IllegalStateException("Pod should be paired first"); + throw new IllegalSetupProgressException(SetupProgress.POD_CONFIGURED, podState.getSetupProgress()); } if (podState.getSetupProgress().isBefore(SetupProgress.STARTING_PRIME)) { service.executeDisableTab5Sub16FaultConfigCommand(communicationService, podState); @@ -57,7 +59,7 @@ public class PrimeAction implements OmnipodAction { updatePrimingStatus(podState, statusResponse); return statusResponse; } else { - throw new IllegalStateException("Illegal setup progress: " + podState.getSetupProgress().name()); + throw new IllegalSetupProgressException(null, podState.getSetupProgress()); } } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/SetBasalScheduleAction.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/SetBasalScheduleAction.java index ec95bc2ee7..2f36a93c27 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/SetBasalScheduleAction.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/SetBasalScheduleAction.java @@ -11,6 +11,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.command.SetI import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.response.StatusResponse; import info.nightscout.androidaps.plugins.pump.omnipod.defs.schedule.BasalSchedule; import info.nightscout.androidaps.plugins.pump.omnipod.defs.state.PodSessionState; +import info.nightscout.androidaps.plugins.pump.omnipod.exception.ActionInitializationException; public class SetBasalScheduleAction implements OmnipodAction { private final PodSessionState podState; @@ -22,13 +23,13 @@ public class SetBasalScheduleAction implements OmnipodAction { public SetBasalScheduleAction(PodSessionState podState, BasalSchedule basalSchedule, boolean confidenceReminder, Duration scheduleOffset, boolean acknowledgementBeep) { if (podState == null) { - throw new IllegalArgumentException("Pod state cannot be null"); + throw new ActionInitializationException("Pod state cannot be null"); } if (basalSchedule == null) { - throw new IllegalArgumentException("Basal schedule cannot be null"); + throw new ActionInitializationException("Basal schedule cannot be null"); } if (scheduleOffset == null) { - throw new IllegalArgumentException("Schedule offset cannot be null"); + throw new ActionInitializationException("Schedule offset cannot be null"); } this.podState = podState; this.basalSchedule = basalSchedule; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/SetTempBasalAction.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/SetTempBasalAction.java index 9ed6f23299..7007fdb741 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/SetTempBasalAction.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/SetTempBasalAction.java @@ -7,6 +7,8 @@ import info.nightscout.androidaps.plugins.pump.omnipod.comm.action.service.SetTe import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.response.StatusResponse; import info.nightscout.androidaps.plugins.pump.omnipod.defs.DeliveryStatus; import info.nightscout.androidaps.plugins.pump.omnipod.defs.state.PodSessionState; +import info.nightscout.androidaps.plugins.pump.omnipod.exception.ActionInitializationException; +import info.nightscout.androidaps.plugins.pump.omnipod.exception.IllegalDeliveryStatusException; public class SetTempBasalAction implements OmnipodAction { private final SetTempBasalService service; @@ -19,13 +21,13 @@ public class SetTempBasalAction implements OmnipodAction { public SetTempBasalAction(SetTempBasalService setTempBasalService, PodSessionState podState, double rate, Duration duration, boolean acknowledgementBeep, boolean completionBeep) { if (setTempBasalService == null) { - throw new IllegalArgumentException("Set temp basal service cannot be null"); + throw new ActionInitializationException("Set temp basal service cannot be null"); } if (podState == null) { - throw new IllegalArgumentException("Pod state cannot be null"); + throw new ActionInitializationException("Pod state cannot be null"); } if (duration == null) { - throw new IllegalArgumentException("Duration cannot be null"); + throw new ActionInitializationException("Duration cannot be null"); } this.service = setTempBasalService; this.podState = podState; @@ -40,8 +42,7 @@ public class SetTempBasalAction implements OmnipodAction { StatusResponse statusResponse = service.cancelTempBasal(communicationService, podState); if (statusResponse.getDeliveryStatus() != DeliveryStatus.NORMAL) { - throw new IllegalStateException("Illegal delivery status: " + - statusResponse.getDeliveryStatus().name()); + throw new IllegalDeliveryStatusException(DeliveryStatus.NORMAL, statusResponse.getDeliveryStatus()); } return service.executeTempBasalCommand(communicationService, podState, rate, duration, diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/service/PairService.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/service/PairService.java index 63ff8ef6bb..e30d3ee817 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/service/PairService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/action/service/PairService.java @@ -11,7 +11,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.command.Conf import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.response.VersionResponse; import info.nightscout.androidaps.plugins.pump.omnipod.defs.PodProgressStatus; import info.nightscout.androidaps.plugins.pump.omnipod.defs.state.PodSetupState; -import info.nightscout.androidaps.plugins.pump.omnipod.exception.OmnipodException; +import info.nightscout.androidaps.plugins.pump.omnipod.exception.IllegalPodProgressException; import info.nightscout.androidaps.plugins.pump.omnipod.util.OmnipodConst; public class PairService { @@ -37,7 +37,7 @@ public class PairService { message, OmnipodConst.DEFAULT_ADDRESS, setupState.getAddress()); if (configurePodResponse.getPodProgressStatus() != PodProgressStatus.PAIRING_SUCCESS) { - throw new OmnipodException("Pairing failed, state: " + configurePodResponse.getPodProgressStatus().name()); + throw new IllegalPodProgressException(PodProgressStatus.PAIRING_SUCCESS, configurePodResponse.getPodProgressStatus()); } return configurePodResponse; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/OmnipodMessage.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/OmnipodMessage.java index 36d4268cd0..646b3dd205 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/OmnipodMessage.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/OmnipodMessage.java @@ -10,8 +10,8 @@ import info.nightscout.androidaps.plugins.pump.common.utils.ByteUtil; import info.nightscout.androidaps.plugins.pump.omnipod.comm.OmnipodCommunicationService; import info.nightscout.androidaps.plugins.pump.omnipod.defs.MessageBlockType; import info.nightscout.androidaps.plugins.pump.omnipod.exception.CrcMismatchException; +import info.nightscout.androidaps.plugins.pump.omnipod.exception.MessageDecodingException; import info.nightscout.androidaps.plugins.pump.omnipod.exception.NotEnoughDataException; -import info.nightscout.androidaps.plugins.pump.omnipod.exception.OmnipodException; import info.nightscout.androidaps.plugins.pump.omnipod.util.OmniCRC; public class OmnipodMessage { @@ -29,7 +29,7 @@ public class OmnipodMessage { public static OmnipodMessage decodeMessage(byte[] data) { if (data.length < 10) { - throw new NotEnoughDataException("Not enough data"); + throw new NotEnoughDataException(data); } int address = ByteUtil.toInt((int) data[0], (int) data[1], (int) data[2], @@ -37,21 +37,20 @@ public class OmnipodMessage { byte b9 = data[4]; int bodyLength = ByteUtil.convertUnsignedByteToInt(data[5]); if (data.length - 8 < bodyLength) { - throw new NotEnoughDataException("not enough data: " + ByteUtil.shortHexString(data)); + throw new NotEnoughDataException(data); } int sequenceNumber = (((int) b9 >> 2) & 0b11111); int crc = ByteUtil.toInt(data[data.length - 2], data[data.length - 1]); int calculatedCrc = OmniCRC.crc16(ByteUtil.substring(data, 0, data.length - 2)); if (crc != calculatedCrc) { - throw new CrcMismatchException("CRC mismatch"); + throw new CrcMismatchException(calculatedCrc, crc); } List blocks = decodeBlocks(ByteUtil.substring(data, 6, data.length - 6 - 2)); if (blocks == null || blocks.size() == 0) { - throw new OmnipodException("No blocks decoded"); + throw new MessageDecodingException("No blocks decoded"); } - OmnipodMessage result = new OmnipodMessage(address, blocks, sequenceNumber); - return result; + return new OmnipodMessage(address, blocks, sequenceNumber); } private static List decodeBlocks(byte[] data) { @@ -65,7 +64,7 @@ public class OmnipodMessage { int blockLength = block.getRawData().length; index += blockLength; } catch (Exception ex) { - throw new OmnipodException("Failed to decode blocks", ex); + throw new MessageDecodingException("Failed to decode blocks", ex); } } @@ -84,7 +83,6 @@ public class OmnipodMessage { header = ByteUtil.concat(header, (byte) (((sequenceNumber & 0x1F) << 2) + ((encodedData.length >> 8) & 0x03))); header = ByteUtil.concat(header, (byte) (encodedData.length & 0xFF)); encodedData = ByteUtil.concat(header, encodedData); - String myString = ByteUtil.shortHexString(encodedData); int crc = OmniCRC.crc16(encodedData); encodedData = ByteUtil.concat(encodedData, ByteUtil.substring(ByteUtil.getBytesFromInt(crc), 2, 2)); return encodedData; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/OmnipodPacket.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/OmnipodPacket.java index 714c0abb4b..a065801081 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/OmnipodPacket.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/OmnipodPacket.java @@ -4,7 +4,7 @@ import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.data.RLMe import info.nightscout.androidaps.plugins.pump.common.utils.ByteUtil; import info.nightscout.androidaps.plugins.pump.omnipod.defs.PacketType; import info.nightscout.androidaps.plugins.pump.omnipod.exception.CrcMismatchException; -import info.nightscout.androidaps.plugins.pump.omnipod.exception.OmnipodException; +import info.nightscout.androidaps.plugins.pump.omnipod.exception.IllegalPacketTypeException; import info.nightscout.androidaps.plugins.pump.omnipod.util.OmniCRC; /** @@ -26,15 +26,12 @@ public class OmnipodPacket implements RLMessage { try { this.packetType = PacketType.fromByte((byte) (((int) encoded[4] & 0xFF) >> 5)); } catch (IllegalArgumentException ex) { - throw new OmnipodException("Invalid packet type", ex); + throw new IllegalPacketTypeException(null, null); } this.sequenceNumber = (encoded[4] & 0b11111); byte crc = OmniCRC.crc8(ByteUtil.substring(encoded, 0, encoded.length - 1)); if (crc != encoded[encoded.length - 1]) { - throw new CrcMismatchException("CRC mismatch: " + - ByteUtil.shortHexString(new byte[]{crc}) + " <> " + - ByteUtil.shortHexString(new byte[]{encoded[encoded.length - 1]}) + - " (packetType=" + packetType.name() + ",packetLength=" + encoded.length + ")"); + throw new CrcMismatchException(crc, encoded[encoded.length - 1]); } this.encodedMessage = ByteUtil.substring(encoded, 5, encoded.length - 1 - 5); valid = true; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/command/BolusExtraCommand.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/command/BolusExtraCommand.java index 6e2a5cbbbb..c623a8c296 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/command/BolusExtraCommand.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/command/BolusExtraCommand.java @@ -5,6 +5,7 @@ import org.joda.time.Duration; import info.nightscout.androidaps.plugins.pump.common.utils.ByteUtil; import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.MessageBlock; import info.nightscout.androidaps.plugins.pump.omnipod.defs.MessageBlockType; +import info.nightscout.androidaps.plugins.pump.omnipod.exception.CommandInitializationException; import info.nightscout.androidaps.plugins.pump.omnipod.util.OmnipodConst; public class BolusExtraCommand extends MessageBlock { @@ -28,9 +29,9 @@ public class BolusExtraCommand extends MessageBlock { boolean acknowledgementBeep, boolean completionBeep, Duration programReminderInterval, Duration timeBetweenPulses) { if (units <= 0D) { - throw new IllegalArgumentException("Units should be > 0"); + throw new CommandInitializationException("Units should be > 0"); } else if (units > OmnipodConst.MAX_BOLUS) { - throw new IllegalArgumentException("Units exceeds max bolus"); + throw new CommandInitializationException("Units exceeds max bolus"); } this.units = units; this.squareWaveUnits = squareWaveUnits; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/command/SetInsulinScheduleCommand.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/command/SetInsulinScheduleCommand.java index f1c28762b3..c1e4005074 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/command/SetInsulinScheduleCommand.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/command/SetInsulinScheduleCommand.java @@ -11,6 +11,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.defs.schedule.BasalSchedu import info.nightscout.androidaps.plugins.pump.omnipod.defs.schedule.BolusDeliverySchedule; import info.nightscout.androidaps.plugins.pump.omnipod.defs.schedule.DeliverySchedule; import info.nightscout.androidaps.plugins.pump.omnipod.defs.schedule.TempBasalDeliverySchedule; +import info.nightscout.androidaps.plugins.pump.omnipod.exception.CommandInitializationException; import info.nightscout.androidaps.plugins.pump.omnipod.util.OmnipodConst; public class SetInsulinScheduleCommand extends NonceResyncableMessageBlock { @@ -50,12 +51,12 @@ public class SetInsulinScheduleCommand extends NonceResyncableMessageBlock { // Temp basal public SetInsulinScheduleCommand(int nonce, double tempBasalRate, Duration duration) { if (tempBasalRate < 0D) { - throw new IllegalArgumentException("Rate should be >= 0"); + throw new CommandInitializationException("Rate should be >= 0"); } else if (tempBasalRate > OmnipodConst.MAX_BASAL_RATE) { - throw new IllegalArgumentException("Rate exceeds max basal rate"); + throw new CommandInitializationException("Rate exceeds max basal rate"); } if (duration.isLongerThan(OmnipodConst.MAX_TEMP_BASAL_DURATION)) { - throw new IllegalArgumentException("Duration exceeds max temp basal duration"); + throw new CommandInitializationException("Duration exceeds max temp basal duration"); } int pulsesPerHour = (int) Math.round(tempBasalRate / OmnipodConst.POD_PULSE_SIZE); int pulsesPerSegment = pulsesPerHour / 2; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/command/TempBasalExtraCommand.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/command/TempBasalExtraCommand.java index 3d072ebc96..3c3a42344a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/command/TempBasalExtraCommand.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/command/TempBasalExtraCommand.java @@ -9,6 +9,7 @@ import info.nightscout.androidaps.plugins.pump.common.utils.ByteUtil; import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.MessageBlock; import info.nightscout.androidaps.plugins.pump.omnipod.defs.MessageBlockType; import info.nightscout.androidaps.plugins.pump.omnipod.defs.schedule.RateEntry; +import info.nightscout.androidaps.plugins.pump.omnipod.exception.CommandInitializationException; import info.nightscout.androidaps.plugins.pump.omnipod.util.OmnipodConst; public class TempBasalExtraCommand extends MessageBlock { @@ -23,12 +24,12 @@ public class TempBasalExtraCommand extends MessageBlock { public TempBasalExtraCommand(double rate, Duration duration, boolean acknowledgementBeep, boolean completionBeep, Duration programReminderInterval) { if (rate < 0D) { - throw new IllegalArgumentException("Rate should be >= 0"); + throw new CommandInitializationException("Rate should be >= 0"); } else if (rate > OmnipodConst.MAX_BASAL_RATE) { - throw new IllegalArgumentException("Rate exceeds max basal rate"); + throw new CommandInitializationException("Rate exceeds max basal rate"); } if (duration.isLongerThan(OmnipodConst.MAX_TEMP_BASAL_DURATION)) { - throw new IllegalArgumentException("Duration exceeds max temp basal duration"); + throw new CommandInitializationException("Duration exceeds max temp basal duration"); } this.acknowledgementBeep = acknowledgementBeep; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/ActionInitializationException.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/ActionInitializationException.java new file mode 100644 index 0000000000..306873b20e --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/ActionInitializationException.java @@ -0,0 +1,7 @@ +package info.nightscout.androidaps.plugins.pump.omnipod.exception; + +public class ActionInitializationException extends OmnipodException { + public ActionInitializationException(String message) { + super(message); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/CommandInitializationException.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/CommandInitializationException.java new file mode 100644 index 0000000000..11682f6ba7 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/CommandInitializationException.java @@ -0,0 +1,7 @@ +package info.nightscout.androidaps.plugins.pump.omnipod.exception; + +public class CommandInitializationException extends OmnipodException { + public CommandInitializationException(String message) { + super(message); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/CommunicationException.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/CommunicationException.java new file mode 100644 index 0000000000..af1bef6ea8 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/CommunicationException.java @@ -0,0 +1,34 @@ +package info.nightscout.androidaps.plugins.pump.omnipod.exception; + +public class CommunicationException extends OmnipodException { + private final Type type; + + public CommunicationException(Type type) { + super(type.getDescription()); + this.type = type; + } + + public CommunicationException(Type type, Throwable cause) { + super(type.getDescription(), cause); + this.type = type; + } + + public Type getType() { + return type; + } + + public enum Type { + TIMEOUT("Communication timeout"), + UNEXPECTED_EXCEPTION("Caught an unexpected Exception"); + + private final String description; + + Type(String description) { + this.description = description; + } + + public String getDescription() { + return description; + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/CrcMismatchException.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/CrcMismatchException.java index be2b5c69cb..18f7194f70 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/CrcMismatchException.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/CrcMismatchException.java @@ -1,11 +1,22 @@ package info.nightscout.androidaps.plugins.pump.omnipod.exception; +import java.util.Locale; + public class CrcMismatchException extends OmnipodException { - public CrcMismatchException(String message) { - super(message); + private final int expected; + private final int actual; + + public CrcMismatchException(int expected, int actual) { + super(String.format(Locale.getDefault(), "CRC mismatch: expected %d, got %d", expected, actual)); + this.expected = expected; + this.actual = actual; } - public CrcMismatchException(String message, Throwable cause) { - super(message, cause); + public int getExpected() { + return expected; + } + + public int getActual() { + return actual; } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/IllegalDeliveryStatusException.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/IllegalDeliveryStatusException.java new file mode 100644 index 0000000000..e7a9f6d63a --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/IllegalDeliveryStatusException.java @@ -0,0 +1,25 @@ +package info.nightscout.androidaps.plugins.pump.omnipod.exception; + +import java.util.Locale; + +import info.nightscout.androidaps.plugins.pump.omnipod.defs.DeliveryStatus; +import info.nightscout.androidaps.plugins.pump.omnipod.defs.PodProgressStatus; + +public class IllegalDeliveryStatusException extends OmnipodException { + private final DeliveryStatus expected; + private final DeliveryStatus actual; + + public IllegalDeliveryStatusException(DeliveryStatus expected, DeliveryStatus actual) { + super(String.format(Locale.getDefault(), "Illegal delivery status: %s, expected: %s", actual, expected)); + this.expected = expected; + this.actual = actual; + } + + public DeliveryStatus getExpected() { + return expected; + } + + public DeliveryStatus getActual() { + return actual; + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/IllegalPacketTypeException.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/IllegalPacketTypeException.java new file mode 100644 index 0000000000..a79265e11a --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/IllegalPacketTypeException.java @@ -0,0 +1,25 @@ +package info.nightscout.androidaps.plugins.pump.omnipod.exception; + +import java.util.Locale; + +import info.nightscout.androidaps.plugins.pump.omnipod.defs.PacketType; + +public class IllegalPacketTypeException extends OmnipodException { + private final PacketType expected; + private final PacketType actual; + + public IllegalPacketTypeException(PacketType expected, PacketType actual) { + super(String.format(Locale.getDefault(), "Illegal packet type: %s, expected %s", + actual, expected)); + this.expected = expected; + this.actual = actual; + } + + public PacketType getExpected() { + return expected; + } + + public PacketType getActual() { + return actual; + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/IllegalPodProgressException.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/IllegalPodProgressException.java new file mode 100644 index 0000000000..9b3c0197f0 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/IllegalPodProgressException.java @@ -0,0 +1,24 @@ +package info.nightscout.androidaps.plugins.pump.omnipod.exception; + +import java.util.Locale; + +import info.nightscout.androidaps.plugins.pump.omnipod.defs.PodProgressStatus; + +public class IllegalPodProgressException extends OmnipodException { + private final PodProgressStatus expected; + private final PodProgressStatus actual; + + public IllegalPodProgressException(PodProgressStatus expected, PodProgressStatus actual) { + super(String.format(Locale.getDefault(), "Illegal setup state: %s, expected: %s", actual, expected)); + this.expected = expected; + this.actual = actual; + } + + public PodProgressStatus getExpected() { + return expected; + } + + public PodProgressStatus getActual() { + return actual; + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/IllegalResponseException.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/IllegalResponseException.java new file mode 100644 index 0000000000..0bd4a743d3 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/IllegalResponseException.java @@ -0,0 +1,25 @@ +package info.nightscout.androidaps.plugins.pump.omnipod.exception; + +import java.util.Locale; + +import info.nightscout.androidaps.plugins.pump.omnipod.defs.MessageBlockType; + +public class IllegalResponseException extends OmnipodException { + private final String actualClass; + private final MessageBlockType expectedType; + + public IllegalResponseException(String actualClass, MessageBlockType expectedType) { + super(String.format(Locale.getDefault(), "Illegal response type: got class of type %s " + + "for message block type %s", actualClass, expectedType)); + this.actualClass = actualClass; + this.expectedType = expectedType; + } + + public String getActualClass() { + return actualClass; + } + + public MessageBlockType getExpectedType() { + return expectedType; + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/IllegalSetupProgressException.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/IllegalSetupProgressException.java new file mode 100644 index 0000000000..88b3d2fdf0 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/IllegalSetupProgressException.java @@ -0,0 +1,24 @@ +package info.nightscout.androidaps.plugins.pump.omnipod.exception; + +import java.util.Locale; + +import info.nightscout.androidaps.plugins.pump.omnipod.defs.SetupProgress; + +public class IllegalSetupProgressException extends OmnipodException { + private final SetupProgress expected; + private final SetupProgress actual; + + public IllegalSetupProgressException(SetupProgress expected, SetupProgress actual) { + super(String.format(Locale.getDefault(), "Illegal setup progress: %s, expected: %s", actual, expected)); + this.expected = expected; + this.actual = actual; + } + + public SetupProgress getExpected() { + return expected; + } + + public SetupProgress getActual() { + return actual; + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/MessageDecodingException.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/MessageDecodingException.java new file mode 100644 index 0000000000..80956f7d21 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/MessageDecodingException.java @@ -0,0 +1,11 @@ +package info.nightscout.androidaps.plugins.pump.omnipod.exception; + +public class MessageDecodingException extends OmnipodException { + public MessageDecodingException(String message) { + super(message); + } + + public MessageDecodingException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/NonceOutOfSyncException.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/NonceOutOfSyncException.java deleted file mode 100644 index dff7b1c6cb..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/NonceOutOfSyncException.java +++ /dev/null @@ -1,13 +0,0 @@ -package info.nightscout.androidaps.plugins.pump.omnipod.exception; - -import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.response.ErrorResponse; - -public class NonceOutOfSyncException extends PodReturnedErrorResponseException { - public NonceOutOfSyncException(ErrorResponse errorResponse) { - super(errorResponse); - } - - public NonceOutOfSyncException(ErrorResponse errorResponse, Throwable cause) { - super(errorResponse, cause); - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/NonceResyncException.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/NonceResyncException.java new file mode 100644 index 0000000000..95983644af --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/NonceResyncException.java @@ -0,0 +1,7 @@ +package info.nightscout.androidaps.plugins.pump.omnipod.exception; + +public class NonceResyncException extends OmnipodException { + public NonceResyncException() { + super("Nonce resync failed"); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/NotEnoughDataException.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/NotEnoughDataException.java index 3ed455213f..6f5c1b0638 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/NotEnoughDataException.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/NotEnoughDataException.java @@ -1,11 +1,16 @@ package info.nightscout.androidaps.plugins.pump.omnipod.exception; +import info.nightscout.androidaps.plugins.pump.common.utils.ByteUtil; + public class NotEnoughDataException extends OmnipodException { - public NotEnoughDataException(String message) { - super(message); + private final byte[] data; + + public NotEnoughDataException(byte[] data) { + super("Not enough data: " + ByteUtil.shortHexString(data)); + this.data = data; } - public NotEnoughDataException(String message, Throwable cause) { - super(message, cause); + public byte[] getData() { + return data; } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/OmnipodException.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/OmnipodException.java index d76d08d8b6..2e0b771fd3 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/OmnipodException.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/OmnipodException.java @@ -1,6 +1,6 @@ package info.nightscout.androidaps.plugins.pump.omnipod.exception; -public class OmnipodException extends RuntimeException { +public abstract class OmnipodException extends RuntimeException { public OmnipodException(String message) { super(message); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/PodFaultException.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/PodFaultException.java index b6bf577245..dbdcef5a4d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/PodFaultException.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/PodFaultException.java @@ -8,27 +8,12 @@ public class PodFaultException extends OmnipodException { private final PodInfoFaultEvent faultEvent; public PodFaultException(PodInfoFaultEvent faultEvent) { - super(describePodFault(faultEvent)); + super(String.format(Locale.getDefault(), "Pod fault (%d): %s", faultEvent.getFaultEventCode().getValue(), + faultEvent.getFaultEventCode().toString())); this.faultEvent = faultEvent; } - public PodFaultException(PodInfoFaultEvent faultEvent, Throwable cause) { - super(describePodFault(faultEvent), cause); - this.faultEvent = faultEvent; - } - - public static String describePodFault(PodInfoFaultEvent faultEvent) { - return String.format(Locale.getDefault(), "Pod fault (%d): %s", faultEvent.getFaultEventCode().getValue(), - faultEvent.getFaultEventCode().toString()); - } - public PodInfoFaultEvent getFaultEvent() { return faultEvent; } - - @Override - public void printStackTrace() { - System.out.println(faultEvent.toString()); - super.printStackTrace(); - } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/PodReturnedErrorResponseException.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/PodReturnedErrorResponseException.java index 8cb743b888..016ceaa511 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/PodReturnedErrorResponseException.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/exception/PodReturnedErrorResponseException.java @@ -6,22 +6,11 @@ public class PodReturnedErrorResponseException extends OmnipodException { private final ErrorResponse errorResponse; public PodReturnedErrorResponseException(ErrorResponse errorResponse) { - super("Pod returned error response"); - this.errorResponse = errorResponse; - } - - public PodReturnedErrorResponseException(ErrorResponse errorResponse, Throwable cause) { - super("Pod returned error response", cause); + super("Pod returned error response: " + errorResponse.getErrorResponseType()); this.errorResponse = errorResponse; } public ErrorResponse getErrorResponse() { return errorResponse; } - - @Override - public void printStackTrace() { - System.out.println(errorResponse.toString()); - super.printStackTrace(); - } }