Ejemplo n.º 1
0
 /// <summary>
 /// Handles connecting to and disconnecting from a set of potential devices by their definition
 /// </summary>
 /// <param name="filterDeviceDefinitions">Device definitions to connect to and disconnect from</param>
 /// <param name="pollMilliseconds">Poll interval in milliseconds, or null if checking is called externally</param>
 public DeviceListener(IEnumerable <FilterDeviceDefinition> filterDeviceDefinitions, int?pollMilliseconds)
 {
     FilterDeviceDefinitions.AddRange(filterDeviceDefinitions);
     if (pollMilliseconds.HasValue)
     {
         _PollTimer          = new timer(pollMilliseconds.Value);
         _PollTimer.Elapsed += _PollTimer_Elapsed;
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Handles connecting to and disconnecting from a set of potential devices by their definition
        /// </summary>
        /// <param name="filterDeviceDefinitions">Device definitions to connect to and disconnect from</param>
        /// <param name="pollMilliseconds">Poll interval in milliseconds, or null if checking is called externally</param>
        public DeviceListener(IEnumerable <FilterDeviceDefinition> filterDeviceDefinitions, int?pollMilliseconds)
        {
            FilterDeviceDefinitions.AddRange(filterDeviceDefinitions);
            _ListenSemaphoreSlim = new SemaphoreSlim(1, 1);
            if (!pollMilliseconds.HasValue)
            {
                return;
            }

            _PollTimer          = new timer(pollMilliseconds.Value);
            _PollTimer.Elapsed += _PollTimer_Elapsed;
        }
Ejemplo n.º 3
0
        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();
                }
            }
        }