Example #1
0
 private void publishButton_Click(object sender, RoutedEventArgs e)
 {
     if (mqttClient.IsConnected)
     {
         var message = new MqttApplicationMessageBuilder()
                       .WithTopic(publishTopicTextBox.Text)
                       .WithPayload(HexCheckBox.IsChecked ?? false ?
                                    Tools.Global.GetEncoding().GetString(Tools.Global.Hex2Byte(PublishTextBox.Text)) :
                                    PublishTextBox.Text)
                       .WithQualityOfServiceLevel(int.Parse(publishQOSComboBox.Text))
                       .Build();
         Task.Run(async() =>
         {
             try
             {
                 await mqttClient.PublishAsync(message, CancellationToken.None);
                 this.Dispatcher.Invoke(new Action(delegate
                 {
                     Tools.Logger.ShowDataRaw(new Tools.DataShowRaw
                     {
                         title = $"MQTT ← {message.Topic}({(int)message.QualityOfServiceLevel})",
                         data  = message.Payload ?? new byte[0],
                         color = Brushes.DarkRed
                     });
                 }));
             }
             catch { }
         });
     }
 }
Example #2
0
        public async Task PublishAsync(string topic, byte[] payload, Dictionary <string, string> properties, Qos qos, CancellationToken cancellationToken)
        {
            var mqttMessageBuilder = new MqttApplicationMessageBuilder()
                                     .WithTopic(topic)
                                     .WithPayload(payload)
                                     .WithQualityOfServiceLevel(MapQos(qos));

            if (properties != null)
            {
                foreach (var entry in properties)
                {
                    mqttMessageBuilder.WithUserProperty(entry.Key, entry.Value);
                }
            }

            MqttClientPublishResult publishResult;

            try
            {
                publishResult = await client.PublishAsync(mqttMessageBuilder.Build(), cancellationToken);
            }
            catch (Exception e)
            {
                throw new MqttException(cause: e);
            }

            ValidatePublishResult(publishResult);
        }
Example #3
0
        public async Task PublishAsync(string topic, string payload, int qos, bool retain)
        {
            var message = new MqttApplicationMessageBuilder()
                          .WithTopic(topic);

            if (!string.IsNullOrEmpty(payload))
            {
                message = message.WithPayload(payload);
            }

            if (qos == 2)
            {
                message = message.WithExactlyOnceQoS();
            }
            else
            {
                message = message.WithAtLeastOnceQoS();
            }

            if (retain)
            {
                message = message.WithRetainFlag();
            }

            await client.PublishAsync(message.Build());
        }
        public async Task PublishAsync(string topic, string payload)
        {
            var message = new MqttApplicationMessageBuilder()
                          .WithTopic($"{settings.Value.TopicRoot}/{topic}")
                          .WithPayload(payload)
                          .Build();

            await client.PublishAsync(message);

            logger.LogInformation($"Published '{settings.Value.TopicRoot}/{topic} {payload}'");
        }
 public async Task SendMessageAsync(string topicName, string message)
 {
     try
     {
         await _client.PublishAsync(topicName, message, MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce, false);
     }
     catch (Exception ex)
     {
         _logger.LogError("SendMessageAsync error:\n" + ex.ToString());
     }
 }
Example #6
0
        public async void PublisherAsync(string theme)
        {
            Signals signals1 = new Signals()
            {
                Name = "PLK1", Data = "11", Time = new DateTime(2001, 1, 1, 0, 0, 0), Quality = 111, Edizm = "Om", Type = 1
            };
            Signals signals2 = new Signals()
            {
                Name = "PLK2", Data = "22", Time = new DateTime(2002, 2, 2, 0, 0, 0), Quality = 222, Edizm = "K", Type = 1
            };
            string json1 = JsonSerializer.Serialize <Signals>(signals1);
            string json2 = JsonSerializer.Serialize <Signals>(signals2);

            string[] json = new string[2] {
                json1, json2
            };
            int i = 0;

            do
            {
                //await Task.Delay(TimeSpan.FromSeconds(1));
                {
                    var message = new MqttApplicationMessageBuilder()
                                  .WithTopic(theme)
                                  .WithPayload(json[i].ToString())
                                  .WithQualityOfServiceLevel(MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce)
                                  .WithRetainFlag(true)
                                  .Build();
                    if (mqttClient.IsConnected)
                    {
                        await mqttClient.PublishAsync(message, CancellationToken.None);

                        i++;
                    }
                }
            } while (i < 2);
        }