/// <summary> /// /// </summary> /// <returns></returns> public virtual async Task InitializeAsync() { ResistorsOnDevice.Clear(); ResistorsOnDevice.AddRange(_resistorFactory.CreateResistorsOnDevice(this.DeviceMomento.State.SchemeTable, this)); _deviceCustomItems = _customItemsFactory.CreateDeviceCustomItems(_deviceMomento.State); DeviceInitialized?.Invoke(); _isDeviceInitialized = true; }
public async Task CheckForDevicesAsync(CancellationToken cancellationToken = default) { try { if (_IsDisposed) { return; } await _ListenSemaphoreSlim.WaitAsync(cancellationToken).ConfigureAwait(false); var connectedDeviceDefinitions = (await DeviceFactory.GetConnectedDeviceDefinitionsAsync(cancellationToken).ConfigureAwait(false)).ToList(); //Iterate through connected devices foreach (var connectedDeviceDefinition in connectedDeviceDefinitions) { //TODO: What to do if there are multiple? IDevice device = null; if (_CreatedDevicesByDefinition.ContainsKey(connectedDeviceDefinition.DeviceId)) { device = _CreatedDevicesByDefinition[connectedDeviceDefinition.DeviceId]; } if (device == null) { //Need to use the connected device def here instead of the filter version because the filter version won't have the id or any details device = await DeviceFactory.GetDeviceAsync(connectedDeviceDefinition, cancellationToken).ConfigureAwait(false); if (device == null) { _logger.LogWarning("A connected device with id {deviceId} was detected but the factory didn't create an instance of it. Bad stuff is going to happen now.", connectedDeviceDefinition.DeviceId); } else { _CreatedDevicesByDefinition.Add(connectedDeviceDefinition.DeviceId, device); } } else { if (device.IsInitialized) { continue; } } _logger.LogDebug("Attempting to initialize with DeviceId of {deviceId}", device.DeviceId); //The device is not initialized so initialize it await device.InitializeAsync(cancellationToken).ConfigureAwait(false); //Let listeners know a registered device was initialized DeviceInitialized?.Invoke(this, new DeviceEventArgs(device)); _logger.LogDebug(Messages.InformationMessageDeviceConnected, device.DeviceId); } var removeDeviceIds = new List <string>(); //Iterate through registered devices foreach (var deviceId in _CreatedDevicesByDefinition.Keys) { var device = _CreatedDevicesByDefinition[deviceId]; if (connectedDeviceDefinitions.Any(cdd => cdd.DeviceId == deviceId)) { continue; } if (!device.IsInitialized) { continue; } //Let listeners know a registered device was disconnected //NOTE: let the rest of the app know before disposal so that the app can stop doing whatever it's doing. DeviceDisconnected?.Invoke(this, new DeviceEventArgs(device)); //The device is no longer connected so close it device.Close(); removeDeviceIds.Add(deviceId); _logger.LogDebug(Messages.InformationMessageDeviceListenerDisconnected); } foreach (var deviceId in removeDeviceIds) { _ = _CreatedDevicesByDefinition.Remove(deviceId); } _logger.LogDebug(Messages.InformationMessageDeviceListenerPollingComplete); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) #pragma warning restore CA1031 // Do not catch general exception types { //Log and move on _logger.LogError(ex, Messages.ErrorMessagePollingError); //TODO: What else to do here? } finally { if (!_IsDisposed) { _ = _ListenSemaphoreSlim.Release(); } } }
public async Task CheckForDevicesAsync() { try { if (_IsDisposed) { return; } await _ListenSemaphoreSlim.WaitAsync(); var connectedDeviceDefinitions = new List <ConnectedDeviceDefinition>(); foreach (var deviceDefinition in FilterDeviceDefinitions) { connectedDeviceDefinitions.AddRange(await DeviceManager.Current.GetConnectedDeviceDefinitionsAsync(deviceDefinition)); } //Iterate through connected devices foreach (var connectedDeviceDefinition in connectedDeviceDefinitions) { var deviceDefinition = FilterDeviceDefinitions.FirstOrDefault(d => DeviceManager.IsDefinitionMatch(d, connectedDeviceDefinition)); if (deviceDefinition == null) { continue; } //TODO: What to do if there are multiple? IDevice device = null; if (_CreatedDevicesByDefinition.ContainsKey(deviceDefinition)) { device = _CreatedDevicesByDefinition[deviceDefinition]; } if (device == null) { //Need to use the connected device def here instead of the filter version because the filter version won't have the id or any details device = DeviceManager.Current.GetDevice(connectedDeviceDefinition); _CreatedDevicesByDefinition.Add(deviceDefinition, device); } if (device.IsInitialized) { continue; } Log($"Attempting to initialize with DeviceId of {device.DeviceId}", null); //The device is not initialized so initialize it await device.InitializeAsync(); //Let listeners know a registered device was initialized DeviceInitialized?.Invoke(this, new DeviceEventArgs(device)); Log(Messages.InformationMessageDeviceConnected, null); } var removeDefs = new List <FilterDeviceDefinition>(); //Iterate through registered devices foreach (var filteredDeviceDefinitionKey in _CreatedDevicesByDefinition.Keys) { var device = _CreatedDevicesByDefinition[filteredDeviceDefinitionKey]; if (connectedDeviceDefinitions.Any(cdd => DeviceManager.IsDefinitionMatch(filteredDeviceDefinitionKey, cdd))) { continue; } if (!device.IsInitialized) { continue; } //Let listeners know a registered device was disconnected //NOTE: let the rest of the app know before disposal so that the app can stop doing whatever it's doing. DeviceDisconnected?.Invoke(this, new DeviceEventArgs(device)); //The device is no longer connected so close it device.Close(); removeDefs.Add(filteredDeviceDefinitionKey); Log(Messages.InformationMessageDeviceListenerDisconnected, null); } foreach (var removeDef in removeDefs) { _CreatedDevicesByDefinition.Remove(removeDef); } Log(Messages.InformationMessageDeviceListenerPollingComplete, null); } catch (Exception ex) { Log(Messages.ErrorMessagePollingError, ex); //TODO: What else to do here? } finally { if (!_IsDisposed) { _ListenSemaphoreSlim.Release(); } } }