Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
MqttNcapTopicNode.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
11
13{
18 {
19 private string entityName;
20 private string ncapId;
21 private byte[] ncapIdBin;
22 private int timeoutMilliseconds = 10000;
23 private int staleSeconds = 60;
24 private int refreshTedsHours = 24;
25
30 {
31 }
32
36 [Page(1, "IEEE 1451")]
37 [Header(25, "Entity Name:", 50)]
38 [ToolTip(26, "Name of entity, as configured in the device.")]
39 public string EntityName
40 {
41 get => this.entityName;
42 set => this.entityName = value;
43 }
44
48 [Page(1, "IEEE 1451")]
49 [Header(2, "NCAP ID:", 100)]
50 [ToolTip(3, "NCAP unique identifier.")]
51 [Required]
52 [RegularExpression("[A-Fa-f0-9]{32}")]
53 public string NcapId
54 {
55 get => this.ncapId;
56 set
57 {
58 this.ncapIdBin = Hashes.StringToBinary(value);
59 this.ncapId = value;
60 }
61 }
62
66 [Page(1, "IEEE 1451")]
67 [Header(16, "Timeout: (ms)", 1000)]
68 [ToolTip(17, "Maximum amount of time to wait (in milliseconds) for a response to a request.")]
69 [Required]
70 [Range(1, int.MaxValue)]
72 {
73 get => this.timeoutMilliseconds;
74 set => this.timeoutMilliseconds = value;
75 }
76
80 [Page(1, "IEEE 1451")]
81 [Header(18, "Stale after: (s)", 2000)]
82 [ToolTip(19, "Flags information as stale (old) after this amount of time, triggering new requests if information is requested.")]
83 [Required]
84 [Range(1, int.MaxValue)]
85 public int StaleSeconds
86 {
87 get => this.staleSeconds;
88 set => this.staleSeconds = value;
89 }
90
94 [Page(1, "IEEE 1451")]
95 [Header(27, "Refresh TEDS: (h)", 3000)]
96 [ToolTip(28, "Re-fetches TEDS from device, if current TEDS is older than this number of hours.")]
97 [Required]
98 [Range(1, int.MaxValue)]
100 {
101 get => this.refreshTedsHours;
102 set => this.refreshTedsHours = value;
103 }
104
108 public byte[] NcapIdBinary => this.ncapIdBin;
109
113 public override string LocalId
114 {
115 get
116 {
117 if (!string.IsNullOrEmpty(this.entityName))
118 return this.entityName;
119 else
120 return this.NodeId;
121 }
122 }
123
127 public override Task<string> GetTypeNameAsync(Language Language)
128 {
129 return Language.GetStringAsync(typeof(Ieee1451Parser), 5, "IEEE 1451.0 NCAP");
130 }
131
138 public override async Task<IEnumerable<Parameter>> GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
139 {
140 LinkedList<Parameter> Parameters = (LinkedList<Parameter>)await base.GetDisplayableParametersAsync(Language, Caller);
141
142 Parameters.AddLast(new StringParameter("NcapId",
143 await Language.GetStringAsync(typeof(MqttNcapTopicNode), 4, "NCAP ID"),
144 this.ncapId));
145
146 return Parameters;
147 }
148
152 public override Task<bool> AcceptsParentAsync(INode Parent)
153 {
154 return Task.FromResult(Parent is DiscoverableTopicNode);
155 }
156
160 public override Task<bool> AcceptsChildAsync(INode Child)
161 {
162 return Task.FromResult(Child is MqttTimTopicNode);
163 }
164
171 {
172 if (Topic.CurrentParentTopic?.Node is MqttTopicNode &&
174 Guid.TryParse(Topic.CurrentSegment, out _))
175 {
176 return Grade.Excellent;
177 }
178 else
179 return Grade.NotAtAll;
180 }
181
187 public override async Task<IMqttTopicNode> CreateNew(MqttTopicRepresentation Topic)
188 {
189 return new MqttNcapTopicNode()
190 {
191 NodeId = await GetUniqueNodeId("NCAP-" + Topic.CurrentSegment),
193 NcapId = Topic.CurrentSegment
194 };
195 }
196
201 public override async Task<IMqttData> GetDefaultDataObject()
202 {
203 MqttTopic Topic = await this.GetTopic();
204 if (Topic is null)
205 return null;
206
207 return new MessageData(Topic, this.ncapIdBin,
208 (this as MqttTimTopicNode)?.TimIdBinary,
209 (ushort)((this as MqttChannelTopicNode)?.ChannelId ?? 0));
210 }
211
218 public bool ResponseReceived(MqttTopic Topic, Ieee1451_0.Messages.Message Message)
219 {
220 if (Topic.Data is MessageData Data)
221 return Data.DataReported(Message);
222 else
223 {
224 Topic.SetData(new MessageData(Topic, Message, this.NcapIdBinary,
225 (this as MqttTimTopicNode)?.TimIdBinary,
226 (ushort)((this as MqttChannelTopicNode)?.ChannelId ?? 0)));
227
228 return false;
229 }
230 }
231
236 public virtual async Task NameReceived(string Name)
237 {
238 bool Changed = false;
239
240 if (this.entityName != Name)
241 {
242 this.entityName = Name;
243 Changed = true;
244 }
245
246 if (string.IsNullOrEmpty(this.Name))
247 {
248 this.Name = Name;
249 Changed = true;
250 }
251
252 if (Changed)
253 await this.NodeUpdated();
254 }
255
256 }
257}
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
Contains methods for simple hash calculations.
Definition: Hashes.cs:59
static byte[] StringToBinary(string s)
Parses a hex string.
Definition: Hashes.cs:102
Contains information about a message logged on a node.
Definition: Message.cs:32
MQTT Topic node that publishes discovery commands in accordance with IEEE 1451.0.
Encapsulates messages from an IEEE1451.1.6 device.
Definition: MessageData.cs:44
Topic node representing an IEEE 1451.0 Channel.
Topic node representing an IEEE 1451.0 NCAP.
override Task< bool > AcceptsParentAsync(INode Parent)
If the node accepts a given parent.
virtual async Task NameReceived(string Name)
Name has been received
int StaleSeconds
Timeout for request/response, in milliseconds.
override Task< string > GetTypeNameAsync(Language Language)
Diaplayable type name for node.
MqttNcapTopicNode()
Topic node representing an IEEE 1451.0 NCAP.
int TimeoutMilliseconds
Timeout for request/response, in milliseconds.
override async Task< IMqttTopicNode > CreateNew(MqttTopicRepresentation Topic)
Creates a new node of the same type.
override Grade Supports(MqttTopicRepresentation Topic)
How well the topic node supports an MQTT topic
override async Task< IEnumerable< Parameter > > GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
Gets displayable parameters.
override Task< bool > AcceptsChildAsync(INode Child)
If the node accepts a given child.
int RefreshTedsHours
Refresh TEDS if older than this number of hours.
override async Task< IMqttData > GetDefaultDataObject()
Gets the default data object, if any.
bool ResponseReceived(MqttTopic Topic, Ieee1451_0.Messages.Message Message)
A response message has been received.
Topic node representing an IEEE 1451.0 TIM.
Static class for IEEE 1451-related parsing tasks.
string Name
If the node is provisioned is not. Property is editable.
virtual async Task NodeUpdated()
Persists changes to the node, and generates a node updated event.
static async Task< string > GetUniqueNodeId(string NodeId)
Gets a Node ID, based on NodeId that is not already available in the database.
MQTT Topic information.
Definition: MqttTopic.cs:19
IMqttTopicNode Node
Reference to the MQTT Topic Node
Definition: MqttTopic.cs:51
IMqttData Data
Current parsed data.
Definition: MqttTopic.cs:71
A Metering node representing an MQTT topic
async Task< MqttTopic > GetTopic()
TODO
string LocalTopic
Local Topic segment
Contains information about an MQTT topic
string CurrentSegment
Current segment being processed.
MqttTopic CurrentParentTopic
Current parent topic.
Tokens available in request.
Definition: RequestOrigin.cs:9
Interface for nodes that are published through the concentrator interface.
Definition: INode.cs:49
new INode Parent
Parent Node, or null if a root node.
Grade
Grade enumeration
Definition: Grade.cs:7