Beispiel #1
0
        private void DeviceAddedHandler(object aObj, DeviceAddedEventArgs aEvent)
        {
            // Devices can be turned off by the time they get to this point, at which point they end
            // up null. Make sure the device isn't null.
            if (aEvent.Device == null)
            {
                return;
            }

            var duplicates = from x in Devices
                             where x.Value.Identifier == aEvent.Device.Identifier
                             select x;

            if (duplicates.Any())
            {
                _bpLogger.Debug($"Already have device {aEvent.Device.Name} in Devices list");
                return;
            }

            // If we get to 4 billion devices connected, this may be a problem.
            var deviceIndex = DeviceIds.ContainsKey(aEvent.Device.Identifier) ? DeviceIds[aEvent.Device.Identifier] : (uint)Interlocked.Increment(ref _deviceIndexCounter);

            _bpLogger.Info((DeviceIds.ContainsKey(aEvent.Device.Identifier) ? "Re-" : string.Empty) + $"Adding Device {aEvent.Device.Name} at index {deviceIndex}");

            DeviceIds[aEvent.Device.Identifier] = deviceIndex;
            Devices[deviceIndex]          = aEvent.Device;
            aEvent.Device.DeviceRemoved  += DeviceRemovedHandler;
            aEvent.Device.MessageEmitted += MessageEmittedHandler;
            var msg = new DeviceAdded(
                deviceIndex,
                aEvent.Device.Name,
                GetAllowedMessageTypesAsDictionary(aEvent.Device, SpecVersion));

            DeviceMessageReceived?.Invoke(this, new MessageReceivedEventArgs(msg));
        }