Example #1
0
        /// <summary>
        /// Get node descriptor
        ///
        /// <returns>true if the message was processed ok</returns>
        /// </summary>
        private async Task <bool> RequestNetworkAddress()
        {
            NetworkAddressRequest networkAddressRequest = new NetworkAddressRequest();

            networkAddressRequest.IeeeAddr           = Node.IeeeAddress;
            networkAddressRequest.RequestType        = 0x00;
            networkAddressRequest.StartIndex         = 0;
            networkAddressRequest.DestinationAddress = new ZigBeeEndpointAddress(ZigBeeBroadcastDestination.GetBroadcastDestination(BroadcastDestination.BROADCAST_ALL_DEVICES).Key);

            CommandResult response = await NetworkManager.SendTransaction(networkAddressRequest, networkAddressRequest);

            NetworkAddressResponse networkAddressResponse = response.GetResponse <NetworkAddressResponse>();

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

            if (networkAddressResponse == null)
            {
                return(false);
            }

            if (networkAddressResponse.Status == ZdoStatus.SUCCESS)
            {
                if (Node.NetworkAddress != networkAddressResponse.NwkAddrRemoteDev)
                {
                    Node.NetworkAddress = networkAddressResponse.NwkAddrRemoteDev;
                    NetworkManager.UpdateNode(_updatedNode);
                }
                return(true);
            }

            return(false);
        }
        /// <summary>
        /// Starts a discovery on a node. This will send a <see cref="NetworkAddressRequest"/> as a broadcast and will receive
        /// the response to trigger a full discovery.
        ///
        /// <param name="ieeeAddress">the <see cref="IeeeAddress"/> of the node to discover</param>
        /// </summary>
        public void RediscoverNode(IeeeAddress ieeeAddress)
        {
            if (!_initialized)
            {
                Log.Debug("Network discovery task: can't perform rediscovery on {IeeeAddress} until initialization complete.",
                          ieeeAddress);
                return;
            }

            Task.Run(async() =>
            {
                Log.Debug("{IeeeAddress}: NWK Discovery starting node rediscovery", ieeeAddress);
                int retries = 0;
                try
                {
                    do
                    {
                        if (Thread.CurrentThread.ThreadState == ThreadState.WaitSleepJoin)
                        {
                            break;
                        }

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

                        NetworkAddressResponse nwkAddressResponse = response.GetResponse <NetworkAddressResponse>();
                        if (nwkAddressResponse != null && nwkAddressResponse.Status == ZdoStatus.SUCCESS)
                        {
                            AddNode(nwkAddressResponse.IeeeAddrRemoteDev, nwkAddressResponse.NwkAddrRemoteDev);
                            StartNodeDiscovery(nwkAddressResponse.NwkAddrRemoteDev);
                            break;
                        }

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

                            await Task.Delay(_retryPeriod);
                        }
                        catch (Exception)
                        {
                            break;
                        }
                    } while (retries++ < _retryCount);
                }
                catch (Exception e)
                {
                    Log.Debug("NWK Discovery error in rediscoverNode ", e);
                }

                Log.Debug("{IeeeAddress}: NWK Discovery finishing node rediscovery", ieeeAddress);
            });
        }