public MQTTBackgroundService(ILogger <MQTTBackgroundService> logger, IMqttService client, MQTTServerConfiguration serverConfig, IEnumerable <MQTTTopicConfiguration> topicsConfig) { _logger = logger; _serverConfig = serverConfig; _topicsConfig = topicsConfig; _client = client; }
public async Task ConnectAsync(MQTTServerConfiguration configuration) { try { var mqqtOptions = new MqttClientOptionsBuilder() //.WithClientId(configuration.ClientId) .WithTcpServer(configuration.TcpServer, configuration.Port) .WithCredentials(configuration.Username, configuration.Password) .WithTls(t => t.UseTls = configuration.UseTls) .WithCleanSession(configuration.CleanSession) .WithCommunicationTimeout(TimeSpan.FromSeconds(5)) .WithKeepAlivePeriod(TimeSpan.FromSeconds(configuration.KeepAlive)) .Build(); var result = await _client.ConnectAsync(mqqtOptions); if (result.ResultCode != MQTTnet.Client.Connecting.MqttClientConnectResultCode.Success) { throw new ConnectionFailedException("Cannot connect to MQTTServer"); } } catch (Exception ex) { throw new ConnectionFailedException("Error while connecting to MQTT server", ex); } }
public MQTTGetStatusGateResponse(bool connected, MQTTServerConfiguration serverConfiguration, bool succes = false) : base(succes) { Connected = connected; ServerConfiguration = serverConfiguration; }