예제 #1
0
        /// <summary>
        /// Send an AT command to a node and wait for a response.
        /// </summary>
        /// <typeparam name="TResponseData">The expected response data type</typeparam>
        /// <param name="command">The command to send</param>
        /// <param name="address">The address of the node.  If this is null the command will be sent to the local node.</param>
        /// <returns>The response data</returns>
        public async Task <TResponseData> ExecuteAtQueryAsync <TResponseData>(AtCommand command,
                                                                              NodeAddress address = null)
            where TResponseData : AtCommandResponseFrameData
        {
            AtCommandResponseFrameContent responseContent;

            if (address == null)
            {
                var atCommandFrame = new AtCommandFrameContent(command);
                AtCommandResponseFrame response = await ExecuteQueryAsync <AtCommandResponseFrame>(atCommandFrame, DefaultLocalQueryTimeout);

                responseContent = response.Content;
            }
            else
            {
                address.ShortAddress = address.LongAddress.IsBroadcast ? ShortAddress.Broadcast : ShortAddress.Disabled;

                var remoteCommand = new RemoteAtCommandFrameContent(address, command);
                RemoteAtCommandResponseFrame response =
                    await ExecuteQueryAsync <RemoteAtCommandResponseFrame>(remoteCommand, DefaultRemoteQueryTimeout);

                responseContent = response.Content;
            }

            if (responseContent.Status != AtCommandStatus.Success)
            {
                throw new AtCommandException(responseContent.Status);
            }

            return(responseContent.Data as TResponseData);
        }
예제 #2
0
        /// <summary>
        ///     Start network discovery.  The discovery of a node will result in a <see cref="NodeDiscovered" /> event.
        /// </summary>
        /// <param name="timeout">The amount of time to wait until discovery responses are ignored</param>
        /// <remarks>During network discovery nodes may be unresponsive</remarks>
        public async Task DiscoverNetworkAsync(TimeSpan timeout)
        {
            var atCommandFrame = new AtCommandFrameContent(new NetworkDiscoveryCommand());

            await ExecuteMultiQueryAsync(atCommandFrame, new Action <AtCommandResponseFrame>(
                                             async frame =>
            {
                var discoveryData = (NetworkDiscoveryResponseData)frame.Content.Data;

                if (NodeDiscovered != null && discoveryData?.LongAddress != null && !discoveryData.IsCoordinator)
                {
                    var address = new NodeAddress(discoveryData.LongAddress, discoveryData.ShortAddress);

                    // XBees have trouble recovering from discovery
                    await Task.Delay(500);

                    try
                    {
                        var node = await GetNodeAsync(address);

                        var signalStrength = discoveryData.ReceivedSignalStrengthIndicator?.SignalStrength;

                        NodeDiscovered?.Invoke(this,
                                               new NodeDiscoveredEventArgs(discoveryData.Name, signalStrength,
                                                                           node));
                    }
                    catch (TimeoutException)
                    {
                        /* if we timeout getting the remote node info, no need to bubble up.
                         * We just won't include the node in discovery */
                    }
                }
            }), timeout);
        }
예제 #3
0
        /// <summary>
        ///     Start network discovery.  The discovery of a node will result in a <see cref="NodeDiscovered" /> event.
        /// </summary>
        /// <param name="timeout">The amount of time to wait until discovery responses are ignored</param>
        /// <param name="cancellationToken"></param>
        /// <remarks>During network discovery nodes may be unresponsive</remarks>
        public Task DiscoverNetworkAsync(TimeSpan timeout,
                                         CancellationToken cancellationToken = default(CancellationToken))
        {
            var atCommandFrame = new AtCommandFrameContent(new NetworkDiscoveryCommand());

            return(ExecuteMultiQueryAsync(atCommandFrame, new Action <AtCommandResponseFrame>(
                                              async frame => await OnNodeDiscovered(frame, cancellationToken)), timeout, cancellationToken));
        }
예제 #4
0
파일: FrameTests.cs 프로젝트: splitice/XBee
        public void AtCommand_CoordinatorEnable_FrameTest()
        {
            var atCommandFrame = new AtCommandFrameContent(new CoordinatorEnableCommand())
            {
                FrameId = 0x33
            };

            var expectedValue = new byte[] { 0x7e, 0x00, 0x04, 0x08, 0x33, 0x43, 0x45, 0x3c };

            Check(atCommandFrame, expectedValue);
        }
예제 #5
0
파일: FrameTests.cs 프로젝트: splitice/XBee
        public void AtCommandFrameTest()
        {
            var atCommandFrame = new AtCommandFrameContent(new ForceSampleCommand())
            {
                FrameId = 0x52
            };

            var expectedValue = new byte[] { 0x7e, 0x00, 0x04, 0x08, 0x52, 0x49, 0x53, 0x09 };

            Check(atCommandFrame, expectedValue);
        }
예제 #6
0
파일: FrameTests.cs 프로젝트: splitice/XBee
        public void AtCommand_BaudRateQuery_FrameTest()
        {
            var atCommandFrame = new AtCommandFrameContent(new BaudRateCommand())
            {
                FrameId = 0x01
            };

            var expectedValue = new byte[] { 0x7e, 0x00, 0x04, 0x08, 0x01, 0x42, 0x44, 0x70 };

            Check(atCommandFrame, expectedValue);
        }
예제 #7
0
 /// <summary>
 /// Send an AT command to this node.
 /// </summary>
 /// <param name="command"></param>
 /// <param name="address"></param>
 /// <returns></returns>
 public async Task ExecuteAtCommand(AtCommand command, NodeAddress address = null)
 {
     if (address == null)
     {
         var atCommandFrame = new AtCommandFrameContent(command);
         await ExecuteAsync(atCommandFrame);
     }
     else
     {
         var remoteCommand = new RemoteAtCommandFrameContent(address, command);
         await ExecuteAsync(remoteCommand);
     }
 }
예제 #8
0
        /// <summary>
        /// Start network discovery.  The discovery of a node will result in a <see cref="NodeDiscovered"/> event.
        /// </summary>
        /// <param name="timeout">The amount of time to wait until discovery responses are ignored</param>
        /// <remarks>During network discovery nodes may be unresponsive</remarks>
        public async Task DiscoverNetwork(TimeSpan timeout)
        {
            var atCommandFrame = new AtCommandFrameContent(new NetworkDiscoveryCommand());

            await ExecuteMultiQueryAsync(atCommandFrame, new Action <AtCommandResponseFrame>(
                                             async frame =>
            {
                var discoveryData = (NetworkDiscoveryResponseData)frame.Content.Data;

                if (NodeDiscovered != null && discoveryData != null && !discoveryData.IsCoordinator)
                {
                    var address = new NodeAddress(discoveryData.LongAddress, discoveryData.ShortAddress);

                    /* For some reason it doesn't like answering us during ND */
                    // TODO find better approach for this
                    XBeeNode node = null;
                    for (int i = 0; i < 5; i++)
                    {
                        try
                        {
                            node = await GetRemoteNodeAsync(address);
                            break;
                        }
                        catch (TimeoutException)
                        {
                        }
                        catch (AtCommandException)
                        {
                        }
                    }

                    if (node == null)
                    {
                        throw new TimeoutException();
                    }

                    var signalStrength = discoveryData.ReceivedSignalStrengthIndicator?.SignalStrength;

                    NodeDiscovered(this,
                                   new NodeDiscoveredEventArgs(discoveryData.Name, signalStrength,
                                                               node));
                }
            }), timeout);
        }