Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ScriptReferenceNode.cs
1using System;
3using System.Threading.Tasks;
5using Waher.Script;
10
11namespace Waher.Things.Script
12{
17 {
22 : base()
23 {
24 }
25
29 [Page(2, "Script", 100)]
30 [Header(6, "Script Node:")]
31 [ToolTip(7, "ID of template node that defines how the node operates.")]
32 [Required]
33 public string ScriptNodeId { get; set; }
34
40 public override Task<string> GetTypeNameAsync(Language Language)
41 {
42 return Language.GetStringAsync(typeof(ScriptNode), 8, "Script Reference Node");
43 }
44
50 public override Task<bool> AcceptsChildAsync(INode Child)
51 {
52 return Task.FromResult(
53 Child is ScriptNode ||
54 Child is ScriptReferenceNode);
55 }
56
62 public override Task<bool> AcceptsParentAsync(INode Parent)
63 {
64 return Task.FromResult(
65 Parent is Root ||
67 Parent is VirtualNode ||
68 Parent is ScriptNode ||
70 }
71
75 public override bool IsReadable
76 {
77 get
78 {
79 if (this.Disabled)
80 return false;
81
82 if (base.IsReadable)
83 return true;
84
85 if (string.IsNullOrEmpty(this.ScriptNodeId))
86 return false;
87
88 return true;
89 }
90 }
91
98 public override async Task StartReadout(ISensorReadout Request, bool DoneAfter)
99 {
100 try
101 {
102 await base.StartReadout(Request, false);
103
104 if (string.IsNullOrEmpty(this.ScriptNodeId))
105 throw new Exception("Script Node ID not defined.");
106
107 INode Node = await MeteringTopology.GetNode(this.ScriptNodeId)
108 ?? throw new Exception("Node not found: " + this.ScriptNodeId);
109
110 if (!(Node is ScriptNode ScriptNode))
111 throw new Exception("Script Node reference does not point to a script node.");
112
113 Variables v = new Variables()
114 {
115 ["this"] = this
116 };
117
118 this.PopulateVariables(v);
119
120 v.OnPreview += (Sender, e) =>
121 {
122 ScriptNode.ReportFields(Request, e.Preview.AssociatedObjectValue, false);
123 return Task.CompletedTask;
124 };
125
126 object Obj = await ScriptNode.ParsedSensorDataScript.EvaluateAsync(v);
127 ScriptNode.ReportFields(Request, Obj, DoneAfter);
128
129 await this.RemoveErrorAsync("ScriptError");
130 }
131 catch (Exception ex)
132 {
133 await this.LogErrorAsync("ScriptError", ex.Message);
134 await Request.ReportErrors(true, new ThingError(this, ex.Message));
135 }
136 }
137
143 {
144 if (!(this.MetaData is null))
145 {
146 foreach (MetaDataValue Tag in this.MetaData)
147 Variables[Tag.Name] = Tag.Value;
148 }
149 }
150
154 public override Task<IEnumerable<ICommand>> Commands => this.GetCommands();
155
156 internal async Task<IEnumerable<ICommand>> GetCommands()
157 {
158 INode Node = await MeteringTopology.GetNode(this.ScriptNodeId)
159 ?? throw new Exception("Node not found: " + this.ScriptNodeId);
160
161 List<ICommand> Commands = new List<ICommand>();
162 Commands.AddRange(await base.Commands);
163
164 if (Node.HasChildren)
165 {
166 IEnumerable<INode> Children = await Node.ChildNodes;
167 if (!(Children is null))
168 {
169 foreach (INode Child in Children)
170 {
171 if (Child is ScriptCommandNodeBase CommandNode)
172 Commands.Add(await CommandNode.GetCommand(this));
173 }
174 }
175 }
176
177 return Commands.ToArray();
178 }
179 }
180}
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
async Task< object > EvaluateAsync(Variables Variables)
Evaluates the expression, using the variables provided in the Variables collection....
Definition: Expression.cs:4461
Collection of variables.
Definition: Variables.cs:25
virtual Task LogErrorAsync(string Body)
Logs an error message on the node.
virtual Task< bool > RemoveErrorAsync()
Removes error messages with an empty event ID from the node.
Defines the Metering Topology data source. This data source contains a tree structure of persistent r...
static Task< MeteringNode > GetNode(string NodeId)
Gets a node from the Metering Topology
Class for the root node of the Metering topology.
Definition: Root.cs:10
Abstract base class for script node commands and queries.
Node defined by script.
Definition: ScriptNode.cs:19
Expression ParsedSensorDataScript
Parsed sensor-data script.
Definition: ScriptNode.cs:134
Node referencing a script node.
override bool IsReadable
If the node can be read.
override Task< bool > AcceptsChildAsync(INode Child)
If the node accepts a presumptive child, i.e. can receive as a child (if that child accepts the node ...
virtual void PopulateVariables(Variables Variables)
Populates a variable collection with variables before script execution.
string ScriptNodeId
ID of node containing script defining node.
override Task< IEnumerable< ICommand > > Commands
Available command objects. If no commands are available, null is returned.
ScriptReferenceNode()
Node referencing a script node.
override async Task StartReadout(ISensorReadout Request, bool DoneAfter)
Starts the readout of the sensor.
override Task< bool > AcceptsParentAsync(INode Parent)
If the node accepts a presumptive parent, i.e. can be added to that parent (if that parent accepts th...
override Task< string > GetTypeNameAsync(Language Language)
Gets the type name of the node.
Contains information about an error on a thing
Definition: ThingError.cs:10
Class representing a meta-data value.
Virtual node, that can be used as a placeholder for services.
Definition: VirtualNode.cs:28
MetaDataValue[] MetaData
Meta-data attached to virtual node.
Definition: VirtualNode.cs:47
Interface for nodes that are published through the concentrator interface.
Definition: INode.cs:49
bool HasChildren
If the source has any child sources.
Definition: INode.cs:76
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 sensor nodes.
Definition: ISensor.cs:9
Interface for classes managing sensor data readouts.
Task ReportErrors(bool Done, params ThingError[] Errors)
Report error states to the client.