Example #1
0
        public async Task PublishLeafDeviceMessage(LocalDeviceMqttPublicationCategory category, string topicName, string payload, IDictionary <string, string> propertyBag, CancellationToken stopToken)
        {
            var propertyUrlString = "";

            if (propertyBag != null)
            {
                propertyUrlString = UrlEncodedDictionarySerializer.Serialize(propertyBag);
            }


            if (category == LocalDeviceMqttPublicationCategory.Twin)
            {
                // if topic name is null send to all topics
                foreach (var t in device.LocalDeviceMqttPublications.Twin)
                {
                    var t1 = t.Topic.Trim('/') + "/" + propertyUrlString;  // Ensure topic ends with a forward slash /

                    if (topicName == null)
                    {
                        // send to all topics
                        logger.LogInformation($"SendMessage: Topic {t1} with Payload {payload}");
                        var msg = new MqttApplicationMessageBuilder()
                                  .WithTopic(t1)
                                  .WithPayload(payload)
                                  .WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce) // qos = 1
                                  .Build();

                        await downstreamClient.PublishAsync(msg, stopToken);
                    }
                    else if (topicName == t.Name)
                    {
                        logger.LogInformation($"SendMessage: Topic {t1} with Payload {payload}");
                        var msg = new MqttApplicationMessageBuilder()
                                  .WithTopic(t1)
                                  .WithPayload(payload)
                                  .WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce) // qos = 1
                                  .Build();

                        await downstreamClient.PublishAsync(msg, stopToken);
                    }
                }
            }
        }
Example #2
0
 public PublishToLocalDeviceTopicResult(LocalDeviceMqttPublicationCategory category, string payload)
 {
     Category = category;
     Payload  = payload;
 }