项目作者: pBouillon

项目描述 :
:postbox: Build valid and verified MQTT topics
高级语言: C#
项目地址: git://github.com/pBouillon/MqttTopicBuilder.git
创建时间: 2019-07-31T21:09:54Z
项目社区:https://github.com/pBouillon/MqttTopicBuilder

开源协议:MIT License

下载




MqttTopicBuilder



Build valid and verified MQTT topics





MqttTopicBuilder is a small library without any dependency that can help you to
enforce the validity of your MQTT topics
by using the provided builder.

Installation

You can find this projet on NuGet.

From the command line:

  1. dotnet add package MqttTopicBuilder

From the package manager:

  1. Install-Package MqttTopicBuilder

Usage

Using a custom builder, MqttTopicBuilder allows you to build topics and ensure
their validity regarding the way you are planning to use them.

  1. var subscribeTo = new TopicBuilder(TopicConsumer.Subscriber)
  2. .AddTopic("Hello")
  3. .AddTopic("From")
  4. .AddTopics("Mqtt", "Topic", "Builder")
  5. .AddMultiLevelWildcard()
  6. .Build();
  7. Console.WriteLine(subscribeTo);
  8. // -> "Hello/From/Mqtt/Topic/Builder/#"
  9. var publishTop = new TopicBuilder(TopicConsumer.Publisher)
  10. .AddTopic("Hello")
  11. .AddTopic("From")
  12. .AddTopics("Mqtt", "Topic", "Builder")
  13. .AddMultiLevelWildcard()
  14. .Build();
  15. // Will throw an exception since wildcards are not allowed when publishing
  16. // on a topic

The object built is a Topic object. It can be used to both access the topic
but also gather informations about it such as its level.

  1. var topic = new TopicBuilder(TopicConsumer.Subscriber)
  2. .AddTopic("Hello")
  3. .AddTopic("World")
  4. .Build();
  5. Console.WriteLine(topic.Value);
  6. // -> "Hello/World"
  7. Console.WriteLine(topic.Levels);
  8. // -> 2

Topics can also be built using the regular constructor or the extension method:

  1. var topic = Topic.FromString("Hello/World");
  2. // or: var topic = (Topic) "Hello/World";
  3. Console.WriteLine(topic.Value);
  4. // -> "Hello/World"
  5. Console.WriteLine(topic.Levels);
  6. // -> 2

Topic integrity can also be checked using the TopicValidator methods

  1. TopicValidator.ValidateTopic("a/wrong/#/Topic");
  2. // Will throw an exception since no topic is allowed after '#'
  3. "wrong+Topic".ValidateTopicForAppending();
  4. // Will throw an exception since '+' is not allowed in a topic

Contributions

All contributions are welcome, please feel free to suggest pull requests !
You can read more about it in the CONTRIBUTING.md.