Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
MqttTopicNode.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
13
14namespace Waher.Things.Mqtt
15{
20 {
21 private string localTopic = string.Empty;
22
27 : base()
28 {
29 }
30
34 [Page(2, "MQTT")]
35 [Header(22, "Local Topic:")]
36 [ToolTip(23, "Local topic, unique among siblings.")]
37 [DefaultValueStringEmpty]
38 public string LocalTopic
39 {
40 get => this.localTopic;
41 set => this.localTopic = value;
42 }
43
47 public override string LocalId => this.localTopic;
48
52 public async Task<string> GetFullTopic()
53 {
54 string Result = this.localTopic;
55
56 IMqttTopicNode Loop = await this.GetParent() as IMqttTopicNode;
57 while (!(Loop is null))
58 {
59 Result = Loop.LocalTopic + "/" + Result;
60 Loop = await Loop.GetParent() as IMqttTopicNode;
61 }
62
63 return Result;
64 }
65
69 public async Task<MqttBrokerNode> GetBroker()
70 {
71 INode Parent = await this.GetParent();
72
73 while (!(Parent is null))
74 {
75 if (Parent is MqttBrokerNode Broker)
76 return Broker;
79 else
81 }
82
83 return null;
84 }
85
89 public override Task<bool> AcceptsChildAsync(INode Child)
90 {
91 return Task.FromResult(Child is IMqttTopicNode);
92 }
93
97 public override Task<bool> AcceptsParentAsync(INode Parent)
98 {
99 return Task.FromResult(Parent is IMqttTopicNode || Parent is MqttBrokerNode);
100 }
101
105 public override Task<string> GetTypeNameAsync(Language Language)
106 {
107 return Language.GetStringAsync(typeof(IMqttTopicNode), 24, "Topic");
108 }
109
113 public async Task<MqttTopic> GetTopic()
114 {
115 MqttBrokerNode BrokerNode = await this.GetBroker();
116 MqttBroker Broker = await BrokerNode.GetBroker();
117 if (Broker is null)
118 return null;
119 else
120 return await Broker.GetTopic(await this.GetFullTopic(), false, false);
121 }
122
129 public override async Task<IEnumerable<Parameter>> GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
130 {
131 LinkedList<Parameter> Parameters = (LinkedList<Parameter>)await base.GetDisplayableParametersAsync(Language, Caller);
132 MqttTopic Topic = await this.GetTopic();
133
134 if (!(Topic is null))
135 await Topic.GetDisplayableParametersAsync(Parameters, Language, Caller);
136
137 return Parameters;
138 }
139
143 public async override Task<bool> RemoveAsync(INode Child)
144 {
145 if (Child is IMqttTopicNode Topic)
146 (await this.GetTopic())?.Remove(Topic.LocalTopic);
147
148 return await base.RemoveAsync(Child);
149 }
150
155 public override async Task<ControlParameter[]> GetControlParameters()
156 {
157 List<ControlParameter> Parameters = new List<ControlParameter>();
158 Parameters.AddRange(await base.GetControlParameters());
159
160 if (this.GetTopic()?.Result?.GetControlParameters() is ControlParameter[] P)
161 Parameters.AddRange(P);
162
163 return Parameters.ToArray();
164 }
165
169 public override bool IsReadable => true;
170
177 public override async Task StartReadout(ISensorReadout Request, bool DoneAfter)
178 {
179 try
180 {
181 MqttTopic Topic = await this.GetTopic();
182
183 await base.StartReadout(Request, (Topic is null) && DoneAfter);
184
185 if (!(Topic is null))
186 await Topic.StartReadout(Request, DoneAfter);
187 }
188 catch (Exception ex)
189 {
190 await Request.ReportErrors(true, new ThingError(this, ex.Message));
191 }
192 }
193
200 {
201 return Grade.Barely;
202 }
203
209 public virtual async Task<IMqttTopicNode> CreateNew(MqttTopicRepresentation Topic)
210 {
211 return new MqttTopicNode()
212 {
214 LocalTopic = Topic.CurrentSegment
215 };
216 }
217
222 public virtual Task<IMqttData> GetDefaultDataObject()
223 {
224 return Task.FromResult<IMqttData>(null);
225 }
226
227 }
228}
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
Abstract base class for control parameters.
Base class for all metering nodes.
Definition: MeteringNode.cs:28
static async Task< string > GetUniqueNodeId(string NodeId)
Gets a Node ID, based on NodeId that is not already available in the database.
async Task< INode > GetParent()
Gets the parent of the node.
MQTT Broker connection object.
Definition: MqttBroker.cs:17
async Task< MqttTopic > GetTopic(string TopicString, bool CreateNew, bool IgnoreGuids)
Gets the Node responsible for managing a Topic
Definition: MqttBroker.cs:309
MQTT Topic information.
Definition: MqttTopic.cs:19
async Task< IEnumerable< Parameter > > GetDisplayableParametersAsync(LinkedList< Parameter > Parameters, Language Language, RequestOrigin _)
TODO
Definition: MqttTopic.cs:382
Task StartReadout(ISensorReadout Request, bool DoneAfter)
Starts the readout of the sensor.
Definition: MqttTopic.cs:319
Node representing a connection to an MQTT broker.
Task< MqttBroker > GetBroker()
Gets the corresponding broker node.
A Metering node representing an MQTT topic
virtual Task< IMqttData > GetDefaultDataObject()
Gets the default data object, if any.
override string LocalId
Local ID
async Task< MqttBrokerNode > GetBroker()
TODO
override Task< bool > AcceptsChildAsync(INode Child)
If the node accepts a given child.
override bool IsReadable
If the node can be read.
async Task< MqttTopic > GetTopic()
TODO
override async Task< IEnumerable< Parameter > > GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
Gets displayable parameters.
virtual async Task< IMqttTopicNode > CreateNew(MqttTopicRepresentation Topic)
Creates a new node of the same type.
MqttTopicNode()
A Metering node representing an MQTT topic
override async Task StartReadout(ISensorReadout Request, bool DoneAfter)
Starts the readout of the sensor.
async override Task< bool > RemoveAsync(INode Child)
TODO
override async Task< ControlParameter[]> GetControlParameters()
Get control parameters for the actuator.
override Task< bool > AcceptsParentAsync(INode Parent)
If the node accepts a given parent.
string LocalTopic
Local Topic segment
virtual Grade Supports(MqttTopicRepresentation Topic)
How well the topic node supports an MQTT topic
async Task< string > GetFullTopic()
Gets the full topic string.
override Task< string > GetTypeNameAsync(Language Language)
Diaplayable type name for node.
Contains information about an MQTT topic
string CurrentSegment
Current segment being processed.
Tokens available in request.
Definition: RequestOrigin.cs:9
Contains information about an error on a thing
Definition: ThingError.cs:10
Virtual node, that can be used as a placeholder for services.
Definition: VirtualNode.cs:28
Interface for actuator nodes.
Definition: IActuator.cs:10
Interface for nodes that are published through the concentrator interface.
Definition: INode.cs:49
INode Parent
Parent Node, or null if a root node.
Definition: INode.cs:116
Interface for sensor nodes.
Definition: ISensor.cs:9
Interface for classes managing sensor data readouts.
Task ReportErrors(bool Done, params ThingError[] Errors)
Report error states to the client.
new INode Parent
Parent Node, or null if a root node.
Task< INode > GetParent()
Gets the parent of the node.
Interface for MQTT Topic nodes.
Interface for MQTT Data encapsulations
Definition: IMqttData.cs:38
Grade
Grade enumeration
Definition: Grade.cs:7