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;
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
154 public override bool IsControllable
155 {
156 get
157 {
158 if (this.Disabled)
159 return false;
160
161 if (base.IsControllable)
162 return true;
163
164 return this.GetTopic().Result?.IsControllable ?? false;
165 }
166 }
167
172 public override async Task<ControlParameter[]> GetControlParameters()
173 {
174 List<ControlParameter> Parameters = new List<ControlParameter>();
175 Parameters.AddRange(await base.GetControlParameters());
176
177 if ((await this.GetTopic())?.GetControlParameters() is ControlParameter[] P)
178 Parameters.AddRange(P);
179
180 return Parameters.ToArray();
181 }
182
186 public override bool IsReadable => !this.Disabled;
187
194 public override async Task StartReadout(ISensorReadout Request, bool DoneAfter)
195 {
196 try
197 {
198 MqttTopic Topic = await this.GetTopic();
199
200 await base.StartReadout(Request, (Topic is null) && DoneAfter);
201
202 if (!(Topic is null))
203 await Topic.StartReadout(Request, DoneAfter);
204 }
205 catch (Exception ex)
206 {
207 await Request.ReportErrors(true, new ThingError(this, ex.Message));
208 }
209 }
210
217 {
218 return Grade.Barely;
219 }
220
226 public virtual async Task<IMqttTopicNode> CreateNew(MqttTopicRepresentation Topic)
227 {
228 return new MqttTopicNode()
229 {
231 LocalTopic = Topic.CurrentSegment
232 };
233 }
234
239 public virtual Task<IMqttData> GetDefaultDataObject()
240 {
241 return Task.FromResult<IMqttData>(null);
242 }
243
244 }
245}
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:30
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:302
MQTT Topic information.
Definition: MqttTopic.cs:19
async Task< IEnumerable< Parameter > > GetDisplayableParametersAsync(LinkedList< Parameter > Parameters, Language Language, RequestOrigin _)
TODO
Definition: MqttTopic.cs:389
Task StartReadout(ISensorReadout Request, bool DoneAfter)
Starts the readout of the sensor.
Definition: MqttTopic.cs:317
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 bool IsControllable
If the node can be controlled.
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