/// <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); }
private async Task OnNodeDiscovered(AtCommandResponseFrame frame, CancellationToken cancellationToken) { var discoveryData = (NetworkDiscoveryResponseData)frame.Content.Data; if (NodeDiscovered == null || discoveryData?.LongAddress == null || discoveryData.IsCoordinator) { return; } var address = new NodeAddress(discoveryData.LongAddress, discoveryData.ShortAddress); // XBees have trouble recovering from discovery await Task.Delay(1000, cancellationToken); 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 */ } }
public void AtCommandResponseFrameTest() { var atResponseCommandFrame = new AtCommandResponseFrame { FrameId = 0x01, Content = new AtCommandResponseFrameContent { AtCommand = "BD" } }; var expectedValue = new byte[] { 0x7e, 0x00, 0x05, 0x88, 0x01, 0x42, 0x44, 0x00, 0xf0 }; Check(atResponseCommandFrame, expectedValue); }