001/**
002 * Copyright (c) 2014-2015 Digi International Inc.,
003 * All rights not expressly granted are reserved.
004 *
005 * This Source Code Form is subject to the terms of the Mozilla Public
006 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
007 * You can obtain one at http://mozilla.org/MPL/2.0/.
008 *
009 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
010 * =======================================================================
011 */
012package com.digi.xbee.api.exceptions;
013
014import com.digi.xbee.api.models.XBeeTransmitStatus;
015
016/**
017 * This exception will be thrown when receiving a transmit status different than 
018 * {@code XBeeTransmitStatus#SUCCESS} after sending an XBee API packet.
019 * 
020 * @see CommunicationException
021 * @see com.digi.xbee.api.models.XBeeTransmitStatus
022 */
023public class TransmitException extends CommunicationException {
024
025        // Constants.
026        private static final long serialVersionUID = 1L;
027        private static final String DEFAULT_MESSAGE = "There was a problem transmitting the XBee API packet.";
028        
029        // Variables.
030        private final XBeeTransmitStatus transmitStatus;
031        
032        /**
033         * Creates a {@code TransmitException} with the provided 
034         * {@code XBeeTransmitStatus} indicating the status of the transmission and 
035         * {@value #DEFAULT_MESSAGE} as its error detail message.
036         * 
037         * @param transmitStatus The status of the transmission.
038         * 
039         * @see com.digi.xbee.api.models.XBeeTransmitStatus
040         */
041        public TransmitException(XBeeTransmitStatus transmitStatus) {
042                super(DEFAULT_MESSAGE);
043                this.transmitStatus = transmitStatus;
044        }
045        
046        /**
047         * Creates a {@code TransmitException} with the specified message and 
048         * {@code XBeeTransmitStatus} indicating the status of the transmission.
049         * 
050         * @param message The associated message.
051         * @param transmitStatus The status of the transmission.
052         * 
053         * @see com.digi.xbee.api.models.XBeeTransmitStatus
054         */
055        public TransmitException(String message, XBeeTransmitStatus transmitStatus) {
056                super(message);
057                this.transmitStatus = transmitStatus;
058        }
059        
060        /**
061         * Creates a {@code TransmitException} with the specified message, cause and 
062         * {@code XBeeTransmitStatus} indicating the status of the transmission.
063         * 
064         * @param message The associated message.
065         * @param cause The cause of this exception.
066         * @param transmitStatus The status of the transmission.
067         * 
068         * @see Throwable
069         * @see com.digi.xbee.api.models.XBeeTransmitStatus
070         */
071        public TransmitException(String message, Throwable cause, XBeeTransmitStatus transmitStatus) {
072                super(message, cause);
073                this.transmitStatus = transmitStatus;
074        }
075        
076        /**
077         * Returns the {@code XBeeTransmitStatus} of the exception containing 
078         * information about the transmission.
079         * 
080         * @return The status of the transmission.
081         * 
082         * @see com.digi.xbee.api.models.XBeeTransmitStatus
083         */
084        public XBeeTransmitStatus getTransmitStatus() {
085                return transmitStatus;
086        }
087        
088        /**
089         * Returns the text containing the status of the transmission from 
090         * the exception.
091         * 
092         * @return The text with the status of the transmission.
093         */
094        public String getTransmitStatusMessage() {
095                if (transmitStatus != null)
096                        return transmitStatus.getDescription();
097                return null;
098        }
099        
100        /*
101         * (non-Javadoc)
102         * @see java.lang.Throwable#getMessage()
103         */
104        @Override
105        public String getMessage() {
106                if (transmitStatus != null)
107                        return super.getMessage() + " > " + transmitStatus.getDescription();
108                return super.getMessage();
109        }
110}