Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
NewMomentaryValues.cs
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Text;
5using System.Threading.Tasks;
6using System.Xml;
10using Waher.Events;
14using Waher.Script;
15using Waher.Things;
17
19{
24 {
25 private FieldNode[] fields;
26 private string sensor;
27
34 : base(Parent, Model)
35 {
36 }
37
41 public override string LocalName => nameof(NewMomentaryValues);
42
50 {
51 return new NewMomentaryValues(Parent, Model);
52 }
53
58 public override async Task FromXml(XmlElement Definition)
59 {
60 this.sensor = XML.Attribute(Definition, "sensor");
61
62 await base.FromXml(Definition);
63
64 List<FieldNode> Fields = new List<FieldNode>();
65
66 foreach (ISimulationNode Node in this.Children)
67 {
68 if (Node is FieldNode Field)
69 Fields.Add(Field);
70 }
71
72 this.fields = Fields.ToArray();
73 }
74
80 public override Task<LinkedListNode<IActivityNode>> Execute(Variables Variables)
81 {
82 if (!Variables.TryGetVariable(this.sensor, out Variable v))
83 throw new Exception("Sensor not found: " + this.sensor);
84
85 if (!(v.ValueObject is SensorServer Sensor))
86 throw new Exception("Not a sensor server object: " + this.sensor);
87
88 Dictionary<ThingReference, LinkedList<Field>> FieldsByThing = new Dictionary<ThingReference, LinkedList<Field>>();
90
91 foreach (FieldNode Field in this.fields)
92 {
93 Ref = Field.ThingReference;
94 if (!FieldsByThing.TryGetValue(Ref, out LinkedList<Field> Fields))
95 {
96 Fields = new LinkedList<Field>();
97 FieldsByThing[Ref] = Fields;
98 }
99
100 try
101 {
102 Field.AddFields(Fields, Variables);
103 }
104 catch (Exception ex)
105 {
106 Log.Exception(ex);
107 }
108 }
109
110 foreach (KeyValuePair<ThingReference, LinkedList<Field>> P in FieldsByThing)
111 {
112 LinkedList<Field> Fields = P.Value;
113 LinkedListNode<Field> Loop = Fields.First;
114 LinkedListNode<Field> Next;
115 bool Found = false;
116
117 while (Loop != null)
118 {
119 Next = Loop.Next;
120 if (Loop.Value.Type.HasFlag(Waher.Things.SensorData.FieldType.Momentary))
121 Found = true;
122 else
123 Fields.Remove(Loop);
124
125 Loop = Next;
126 }
127
128 if (Found)
129 {
130 Sensor.NewMomentaryValues(P.Key, Fields);
131
132 if (Sensor.Client.TryGetExtension(typeof(PepClient), out IXmppExtension Extension) &&
133 Extension is PepClient PepClient)
134 {
135 PepClient.Publish(new SensorData(Fields), null, null);
136 }
137 }
138 }
139
140 return Task.FromResult<LinkedListNode<IActivityNode>>(null);
141 }
142
148 public static string[] GetThingReferences(FieldNode[] FieldNodes)
149 {
150 SortedDictionary<string, bool> Sorted = new SortedDictionary<string, bool>();
151
152 foreach (FieldNode Node in FieldNodes)
153 Sorted[GetReference(Node.ThingReference)] = true;
154
155 string[] Result = new string[Sorted.Count];
156 Sorted.Keys.CopyTo(Result, 0);
157
158 return Result;
159 }
160
166 public static string GetReference(ThingReference Ref)
167 {
168 StringBuilder sb = new StringBuilder();
169
170 if (!string.IsNullOrEmpty(Ref.SourceId))
171 {
172 sb.Append(Ref.SourceId);
173 sb.Append('.');
174 }
175
176 if (!string.IsNullOrEmpty(Ref.Partition))
177 {
178 sb.Append(Ref.Partition);
179 sb.Append('.');
180 }
181
182 sb.Append(Ref.NodeId);
183
184 return sb.ToString();
185 }
186
192 public static string Join(string[] References)
193 {
194 StringBuilder sb = new StringBuilder();
195 bool First = true;
196
197 foreach (string Ref in References)
198 {
199 if (First)
200 First = false;
201 else
202 sb.Append(", ");
203
204 sb.Append(Ref);
205 }
206
207 return sb.ToString();
208 }
209
216 public override void ExportPlantUml(StreamWriter Output, int Indentation, char QuoteChar)
217 {
218 Indent(Output, Indentation);
219 Output.Write(":NewMomentaryFields(");
220 Output.Write(this.sensor);
221
222 string s = Join(GetThingReferences(this.fields));
223 if (!string.IsNullOrEmpty(s))
224 {
225 Output.Write(", ");
226 Output.Write(s);
227 }
228
229 Output.WriteLine(");");
230 }
231
232 }
233}
Root node of a simulation model
Definition: Model.cs:49
static void Indent(StreamWriter Output, int Indentation)
Adds indentation to the current row.
Abstract base class for sensor data field nodes.
Definition: FieldNode.cs:16
Reports new momentary sensor data fields.
static string Join(string[] References)
Joins an array of references, and delimits them with ", ".
NewMomentaryValues(ISimulationNode Parent, Model Model)
Reports new momentary sensor data fields.
override void ExportPlantUml(StreamWriter Output, int Indentation, char QuoteChar)
Exports PlantUML
override async Task FromXml(XmlElement Definition)
Sets properties and attributes of class in accordance with XML definition.
static string[] GetThingReferences(FieldNode[] FieldNodes)
Gets referenced thing references
override ISimulationNode Create(ISimulationNode Parent, Model Model)
Creates a new instance of the node.
override Task< LinkedListNode< IActivityNode > > Execute(Variables Variables)
Executes a node.
static string GetReference(ThingReference Ref)
Gets a string reference representing a ThingReference.
override string LocalName
Local name of XML element defining contents of class.
Abstract base class for IoT XMPP activity nodes.
Helps with common XML-related tasks.
Definition: XML.cs:19
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:914
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1647
Client managing the Personal Eventing Protocol (XEP-0163). https://xmpp.org/extensions/xep-0163....
Definition: PepClient.cs:19
Task Publish(string Node, EventHandlerAsync< ItemResultEventArgs > Callback, object State)
Publishes an item on a node.
Definition: PepClient.cs:110
Contains personal sensor data.
Definition: SensorData.cs:15
Implements an XMPP sensor server interface.
Definition: SensorServer.cs:27
Contains information about a variable.
Definition: Variable.cs:10
Collection of variables.
Definition: Variables.cs:25
virtual bool TryGetVariable(string Name, out Variable Variable)
Tries to get a variable object, given its name.
Definition: Variables.cs:52
Base class for all sensor data fields.
Definition: Field.cs:20
Contains a reference to a thing
string NodeId
ID of node.
string Partition
Optional partition in which the Node ID is unique.
string SourceId
Optional ID of source containing node.
Basic interface for simulator nodes. Implementing this interface allows classes with default contruct...
ISimulationNode Parent
Parent node in the simulation model.
Definition: App.xaml.cs:4