public void XbeeEventReceived(IXBeeEvent xbeeEvent)
        {
            if (xbeeEvent is XBeeReceivePacketExplicitEvent rxMessage)
            {
                ZigBeeApsFrame apsFrame = new ZigBeeApsFrame
                {
                    Cluster             = (ushort)rxMessage.GetClusterId(),
                    DestinationEndpoint = (byte)rxMessage.GetDestinationEndpoint(),
                    Profile             = (ushort)rxMessage.GetProfileId(),
                    SourceEndpoint      = (byte)rxMessage.GetSourceEndpoint(),

                    SourceAddress = (ushort)rxMessage.GetNetworkAddress(),
                    Payload       = rxMessage.GetData().Select(item => (byte)item).ToArray()
                };

                _zigBeeTransportReceive.ReceiveCommand(apsFrame);
                return;
            }

            // Handle dongle status messages
            if (xbeeEvent is XBeeModemStatusEvent modemStatus)
            {
                ModemStatus modemCurrentStatus = modemStatus.GetStatus();
                switch (modemCurrentStatus)
                {
                case ModemStatus.COORDINATOR_STARTED:
                {
                    _coordinatorStarted = true;
                    SetNetworkState(ZigBeeTransportState.ONLINE);
                }
                break;

                case ModemStatus.DISASSOCIATED:
                {
                    SetNetworkState(ZigBeeTransportState.OFFLINE);
                }
                break;

                case ModemStatus.HARDWARE_RESET:
                case ModemStatus.JOINED_NETWORK:
                case ModemStatus.NETWORK_SECURITY_KEY_UPDATED:
                case ModemStatus.WATCHDOG_TIMER_RESET:
                    break;

                default:
                    break;
                }
                return;
            }

            Log.Debug($"Unhandled XBee Frame: {xbeeEvent.ToString()}");
        }
        /// <summary>
        /// Listeners are called when a new <see cref="IXBeeEvent"/> is received
        /// </summary>
        /// <typeparam name="T">Expected type of xbee event</typeparam>
        /// <param name="xbeeEvent">The received <see cref="IXBeeEvent"/>.</param>
        public void XbeeEventReceived(IXBeeEvent xbeeEvent)
        {
            if (xbeeEvent.GetType() != _eventClass)
            {
                return;
            }

            ReceivedEvent = xbeeEvent;

            lock (_lockObject)
            {
                Complete = true;
            }
        }
Beispiel #3
0
        public void TestGetEvent()
        {
            int[]      data  = GetPacketData("00 1A 91 00 17 88 01 02 13 65 36 F7 7B 02 01 00 01 01 04 41 18 7C 01 21 00 00 20 C8 C4");
            IXBeeEvent frame = XBeeEventFactory.GetXBeeFrame(data);

            Assert.True(frame is XBeeReceivePacketExplicitEvent);
            _output.WriteLine(frame.ToString());

            XBeeReceivePacketExplicitEvent xbeeEvent = (XBeeReceivePacketExplicitEvent)frame;

            Assert.Equal(1, xbeeEvent.GetClusterId());
            Assert.Equal(1, xbeeEvent.GetDestinationEndpoint());
            Assert.Equal(63355, xbeeEvent.GetNetworkAddress());
            Assert.Equal(0x104, xbeeEvent.GetProfileId());
        }
Beispiel #4
0
        public void TestBootloaderEvent()
        {
            int[]      data  = GetPacketData("00 16 A0 00 13 A2 00 41 62 F6 1A 00 00 01 40 00 00 00 00 00 00 00 FF FF B8");
            IXBeeEvent frame = XBeeEventFactory.GetXBeeFrame(data);

            Assert.True(frame is XBeeOtaFirmwareUpdateStatusEvent);

            _output.WriteLine(frame.ToString());

            XBeeOtaFirmwareUpdateStatusEvent xbeeEvent = (XBeeOtaFirmwareUpdateStatusEvent)frame;

            Assert.Equal(0, xbeeEvent.GetBlockNumber());
            Assert.Equal(new IeeeAddress(BigInteger.Parse("0013A2004162F61A", System.Globalization.NumberStyles.HexNumber)), xbeeEvent.GetIeeeAddress());
            Assert.Equal(0, xbeeEvent.GetNetworkAddress());
        }
Beispiel #5
0
 /// <summary>
 /// Notify any event listeners when we receive an event.
 /// </summary>
 /// <param name="xBeeEvent">the <see cref="IXBeeEvent"/> received.</param>
 private void NotifyEventReceived(IXBeeEvent xBeeEvent)
 {
     Log.Debug($"RX XBEE: {xBeeEvent.ToString()}");
     lock (_eventListeners)
     {
         foreach (IXBeeEventListener listener in _eventListeners)
         {
             try
             {
                 listener.XbeeEventReceived(xBeeEvent);
             }
             catch (Exception e)
             {
                 Log.Debug($"Exception processing XBee frame: {xBeeEvent}: ", e);
             }
         }
     }
 }
Beispiel #6
0
 public static IXBeeEvent GetXBeeFrame(int[] data)
 {
     _events.TryGetValue(data[2], out Type xbeeClass);
     if (xbeeClass == null)
     {
         return(null);
     }
     try
     {
         IXBeeEvent xbeeFrame = Activator.CreateInstance(xbeeClass) as IXBeeEvent;
         xbeeFrame.Deserialize(data);
         return(xbeeFrame);
     }
     catch (Exception ex)
     {
         Log.Debug("Error creating instance of IXBeeResponse", ex);
     }
     return(null);
 }
Beispiel #7
0
        /// <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.");
            });
        }