Ejemplo n.º 1
0
        public async Task CloseAsync()
        {
            if (_sendLoop != null)
            {
                _cts.Cancel();

                await _sendLoop;
                _sendLoop = null;

                _cts.Dispose();
                _cts = null;
            }

            await _deviceClient.CloseAsync();

            StateCollection <DeviceClientState> .Remove(_device.DeviceID);
        }
Ejemplo n.º 2
0
        private async Task SendLoop(CancellationToken ct)
        {
            bool isDeviceClientAvailable = true;

            StateCollection <DeviceClientState> .Set(_device.DeviceID, DeviceClientState.Up);

            while (!ct.IsCancellationRequested)
            {
                await Task.Delay(TimeSpan.FromSeconds(1));

                // Re-create device client if it is not available (due to any exception)
                if (!isDeviceClientAvailable)
                {
                    try
                    {
                        _deviceClient?.Dispose();
                        _deviceClient = null;   // Avoid duplicated disposing the old device client, if the new one failed to be created

                        _deviceClient = await CreateDeviceClient();

                        _logger.LogInfo($"Transport opened for device {_device.DeviceID} with type {_transportType}");
                        isDeviceClientAvailable = true;
                        StateCollection <DeviceClientState> .Set(_device.DeviceID, DeviceClientState.Up);
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError($"Exception raised while trying to open transport for device {_device.DeviceID}: {ex}");
                        continue;
                    }
                }

                if (_reports.Any())
                {
                    try
                    {
                        await _deviceClient.UpdateReportedPropertiesAsync(_reports.First.Value);

                        lock (_reports)
                        {
                            _reports.RemoveFirst();
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError($"Exception raised while device {_device.DeviceID} trying to update reported properties: {ex}");
                        isDeviceClientAvailable = false;
                        StateCollection <DeviceClientState> .Set(_device.DeviceID, DeviceClientState.Down);

                        continue;
                    }
                }

                if (_messages.Any())
                {
                    // Messages failed to be sent will be dropped
                    var sendingMessages = new List <Client.Message>();
                    lock (_messages)
                    {
                        sendingMessages.AddRange(_messages);
                        _messages.Clear();
                    }

                    try
                    {
                        await _deviceClient.SendEventBatchAsync(sendingMessages);
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError($"Exception raised while device {_device.DeviceID} trying to send events: {ex}");
                        isDeviceClientAvailable = false;
                        StateCollection <DeviceClientState> .Set(_device.DeviceID, DeviceClientState.Down);

                        continue;
                    }
                }
            }
        }