private void mqttClient_Connected(object sender, MqttClientConnectedEventArgs e) { try { if (string.IsNullOrEmpty(_options.ScadaId) == false) { string scadaCmdTopic = string.Format("iot-2/evt/wacmd/fmt/{0}", _options.ScadaId); string deviceCmdTopic = string.Format("iot-2/evt/wacmd/fmt/{0}/{1}", _options.ScadaId, _options.ScadaId); _configTopic = string.Format("iot-2/evt/wacfg/fmt/{0}", _options.ScadaId); _dataTopic = string.Format("iot-2/evt/wadata/fmt/{0}", _options.ScadaId); _scadaConnTopic = string.Format("iot-2/evt/waconn/fmt/{0}", _options.ScadaId); _wastTopic = string.Format("iot-2/evt/wast/fmt/{0}", _options.ScadaId); } if (Connected != null) { Connected(this, new EdgeAgentConnectedEventArgs(e.IsSessionPresent)); } // subscribe _mqttClient.SubscribeAsync(new TopicFilterBuilder().WithTopic(_configTopic).WithAtLeastOnceQoS().Build()); _mqttClient.SubscribeAsync(new TopicFilterBuilder().WithTopic(_dataTopic).WithAtLeastOnceQoS().Build()); // publish ConnectMessage connectMsg = new ConnectMessage(); string payload = JsonConvert.SerializeObject(connectMsg); var message = new MqttApplicationMessageBuilder() .WithTopic(_scadaConnTopic) .WithPayload(payload) .WithAtLeastOnceQoS() .WithRetainFlag(true) .Build(); _mqttClient.PublishAsync(message); var message1 = new MqttApplicationMessageBuilder() .WithTopic(_wastTopic) .WithPayload(payload) .WithAtLeastOnceQoS() .WithRetainFlag(true) .Build(); _mqttClient.PublishAsync(message1); } catch (Exception ex) { //_logger.Error( ex.ToString() ); } }
public static async Task SubscribeMQTT(List <string> topicList) { foreach (string topic in topicList) { if (topic != "") { var topicFilter = new MqttTopicFilterBuilder() .WithAtLeastOnceQoS() .WithTopic(topic) .Build(); await mqttClient.SubscribeAsync(topicFilter); } } mqttClient.UseApplicationMessageReceivedHandler(e => { MqttMessageEventArgs messageReceivedEventArgs = new() { Topic = e.ApplicationMessage.Topic, Message = System.Text.Encoding.Default.GetString(e.ApplicationMessage.Payload), }; MqttMessageReceived?.Invoke(null, messageReceivedEventArgs); Debug.WriteLine("Received Message int topic: " + e.ApplicationMessage.Topic + " : " + System.Text.Encoding.Default.GetString(e.ApplicationMessage.Payload)); }); return; }
public override async void SubscribeToMqttTopics() { Logger.LogTrace(LogCategory.Processor, this.Name, $"Subscribing to topic: {ValueReceiveTopic}"); await ManagedMqttClient.SubscribeAsync(new List <TopicFilter> { new TopicFilterBuilder().WithTopic(ValueReceiveTopic).WithAtLeastOnceQoS().Build() }); }
public async Task Subscriptions_Are_Cleared_At_Logout() { using (var testEnvironment = new TestEnvironment(TestContext)) { await testEnvironment.StartServerAsync().ConfigureAwait(false); var sendingClient = await testEnvironment.ConnectClientAsync().ConfigureAwait(false); await sendingClient.PublishAsync(new MqttApplicationMessage { Topic = "topic", Payload = new byte[] { 1 }, Retain = true }); // Wait a bit for the retained message to be available await Task.Delay(500); await sendingClient.DisconnectAsync(); // Now use the managed client and check if subscriptions get cleared properly. var clientOptions = new MqttClientOptionsBuilder() .WithTcpServer("localhost", testEnvironment.ServerPort); var receivedManagedMessages = new List <MqttApplicationMessage>(); var managedClient = new ManagedMqttClient(testEnvironment.CreateClient(), new MqttNetLogger()); managedClient.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(c => { receivedManagedMessages.Add(c.ApplicationMessage); }); await managedClient.SubscribeAsync("topic"); await managedClient.StartAsync(new ManagedMqttClientOptionsBuilder() .WithClientOptions(clientOptions) .WithAutoReconnectDelay(TimeSpan.FromSeconds(1)) .Build()); await Task.Delay(500); Assert.AreEqual(1, receivedManagedMessages.Count); await managedClient.StopAsync(); await Task.Delay(500); await managedClient.StartAsync(new ManagedMqttClientOptionsBuilder() .WithClientOptions(clientOptions) .WithAutoReconnectDelay(TimeSpan.FromSeconds(1)) .Build()); await Task.Delay(1000); // After reconnect and then some delay, the retained message must not be received, // showing that the subscriptions were cleared Assert.AreEqual(1, receivedManagedMessages.Count); // Make sure that it gets received after subscribing again. await managedClient.SubscribeAsync("topic"); await Task.Delay(500); Assert.AreEqual(2, receivedManagedMessages.Count); } }
private async Task SubscribeHydrophonics(String Topic) { await mqttClient.SubscribeAsync(Topic); }
private void mqttClient_Connected(object sender, MqttClientConnectedEventArgs e) { try { if (string.IsNullOrEmpty(_options.ScadaId) == false) { string scadaCmdTopic = string.Format("iot-2/evt/wacmd/fmt/{0}", _options.ScadaId); string deviceCmdTopic = string.Format("iot-2/evt/wacmd/fmt/{0}/{1}", _options.ScadaId, _options.ScadaId); _configTopic = string.Format("iot-2/evt/wacfg/fmt/{0}", _options.ScadaId); _dataTopic = string.Format("iot-2/evt/wadata/fmt/{0}", _options.ScadaId); _scadaConnTopic = string.Format("iot-2/evt/waconn/fmt/{0}", _options.ScadaId); // kelly _deviceConnTopic = string.Format("iot-2/evt/waconn/fmt/{0}", _options.ScadaId); // _actcTopic = string.Format("iot-2/evt/waactc/fmt/{0}/{1}", _options.ScadaId, _options.ScadaId); _actdTopic = string.Format("iot-2/evt/waactd/fmt/{0}/{1}", _options.ScadaId, _options.ScadaId); _stTopic = string.Format("iot-2/evt/wast/fmt/{0}", _options.ScadaId); if (_options.Type == EdgeType.Gateway) { _cmdTopic = scadaCmdTopic; } else { _cmdTopic = deviceCmdTopic; } } if (_options.Heartbeat > 0) { if (_heartbeatTimer == null) { _heartbeatTimer = new Timer(); } _heartbeatTimer.Enabled = false; _heartbeatTimer.Interval = _options.Heartbeat; } if (Connected != null) { Connected(this, new EdgeAgentConnectedEventArgs(e.IsSessionPresent)); } // subscribe _mqttClient.SubscribeAsync(new TopicFilterBuilder().WithTopic(_cmdTopic).WithAtLeastOnceQoS().Build()); //_mqttClient.SubscribeAsync(new TopicFilterBuilder().WithTopic(_actdTopic).WithAtLeastOnceQoS().Build()); _mqttClient.SubscribeAsync(new TopicFilterBuilder().WithTopic(_actcTopic).WithAtLeastOnceQoS().Build()); _mqttClient.SubscribeAsync(new TopicFilterBuilder().WithTopic(_stTopic).WithAtLeastOnceQoS().Build()); // publish ConnectMessage connectMsg = new ConnectMessage(); connectMsg.ScadaList = new Dictionary <string, ConnectMessage.DObject>(); ConnectMessage.DObject scada = new ConnectMessage.DObject(); connectMsg.ScadaList.Add(_options.ScadaId, scada); string payload = JsonConvert.SerializeObject(connectMsg); var message = new MqttApplicationMessageBuilder() .WithTopic((_options.Type == EdgeType.Gateway) ? _scadaConnTopic : _deviceConnTopic) .WithPayload(payload) .WithAtLeastOnceQoS() .WithRetainFlag(true) .Build(); _mqttClient.PublishAsync(message); // start heartbeat timer _heartbeatTimer.Enabled = true; } catch (Exception ex) { //_logger.Error( ex.ToString() ); } }