Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
XmlData.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Xml;
5using System.Threading.Tasks;
6using Waher.Content;
15
17{
21 public class XmlData : MqttData
22 {
23 private string xml;
24 private XmlDocument value;
25 private bool first = true;
26
30 public XmlData()
31 : base()
32 {
33 }
34
41 public XmlData(MqttTopic Topic, string Xml, XmlDocument Value)
42 : base(Topic)
43 {
44 this.xml = Xml;
45 this.value = Value;
46 }
47
54 public override Task<DataProcessingResult> DataReported(MqttTopic Topic, MqttContent Content)
55 {
56 try
57 {
58 string s = Content.DataString;
59
60 if (this.first)
61 {
62 if (!XML.IsValidXml(s))
63 return Task.FromResult(DataProcessingResult.Incompatible);
64
65 this.first = false;
66 }
67
68 this.value = new XmlDocument()
69 {
70 PreserveWhitespace = false
71 };
72 this.value.LoadXml(s);
73 this.xml = s;
74 this.Timestamp = DateTime.UtcNow;
75 this.QoS = Content.Header.QualityOfService;
76 this.Retain = Content.Header.Retain;
77
78 return Task.FromResult(DataProcessingResult.ProcessedNewMomentaryValues);
79 }
80 catch (Exception)
81 {
82 return Task.FromResult(DataProcessingResult.Incompatible);
83 }
84 }
85
89 public override Task<string> GetTypeName(Language Language)
90 {
91 return Language.GetStringAsync(typeof(MqttTopicNode), 41, "XML");
92 }
93
101 public override Task StartReadout(ThingReference ThingReference, ISensorReadout Request, string Prefix, bool Last)
102 {
103 List<Field> Fields = new List<Field>();
104
105 if (!(this.value.DocumentElement is null))
106 this.AppendFields(ThingReference, Fields, Request, this.value.DocumentElement, Prefix);
107
108 Request.ReportFields(Last, Fields.ToArray());
109
110 return Task.CompletedTask;
111 }
112
113 private void AppendFields(ThingReference ThingReference, List<Field> Fields, ISensorReadout Request, XmlElement Value, string Prefix)
114 {
115 if (Array.IndexOf(SensorClient.NamespacesSensorData, Value.NamespaceURI) >= 0)
116 {
118 if (!(SensorData.Fields is null))
119 Fields.AddRange(SensorData.Fields);
120 }
121 else
122 {
123 foreach (XmlAttribute Attribute in Value.Attributes)
124 {
125 if (string.IsNullOrEmpty(Prefix))
126 this.Add(ThingReference, Fields, Attribute.Name, Attribute.Value, Request);
127 else
128 this.Add(ThingReference, Fields, this.Append(Prefix, Attribute.Name), Attribute.Value, Request);
129 }
130
131 Dictionary<string, int> Repetitions = null;
132 string s;
133
134 foreach (XmlNode N in Value)
135 {
136 if (N is XmlElement ChildElement)
137 {
138 s = ChildElement.LocalName;
139
140 if (Repetitions is null)
141 Repetitions = new Dictionary<string, int>();
142
143 if (Repetitions.TryGetValue(s, out int i))
144 Repetitions[s] = i - 1;
145 else
146 Repetitions[s] = 0;
147 }
148 }
149
150 foreach (XmlNode N in Value)
151 {
152 if (N is XmlElement ChildElement)
153 {
154 s = ChildElement.LocalName;
155
156 if (Repetitions.TryGetValue(s, out int i))
157 {
158 if (i < 0)
159 Repetitions[s] = i = 1;
160 else if (i > 0)
161 Repetitions[s] = ++i;
162
163 if (i != 0)
164 s += ", #" + i.ToString();
165 }
166
167 if (string.IsNullOrEmpty(Prefix))
168 this.AppendFields(ThingReference, Fields, Request, ChildElement, s);
169 else
170 this.AppendFields(ThingReference, Fields, Request, ChildElement, Prefix + ", " + s);
171 }
172 else if (N is XmlText XmlText)
173 {
174 if (string.IsNullOrEmpty(Prefix))
175 this.Add(ThingReference, Fields, "Value", XmlText.InnerText, Request);
176 else
177 this.Add(ThingReference, Fields, Prefix, XmlText.InnerText, Request);
178 }
179 }
180 }
181 }
182
183 private void Add(ThingReference ThingReference, List<Field> Fields, string Name, string Value, ISensorReadout Request)
184 {
185 if (int.TryParse(Value, out int i))
186 this.Add(Fields, new Int32Field(ThingReference, this.Timestamp, Name, i, FieldType.Momentary, FieldQoS.AutomaticReadout), Request);
187 else if (long.TryParse(Value, out long l))
188 this.Add(Fields, new Int64Field(ThingReference, this.Timestamp, Name, l, FieldType.Momentary, FieldQoS.AutomaticReadout), Request);
189 else if (CommonTypes.TryParse(Value, out bool b))
190 this.Add(Fields, new BooleanField(ThingReference, this.Timestamp, Name, b, FieldType.Momentary, FieldQoS.AutomaticReadout), Request);
191 else if (CommonTypes.TryParse(Value, out double d, out byte NrDec))
192 this.Add(Fields, new QuantityField(ThingReference, this.Timestamp, Name, d, NrDec, string.Empty, FieldType.Momentary, FieldQoS.AutomaticReadout), Request);
193 else if (CommonTypes.TryParse(Value, out decimal d2, out NrDec))
194 this.Add(Fields, new QuantityField(ThingReference, this.Timestamp, Name, (double)d2, NrDec, string.Empty, FieldType.Momentary, FieldQoS.AutomaticReadout), Request);
195 else if (System.TimeSpan.TryParse(Value, out TimeSpan TimeSpan))
196 this.Add(Fields, new TimeField(ThingReference, this.Timestamp, Name, TimeSpan, FieldType.Momentary, FieldQoS.AutomaticReadout), Request);
197 else if (System.DateTime.TryParse(Value, out DateTime DateTime))
198 {
199 if (DateTime.TimeOfDay == TimeSpan.Zero)
200 this.Add(Fields, new DateField(ThingReference, this.Timestamp, Name, DateTime, FieldType.Momentary, FieldQoS.AutomaticReadout), Request);
201 else
202 this.Add(Fields, new DateTimeField(ThingReference, this.Timestamp, Name, DateTime, FieldType.Momentary, FieldQoS.AutomaticReadout), Request);
203 }
204 else if (CommonTypes.TryParseRfc822(Value, out DateTimeOffset DateTimeOffset))
205 this.Add(Fields, new DateTimeField(ThingReference, this.Timestamp, Name, DateTimeOffset.DateTime, FieldType.Momentary, FieldQoS.AutomaticReadout), Request);
206 else if (Content.Duration.TryParse(Value, out Duration Duration))
207 this.Add(Fields, new DurationField(ThingReference, this.Timestamp, Name, Duration, FieldType.Momentary, FieldQoS.AutomaticReadout), Request);
208 else
209 this.Add(Fields, new StringField(ThingReference, this.Timestamp, Name, Value, FieldType.Momentary, FieldQoS.AutomaticReadout), Request);
210 }
211
215 public override bool IsControllable => true;
216
221 {
222 return new ControlParameter[]
223 {
224 new MultiLineTextControlParameter("Value", "Publish", "Value:", "XML value of topic.",
225 (n) => Task.FromResult<string>(this.xml),
226 (n, v) =>
227 {
228 XmlDocument Doc = new XmlDocument()
229 {
230 PreserveWhitespace = true
231 };
232 Doc.LoadXml(v);
233 this.value = Doc;
234 this.xml = v;
235 this.Topic.MqttClient.PUBLISH(this.Topic.FullTopic, this.QoS, this.Retain, Encoding.UTF8.GetBytes(v));
236 return Task.CompletedTask;
237 })
238 };
239 }
240
244 public override void SnifferOutput(ICommunicationLayer Output)
245 {
246 this.Information(Output, this.xml);
247 }
248
252 public override Grade DefaultSupport => Grade.Excellent;
253
260 public override IMqttData CreateNew(MqttTopic Topic, MqttContent Content)
261 {
262 IMqttData Result = new XmlData(Topic, default, default);
263 Result.DataReported(Topic, Content);
264 return Result;
265 }
266 }
267}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static bool TryParseRfc822(string s, out DateTimeOffset Value)
Parses a date and time value encoded according to RFC 822, §5.
Definition: CommonTypes.cs:170
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Definition: CommonTypes.cs:46
Helps with common XML-related tasks.
Definition: XML.cs:19
static bool IsValidXml(string Xml)
Checks if a string is valid XML
Definition: XML.cs:1223
Information about content received from the MQTT server.
Definition: MqttContent.cs:9
string DataString
String representation of UTF-8 encoded binary data.
Definition: MqttContent.cs:56
MqttHeader Header
MQTT Header
Definition: MqttContent.cs:35
Implements an XMPP sensor client interface.
Definition: SensorClient.cs:21
static readonly string[] NamespacesSensorData
Supported sensor-data namespaces.
Definition: SensorClient.cs:40
static SensorData ParseFields(XmlElement Content)
Parses sensor data field definitions.
Contains personal sensor data.
Definition: SensorData.cs:15
IEnumerable< Field > Fields
Sensor data fields.
Definition: SensorData.cs:73
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.
Abstract base class for MQTT data encapsulations.
Definition: MqttData.cs:18
DateTime Timestamp
Timestamp of data reception.
Definition: MqttData.cs:39
string Append(string Prefix, string Name)
Appends a name to a topic name.
Definition: MqttData.cs:100
Represents an MQTT topic with XML data.
Definition: XmlData.cs:22
XmlData()
Represents an MQTT topic with XML data.
Definition: XmlData.cs:30
override void SnifferOutput(ICommunicationLayer Output)
Outputs the parsed data to the sniffer.
Definition: XmlData.cs:244
override ControlParameter[] GetControlParameters()
TODO
Definition: XmlData.cs:220
override IMqttData CreateNew(MqttTopic Topic, MqttContent Content)
Creates a new instance of the data.
Definition: XmlData.cs:260
override Task StartReadout(ThingReference ThingReference, ISensorReadout Request, string Prefix, bool Last)
Starts a readout of the data.
Definition: XmlData.cs:101
override Task< string > GetTypeName(Language Language)
Type name representing data.
Definition: XmlData.cs:89
XmlData(MqttTopic Topic, string Xml, XmlDocument Value)
Represents an MQTT topic with XML data.
Definition: XmlData.cs:41
override Task< DataProcessingResult > DataReported(MqttTopic Topic, MqttContent Content)
Called when new data has been published.
Definition: XmlData.cs:54
MQTT Topic information.
Definition: MqttTopic.cs:19
A Metering node representing an MQTT topic
Represents a boolean value that can be either true or false.
Definition: BooleanField.cs:11
Represents a date value.
Definition: DateField.cs:11
Represents a date and optional time value.
Represents a duration value. Duration values adhere to the type specified by xsd:duration.
Represents a 32-bit integer value.
Definition: Int32Field.cs:10
Represents a 64-bit integer value.
Definition: Int64Field.cs:10
Represents a physical quantity value.
Represents a string value.
Definition: StringField.cs:10
Represents a time value. Time values adhere to the type specified by xsd:time.
Definition: TimeField.cs:14
Contains a reference to a thing
Interface for observable classes implementing communication protocols.
Interface for classes managing sensor data readouts.
Task ReportFields(bool Done, params Field[] Fields)
Report read fields to the client.
Interface for MQTT Data encapsulations
Definition: IMqttData.cs:38
Task< DataProcessingResult > DataReported(MqttTopic Topic, MqttContent Content)
Called when new data has been published.
Grade
Grade enumeration
Definition: Grade.cs:7
DataProcessingResult
Results from processing an incoming message.
Definition: IMqttData.cs:17
FieldQoS
Field Quality of Service flags
Definition: FieldQoS.cs:10
FieldType
Field Type flags
Definition: FieldType.cs:10
Represents a duration value, as defined by the xsd:duration data type: http://www....
Definition: Duration.cs:13
MqttQualityOfService QualityOfService
Quality of Service level.
Definition: MqttHeader.cs:16