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;
2using System.Collections.Generic;
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(Child is ScriptNode || Child is ScriptReferenceNode);
53 }
54
60 public override Task<bool> AcceptsParentAsync(INode Parent)
61 {
62 return Task.FromResult(Parent is Root || Parent is VirtualNode || Parent is ScriptNode || Parent is ScriptReferenceNode);
63 }
64
68 public override bool IsReadable
69 {
70 get
71 {
72 if (base.IsReadable)
73 return true;
74
75 if (string.IsNullOrEmpty(this.ScriptNodeId))
76 return false;
77
78 return true;
79 }
80 }
81
88 public override async Task StartReadout(ISensorReadout Request, bool DoneAfter)
89 {
90 try
91 {
92 await base.StartReadout(Request, false);
93
94 if (string.IsNullOrEmpty(this.ScriptNodeId))
95 throw new Exception("Script Node ID not defined.");
96
98 ?? throw new Exception("Node not found: " + this.ScriptNodeId);
99
100 if (!(Node is ScriptNode ScriptNode))
101 throw new Exception("Script Node reference does not point to a script node.");
102
103 Variables v = new Variables()
104 {
105 ["this"] = this
106 };
107
108 this.PopulateVariables(v);
109
110 v.OnPreview += (Sender, e) =>
111 {
112 ScriptNode.ReportFields(Request, e.Preview.AssociatedObjectValue, false);
113 return Task.CompletedTask;
114 };
115
116 object Obj = await ScriptNode.ParsedSensorDataScript.EvaluateAsync(v);
117 ScriptNode.ReportFields(Request, Obj, DoneAfter);
118
119 await this.RemoveErrorAsync("ScriptError");
120 }
121 catch (Exception ex)
122 {
123 await this.LogErrorAsync("ScriptError", ex.Message);
124 await Request.ReportErrors(true, new ThingError(this, ex.Message));
125 }
126 }
127
133 {
134 if (!(this.MetaData is null))
135 {
136 foreach (MetaDataValue Tag in this.MetaData)
137 Variables[Tag.Name] = Tag.Value;
138 }
139 }
140
144 public override Task<IEnumerable<ICommand>> Commands => this.GetCommands();
145
146 internal async Task<IEnumerable<ICommand>> GetCommands()
147 {
148 INode Node = await MeteringTopology.GetNode(this.ScriptNodeId)
149 ?? throw new Exception("Node not found: " + this.ScriptNodeId);
150
151 List<ICommand> Commands = new List<ICommand>();
152 Commands.AddRange(await base.Commands);
153
154 if (Node.HasChildren)
155 {
156 IEnumerable<INode> Children = await Node.ChildNodes;
157 if (!(Children is null))
158 {
159 foreach (INode Child in Children)
160 {
161 if (Child is ScriptCommandNodeBase CommandNode)
162 Commands.Add(await CommandNode.GetCommand(this));
163 }
164 }
165 }
166
167 return Commands.ToArray();
168 }
169 }
170}
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:4275
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:11
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:126
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.