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() && (duplicates.Count() > 1 || duplicates.First().Value.IsConnected)) { _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 = duplicates.Any() ? duplicates.First().Key : (uint)Interlocked.Increment(ref _deviceIndexCounter); _bpLogger.Info((duplicates.Any() ? "Re-" : string.Empty) + $"Adding Device {aEvent.Device.Name} at index {deviceIndex}"); _devices[deviceIndex] = aEvent.Device; aEvent.Device.DeviceRemoved += DeviceRemovedHandler; var msg = new DeviceAdded(deviceIndex, aEvent.Device.Name, GetAllowedMessageTypesAsStrings(aEvent.Device).ToArray()); DeviceMessageReceived?.Invoke(this, new MessageReceivedEventArgs(msg)); }
protected void InvokeDeviceAdded([NotNull] DeviceAddedEventArgs aEventArgs) { DeviceAdded?.Invoke(this, aEventArgs); }