public async Task ConnectAsync() { _client = _factory.CreateMqttClient(); var clientOptions = new MqttClientOptions { ChannelOptions = new MqttClientTcpOptions { Server = _broker } }; _client.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(async e => { Console.WriteLine("### RECEIVED MESSAGE FROM SHELLY 1PM ###"); Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}"); var payload = Encoding.UTF8.GetString(e.ApplicationMessage.Payload); Console.WriteLine($"+ Payload = {payload}"); Console.WriteLine($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}"); Console.WriteLine($"+ Retain = {e.ApplicationMessage.Retain}"); Console.WriteLine(); await Task.Delay(0); }); _client.ConnectedHandler = new MqttClientConnectedHandlerDelegate(async e => { Console.WriteLine("### RELAY-CLIENT: CONNECTED WITH SERVER ###"); await Task.Delay(0); }); _client.DisconnectedHandler = new MqttClientDisconnectedHandlerDelegate(async e => { Console.WriteLine("### RELAY-CLIENT: DISCONNECTED FROM SERVER ###"); await Task.Delay(TimeSpan.FromSeconds(5)); try { await _client.ConnectAsync(clientOptions); } catch { Console.WriteLine("### RELAY-CLIENT: RECONNECTING FAILED ###"); } }); try { await _client.ConnectAsync(clientOptions); } catch (Exception exception) { Console.WriteLine("### RELAY-CLIENT: CONNECTING FAILED ###" + Environment.NewLine + exception); } }
public static IAsyncEnumMqttClient CreateAsyncEnumMqttClient(this IMqttFactory factory) { if (factory == null) { throw new ArgumentNullException(nameof(factory)); } return(new AsyncEnumMqttClient(factory.CreateMqttClient(), factory.DefaultLogger)); }
public static IManagedMqttClient CreateManagedMqttClient(this IMqttFactory factory) { if (factory == null) { throw new ArgumentNullException(nameof(factory)); } return(new ManagedMqttClient(factory.CreateMqttClient(), factory.DefaultLogger.CreateChildLogger())); }
public static IAsyncEnumMqttClient CreateAsyncEnumMqttClient(this IMqttFactory factory, IMqttNetLogger logger) { if (factory == null) { throw new ArgumentNullException(nameof(factory)); } if (logger == null) { throw new ArgumentNullException(nameof(logger)); } return(new AsyncEnumMqttClient(factory.CreateMqttClient(logger), logger)); }
public static IManagedMqttClient CreateManagedMqttClient(this IMqttFactory factory, IMqttNetLogger logger) { if (factory == null) { throw new ArgumentNullException(nameof(factory)); } if (logger == null) { throw new ArgumentNullException(nameof(logger)); } return(new ManagedMqttClient(factory.CreateMqttClient(logger), logger.CreateChildLogger())); }
public MQTTPublisher(IMqttFactory factory, MQTTOptions options) { _mqttClient = factory.CreateMqttClient(); _options = options; var builder = new MqttClientOptionsBuilder().WithTcpServer(_options.Host, _options.Port); if (!string.IsNullOrEmpty(_options.Username) || !string.IsNullOrEmpty(_options.Password.ToString())) { builder.WithCredentials(_options.Username, _options.Password.ToString()); } _mqttClientOptions = builder.Build(); }
protected ReliableMqttClient(ILogger logger, IMqttFactory factory, TConfiguration configuration, IHostApplicationLifetime applicationLifetime) { if (factory == null) { throw new ArgumentNullException(nameof(factory)); } if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } _foreverJitter = new ForeverJitter(TimeSpan.FromSeconds(1), 10, TimeSpan.FromSeconds(60)); _jitterBackoff = new JitterBackoff(_foreverJitter); Logger = logger; ApplicationLifetime = applicationLifetime; Client = factory.CreateMqttClient(); Options = configuration.GetOptions(); Configuration = configuration; _policy = Policy.Handle <MqttCommunicationException>() .WaitAndRetryAsync(_foreverJitter); }
public MqttNetAdapter(IMqttFactory mqttFactory, IOptions <MqttConfig> mqttConfig) { _mqtt = mqttFactory.CreateMqttClient(); _mqttConfig = mqttConfig.Value; }