MqttApplicationMessageBuilder.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Text;
  5. using MQTTnet.Exceptions;
  6. using MQTTnet.Packets;
  7. using MQTTnet.Protocol;
  8. namespace MQTTnet
  9. {
  10. public class MqttApplicationMessageBuilder
  11. {
  12. private MqttQualityOfServiceLevel _qualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce;
  13. private string _topic;
  14. private byte[] _payload;
  15. private bool _retain;
  16. private string _contentType;
  17. private string _responseTopic;
  18. private byte[] _correlationData;
  19. private ushort? _topicAlias;
  20. private List<uint> _subscriptionIdentifiers;
  21. private uint? _messageExpiryInterval;
  22. private MqttPayloadFormatIndicator? _payloadFormatIndicator;
  23. private List<MqttUserProperty> _userProperties;
  24. public MqttApplicationMessageBuilder WithTopic(string topic)
  25. {
  26. _topic = topic;
  27. return this;
  28. }
  29. public MqttApplicationMessageBuilder WithPayload(byte[] payload)
  30. {
  31. _payload = payload;
  32. return this;
  33. }
  34. public MqttApplicationMessageBuilder WithPayload(IEnumerable<byte> payload)
  35. {
  36. if (payload == null)
  37. {
  38. _payload = null;
  39. return this;
  40. }
  41. _payload = payload as byte[];
  42. if (_payload == null)
  43. {
  44. _payload = payload.ToArray();
  45. }
  46. return this;
  47. }
  48. public MqttApplicationMessageBuilder WithPayload(Stream payload)
  49. {
  50. if (payload == null)
  51. {
  52. _payload = null;
  53. return this;
  54. }
  55. return WithPayload(payload, payload.Length - payload.Position);
  56. }
  57. public MqttApplicationMessageBuilder WithPayload(Stream payload, long length)
  58. {
  59. if (payload == null)
  60. {
  61. _payload = null;
  62. return this;
  63. }
  64. if (payload.Length == 0)
  65. {
  66. _payload = null;
  67. }
  68. else
  69. {
  70. _payload = new byte[length];
  71. payload.Read(_payload, 0, _payload.Length);
  72. }
  73. return this;
  74. }
  75. public MqttApplicationMessageBuilder WithPayload(string payload)
  76. {
  77. if (payload == null)
  78. {
  79. _payload = null;
  80. return this;
  81. }
  82. _payload = string.IsNullOrEmpty(payload) ? null : Encoding.UTF8.GetBytes(payload);
  83. return this;
  84. }
  85. public MqttApplicationMessageBuilder WithQualityOfServiceLevel(MqttQualityOfServiceLevel qualityOfServiceLevel)
  86. {
  87. _qualityOfServiceLevel = qualityOfServiceLevel;
  88. return this;
  89. }
  90. public MqttApplicationMessageBuilder WithRetainFlag(bool value = true)
  91. {
  92. _retain = value;
  93. return this;
  94. }
  95. public MqttApplicationMessageBuilder WithAtLeastOnceQoS()
  96. {
  97. _qualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce;
  98. return this;
  99. }
  100. public MqttApplicationMessageBuilder WithAtMostOnceQoS()
  101. {
  102. _qualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce;
  103. return this;
  104. }
  105. public MqttApplicationMessageBuilder WithExactlyOnceQoS()
  106. {
  107. _qualityOfServiceLevel = MqttQualityOfServiceLevel.ExactlyOnce;
  108. return this;
  109. }
  110. /// <summary>
  111. /// This is only supported when using MQTTv5.
  112. /// </summary>
  113. public MqttApplicationMessageBuilder WithUserProperty(string name, string value)
  114. {
  115. if (_userProperties == null)
  116. {
  117. _userProperties = new List<MqttUserProperty>();
  118. }
  119. _userProperties.Add(new MqttUserProperty(name, value));
  120. return this;
  121. }
  122. /// <summary>
  123. /// This is only supported when using MQTTv5.
  124. /// </summary>
  125. public MqttApplicationMessageBuilder WithContentType(string contentType)
  126. {
  127. _contentType = contentType;
  128. return this;
  129. }
  130. /// <summary>
  131. /// This is only supported when using MQTTv5.
  132. /// </summary>
  133. public MqttApplicationMessageBuilder WithResponseTopic(string responseTopic)
  134. {
  135. _responseTopic = responseTopic;
  136. return this;
  137. }
  138. /// <summary>
  139. /// This is only supported when using MQTTv5.
  140. /// </summary>
  141. public MqttApplicationMessageBuilder WithCorrelationData(byte[] correlationData)
  142. {
  143. _correlationData = correlationData;
  144. return this;
  145. }
  146. /// <summary>
  147. /// This is only supported when using MQTTv5.
  148. /// </summary>
  149. public MqttApplicationMessageBuilder WithTopicAlias(ushort topicAlias)
  150. {
  151. _topicAlias = topicAlias;
  152. return this;
  153. }
  154. /// <summary>
  155. /// This is only supported when using MQTTv5.
  156. /// </summary>
  157. public MqttApplicationMessageBuilder WithSubscriptionIdentifier(uint subscriptionIdentifier)
  158. {
  159. if (_subscriptionIdentifiers == null)
  160. {
  161. _subscriptionIdentifiers = new List<uint>();
  162. }
  163. _subscriptionIdentifiers.Add(subscriptionIdentifier);
  164. return this;
  165. }
  166. /// <summary>
  167. /// This is only supported when using MQTTv5.
  168. /// </summary>
  169. public MqttApplicationMessageBuilder WithMessageExpiryInterval(uint messageExpiryInterval)
  170. {
  171. _messageExpiryInterval = messageExpiryInterval;
  172. return this;
  173. }
  174. /// <summary>
  175. /// This is only supported when using MQTTv5.
  176. /// </summary>
  177. public MqttApplicationMessageBuilder WithPayloadFormatIndicator(MqttPayloadFormatIndicator payloadFormatIndicator)
  178. {
  179. _payloadFormatIndicator = payloadFormatIndicator;
  180. return this;
  181. }
  182. public MqttApplicationMessage Build()
  183. {
  184. if (string.IsNullOrEmpty(_topic))
  185. {
  186. throw new MqttProtocolViolationException("Topic is not set.");
  187. }
  188. var applicationMessage = new MqttApplicationMessage
  189. {
  190. Topic = _topic,
  191. Payload = _payload,
  192. QualityOfServiceLevel = _qualityOfServiceLevel,
  193. Retain = _retain,
  194. ContentType = _contentType,
  195. ResponseTopic = _responseTopic,
  196. CorrelationData = _correlationData,
  197. TopicAlias = _topicAlias,
  198. SubscriptionIdentifiers = _subscriptionIdentifiers,
  199. MessageExpiryInterval = _messageExpiryInterval,
  200. PayloadFormatIndicator = _payloadFormatIndicator,
  201. UserProperties = _userProperties
  202. };
  203. return applicationMessage;
  204. }
  205. }
  206. }