Ejemplo n.º 1
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());
        }
Ejemplo n.º 2
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());
        }
Ejemplo n.º 3
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.");
            });
        }