001/**
002 * Copyright (c) 2014 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.ATCommandStatus;
015
016/**
017 * This exception will be thrown when receiving a command response containing 
018 * a status different than {@code ATCommandStatus#OK} after sending an XBee 
019 * AT command.
020 * 
021 * @see CommunicationException
022 * @see com.digi.xbee.api.models.ATCommandStatus
023 */
024public class ATCommandException extends CommunicationException {
025
026        // Constants.
027        private static final long serialVersionUID = 1L;
028        private static final String DEFAULT_MESSAGE = "There was a problem sending the AT command packet.";
029        
030        // Variables.
031        private final ATCommandStatus atCommandStatus;
032        
033        /**
034         * Creates an {@code ATCommandException} with the provided 
035         * {@code ATCommandStatus} indicating the status of the AT command response 
036         * and {@value #DEFAULT_MESSAGE} as its error detail message.
037         * 
038         * @param atCommandStatus The status of the AT command response.
039         * 
040         * @see com.digi.xbee.api.models.ATCommandStatus
041         */
042        public ATCommandException(ATCommandStatus atCommandStatus) {
043                super(DEFAULT_MESSAGE);
044                this.atCommandStatus = atCommandStatus;
045        }
046        
047        /**
048         * Creates an {@code ATCommandException} with the specified message and 
049         * {@code ATCommandStatus} indicating the status of the AT command response.
050         * 
051         * @param message The associated message.
052         * @param atCommandStatus The status of the AT command response.
053         * 
054         * @see com.digi.xbee.api.models.ATCommandStatus
055         */
056        public ATCommandException(String message, ATCommandStatus atCommandStatus) {
057                super(message);
058                this.atCommandStatus = atCommandStatus;
059        }
060        
061        /**
062         * Creates an {@code ATCommandException} with the specified message, cause 
063         * and {@code ATCommandStatus} indicating the status of the AT command 
064         * response.
065         * 
066         * @param message The associated message.
067         * @param cause The cause of this exception.
068         * @param atCommandStatus The status of the AT command response.
069         * 
070         * @see Throwable
071         * @see com.digi.xbee.api.models.ATCommandStatus
072         */
073        public ATCommandException(String message, Throwable cause, ATCommandStatus atCommandStatus) {
074                super(message, cause);
075                this.atCommandStatus = atCommandStatus;
076        }
077        
078        /**
079         * Returns the {@code ATCommandStatus} of the exception containing 
080         * information about the AT command response.
081         * 
082         * @return The status of the AT command response.
083         * 
084         * @see com.digi.xbee.api.models.ATCommandStatus
085         */
086        public ATCommandStatus getCommandStatus() {
087                return atCommandStatus;
088        }
089        
090        /**
091         * Returns the text containing the status of the AT command response 
092         * from the exception.
093         * 
094         * @return The text with the status of the AT command response.
095         */
096        public String getCommandStatusMessage() {
097                if (atCommandStatus != null)
098                        return atCommandStatus.getDescription();
099                return null;
100        }
101        
102        /*
103         * (non-Javadoc)
104         * @see java.lang.Throwable#getMessage()
105         */
106        @Override
107        public String getMessage() {
108                if (atCommandStatus != null)
109                        return super.getMessage() + " > " + atCommandStatus.getDescription();
110                return super.getMessage();
111        }
112}