Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ControlActuator.cs
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Threading.Tasks;
5using System.Xml;
6using SkiaSharp;
10using Waher.Content;
14using Waher.Script;
15using Waher.Things;
16
18{
23 {
24 private ThingReference[] nodes;
25 private IValue value = null;
26 private StringAttribute to;
27 private StringAttribute parameter;
28 private StringAttribute controller;
29
36 : base(Parent, Model)
37 {
38 }
39
43 public IValue Value => this.value;
44
48 public override string LocalName => nameof(ControlActuator);
49
57 {
58 return new ControlActuator(Parent, Model);
59 }
60
65 public override async Task FromXml(XmlElement Definition)
66 {
67 this.controller = new StringAttribute(XML.Attribute(Definition, "controller"));
68 this.to = new StringAttribute(XML.Attribute(Definition, "to"));
69 this.parameter = new StringAttribute(XML.Attribute(Definition, "parameter"));
70
71 await base.FromXml(Definition);
72
73 List<ThingReference> Nodes = new List<ThingReference>();
74
75 foreach (ISimulationNode Node in this.Children)
76 {
77 if (Node is NodeReference NodeRef)
78 Nodes.Add(NodeRef.ThingReference);
79 }
80
81 this.nodes = Nodes.ToArray();
82 }
83
88 public void Register(IValue Value)
89 {
90 if (this.value is null)
91 this.value = Value;
92 else
93 throw new Exception("Value already defined.");
94 }
95
101 public override async Task<LinkedListNode<IActivityNode>> Execute(Variables Variables)
102 {
103 string ControllerName = await this.controller.GetValueAsync(Variables);
104
105 if (!Variables.TryGetVariable(ControllerName, out Waher.Script.Variable v))
106 throw new Exception("Controller not found: " + ControllerName);
107
108 object Obj = v.ValueObject;
109 if (!(Obj is ControlClient Controller))
110 throw new Exception("Not a control client object: " + ControllerName);
111
112 string To = await this.to.GetValueAsync(Variables);
113 string Parameter = await this.parameter.GetValueAsync(Variables);
114 object Value = await this.value.EvaluateAsync(Variables);
115
116 if (XmppClient.BareJidRegEx.IsMatch(To))
117 {
118 RosterItem Item = Controller.Client[To]
119 ?? throw new Exception("No connection in roster with Bare JID: " + To);
120
121 if (!Item.HasLastPresence || !Item.LastPresence.IsOnline)
122 throw new Exception("Contact not online: " + To);
123
124 To = Item.LastPresenceFullJid;
125 }
126
127 if (Value is bool b)
128 await Controller.Set(To, Parameter, b, this.nodes);
129 else if (Value is double d)
130 await Controller.Set(To, Parameter, d, this.nodes);
131 else if (Value is Enum e)
132 await Controller.Set(To, Parameter, e, this.nodes);
133 else if (Value is int i)
134 await Controller.Set(To, Parameter, i, this.nodes);
135 else if (Value is long l)
136 await Controller.Set(To, Parameter, l, this.nodes);
137 else if (Value is string s)
138 await Controller.Set(To, Parameter, s, this.nodes);
139 else if (Value is TimeSpan TS)
140 await Controller.Set(To, Parameter, TS, this.nodes);
141 else if (Value is System.DateTime TP)
142 await Controller.Set(To, Parameter, TP, TP.TimeOfDay == TimeSpan.Zero, this.nodes);
143 else if (Value is Waher.Content.Duration d2)
144 await Controller.Set(To, Parameter, d2, this.nodes);
145 else if (Value is ColorReference cl)
146 await Controller.Set(To, Parameter, cl, this.nodes);
147 else if (Value is SKColor cl2)
148 await Controller.Set(To, Parameter, new ColorReference(cl2.Red, cl2.Green, cl2.Red, cl2.Alpha), this.nodes);
149 else
150 throw new Exception("Unsupported control type: " + Value.GetType().FullName);
151
152 return null;
153 }
154
161 public override void ExportPlantUml(StreamWriter Output, int Indentation, char QuoteChar)
162 {
163 Indent(Output, Indentation);
164 Output.Write(":ControlActuator(");
165 Output.Write(this.controller.Value);
166 Output.Write(", ");
167 Output.Write(this.to.Value);
168
169 string s = NewMomentaryValues.Join(GetThingReferences(this.nodes));
170 if (!string.IsNullOrEmpty(s))
171 {
172 Output.Write(", ");
173 Output.Write(s);
174 }
175
176 Output.WriteLine(");");
177 }
178
184 public static string[] GetThingReferences(ThingReference[] Nodes)
185 {
186 SortedDictionary<string, bool> Sorted = new SortedDictionary<string, bool>();
187
188 foreach (ThingReference Node in Nodes)
189 Sorted[NewMomentaryValues.GetReference(Node)] = true;
190
191 string[] Result = new string[Sorted.Count];
192 Sorted.Keys.CopyTo(Result, 0);
193
194 return Result;
195 }
196
197 }
198}
Root node of a simulation model
Definition: Model.cs:49
static void Indent(StreamWriter Output, int Indentation)
Adds indentation to the current row.
Contains the value of a string attribute, possibly with embedded script.
async Task< string > GetValueAsync(Variables Variables)
Gets the value of the attribute.
string Value
String value, from definition
Abstract base class for values
Definition: Value.cs:11
Controls an actuator by setting control parameters.
override string LocalName
Local name of XML element defining contents of class.
override async Task FromXml(XmlElement Definition)
Sets properties and attributes of class in accordance with XML definition.
static string[] GetThingReferences(ThingReference[] Nodes)
Gets referenced node references
override async Task< LinkedListNode< IActivityNode > > Execute(Variables Variables)
Executes a node.
override void ExportPlantUml(StreamWriter Output, int Indentation, char QuoteChar)
Exports PlantUML
ControlActuator(ISimulationNode Parent, Model Model)
Controls an actuator by setting control parameters.
override ISimulationNode Create(ISimulationNode Parent, Model Model)
Creates a new instance of the node.
void Register(IValue Value)
Registers a value for the argument.
Reports new momentary sensor data fields.
static string Join(string[] References)
Joins an array of references, and delimits them with ", ".
static string GetReference(ThingReference Ref)
Gets a string reference representing a ThingReference.
Abstract base class for IoT XMPP activity nodes.
Color reference. Separate class to avoid reference to Windows Forms or WPF libraries.
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
Implements an XMPP control client interface.
Maintains information about an item in the roster.
Definition: RosterItem.cs:75
bool HasLastPresence
If the roster item has received presence from an online resource having the given bare JID.
Definition: RosterItem.cs:425
string LastPresenceFullJid
Full JID of last resource sending online presence.
Definition: RosterItem.cs:343
PresenceEventArgs LastPresence
Last presence received from a resource having this bare JID.
Definition: RosterItem.cs:356
Manages an XMPP client connection. Implements XMPP, as defined in https://tools.ietf....
Definition: XmppClient.cs:59
static readonly Regex BareJidRegEx
Regular expression for Bare JIDs
Definition: XmppClient.cs:188
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
Contains a reference to a thing
ISimulationNode[] Children
Child nodes.
Basic interface for simulator nodes. Implementing this interface allows classes with default contruct...
ISimulationNode Parent
Parent node in the simulation model.
Task< object > EvaluateAsync(Variables Variables)
Evaluates the value.
Interface for nodes holding a value node
Definition: App.xaml.cs:4
Represents a duration value, as defined by the xsd:duration data type: http://www....
Definition: Duration.cs:13