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.models;
013
014import com.digi.xbee.api.RemoteXBeeDevice;
015
016/**
017 * This class represents an XBee message containing the remote XBee device the 
018 * message belongs to, the content (data) of the message and a flag indicating 
019 * if the message is a broadcast message (was received or is being sent via 
020 * broadcast). 
021 * 
022 * <p>This class is used within the XBee Java Library to read data sent by 
023 * remote devices.</p>
024 */
025public class XBeeMessage {
026
027        // Variables.
028        private final RemoteXBeeDevice remoteXBeeDevice;
029        private final byte[] data;
030        private boolean isBroadcast;
031        
032        /**
033         * Class constructor. Instantiates a new object of type 
034         * {@code XBeeMessage} with the given parameters.
035         * 
036         * @param remoteXBeeDevice The remote XBee device the message belongs to.
037         * @param data Byte array containing the data of the message.
038         * 
039         * @throws NullPointerException if {@code address == null} or
040         *                              if {@code data == null}.
041         * 
042         * @see com.digi.xbee.api.RemoteXBeeDevice
043         */
044        public XBeeMessage(RemoteXBeeDevice remoteXBeeDevice, byte[] data) {
045                this(remoteXBeeDevice, data, false);
046        }
047        
048        /**
049         * Class constructor. Instantiates a new object of type 
050         * {@code XBeeMessage} with the given parameters.
051         * 
052         * @param remoteXBeeDevice The remote XBee device the message belongs to.
053         * @param data Byte array containing the data of the message.
054         * @param isBroadcast Indicates if the message was received via broadcast.
055         * 
056         * @throws NullPointerException if {@code xbeeAddress == null} or
057         *                              if {@code data == null}.
058         * 
059         * @see com.digi.xbee.api.RemoteXBeeDevice
060         */
061        public XBeeMessage(RemoteXBeeDevice remoteXBeeDevice, byte[] data, boolean isBroadcast) {
062                if (remoteXBeeDevice == null)
063                        throw new NullPointerException("Remote XBee device cannot be null.");
064                if (data == null)
065                        throw new NullPointerException("Data cannot be null.");
066                
067                this.remoteXBeeDevice = remoteXBeeDevice;
068                this.data = data;
069                this.isBroadcast = isBroadcast;
070        }
071        
072        /**
073         * Returns the remote XBee device this message is associated to.
074         * 
075         * @return The remote XBee device this message is associated to.
076         * 
077         * @see com.digi.xbee.api.RemoteXBeeDevice
078         */
079        public RemoteXBeeDevice getDevice() {
080                return remoteXBeeDevice;
081        }
082        
083        /**
084         * Returns the a byte array containing the data of the message.
085         * 
086         * @return A byte array containing the data of the message.
087         */
088        public byte[] getData() {
089                return data;
090        }
091        
092        /**
093         * Returns the data of the message in string format.
094         * 
095         * @return The data of the message in string format.
096         */
097        public String getDataString() {
098                return new String(data);
099        }
100        
101        /**
102         * Returns whether or not the message was received via broadcast.
103         * 
104         * @return {@code true} if the message was received via broadcast, 
105         *         {@code false} otherwise.
106         */
107        public boolean isBroadcast() {
108                return isBroadcast;
109        }
110}