/// <summary> /// Adds the given remote device to the network. /// </summary> /// <remarks>Notice that this operation does not join the remote XBee device to the network; it /// just tells the network that it contains that device. However, the device has only been added /// to the device list, and may not be physically in the same network. /// /// The way of adding a device to the network is based on the 64-bit address. If it is not /// configured: /// <list type="bullet"> /// <item><description>For 802.15.4 and ZigBee devices, it will use the 16-bit address.</description></item> /// <item><description>For the rest will return <c>true</c> as the result of the addition.</description></item> /// </list></remarks> /// <param name="remoteDevice">The remote device to be added to the network.</param> /// <returns>The remote XBee Device instance in the network, <c>null</c> if the device could not be /// successfully added.</returns> /// <exception cref="ArgumentNullException">If <c><paramref name="remoteDevice"/> == null</c>.</exception> public RemoteXBeeDevice AddRemoteDevice(RemoteXBeeDevice remoteDevice) { if (remoteDevice == null) { throw new ArgumentNullException("Remote device cannot be null."); } logger.DebugFormat("{0}Adding device '{1}' to network.", localDevice.ToString(), remoteDevice.ToString()); RemoteXBeeDevice devInNetwork = null; XBee64BitAddress addr64 = remoteDevice.XBee64BitAddr; XBee16BitAddress addr16 = Get16BitAddress(remoteDevice); // Check if the device has 64-bit address. if (addr64 != null && !addr64.Equals(XBee64BitAddress.UNKNOWN_ADDRESS)) { // The device has 64-bit address, so look in the 64-bit map. if (remotesBy64BitAddr.TryGetValue(addr64, out devInNetwork)) { // The device exists in the 64-bit map, so update the reference and return it. logger.DebugFormat("{0}Existing device '{1}' in network.", localDevice.ToString(), devInNetwork.ToString()); devInNetwork.UpdateDeviceDataFrom(remoteDevice); return(devInNetwork); } else { // The device does not exist in the 64-bit map, so check its 16-bit address. if (addr16 != null && !addr16.Equals(XBee16BitAddress.UNKNOWN_ADDRESS)) { // The device has 16-bit address, so look in the 16-bit map. if (remotesBy16BitAddr.TryGetValue(addr16, out devInNetwork)) { // The device exists in the 16-bit map, so remove it and add it to the 64-bit map. logger.DebugFormat("{0}Existing device '{1}' in network.", localDevice.ToString(), devInNetwork.ToString()); remotesBy16BitAddr.TryRemove(addr16, out devInNetwork); devInNetwork.UpdateDeviceDataFrom(remoteDevice); remotesBy64BitAddr.AddOrUpdate(addr64, devInNetwork, (k, v) => devInNetwork); return(devInNetwork); } else { // The device does not exist in the 16-bit map, so add it to the 64-bit map. remotesBy64BitAddr.AddOrUpdate(addr64, remoteDevice, (k, v) => remoteDevice); return(remoteDevice); } } else { // The device has not 16-bit address, so add it to the 64-bit map. remotesBy64BitAddr.AddOrUpdate(addr64, remoteDevice, (k, v) => remoteDevice); return(remoteDevice); } } } // If the device has not 64-bit address, check if it has 16-bit address. if (addr16 != null && !addr16.Equals(XBee16BitAddress.UNKNOWN_ADDRESS)) { // The device has 16-bit address, so look in the 64-bit map. ICollection <RemoteXBeeDevice> devices = remotesBy64BitAddr.Values; foreach (RemoteXBeeDevice d in devices) { XBee16BitAddress a = Get16BitAddress(d); if (a != null && a.Equals(addr16)) { devInNetwork = d; break; } } // Check if the device exists in the 64-bit map. if (devInNetwork != null) { // The device exists in the 64-bit map, so update the reference and return it. logger.DebugFormat("{0}Existing device '{1}' in network.", localDevice.ToString(), devInNetwork.ToString()); devInNetwork.UpdateDeviceDataFrom(remoteDevice); return(devInNetwork); } else { // The device does not exist in the 64-bit map, so look in the 16-bit map. if (remotesBy16BitAddr.TryGetValue(addr16, out devInNetwork)) { // The device exists in the 16-bit map, so update the reference and return it. logger.DebugFormat("{0}Existing device '{1}' in network.", localDevice.ToString(), devInNetwork.ToString()); devInNetwork.UpdateDeviceDataFrom(remoteDevice); return(devInNetwork); } else { // The device does not exist in the 16-bit map, so add it. remotesBy16BitAddr.AddOrUpdate(addr16, remoteDevice, (k, v) => remoteDevice); return(remoteDevice); } } } // If the device does not contain a valid address, return null. logger.ErrorFormat("{0}Remote device '{1}' cannot be added: 64-bit and 16-bit addresses must be specified.", localDevice.ToString(), remoteDevice.ToString()); return(null); }