    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;
    013
    014import java.io.IOException;
    015
    016import com.digi.xbee.api.exceptions.InterfaceNotOpenException;
    017import com.digi.xbee.api.exceptions.TimeoutException;
    018import com.digi.xbee.api.exceptions.XBeeException;
    019import com.digi.xbee.api.models.ATCommand;
    020import com.digi.xbee.api.models.ATCommandResponse;
    021import com.digi.xbee.api.models.XBee16BitAddress;
    022import com.digi.xbee.api.models.XBee64BitAddress;
    023import com.digi.xbee.api.models.XBeeProtocol;
    024
    025/**
    026 * This class represents a remote XBee device.
    027 *
    028 * @see RemoteDigiMeshDevice
    029 * @see RemoteDigiPointDevice
    030 * @see RemoteRaw802Device
    031 * @see RemoteZigBeeDevice
    032 */
    033public class RemoteXBeeDevice extends AbstractXBeeDevice {
    034
    035        /**
    036         * Class constructor. Instantiates a new {@code RemoteXBeeDevice} object
    037         * with the given local {@code XBeeDevice} which contains the connection
    038         * interface to be used.
    039         *
    040         * @param localXBeeDevice The local XBee device that will behave as
    041         *                        connection interface to communicate with this
    042         *                        remote XBee device.
    043         * @param addr64 The 64-bit address to identify this remote XBee device.
    044         *
    045         * @throws IllegalArgumentException if {@code localXBeeDevice.isRemote() == true}.
    046         * @throws NullPointerException if {@code localXBeeDevice == null} or
    047         *                              if {@code addr64 == null}.
    048         *
    049         * @see com.digi.xbee.api.models.XBee64BitAddress
    050         */
    051        public RemoteXBeeDevice(XBeeDevice localXBeeDevice, XBee64BitAddress addr64) {
    052                super(localXBeeDevice, addr64);
    053        }
    054
    055        /**
    056         * Class constructor. Instantiates a new {@code RemoteXBeeDevice} object
    057         * with the given local {@code XBeeDevice} which contains the connection
    058         * interface to be used.
    059         *
    060         * @param localXBeeDevice The local XBee device that will behave as
    061         *                        connection interface to communicate with this
    062         *                        remote XBee device.
    063         * @param addr64 The 64-bit address to identify this remote XBee device.
    064         * @param addr16 The 16-bit address to identify this remote XBee device. It
    065         *               might be {@code null}.
    066         * @param ni The node identifier of this remote XBee device. It might be
    067         *           {@code null}.
    068         *
    069         * @throws IllegalArgumentException if {@code localXBeeDevice.isRemote() == true}.
    070         * @throws NullPointerException if {@code localXBeeDevice == null} or
    071         *                              if {@code addr64 == null}.
    072         *
    073         * @see com.digi.xbee.api.models.XBee16BitAddress
    074         * @see com.digi.xbee.api.models.XBee64BitAddress
    075         */
    076        public RemoteXBeeDevice(XBeeDevice localXBeeDevice, XBee64BitAddress addr64,
    077                        XBee16BitAddress addr16, String ni) {
    078                super(localXBeeDevice, addr64, addr16, ni);
    079        }
    080
    081        /**
    082         * Always returns {@code true}, since it is a remote device.
    083         *
    084         * @return {@code true} always.
    085         */
    086        @Override
    087        public boolean isRemote() {
    088                return true;
    089        }
    090
    091        /*
    092         * (non-Javadoc)
    093         * @see com.digi.xbee.api.AbstractXBeeDevice#reset()
    094         */
    095        @Override
    096        public void reset() throws TimeoutException, XBeeException {
    097                // Check connection.
    098                if (!connectionInterface.isOpen())
    099                        throw new InterfaceNotOpenException();
    100
    101                logger.info(toString() + "Resetting the remote module ({})...", get64BitAddress());
    102
    103                ATCommandResponse response = null;
    104                try {
    105                        response = sendATCommand(new ATCommand("FR"));
    106                } catch (IOException e) {
    107                        throw new XBeeException("Error writing in the communication interface.", e);
    108                } catch (TimeoutException e) {
    109                        // Remote 802.15.4 devices do not respond to the AT command.
    110                        if (localXBeeDevice.getXBeeProtocol() == XBeeProtocol.RAW_802_15_4)
    111                                return;
    112                        else
    113                                throw e;
    114                }
    115
    116                // Check if AT Command response is valid.
    117                checkATCommandResponseIsValid(response);
    118        }
    119
    120        /*
    121         * (non-Javadoc)
    122         * @see com.digi.xbee.api.AbstractXBeeDevice#toString()
    123         */
    124        @Override
    125        public String toString() {
    126                String id = getNodeID();
    127                if (id == null)
    128                        id = "";
    129                return String.format("%s - %s", get64BitAddress(), getNodeID());
    130        }
    131}