Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ProxyMqttNcapTopicNode.cs
1using System.Collections.Generic;
2using System.Text;
3using System.Threading.Tasks;
14
16{
21 {
26 {
27 }
28
32 public override Task<string> GetTypeNameAsync(Language Language)
33 {
34 return Language.GetStringAsync(typeof(Ieee1451Parser), 8, "IEEE 1451.0 Proxy NCAP");
35 }
36
40 public override Task<bool> AcceptsParentAsync(INode Parent)
41 {
42 return Task.FromResult(Parent is DiscoverableTopicNode);
43 }
44
48 public override Task<bool> AcceptsChildAsync(INode Child)
49 {
50 return Task.FromResult(Child is ProxyMqttTimTopicNode);
51 }
52
58 public override Grade Supports(MqttTopicRepresentation Topic)
59 {
60 return Grade.NotAtAll;
61 }
62
68 {
69 if (!(await this.GetParent() is DiscoverableTopicNode Parent))
70 return;
71
72 MqttBrokerNode BrokerNode = await Parent.GetBroker();
73 if (BrokerNode is null)
74 return;
75
76 MqttBroker Broker = await BrokerNode.GetBroker();
77 if (Broker is null)
78 return;
79
80 string Topic = await Parent.GetFullTopic();
81 if (string.IsNullOrEmpty(Topic))
82 return;
83
85 {
86 case DiscoveryService.NCAPDiscovery:
87 StringBuilder ToSniffer = BrokerNode.HasSniffers ? new StringBuilder() : null;
88 byte[] Response = DiscoveryMessage.SerializeResponse(0, this.NcapIdBinary, this.EntityName, ToSniffer);
89
90 if (!(ToSniffer is null))
91 await BrokerNode.Information(ToSniffer.ToString());
92
93 await Broker.Publish(Topic, MqttQualityOfService.AtLeastOnce, false, Response);
94 break;
95
96 case DiscoveryService.NCAPTIMDiscovery:
97 List<byte[]> TimIds = new List<byte[]>();
98 List<string> Names = new List<string>();
99
100 foreach (INode Child in await this.ChildNodes)
101 {
102 if (!(Child is ProxyMqttTimTopicNode TimNode))
103 continue;
104
105 TimIds.Add(TimNode.TimIdBinary);
106 Names.Add(TimNode.EntityName);
107 }
108
109 if (TimIds.Count == 0)
110 break;
111
112 ToSniffer = BrokerNode.HasSniffers ? new StringBuilder() : null;
113 Response = DiscoveryMessage.SerializeResponse(0, this.NcapIdBinary, TimIds.ToArray(), Names.ToArray(), ToSniffer);
114
115 if (!(ToSniffer is null))
116 await BrokerNode.Information(ToSniffer.ToString());
117
118 await Broker.Publish(Topic, MqttQualityOfService.AtLeastOnce, false, Response);
119 break;
120 }
121 }
122
127 public override Task NameReceived(string Name)
128 {
129 return Task.CompletedTask; // Name controlled from broker, not from external sources.
130 }
131
140 TedsAccessCode TedsAccessCode, uint TedsOffset, double TimeoutSeconds)
141 {
142 if (!(await this.GetParent() is DiscoverableTopicNode CommunicationNode))
143 return;
144
145 MqttBrokerNode BrokerNode = await CommunicationNode.GetBroker();
146 if (BrokerNode is null)
147 return;
148
149 MqttBroker Broker = await BrokerNode.GetBroker();
150 if (Broker is null)
151 return;
152
153 string Topic = await CommunicationNode.GetFullTopic();
154 if (string.IsNullOrEmpty(Topic))
155 return;
156
157 byte[] Response;
158 StringBuilder ToSniffer;
159
160 switch (TedsAccessCode)
161 {
162 case TedsAccessCode.MetaTEDS:
163 ToSniffer = BrokerNode.HasSniffers ? new StringBuilder() : null;
164 Response = TedsAccessMessage.SerializeResponse(0, this.NcapIdBinary, null, 0, ToSniffer,
165 new TedsId(99, 255, (byte)TedsAccessCode.MetaTEDS, 2, 1),
166 new Uuid(this.NcapIdBinary));
167 break;
168
169 case TedsAccessCode.XdcrName:
170 ToSniffer = BrokerNode.HasSniffers ? new StringBuilder() : null;
171 Response = TedsAccessMessage.SerializeResponse(0, this.NcapIdBinary, null, 0, ToSniffer,
172 new TedsId(99, 255, (byte)TedsAccessCode.XdcrName, 2, 1),
173 new Format(true),
174 new Ieee1451_0.TEDS.FieldTypes.TransducerNameTeds.Content(this.EntityName));
175 break;
176
177 default:
178 return;
179 }
180
181 if (!(ToSniffer is null))
182 await BrokerNode.Information(ToSniffer.ToString());
183
184 await Broker.Publish(Topic, MqttQualityOfService.AtLeastOnce, false, Response);
185 }
186
187 }
188}
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
static byte[] SerializeResponse(ushort ErrorCode, byte[] NcapId, string Name, StringBuilder SnifferOutput)
Serializes an NCAP discovery response.
static byte[] SerializeResponse(ushort ErrorCode, byte[] NcapId, byte[] TimId, ushort ChannelId, StringBuilder SnifferOutput, TedsId TedsHeader, params TedsRecord[] Records)
Serializes a response to a TEDS request.
TEDS Universal unique identifier (§6.4.2.2)
Definition: Uuid.cs:15
TEDS identification header (§6.3)
Definition: TedsId.cs:14
MQTT Topic node that publishes discovery commands in accordance with IEEE 1451.0.
Topic node representing an IEEE 1451.0 NCAP.
Topic node representing an IEEE 1451.0 NCAP Proxy.
override Grade Supports(MqttTopicRepresentation Topic)
How well the topic node supports an MQTT topic
override Task NameReceived(string Name)
Name has been received
override Task< bool > AcceptsParentAsync(INode Parent)
If the node accepts a given parent.
async Task DiscoveryRequest(DiscoveryMessage DiscoveryMessage)
A request for TEDS data has been received.
ProxyMqttNcapTopicNode()
Topic node representing an IEEE 1451.0 NCAP Proxy.
override Task< string > GetTypeNameAsync(Language Language)
Diaplayable type name for node.
async Task TedsRequest(TedsAccessMessage TedsAccessMessage, TedsAccessCode TedsAccessCode, uint TedsOffset, double TimeoutSeconds)
A request for TEDS data has been received.
override Task< bool > AcceptsChildAsync(INode Child)
If the node accepts a given child.
Topic node representing an IEEE 1451.0 TIM Proxy.
Static class for IEEE 1451-related parsing tasks.
string Name
If the node is provisioned is not. Property is editable.
MQTT Broker connection object.
Definition: MqttBroker.cs:17
Task Publish(string Topic, MqttQualityOfService QoS, bool Retain, byte[] Data)
Publishes binary data to a topic.
Definition: MqttBroker.cs:147
Node representing a connection to an MQTT broker.
Task< MqttBroker > GetBroker()
Gets the corresponding broker node.
async Task Information(string Comment)
Called to inform the viewer of something.
Contains information about an MQTT topic
Interface for nodes that are published through the concentrator interface.
Definition: INode.cs:49
Task< IEnumerable< INode > > ChildNodes
Child nodes. If no child nodes are available, null is returned.
Definition: INode.cs:140
INode Parent
Parent Node, or null if a root node.
Definition: INode.cs:116
Interface for nodes that can be discovered on an IEEE 1451.0 network.
Interface for nodes that can return TEDS.
Definition: ITedsNode.cs:10
Task< INode > GetParent()
Gets the parent of the node.
MqttQualityOfService
MQTT Quality of Service level.
Grade
Grade enumeration
Definition: Grade.cs:7