/** * \fn changeStateMqttFlow * \brief change the state of the mqtt workflow * \param [in] state : target input state */ private void changeStateMqttFlow(bool state) { if (true == state) { this.listConsole.Items.Clear(); // this.m_currentSubscribeInformation = new MqttSubscribeInfo(new string[] { MQTT_MAIN_TOPIC }, new byte[] { 0x00 }); int port = 0; if (true == int.TryParse(this.tbBrokerPortInput.Text, out port)) { this.m_currentBrokerInfos = new MqttBrokerInfo(this.tbBrokerHostnameInput.Text, port); // this.m_client = new SampleMqttClient(this.m_currentBrokerInfos, false, null, null, uPLibrary.Networking.M2Mqtt.MqttSslProtocols.None); this.m_client.Connect(); this.m_client.Subscribe(this.m_currentSubscribeInformation, SampleMqttClient_MqttMsgPublishReceived); // this.btnManageWorkflow.Content = DISPLAY_STOP_WF; this.listConsole.Items.Add($"=========> : {DISPLAY_START_WF}"); } } else { this.m_client.Unsubscribe(this.m_currentSubscribeInformation, SampleMqttClient_MqttMsgPublishReceived); this.m_client.Disconnect(); this.m_client = null; // this.m_currentSubscribeInformation = null; this.m_currentBrokerInfos = null; // this.btnManageWorkflow.Content = DISPLAY_START_WF; this.listConsole.Items.Add($"=========> : {DISPLAY_STOP_WF}"); } this.m_bWorkflowStarted = state; }
protected override async Task ExecuteAsync(CancellationToken cancellationToken) { MqttBrokerInfo hostInfo; if (!string.IsNullOrWhiteSpace(_config.BrokerEndpoint)) { hostInfo = new MqttBrokerInfo(_config.BrokerEndpoint.Split(':')[0], int.Parse(_config.BrokerEndpoint.Split(':')[1]), "BrokerEndpoint"); } else { _logger.LogInformation($"Resolving MQTT Broker with DNS name: {_config.BrokerServiceDnsName}"); hostInfo = await MqttBrokerResolver.ResolveMqttBroker(_config.BrokerServiceDnsName); } _logger.LogInformation($"Connecting to MQTT broker: {hostInfo.DisplayName} - {hostInfo.IpAddress}:{hostInfo.Port}"); var certs = new X509Certificate2Collection { new X509Certificate2(_config.ClientCertificatePath, "") }; _client = new CustomMqttClientFactory().CreateMqttClient(new MqttClientOptions { Server = hostInfo.IpAddress, Port = hostInfo.Port, TlsOptions = new MqttClientTlsOptions { UseTls = true, //Certificates = new List<byte[]> //{ // X509Certificate.CreateFromSignedFile(_config.ClientCertificatePath).GetRawCertData() // //new X509Certificate(_config.ClientCertificatePath).Export(X509ContentType.Cert) // //.Export(X509ContentType.Cert) //}, CheckCertificateRevocation = true } }, certs); _client.Connected += async(sender, eventArgs) => { _logger.LogInformation("### CONNECTED WITH SERVER ###"); var topicFilters = _config.Topics.Select(topic => new TopicFilter(topic, MqttQualityOfServiceLevel.AtMostOnce)) .ToList(); await _client.SubscribeAsync(topicFilters); _logger.LogInformation("### SUBSCRIBED ###"); }; _client.Disconnected += async(s, e) => { _logger.LogInformation("### DISCONNECTED FROM SERVER ###"); await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); try { await _client.ConnectAsync(); } catch { _logger.LogInformation("### RECONNECTING FAILED ###"); } }; _client.ApplicationMessageReceived += async(sender, eventArgs) => { _logger.LogDebug($"{eventArgs.ApplicationMessage.Topic} {Encoding.UTF8.GetString(eventArgs.ApplicationMessage.Payload)}"); var type = _messageTypeMap(eventArgs.ApplicationMessage.Topic); var message = _messageSerializer.Deserialize(Encoding.UTF8.GetString(eventArgs.ApplicationMessage.Payload), type); await InvokeEnrichers(message as IMqttEvent); await InvokeHandlers(message as IMqttEvent); }; while (!cancellationToken.IsCancellationRequested) { try { await _client.ConnectAsync(); } catch (Exception e) { _logger.LogError("### CONNECTING FAILED ###"); _logger.LogError(e.ToString()); } await Task.WhenAny(Task.Delay(-1, cancellationToken)); } }
/** * \brief constructor * \param [in] brokerInfo : informations relative to the broker * \param [in] secure : define if the communication is secure or not * \param [in] caCert : * \param [in] clientCert : * \param [in] ssProtocol : */ public SampleMqttClient(MqttBrokerInfo brokerInfo, bool secure, X509Certificate caCert, X509Certificate clientCert, MqttSslProtocols sslProtocol) : base(brokerInfo.broker_address, brokerInfo.broker_port, secure, caCert, clientCert, sslProtocol) { BrokerInfo = brokerInfo; ID = Guid.NewGuid().ToString(); }