public static IXBeeResponse GetXBeeFrame(int[] data) { Type xbeeClass = null; if (data[2] == 0x88) { xbeeClass = _atCommands[(data[4] << 8) + data[5]]; } if (xbeeClass == null) { _events.TryGetValue(data[2], out xbeeClass); } if (xbeeClass == null) { return(null); } try { IXBeeResponse xbeeFrame = Activator.CreateInstance(xbeeClass) as IXBeeResponse; xbeeFrame.Deserialize(data); return(xbeeFrame); } catch (Exception ex) { Log.Debug("Error creating instance of IXBeeResponse", ex); } return(null); }
/// <summary> /// Notify any transaction listeners when we receive a response. /// </summary> /// <param name="response">The <see cref="IXBeeEvent"/> data received</param> /// <returns>true if the response was processed</returns> private bool NotifyResponseReceived(IXBeeResponse response) { bool processed = false; Log.Debug($"RX XBEE: {response.ToString()}"); lock (_transactionListeners) { foreach (IXBeeListener listener in _transactionListeners) { try { if (listener.TransactionEvent(response)) { processed = true; } } catch (Exception e) { Log.Debug("Exception processing XBee frame: {}: ", response, e); } } } return(processed); }
public void TestGetResponse2() { int[] data = GetPacketData("00 05 88 10 4F 49 02 CD"); IXBeeResponse frame = XBeeResponseFactory.GetXBeeFrame(data); Assert.True(frame is XBeePanIdResponse); _output.WriteLine(frame.ToString()); XBeePanIdResponse responseEvent = (XBeePanIdResponse)frame; Assert.Equal(CommandStatus.INVALID_COMMAND, responseEvent.GetCommandStatus()); }
public void TestGetResponse1() { int[] data = GetPacketData("00 07 8B 8F F7 7B 00 00 40 33"); IXBeeResponse frame = XBeeResponseFactory.GetXBeeFrame(data); Assert.True(frame is XBeeTransmitStatusResponse); _output.WriteLine(frame.ToString()); XBeeTransmitStatusResponse responseEvent = (XBeeTransmitStatusResponse)frame; Assert.Equal(143, responseEvent.GetFrameId()); Assert.Equal(0, responseEvent.GetTransmitRetryCount()); Assert.Equal(DeliveryStatus.SUCCESS, responseEvent.GetDeliveryStatus()); Assert.Equal(DiscoveryStatus.EXTENDED_TIMEOUT_DISCOVERY, responseEvent.GetDiscoveryStatus()); }
/// <summary> /// Transactions the event. /// </summary> /// <param name="response">The response.</param> /// <returns></returns> public bool TransactionEvent(IXBeeResponse response) { // Check if this response completes our transaction int frameId = response.GetFrameId(); if (response.GetFrameId() != OurFrameId) { return(false); } lock (_lockObject) { CompletionResponse = response; Complete = true; } return(true); }
/// <summary> /// Construct which sets input stream where the packet is read from the and /// handler which further processes the received packet. /// </summary> /// <param name="serialPort">The serial port.</param> public void Start(IZigBeePort serialPort) { Interlocked.Exchange(ref _frameId, 1); _serialPort = serialPort; _timeoutTimer = new Timer(new TimerCallback(TimeoutCallback)); // TODO af: find the equivalent in C# --> maybe ThreadPool-Class is the right one //timeoutScheduler = Executors.newSingleThreadScheduledExecutor(); // Clear anything in the receive buffer before we start EmptyRxBuffer(); // TODO af: find a more elegant way to solve this --> maybe async/await // This might be resolved with a TaskCompletionSource // See the refactored while loop in ZigBeeTransaction.cs line 84 _parserThread = _taskFactory.StartNew(() => { Log.Debug("XBeeFrameHandler task started."); while (!_closeHandler) { try { lock (_commandLock) { if (_sentCommand == null) { SendNextFrame(); } } // Get a packet from the serial port int[] responseData = GetPacket(); if (responseData == null) { lock (_commandLock) { _sentCommand = null; } continue; } StringBuilder builder = new StringBuilder(); foreach (int value in responseData) { builder.Append(string.Format(" 0x{0:X2}", value.ToString())); } Log.Verbose($"RX XBEE Data: {builder}"); // Use the Event Factory to get an event IXBeeEvent xBeeEvent = XBeeEventFactory.GetXBeeFrame(responseData); if (xBeeEvent != null) { NotifyEventReceived(xBeeEvent); } // Use the Response Factory to get a response IXBeeResponse response = XBeeResponseFactory.GetXBeeFrame(responseData); if (response != null && NotifyResponseReceived(response)) { lock (_commandLock) { _sentCommand = null; } } } catch (Exception ex) { Log.Error(ex, "XBeeFrameHandler exception {Exception}", ex.Message); } } Log.Debug("XBeeFrameHandler thread exited."); }); }