public BusClient(IOptions <MqttBrokerSettings> options, ILogger <BusClient> logger, IMqttClientFactory mqttClientFactory) { _settings = options.Value; //var factory = new MqttFactory(); _mqttClient = mqttClientFactory.CreateMqttClient(); _logger = logger; }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { //var factory = new MqttFactory(); var client = _mqttFactory.CreateMqttClient(); var clientOptions = new MqttClientOptions { ChannelOptions = new MqttClientTcpOptions { Server = _mqttServer } }; client.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(MessageReceiver()); client.ConnectedHandler = new MqttClientConnectedHandlerDelegate(ClientConnect(client)); client.DisconnectedHandler = new MqttClientDisconnectedHandlerDelegate(ClientDisconnect(client, clientOptions)); try { await client.ConnectAsync(clientOptions); } catch (Exception exception) { _logger.LogInformation($"Connection Failed {exception?.Message}"); } _logger.LogInformation("Waiting for messages."); while (!stoppingToken.IsCancellationRequested) { await Task.Delay(1000, stoppingToken); } }
private async void Start() { m_Factory = new MqttFactory(); m_Client = m_Factory.CreateMqttClient(); m_ClientOptions = new MqttClientOptionsBuilder() .WithClientId("incluMOVE-GUI") .WithTcpServer("localhost", 1883) .Build(); Aci.Unity.Logging.AciLog.Log("Network", "Connecting to Mqtt-Server."); try { m_Client.ConnectAsync(m_ClientOptions, CancellationToken.None); } catch (SocketException e) { Aci.Unity.Logging.AciLog.Log("Network", $"Failed to find MQTT-Broker, failed with Exception {e.SocketErrorCode}, {e.Message}"); m_Client = null; return; } Aci.Unity.Logging.AciLog.Log("Network", "Connected to Mqtt-Server."); m_Client.UseDisconnectedHandler(HandleDisconnectedAsync); m_Client.UseApplicationMessageReceivedHandler(HandleApplicationMessageReceivedAsync); m_Client.UseConnectedHandler(HandleConnectedAsync); }
public MqttClientAdapter(ILogger <MqttClientAdapter> logger, IOptions <MqttOptions> options, IMqttClientFactory mqttClientFactory) { this.logger = logger; this.options = options.Value; Client = mqttClientFactory.CreateMqttClient(); }
public static IManagedMqttClient CreateManagedMqttClient(this IMqttClientFactory factory) { if (factory == null) { throw new ArgumentNullException(nameof(factory)); } return(new ManagedMqttClient(factory.CreateMqttClient(), new MqttNetLogger().CreateChildLogger())); }
public MqttAdapter(IMqttClientOptions options, IMqttClientFactory factory, RetryOptions retryOptions, IMqttAnnounceBuilder announceBuilder, ILogger <MqttAdapter> logger) { if (factory == null) { throw new ArgumentNullException(nameof(factory)); } this.options = options ?? throw new ArgumentNullException(nameof(options)); this.retryOptions = retryOptions ?? throw new ArgumentNullException(nameof(retryOptions)); this.announceBuilder = announceBuilder ?? throw new ArgumentNullException(nameof(announceBuilder)); this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); client = factory.CreateMqttClient(); client.UseDisconnectedHandler(HandleClientDisconnected); }
public async Task ConnectAsync() { _logger.LogInformation("Connecting to Mosquitto"); _mqttClient = _mqttFactory.CreateMqttClient(); var options = new MqttClientOptionsBuilder() .WithClientId(_clientId) .WithTcpServer(_mqttConfiguration.Hostname, _mqttConfiguration.Port) .WithKeepAlivePeriod(TimeSpan.FromSeconds(30)) .WithCommunicationTimeout(TimeSpan.FromSeconds(30)) .Build(); _mqttClient.UseConnectedHandler(args => _logger.LogInformation("MQTT connected")); bool isConnected = false; do { try { await _mqttClient.ConnectAsync(options); isConnected = true; } catch (MqttCommunicationException) { _logger.LogWarning("Failed to connect to Mosquitto, reconnecting..."); await Task.Delay(2000); } }while (!isConnected); _mqttClient.UseDisconnectedHandler(async e => { _logger.LogWarning("Disconnected from MQTT server, reconnecting..."); await Task.Delay(TimeSpan.FromSeconds(5)); try { await _mqttClient.ConnectAsync(options); } catch { _logger.LogWarning("Reconnecting to MQTT failed"); } }); }
public static IMqttClient CreateClient() => ClientFactory.CreateMqttClient();
public BusSubscriber(IOptions <MqttBrokerSettings> options, ILogger <BusSubscriber> logger, IMqttClientFactory mqttClientFactory) { _settings = options.Value; _logger = logger; _mqttClient = mqttClientFactory.CreateMqttClient(); }