Exemplo n.º 1
0
 /// <summary>
 /// Opens the MQTT connection
 /// </summary>
 public async Task StartAsync()
 {
     try
     {
         lock (startupLock)
         {
             if (_managedMqttClient != null || ConnectionState == ConnectionState.Connected)
             {
                 return;
             }
             ConnectionState    = ConnectionState.Connecting;
             _managedMqttClient = _mqttClientFactory.CreateManagedMqttClient();
             _managedMqttClient.ApplicationMessageReceived  += ManagedMqttClientApplicationMessageReceived;
             _managedMqttClient.ApplicationMessageProcessed += ManagedMqttClientApplicationMessageProcessed;
             _managedMqttClient.Connected    += ManagedMqttClientConnected;
             _managedMqttClient.Disconnected += ManagedMqttClientDisconnected;
         }
         await _managedMqttClient.StartAsync(_config.Options).ConfigureAwait(false);
     }
     catch (Exception e)
     {
         _logger.LogCritical($"Exception while setting up the mqttclient to {this}", e);
         throw new MqttConnectionException($"Exception while setting up the mqttclient to {this}", e);
     }
 }
        public async Task StartAsync()
        {
            if (disposedValue)
            {
                throw new InvalidOperationException("Client is disposed.");
            }
            await clientLock.WaitAsync();

            try
            {
                var options = new ManagedMqttClientOptionsBuilder()
                              .WithAutoReconnectDelay(TimeSpan.FromSeconds(10))
                              .WithClientOptions(new MqttClientOptionsBuilder()
                                                 .WithClientId(Guid.NewGuid().ToString())
                                                 .WithTcpServer(Server, Port)
                                                 .WithCredentials(AccessToken)
                                                 .WithTls(new MqttClientOptionsBuilderTlsParameters {
                    UseTls = UseTls
                })
                                                 .Build())
                              .Build();
                client = factory.CreateManagedMqttClient();
                await client.StartAsync(options);
            }
            finally
            {
                clientLock.Release();
            }
        }
Exemplo n.º 3
0
        public MqttClient(
            MqttClientOptions config,
            string topicName)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            if (string.IsNullOrWhiteSpace(topicName))
            {
                throw new ArgumentException("Value cannot be empty.", nameof(topicName));
            }

            _config    = config;
            _topicName = topicName;

            _mqttFactory     = new MqttFactory();
            _innerMqttClient = _mqttFactory.CreateManagedMqttClient();
        }
        private async Task StartMqtt(CancellationToken cancellationToken)
        {
            if (_cancellationTokenSource.IsCancellationRequested || cancellationToken.IsCancellationRequested)
            {
                return;
            }
            try
            {
                _managedMqttClient = _mqttClientFactory.CreateManagedMqttClient();
                _managedMqttClient.ApplicationMessageReceived += ManagedMqttClientApplicationMessageReceived;
                _managedMqttClient.Connected    += ManagedMqttClientConnected;
                _managedMqttClient.Disconnected += ManagedMqttClientDisconnected;

                await _managedMqttClient.SubscribeAsync(_config.Topics).ConfigureAwait(false);

                await _managedMqttClient.StartAsync(_config.Options).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                _logger.LogCritical("Unhandled exception while settingup the mqttclient to {descriptor}", e);
                throw new MqttListenerInitializationException("Unhandled exception while connectin to {descriptor}", e);
            }
        }
 public IManagedMqttClient CreateManagedMqttClient()
 {
     return(_mqttClientFactory.CreateManagedMqttClient());
 }