/// <summary>
        /// Discovers the IeeeAddress of a remote device. This uses a broadcast request to try to discover the
        /// device.
        ///
        /// <param name="networkAddress">the network address of the node</param>
        /// <returns>true if the message was processed ok</returns>
        /// </summary>
        private async Task <bool> GetIeeeAddress(ushort networkAddress)
        {
            // Request basic response, start index for associated list is 0
            IeeeAddressRequest request = new IeeeAddressRequest();

            request.RequestType        = 0;
            request.StartIndex         = 0;
            request.NwkAddrOfInterest  = networkAddress;
            request.DestinationAddress = new ZigBeeEndpointAddress(ZigBeeBroadcastDestination.GetBroadcastDestination(BroadcastDestination.BROADCAST_RX_ON).Key);
            CommandResult response = await _networkManager.SendTransaction(request, request);

            if (response.IsError())
            {
                return(false);
            }

            IeeeAddressResponse ieeeAddressResponse = (IeeeAddressResponse)response.GetResponse();

            Log.Debug("{NetworkAddress}: NWK Discovery IeeeAddressRequest returned {IeeeAddressResponse}", networkAddress, ieeeAddressResponse);

            if (ieeeAddressResponse != null && ieeeAddressResponse.Status == ZdoStatus.SUCCESS)
            {
                AddNode(ieeeAddressResponse.IeeeAddrRemoteDev, ieeeAddressResponse.NwkAddrRemoteDev);
                StartNodeDiscovery(ieeeAddressResponse.NwkAddrRemoteDev);
                return(true);
            }

            return(false);
        }
示例#2
0
        /// <summary>
        /// Get Node Network address and the list of associated devices
        ///
        /// <returns>true if the message was processed ok</returns>
        /// </summary>
        private async Task <bool> RequestAssociatedNodes()
        {
            byte             startIndex             = 0;
            int              totalAssociatedDevices = 0;
            HashSet <ushort> associatedDevices      = new HashSet <ushort>();

            do
            {
                // Request extended response, to get associated list
                IeeeAddressRequest ieeeAddressRequest = new IeeeAddressRequest();
                ieeeAddressRequest.DestinationAddress = new ZigBeeEndpointAddress((ushort)Node.NetworkAddress);
                ieeeAddressRequest.RequestType        = 1;
                ieeeAddressRequest.StartIndex         = startIndex;
                ieeeAddressRequest.NwkAddrOfInterest  = Node.NetworkAddress;
                CommandResult response = await NetworkManager.SendTransaction(ieeeAddressRequest, ieeeAddressRequest);

                IeeeAddressResponse ieeeAddressResponse = (IeeeAddressResponse)response.Response;

                Log.Debug("{IeeeAddress}: Node SVC Discovery: IeeeAddressResponse returned {Response}", Node.IeeeAddress, ieeeAddressResponse);

                if (ieeeAddressResponse != null && ieeeAddressResponse.Status == ZdoStatus.SUCCESS)
                {
                    associatedDevices.UnionWith(ieeeAddressResponse.NwkAddrAssocDevList);

                    startIndex            += (byte)ieeeAddressResponse.NwkAddrAssocDevList.Count;
                    totalAssociatedDevices = ieeeAddressResponse.NwkAddrAssocDevList.Count;
                }
            } while (startIndex < totalAssociatedDevices);

            _updatedNode.AssociatedDevices = associatedDevices;

            return(true);
        }
        /**
         * Get Node IEEE address
         *
         * @param networkAddress the network address of the node
         * @return true if the message was processed ok
         */
        private async Task <bool> GetIeeeAddress(ushort networkAddress)
        {
            try
            {
                byte          startIndex             = 0;
                int           totalAssociatedDevices = 0;
                List <ushort> associatedDevices      = new List <ushort>();
                IeeeAddress   ieeeAddress            = null;

                do
                {
                    // Request extended response, start index for associated list is 0
                    IeeeAddressRequest ieeeAddressRequest = new IeeeAddressRequest();
                    ieeeAddressRequest.DestinationAddress = new ZigBeeEndpointAddress(networkAddress);
                    ieeeAddressRequest.RequestType        = 1;
                    ieeeAddressRequest.StartIndex         = startIndex;
                    ieeeAddressRequest.NwkAddrOfInterest  = networkAddress;
                    CommandResult response = await _networkManager.SendTransaction(ieeeAddressRequest, ieeeAddressRequest);

                    if (response.IsError())
                    {
                        return(false);
                    }

                    IeeeAddressResponse ieeeAddressResponse = response.GetResponse <IeeeAddressResponse>();
                    _logger.Debug("{NetworkAddress}: NWK Discovery IeeeAddressRequest returned {IeeeAddress}", networkAddress, ieeeAddressResponse);
                    if (ieeeAddressResponse != null && ieeeAddressResponse.Status == ZdoStatus.SUCCESS)
                    {
                        ieeeAddress = ieeeAddressResponse.IeeeAddrRemoteDev;
                        if (startIndex.Equals(ieeeAddressResponse.StartIndex))
                        {
                            associatedDevices.AddRange(ieeeAddressResponse.NwkAddrAssocDevList);

                            startIndex            += (byte)ieeeAddressResponse.NwkAddrAssocDevList.Count;
                            totalAssociatedDevices = ieeeAddressResponse.NwkAddrAssocDevList.Count;
                        }
                    }
                } while (startIndex < totalAssociatedDevices);

                AddNode(ieeeAddress, networkAddress);

                // Start discovery for any associated nodes
                foreach (ushort deviceNetworkAddress in associatedDevices)
                {
                    StartNodeDiscovery(deviceNetworkAddress);
                }
            }
            catch (Exception e)
            {
                _logger.Debug("NWK Discovery Error in checkIeeeAddressResponse {Error}", e);
            }

            return(true);
        }
        /**
         * Get the associated nodes for this address, and start a discovery of the associated nodes.
         *
         * <param name="networkAddress">networkAddress the network address of the node</param>
         * <returns>true if the message was processed ok</returns>
         */
        private async Task <bool> GetAssociatedNodes(ushort networkAddress)
        {
            int           startIndex             = 0;
            int           totalAssociatedDevices = 0;
            List <ushort> associatedDevices      = new List <ushort>();

            do
            {
                // Request extended response, start index for associated list is 0
                IeeeAddressRequest ieeeAddressRequest = new IeeeAddressRequest();
                ieeeAddressRequest.DestinationAddress = new ZigBeeEndpointAddress(networkAddress);
                ieeeAddressRequest.RequestType        = 1;
                ieeeAddressRequest.StartIndex         = (byte)startIndex;
                ieeeAddressRequest.NwkAddrOfInterest  = networkAddress;
                CommandResult response = await _networkManager.SendTransaction(ieeeAddressRequest, ieeeAddressRequest);

                if (response.IsError())
                {
                    return(false);
                }

                IeeeAddressResponse ieeeAddressResponse = (IeeeAddressResponse)response.GetResponse();

                Log.Debug("{NetworkAddress}: NWK Discovery IeeeAddressRequest returned {IeeeAddressResponse}", networkAddress, ieeeAddressResponse);

                if (ieeeAddressResponse != null && ieeeAddressResponse.Status == ZdoStatus.SUCCESS && startIndex.Equals(ieeeAddressResponse.StartIndex))
                {
                    associatedDevices.AddRange(ieeeAddressResponse.NwkAddrAssocDevList);

                    startIndex            += ieeeAddressResponse.NwkAddrAssocDevList.Count;
                    totalAssociatedDevices = ieeeAddressResponse.NwkAddrAssocDevList.Count;
                }
            } while (startIndex < totalAssociatedDevices);

            // Start discovery for any associated nodes
            foreach (var deviceNetworkAddress in associatedDevices)
            {
                StartNodeDiscovery(deviceNetworkAddress);
            }

            return(true);
        }
        /// <summary>
        /// Starts a discovery on a node given the network address.
        ///
        /// <param name="requeryPeriod">networkAddress the network address of the node to discover</param>
        /// </summary>
        public void RediscoverNode(ushort networkAddress)
        {
            if (!_initialized)
            {
                Log.Debug("Network discovery task: can't perform rediscovery on {NetworkAddress} until initialization complete.", networkAddress);
                return;
            }

            Log.Debug("{NetworkAddress}: NWK Discovery starting node rediscovery", networkAddress);
            int retries = 0;

            Task.Run(async() =>
            {
                try
                {
                    do
                    {
                        // Request basic response, start index for associated list is 0
                        IeeeAddressRequest ieeeAddressRequest = new IeeeAddressRequest();
                        ieeeAddressRequest.DestinationAddress = new ZigBeeEndpointAddress(ZigBeeBroadcastDestination.GetBroadcastDestination(BroadcastDestination.BROADCAST_RX_ON).Key);
                        ieeeAddressRequest.RequestType        = 0;
                        ieeeAddressRequest.StartIndex         = 0;
                        ieeeAddressRequest.NwkAddrOfInterest  = networkAddress;
                        CommandResult response = await _networkManager.SendTransaction(ieeeAddressRequest, ieeeAddressRequest);

                        if (response.IsError())
                        {
                            return;
                        }

                        IeeeAddressResponse ieeeAddressResponse = (IeeeAddressResponse)response.GetResponse();

                        Log.Debug("{NetworkAddress}: NWK Discovery IeeeAddressRequest returned {IeeeAddressResponse}", networkAddress, ieeeAddressResponse);

                        if (ieeeAddressResponse != null && ieeeAddressResponse.Status == ZdoStatus.SUCCESS)
                        {
                            AddNode(ieeeAddressResponse.IeeeAddrRemoteDev, ieeeAddressResponse.NwkAddrRemoteDev);
                            StartNodeDiscovery(ieeeAddressResponse.NwkAddrRemoteDev);
                            break;
                        }

                        // We failed with the last request. Wait a bit then retry
                        try
                        {
                            Log.Debug("{NetworkAddress}: NWK Discovery node rediscovery request failed. Wait before retry.", networkAddress);

                            await Task.Delay(_retryPeriod);
                        }
                        catch (ThreadAbortException)
                        {
                            break;
                        }
                    } while (retries++ < _retryCount);
                }
                catch (Exception e) // TODO: Handle more secific Exception here (ThreadAbortException etc.)
                {
                    Log.Debug("NWK Discovery Error in checkIeeeAddressResponse ", e);
                }

                Log.Debug("{NetworkAddress}: NWK Discovery finishing node rediscovery after {Retries} attempts", networkAddress, retries);
            });

            // StartNodeDiscovery(nodeAddress);
        }