Exemplo n.º 1
0
        private async void AttemptConnection()
        {
            if (!Client.IsConnected && !IsConnecting)
            {
                try
                {
                    SetStatus("Connecting");
                    IsConnecting = true;
                    MqttClientOptionsBuilder optionsBuilder = new MqttClientOptionsBuilder()
                        .WithTcpServer(Host, Port)
                        .WithCommunicationTimeout(new TimeSpan(0, 0, 1, 30, 0))
                        ;

                    ClientOptions = optionsBuilder.Build();
#if HAVE_SYNC
                    if (IsSync)
                        Client.Connect(ClientOptions);
                    else
#endif
                        await Client.ConnectAsync(ClientOptions);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    IsConnecting = false;
                }
            }
        }
Exemplo n.º 2
0
        public async Task Run(CancellationToken stoppingToken)
        {
            _logger.LogInformation($"Start {nameof(EnvSensorsService)}");
            await _mqttClient.Connect("EnvSensors", async() =>
            {
                _logger.LogInformation($"Subscribing to {_options.EnvSensorsSenseHatTopic}");
                await _mqttClient.Subscribe(_options.EnvSensorsSenseHatTopic, HandleMessage);
            });

            stoppingToken.LoopUntilCancelled();

            _logger.LogInformation($"End {nameof(EnvSensorsService)}");
        }
Exemplo n.º 3
0
        public async Task Run(CancellationToken stoppingToken)
        {
            _logger.LogInformation($"Start {nameof(OccupancyAlertService)}");

            await CheckLastFiredAlert();

            await _mqttClient.Connect("Occupancy", async() =>
            {
                _logger.LogInformation($"Subscribing to {_options.OccupancyTopic}");
                await _mqttClient.Subscribe(_options.OccupancyTopic, HandleMessage);
                _logger.LogInformation($"Subscribed to {_options.OccupancyTopic}");
            });

            stoppingToken.LoopUntilCancelled();
            _logger.LogInformation($"End {nameof(OccupancyAlertService)}");
        }
Exemplo n.º 4
0
        public Task Connect(IPEndPoint endpoint,
                            ushort keepAliveSeconds = DefaultKeepAliveSeconds,
                            bool cleanSession       = false,
                            string userName         = null,
                            string password         = null)
        {
            _reconnectEndpoint  = endpoint;
            _reconnectKeepAlive = keepAliveSeconds;

            _client.Connect(endpoint).Await();
            _client.Receive();
            KeepAliveSeconds = keepAliveSeconds;
            _clientState     = ClientState.Connecting;

            var connectFlow = new ConnectSendFlow(_manager);

            var connectCommand = new Connect(ClientId, keepAliveSeconds);

            connectCommand.Details.ConnectFlags.CleanSession = cleanSession;
            if (string.IsNullOrEmpty(userName) == false)
            {
                connectCommand.Details.ConnectFlags.UserName = true;
                connectCommand.UserName = userName;
            }
            if (string.IsNullOrEmpty(password) == false)
            {
                connectCommand.Details.ConnectFlags.Password = true;
                connectCommand.Password = password;
            }

            return(connectFlow.Start(connectCommand, startCmd =>
            {
                _clientState = ClientState.Connected;
                ResetTimer();
            }));
        }