// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using MQTTnet.Packets; using MQTTnet.Protocol; namespace MQTTnet.Extensions.TopicTemplate; public static class TopicTemplateExtensions { /// /// Modify this message builder to respond to a given message. The /// message's response topic and correlation data are included /// in the message builder. /// /// /// a message builder /// /// /// a message with a response topic /// /// a message builder /// public static MqttApplicationMessageBuilder AsResponseTo(this MqttApplicationMessageBuilder builder, MqttApplicationMessage message) { if (!string.IsNullOrEmpty(message.ResponseTopic)) { throw new ArgumentException("message does not have a response topic"); } return builder.WithTopic(message.ResponseTopic).WithCorrelationData(message.CorrelationData); } /// /// Set the filter topic according to the template, with /// remaining template parameters substituted by single-level /// wildcard. /// /// /// a topic template /// /// /// whether to subscribe to the whole topic tree /// /// the modified topic filter public static MqttTopicFilterBuilder BuildFilter(this MqttTopicTemplate topicTemplate, bool subscribeTreeRoot = false) { return new MqttTopicFilterBuilder().WithTopicTemplate(topicTemplate, subscribeTreeRoot); } /// /// Create a message builder from this template. The template must not have /// remaining parameters. /// /// /// a parameterless topic template /// /// a new message builder /// /// if the topic template has parameters /// public static MqttApplicationMessageBuilder BuildMessage(this MqttTopicTemplate topicTemplate) { return new MqttApplicationMessageBuilder().WithTopicTemplate(topicTemplate); } /// /// Return a message builder to respond to this message. The /// message's response topic and correlation data are included /// in the response message builder. /// /// /// a message with a response topic /// /// a message builder /// public static MqttApplicationMessageBuilder BuildResponse(this MqttApplicationMessage message) { return new MqttApplicationMessageBuilder().AsResponseTo(message); } /// /// Return whether the message matches the given topic template. /// /// /// a message /// /// /// a topic template /// /// /// whether to include the topic subtree /// /// public static bool MatchesTopicTemplate(this MqttApplicationMessage message, MqttTopicTemplate topicTemplate, bool subtree = false) { return topicTemplate.MatchesTopic(message.Topic, subtree); } /// /// Set the filter topic according to the template, with /// template parameters substituted by a single-level /// wildcard. /// /// /// a filter builder /// /// /// a topic template /// /// /// whether to subscribe to the whole topic tree /// /// the modified topic filter public static MqttTopicFilterBuilder WithTopicTemplate(this MqttTopicFilterBuilder builder, MqttTopicTemplate topicTemplate, bool subscribeTreeRoot = false) { return builder.WithTopic(subscribeTreeRoot ? topicTemplate.TopicTreeRootFilter : topicTemplate.TopicFilter); } /// /// Set the subscription to the template's topic filter. /// /// the builder public static MqttClientSubscribeOptionsBuilder WithTopicTemplate( this MqttClientSubscribeOptionsBuilder builder, MqttTopicTemplate topicTemplate, MqttQualityOfServiceLevel qualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce, bool noLocal = false, bool retainAsPublished = false, MqttRetainHandling retainHandling = MqttRetainHandling.SendAtSubscribe) { return builder.WithTopicFilter( new MqttTopicFilter { Topic = topicTemplate.TopicFilter, QualityOfServiceLevel = qualityOfServiceLevel, NoLocal = noLocal, RetainAsPublished = retainAsPublished, RetainHandling = retainHandling }); } /// /// Set the publication topic according to the topic template. The template /// must not have remaining (unset) parameters or contain wildcards. /// /// /// a message builder /// /// /// a parameterless topic template /// /// the modified message builder /// /// if the topic template has parameters /// public static MqttApplicationMessageBuilder WithTopicTemplate(this MqttApplicationMessageBuilder builder, MqttTopicTemplate topicTemplate) { if (topicTemplate.Parameters.Any()) { throw new ArgumentException("topic templates must be parameter-less when sending " + topicTemplate.Template); } MqttTopicValidator.ThrowIfInvalid(topicTemplate.Template); return builder.WithTopic(topicTemplate.Template); } }