001/**
002 * Copyright 2017, Digi International Inc.
003 *
004 * This Source Code Form is subject to the terms of the Mozilla Public
005 * License, v. 2.0. If a copy of the MPL was not distributed with this
006 * file, you can obtain one at http://mozilla.org/MPL/2.0/.
007 *
008 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 
009 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 
010 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 
011 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 
012 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
013 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
014 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
015 */
016package com.digi.xbee.api.exceptions;
017
018import com.digi.xbee.api.models.ATCommandStatus;
019
020/**
021 * This exception will be thrown when receiving a command response containing 
022 * a status different than {@code ATCommandStatus#OK} after sending an XBee 
023 * AT command.
024 * 
025 * @see CommunicationException
026 * @see com.digi.xbee.api.models.ATCommandStatus
027 */
028public class ATCommandException extends CommunicationException {
029
030        // Constants.
031        private static final long serialVersionUID = 1L;
032        private static final String DEFAULT_MESSAGE = "There was a problem sending the AT command packet.";
033        
034        // Variables.
035        private final ATCommandStatus atCommandStatus;
036        
037        /**
038         * Creates an {@code ATCommandException} with the provided 
039         * {@code ATCommandStatus} indicating the status of the AT command response 
040         * and {@value #DEFAULT_MESSAGE} as its error detail message.
041         * 
042         * @param atCommandStatus The status of the AT command response.
043         * 
044         * @see com.digi.xbee.api.models.ATCommandStatus
045         */
046        public ATCommandException(ATCommandStatus atCommandStatus) {
047                super(DEFAULT_MESSAGE);
048                this.atCommandStatus = atCommandStatus;
049        }
050        
051        /**
052         * Creates an {@code ATCommandException} with the specified message and 
053         * {@code ATCommandStatus} indicating the status of the AT command response.
054         * 
055         * @param message The associated message.
056         * @param atCommandStatus The status of the AT command response.
057         * 
058         * @see com.digi.xbee.api.models.ATCommandStatus
059         */
060        public ATCommandException(String message, ATCommandStatus atCommandStatus) {
061                super(message);
062                this.atCommandStatus = atCommandStatus;
063        }
064        
065        /**
066         * Creates an {@code ATCommandException} with the specified message, cause 
067         * and {@code ATCommandStatus} indicating the status of the AT command 
068         * response.
069         * 
070         * @param message The associated message.
071         * @param cause The cause of this exception.
072         * @param atCommandStatus The status of the AT command response.
073         * 
074         * @see Throwable
075         * @see com.digi.xbee.api.models.ATCommandStatus
076         */
077        public ATCommandException(String message, Throwable cause, ATCommandStatus atCommandStatus) {
078                super(message, cause);
079                this.atCommandStatus = atCommandStatus;
080        }
081        
082        /**
083         * Returns the {@code ATCommandStatus} of the exception containing 
084         * information about the AT command response.
085         * 
086         * @return The status of the AT command response.
087         * 
088         * @see com.digi.xbee.api.models.ATCommandStatus
089         */
090        public ATCommandStatus getCommandStatus() {
091                return atCommandStatus;
092        }
093        
094        /**
095         * Returns the text containing the status of the AT command response 
096         * from the exception.
097         * 
098         * @return The text with the status of the AT command response.
099         */
100        public String getCommandStatusMessage() {
101                if (atCommandStatus != null)
102                        return atCommandStatus.getDescription();
103                return null;
104        }
105        
106        /*
107         * (non-Javadoc)
108         * @see java.lang.Throwable#getMessage()
109         */
110        @Override
111        public String getMessage() {
112                String message = super.getMessage();
113                
114                if (message == null)
115                        message = "";
116                
117                if (atCommandStatus != null) {
118                        if (message.length() > 0)
119                                message = message + " > ";
120                        message = message + atCommandStatus.getDescription();
121                }
122                
123                return message;
124        }
125}