コード例 #1
0
        public static void OnDeviceDataReceieved(object sender, FrameReceivedEventArgs e)
        {
            N2kFrame    n2kFrame = (N2kFrame)e.ReceivedFrame;
            CANStreamer stream   = sender as CANStreamer;

            CANDevice dev = UpdateAddDevice(n2kFrame, stream.Name);
        }
コード例 #2
0
        public static CANDevice FindDeviceByRule(string searchRule)
        {
            CANDevice dev = null;
            int       Source;
            UInt64    ID;

            try {
                if (int.TryParse(searchRule, out Source))
                {
                    _Devices.TryGetValue(Source, out dev);
                }
                else
                {
                    if (searchRule.Substring(0, 3) == "ID:")
                    {
                        ID  = Convert.ToUInt64(searchRule.Substring(3), 16);
                        dev = FindDeviceByID(ID);
                    }
                }
            } catch
            {
                dev = null;
            }

            return(dev);
        }
コード例 #3
0
        private static void UpdateDevice(CANDevice dev, N2kFrame n2kFrame)
        {
            var e = DeviceListChange;

            if (dev.UpdateDevice(n2kFrame) && e != null)
            {
                e();
            }
        }
コード例 #4
0
        private static CANDevice FindDeviceByID(UInt64 ID)
        {
            CANDevice dev = null;

            lock (Lock)
            {
                int Key = FindDeviceKeyByID(ID);
                if (Key != -1)
                {
                    dev = _Devices[Key];
                }
            }

            return(dev);
        }
コード例 #5
0
        private static CANDevice AddDevice(N2kFrame n2kFrame, string streamName)
        {
            CANDevice dev = null;
            int       Source;

            if (Int32.TryParse(n2kFrame.Header.Source, out Source))
            {
                dev = new CANDevice(Source, streamName);
                lock (Lock)
                {
                    dev.Source = Source;
                    _Devices.Add(Source, dev);
                }
                UpdateDevice(dev, n2kFrame);
            }

            return(dev);
        }
コード例 #6
0
        private static CANDevice UpdateAddDevice(N2kFrame n2kFrame, string streamName)
        {
            CANDevice dev = null;
            int       Source;

            if (Int32.TryParse(n2kFrame.Header.Source, out Source))
            {
                lock (Lock)
                {
                    // First try to find device with source
                    _Devices.TryGetValue(Source, out dev);

                    if (dev != null)   // If we found device, just update it
                    {
                        UpdateDevice(dev, n2kFrame);
                    }
                    else
                    {
                        // If device does not exist, check is it on different source.
                        // Source may change due to address claiming.
                        if (n2kFrame.Header.PGN == 60928)
                        {
                            UInt64 ID  = BitConverter.ToUInt64(n2kFrame.Data, 0);
                            int    Key = FindDeviceKeyByID(ID);
                            if (Key != -1) // We found device, so register it with new key.
                            {
                                dev = _Devices[Key];
                                _Devices.Remove(Key);
                                _Devices.Remove(Source);
                                _Devices.Add(Source, dev);
                                lock (dev.Lock)
                                {
                                    dev.Source = Source;
                                }
                                // Notify that device address has been changed
                                var e = SourceChange; if (e != null)
                                {
                                    e();
                                }
                                e = DeviceListChange; if (e != null)
                                {
                                    e();
                                }
                            }
                            else
                            {
                                dev = AddDevice(n2kFrame, streamName);
                                var e = SourceChange; if (e != null)
                                {
                                    e();
                                }
                            }
                        }
                        else
                        {
                            // We did not find the device, but we can not add it, since message was
                            // not ISO Address claim PGN 60928
                        }
                    } // device was null
                }
            }
            return(dev);
        }