Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
MqttBrokerNode.cs
1using System;
4using System.Threading.Tasks;
13using Waher.Things.Ip;
15
16namespace Waher.Things.Mqtt
17{
22 {
23 private MqttQualityOfService willQoS = MqttQualityOfService.AtLeastOnce;
24 private string userName = string.Empty;
25 private string password = string.Empty;
26 private string willTopic = string.Empty;
27 private string willData = string.Empty;
28 private string brokerKey = null;
29 private string connectionSubscription = "#";
30 private bool willRetain = false;
31 private bool trustServer = false;
32
37 : base()
38 {
39 this.Port = 8883;
40 this.Tls = true;
41 }
42
46 [Page(1, "IP")]
47 [Header(44, "Trust Server", 80)]
48 [ToolTip(45, "If the remote server certificate should be trusted even if it is not valid.")]
49 public bool TrustServer
50 {
51 get => this.trustServer;
52 set => this.trustServer = value;
53 }
54
58 [Page(2, "MQTT")]
59 [Header(3, "User Name:")]
60 [ToolTip(4, "User name used during authentication process.")]
61 [DefaultValueStringEmpty]
62 public string UserName
63 {
64 get => this.userName;
65 set => this.userName = value;
66 }
67
71 [Page(2, "MQTT")]
72 [Header(5, "Password:")]
73 [ToolTip(6, "Password used during authentication process. NOTE: Will be sent in clear text. Don't reuse passwords.")]
74 [Masked]
75 [Encrypted(32)]
76 public string Password
77 {
78 get => this.password;
79 set => this.password = value;
80 }
81
85 public string[] EncryptedProperties => new string[] { nameof(this.Password) };
86
90 [Page(2, "MQTT")]
91 [Header(46, "Connection Subscription:")]
92 [ToolTip(47, "Subscription topic executed when connecting. Empty means no subscription will be performed. Multiple subjects can be comma-separated.")]
93 [DefaultValue("#")]
95 {
96 get => this.connectionSubscription;
97 set => this.connectionSubscription = value;
98 }
99
103 [Page(10, "Last Will and Testament")]
104 [Header(11, "Will Topic:")]
105 [ToolTip(12, "When the connection is lost, a Last Will and Testament can be published on this topic to alert subscribers you've lost connection.")]
106 [DefaultValueStringEmpty]
107 public string WillTopic
108 {
109 get => this.willTopic;
110 set => this.willTopic = value;
111 }
112
116 [Page(10, "Last Will and Testament")]
117 [Header(13, "Will Data:")]
118 [ToolTip(14, "When the connection is lost, this content will be published on the topic defined above.")]
119 [DefaultValueStringEmpty]
120 public string WillData
121 {
122 get => this.willData;
123 set => this.willData = value;
124 }
125
129 [Page(10, "Last Will and Testament")]
130 [Header(15, "Retain Will on topic.")]
131 [ToolTip(16, "If the content published on the will should be retained on the topic.")]
132 [DefaultValue(false)]
133 public bool WillRetain
134 {
135 get => this.willRetain;
136 set => this.willRetain = value;
137 }
138
142 [Page(10, "Last Will and Testament")]
143 [Header(17, "Quality of Service:")]
144 [ToolTip(18, "The quality of service used when sending the last will and testament.")]
145 [DefaultValue(MqttQualityOfService.AtLeastOnce)]
146 [Option(MqttQualityOfService.AtMostOnce, 19, "At most once")]
147 [Option(MqttQualityOfService.AtLeastOnce, 20, "At least once")]
148 [Option(MqttQualityOfService.ExactlyOnce, 19, "Exactly once")]
150 {
151 get => this.willQoS;
152 set => this.willQoS = value;
153 }
154
158 public override Task<string> GetTypeNameAsync(Language Language)
159 {
160 return Language.GetStringAsync(typeof(MqttBrokerNode), 1, "MQTT Broker");
161 }
162
166 public override Task<bool> AcceptsChildAsync(INode Child)
167 {
168 return Task.FromResult(Child is MqttTopicNode);
169 }
170
174 public override Task DestroyAsync()
175 {
176 if (!string.IsNullOrEmpty(this.brokerKey))
177 MqttBrokers.DestroyBroker(this.brokerKey);
178
179 return base.DestroyAsync();
180 }
181
185 [IgnoreMember]
186 public string Key
187 {
188 get
189 {
190 string PrevKey = this.brokerKey;
191 this.brokerKey = MqttBrokers.GetKey(this.Host, this.Port, this.Tls, this.trustServer, this.userName, this.password,
192 this.connectionSubscription);
193
194 if (PrevKey != this.brokerKey && !string.IsNullOrEmpty(PrevKey))
195 MqttBrokers.DestroyBroker(PrevKey);
196
197 return this.brokerKey;
198 }
199 }
200
204 protected override Task NodeUpdated()
205 {
206 this.GetBroker();
207
208 return base.NodeUpdated();
209 }
210
215 public Task<MqttBroker> GetBroker()
216 {
217 return MqttBrokers.GetBroker(this, this.Key, this.Host, this.Port, this.Tls, this.TrustServer, this.userName, this.password,
218 this.connectionSubscription, this.willTopic, this.willData, this.willRetain, this.willQoS);
219 }
220
226 {
227 return MqttBrokers.GetCachedBroker(this.Key);
228 }
229
233 public override async Task<bool> RemoveAsync(INode Child)
234 {
235 if (Child is MqttTopicNode Topic)
236 (await this.GetBroker()).Remove(Topic.LocalTopic);
237
238 return await base.RemoveAsync(Child);
239 }
240
241 #region ICommunicationLayer
242
247 public bool DecoupledEvents => true;
248
252 public void Add(ISniffer Sniffer)
253 {
254 this.GetBroker().Result.Client?.Add(Sniffer); // TODO: Avoid blocking call
255 }
256
260 public void AddRange(IEnumerable<ISniffer> Sniffers)
261 {
262 this.GetBroker().Result.Client?.AddRange(Sniffers); // TODO: Avoid blocking call
263 }
264
268 public bool Remove(ISniffer Sniffer)
269 {
270 return this.GetBroker().Result.Client?.Remove(Sniffer) ?? false; // TODO: Avoid blocking call
271 }
272
277 {
278 get { return this.GetBroker().Result.Client?.Sniffers ?? Array.Empty<ISniffer>(); } // TODO: Avoid blocking call
279 }
280
284 public bool HasSniffers
285 {
286 get { return this.GetBroker().Result.Client?.HasSniffers ?? false; } // TODO: Avoid blocking call
287 }
288
292 public IEnumerator<ISniffer> GetEnumerator()
293 {
294 return new SnifferEnumerator(this.Sniffers);
295 }
296
297 IEnumerator IEnumerable.GetEnumerator()
298 {
299 return this.GetBroker().Result.Client?.GetEnumerator() ?? Array.Empty<ISniffer>().GetEnumerator(); // TODO: Avoid blocking call
300 }
301
306 public void ReceiveBinary(int Count)
307 {
308 MqttBroker Broker = this.GetCachedBroker();
309 MqttClient Client = Broker?.Client;
310 Client?.ReceiveBinary(Count);
311 }
312
319 public void ReceiveBinary(bool ConstantBuffer, byte[] Data)
320 {
321 this.ReceiveBinary(ConstantBuffer, Data, 0, Data.Length);
322 }
323
332 public void ReceiveBinary(bool ConstantBuffer, byte[] Data, int Offset, int Count)
333 {
334 MqttBroker Broker = this.GetCachedBroker();
335 MqttClient Client = Broker?.Client;
336 Client?.ReceiveBinary(ConstantBuffer, Data, Offset, Count);
337 }
338
343 public void TransmitBinary(int Count)
344 {
345 MqttBroker Broker = this.GetCachedBroker();
346 MqttClient Client = Broker?.Client;
347 Client?.TransmitBinary(Count);
348 }
349
356 public void TransmitBinary(bool ConstantBuffer, byte[] Data)
357 {
358 this.TransmitBinary(ConstantBuffer, Data, 0, Data.Length);
359 }
360
369 public void TransmitBinary(bool ConstantBuffer, byte[] Data, int Offset, int Count)
370 {
371 MqttBroker Broker = this.GetCachedBroker();
372 MqttClient Client = Broker?.Client;
373 Client?.TransmitBinary(ConstantBuffer, Data, Offset, Count);
374 }
375
380 public void ReceiveText(string Text)
381 {
382 MqttBroker Broker = this.GetCachedBroker();
383 MqttClient Client = Broker?.Client;
384 Client?.ReceiveText(Text);
385 }
386
391 public void TransmitText(string Text)
392 {
393 MqttBroker Broker = this.GetCachedBroker();
394 MqttClient Client = Broker?.Client;
395 Client?.TransmitText(Text);
396 }
397
402 public void Information(string Comment)
403 {
404 MqttBroker Broker = this.GetCachedBroker();
405 MqttClient Client = Broker?.Client;
406 Client?.Information(Comment);
407 }
408
413 public void Warning(string Warning)
414 {
415 MqttBroker Broker = this.GetCachedBroker();
416 MqttClient Client = Broker?.Client;
417 Client?.Warning(Warning);
418 }
419
424 public void Error(string Error)
425 {
426 MqttBroker Broker = this.GetCachedBroker();
427 MqttClient Client = Broker?.Client;
428 Client?.Error(Error);
429 }
430
435 public void Exception(Exception Exception)
436 {
437 MqttBroker Broker = this.GetCachedBroker();
438 MqttClient Client = Broker?.Client;
439 Client?.Exception(Exception);
440 }
441
446 public void Exception(string Exception)
447 {
448 MqttBroker Broker = this.GetCachedBroker();
449 MqttClient Client = Broker?.Client;
450 Client?.Exception(Exception);
451 }
452
458 public void ReceiveBinary(DateTime Timestamp, int Count)
459 {
460 MqttBroker Broker = this.GetCachedBroker();
461 MqttClient Client = Broker?.Client;
462 Client?.ReceiveBinary(Timestamp, Count);
463 }
464
472 public void ReceiveBinary(DateTime Timestamp, bool ConstantBuffer, byte[] Data)
473 {
474 this.ReceiveBinary(Timestamp, ConstantBuffer, Data, 0, Data.Length);
475 }
476
486 public void ReceiveBinary(DateTime Timestamp, bool ConstantBuffer, byte[] Data, int Offset, int Count)
487 {
488 MqttBroker Broker = this.GetCachedBroker();
489 MqttClient Client = Broker?.Client;
490 Client?.ReceiveBinary(Timestamp, ConstantBuffer, Data, Offset, Count);
491 }
492
498 public void TransmitBinary(DateTime Timestamp, int Count)
499 {
500 MqttBroker Broker = this.GetCachedBroker();
501 MqttClient Client = Broker?.Client;
502 Client?.TransmitBinary(Timestamp, Count);
503 }
504
512 public void TransmitBinary(DateTime Timestamp, bool ConstantBuffer, byte[] Data)
513 {
514 this.TransmitBinary(Timestamp, ConstantBuffer, Data, 0, Data.Length);
515 }
516
526 public void TransmitBinary(DateTime Timestamp, bool ConstantBuffer, byte[] Data, int Offset, int Count)
527 {
528 MqttBroker Broker = this.GetCachedBroker();
529 MqttClient Client = Broker?.Client;
530 Client?.TransmitBinary(Timestamp, ConstantBuffer, Data, Offset, Count);
531 }
532
538 public void ReceiveText(DateTime Timestamp, string Text)
539 {
540 MqttBroker Broker = this.GetCachedBroker();
541 MqttClient Client = Broker?.Client;
542 Client?.ReceiveText(Timestamp, Text);
543 }
544
550 public void TransmitText(DateTime Timestamp, string Text)
551 {
552 MqttBroker Broker = this.GetCachedBroker();
553 MqttClient Client = Broker?.Client;
554 Client?.TransmitText(Timestamp, Text);
555 }
556
562 public void Information(DateTime Timestamp, string Comment)
563 {
564 MqttBroker Broker = this.GetCachedBroker();
565 MqttClient Client = Broker?.Client;
566 Client?.Information(Timestamp, Comment);
567 }
568
574 public void Warning(DateTime Timestamp, string Warning)
575 {
576 MqttBroker Broker = this.GetCachedBroker();
577 MqttClient Client = Broker?.Client;
578 Client?.Warning(Timestamp, Warning);
579 }
580
586 public void Error(DateTime Timestamp, string Error)
587 {
588 MqttBroker Broker = this.GetCachedBroker();
589 MqttClient Client = Broker?.Client;
590 Client?.Error(Timestamp, Error);
591 }
592
598 public void Exception(DateTime Timestamp, string Exception)
599 {
600 MqttBroker Broker = this.GetCachedBroker();
601 MqttClient Client = Broker?.Client;
602 Client?.Exception(Timestamp, Exception);
603 }
604
610 public void Exception(DateTime Timestamp, Exception Exception)
611 {
612 MqttBroker Broker = this.GetCachedBroker();
613 MqttClient Client = Broker?.Client;
614 Client?.Exception(Timestamp, Exception);
615 }
616
617 #endregion
618
622 public override Task<IEnumerable<ICommand>> Commands => this.GetCommands();
623
627 public async Task<IEnumerable<ICommand>> GetCommands()
628 {
629 List<ICommand> Result = new List<ICommand>();
630
631 Result.AddRange(await base.Commands);
632 Result.Add(new ReconnectCommand((await this.GetBroker()).Client));
633
634 return Result;
635 }
636
643 public async override Task<IEnumerable<Parameter>> GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
644 {
645 LinkedList<Parameter> Result = await base.GetDisplayableParametersAsync(Language, Caller) as LinkedList<Parameter>;
646 MqttBroker Broker = await this.GetBroker();
647
648 Result.AddLast(new StringParameter("State", await Language.GetStringAsync(typeof(MqttBrokerNode), 30, "State"),
649 Broker.Client.State.ToString() ?? string.Empty));
650
651 return Result;
652 }
653
654 }
655}
void TransmitText(string Text)
Called when text has been transmitted.
void Exception(Exception Exception)
Called to inform the viewer of an exception state.
void ReceiveText(string Text)
Called when text has been received.
void Warning(string Warning)
Called to inform the viewer of a warning state.
void TransmitBinary(int Count)
Called when binary data has been transmitted.
void ReceiveBinary(int Count)
Called when binary data has been received.
void Information(string Comment)
Called to inform the viewer of something.
Manages an MQTT connection. Implements MQTT v3.1.1, as defined in http://docs.oasis-open....
Definition: MqttClient.cs:30
MqttState State
Current state of connection.
Definition: MqttClient.cs:821
Contains information about a language.
Definition: Language.cs:17
Task< string > GetStringAsync(Type Type, int Id, string Default)
Gets the string value of a string ID. If no such string exists, a string is created with the default ...
Definition: Language.cs:209
string Host
Host name or IP address.
Definition: IpHost.cs:73
Node representing a port on an IP Host machine.
Definition: IpHostPort.cs:24
bool Tls
If connection is encrypted using TLS or not.
Definition: IpHostPort.cs:70
int Port
Port number.
Definition: IpHostPort.cs:58
MQTT Broker connection object.
Definition: MqttBroker.cs:17
Static class managing connections to MQTT brokers.
Definition: MqttBrokers.cs:13
static async Task< MqttBroker > GetBroker(MqttBrokerNode Node, string Key, string Host, int Port, bool Tls, bool TrustServer, string UserName, string Password, string ConnectionSubscription, string WillTopic, string WillData, bool WillRetain, MqttQualityOfService WillQoS)
Gets an MQTT Broker object, according to connection parameters. If one is not in memory,...
Definition: MqttBrokers.cs:62
static string GetKey(string Host, int Port, bool Tls, bool TrustServer, string UserName, string Password, string ConnectionSubscription)
Gets sort key for MQTT broker
Definition: MqttBrokers.cs:27
static MqttBroker GetCachedBroker(string Key)
Gets an MQTT Broker object, if available in the cache.
Definition: MqttBrokers.cs:46
static Task DestroyBroker(string Key)
TODO
Definition: MqttBrokers.cs:108
Node representing a connection to an MQTT broker.
override Task DestroyAsync()
TODO
string ConnectionSubscription
Startup subscription
ISniffer[] Sniffers
Registered sniffers.
void TransmitText(string Text)
Called when text has been transmitted.
void Warning(string Warning)
Called to inform the viewer of a warning state.
override async Task< bool > RemoveAsync(INode Child)
TODO
void Error(DateTime Timestamp, string Error)
Called to inform the viewer of an error state.
void Information(DateTime Timestamp, string Comment)
Called to inform the viewer of something.
void ReceiveBinary(int Count)
Called when binary data has been received.
void ReceiveText(string Text)
Called when text has been received.
async override Task< IEnumerable< Parameter > > GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
Gets displayable parameters.
void ReceiveBinary(DateTime Timestamp, bool ConstantBuffer, byte[] Data, int Offset, int Count)
Called when binary data has been received.
void TransmitText(DateTime Timestamp, string Text)
Called when text has been transmitted.
void Add(ISniffer Sniffer)
ICommunicationLayer.Add
void ReceiveText(DateTime Timestamp, string Text)
Called when text has been received.
void Exception(DateTime Timestamp, string Exception)
Called to inform the viewer of an exception state.
void AddRange(IEnumerable< ISniffer > Sniffers)
ICommunicationLayer.AddRange
void Information(string Comment)
Called to inform the viewer of something.
string[] EncryptedProperties
Array of properties that are encrypted.
override Task< bool > AcceptsChildAsync(INode Child)
TODO
void ReceiveBinary(bool ConstantBuffer, byte[] Data)
Called when binary data has been received.
void TransmitBinary(bool ConstantBuffer, byte[] Data)
Called when binary data has been transmitted.
void TransmitBinary(DateTime Timestamp, int Count)
Called when binary data has been transmitted.
Task< MqttBroker > GetBroker()
Gets the corresponding broker node.
IEnumerator< ISniffer > GetEnumerator()
IEnumerable<T>.GetEnumerator()
MqttBroker GetCachedBroker()
Gets the corresponding broker node, if available in the cache.
void Warning(DateTime Timestamp, string Warning)
Called to inform the viewer of a warning state.
void Error(string Error)
Called to inform the viewer of an error state.
void TransmitBinary(bool ConstantBuffer, byte[] Data, int Offset, int Count)
Called when binary data has been transmitted.
void Exception(string Exception)
Called to inform the viewer of an exception state.
bool HasSniffers
If there are sniffers registered on the object.
async Task< IEnumerable< ICommand > > GetCommands()
TODO
bool TrustServer
If connection is encrypted using TLS or not.
void ReceiveBinary(DateTime Timestamp, int Count)
Called when binary data has been received.
void TransmitBinary(DateTime Timestamp, bool ConstantBuffer, byte[] Data, int Offset, int Count)
Called when binary data has been transmitted.
bool Remove(ISniffer Sniffer)
ICommunicationLayer.Remove
override Task< string > GetTypeNameAsync(Language Language)
Type name representing data.
void TransmitBinary(int Count)
Called when binary data has been transmitted.
void Exception(Exception Exception)
Called to inform the viewer of an exception state.
void ReceiveBinary(DateTime Timestamp, bool ConstantBuffer, byte[] Data)
Called when binary data has been received.
override Task< IEnumerable< ICommand > > Commands
TODO
MqttQualityOfService WillQoS
TODO
void TransmitBinary(DateTime Timestamp, bool ConstantBuffer, byte[] Data)
Called when binary data has been transmitted.
bool DecoupledEvents
If events raised from the communication layer are decoupled, i.e. executed in parallel with the sourc...
MqttBrokerNode()
Node representing a connection to an MQTT broker.
void Exception(DateTime Timestamp, Exception Exception)
Called to inform the viewer of an exception state.
void ReceiveBinary(bool ConstantBuffer, byte[] Data, int Offset, int Count)
Called when binary data has been received.
A Metering node representing an MQTT topic
Tokens available in request.
Definition: RequestOrigin.cs:9
Interface for observable classes implementing communication protocols.
Interface for sniffers. Sniffers can be added to ICommunicationLayer classes to eavesdrop on communic...
Definition: ISniffer.cs:10
Interface for objects containing encrypted properties. Mark the properties that are encrypted with th...
Interface for nodes that are published through the concentrator interface.
Definition: INode.cs:49
MqttQualityOfService
MQTT Quality of Service level.