Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GetNode.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
5using Waher.Script;
9using Waher.Things;
11
13{
18 {
27 : base(new ScriptNode[] { NodeId }, argumentTypes1Scalar, Start, Length, Expression)
28 {
29 }
30
39 public GetNode(ScriptNode NodeId, ScriptNode SourceId, int Start, int Length, Expression Expression)
40 : base(new ScriptNode[] { NodeId, SourceId }, argumentTypes2Scalar, Start, Length, Expression)
41 {
42 }
43
53 public GetNode(ScriptNode NodeId, ScriptNode SourceId, ScriptNode Partition, int Start, int Length, Expression Expression)
54 : base(new ScriptNode[] { NodeId, SourceId, Partition }, argumentTypes3Scalar, Start, Length, Expression)
55 {
56 }
57
68 public GetNode(ScriptNode NodeId, ScriptNode SourceId, ScriptNode Partition, ScriptNode Jid, int Start, int Length, Expression Expression)
69 : base(new ScriptNode[] { NodeId, SourceId, Partition, Jid }, argumentTypes4Scalar, Start, Length, Expression)
70 {
71 }
72
76 public override string FunctionName => nameof(GetNode);
77
81 public override string[] DefaultArgumentNames => new string[] { "NodeId", "SourceId", "Partition", "JID" };
82
87 public override bool IsAsynchronous => true;
88
96 {
97 return this.EvaluateAsync(Arguments, Variables).Result;
98 }
99
106 public override async Task<IElement> EvaluateAsync(IElement[] Arguments, Variables Variables)
107 {
108 INode Result;
109 int c = Arguments.Length;
110
111 if (c == 0)
112 return ObjectValue.Null;
113 else
114 {
115 object Arg0 = Arguments[0].AssociatedObjectValue;
116 IDataSource Source;
117 string SourceId;
118
119 if (c == 1)
120 {
122 {
123 if (!TryGetDataSource(ThingReference.SourceId, out Source))
124 return ObjectValue.Null;
125
126 Result = await Source.GetNodeAsync(ThingReference);
127 }
128 else
129 {
130 SourceId = MeteringTopology.SourceID;
131 if (!TryGetDataSource(SourceId, out Source))
132 return ObjectValue.Null;
133
134 Result = await Source.GetNodeAsync(new ThingReference(Arg0?.ToString() ?? string.Empty, SourceId));
135 }
136 }
137 else
138 {
139 string NodeId = Arg0?.ToString() ?? string.Empty;
140 object Arg1 = Arguments[1].AssociatedObjectValue;
141
142 SourceId = Arg1?.ToString() ?? string.Empty;
143
144 if (c == 2)
145 {
146 if (!TryGetDataSource(SourceId, out Source))
147 return ObjectValue.Null;
148
149 Result = await Source.GetNodeAsync(new ThingReference(NodeId, SourceId));
150 }
151 else
152 {
153 object Arg2 = Arguments[2].AssociatedObjectValue;
154 string PartitionId = Arg2?.ToString() ?? string.Empty;
155
156 if (c == 3)
157 {
158 if (!TryGetDataSource(SourceId, out Source))
159 return ObjectValue.Null;
160
161 Result = await Source.GetNodeAsync(new ThingReference(NodeId, SourceId, PartitionId));
162 }
163 else
164 {
165 object Arg3 = Arguments[3].AssociatedObjectValue;
166 string Jid = Arg3?.ToString().Trim() ?? string.Empty;
167
168 if (string.Compare(Gateway.XmppClient.BareJID, Jid, true) == 0)
169 {
170 if (!TryGetDataSource(SourceId, out Source))
171 return ObjectValue.Null;
172
173 Result = await Source.GetNodeAsync(new ThingReference(NodeId, SourceId, PartitionId));
174 }
175 else
176 Result = new ExternalNode(NodeId, SourceId, PartitionId, Jid);
177 }
178 }
179 }
180 }
181
182 if (Result is null)
183 return ObjectValue.Null;
184 else
185 {
187
188 if (!await Result.CanViewAsync(Origin))
189 throw new UnauthorizedAccessException("Access to node denied.");
190
191 return new ObjectValue(Result);
192 }
193 }
194
200 public static Task<RequestOrigin> GetOriginOfRequest(Variables Variables)
201 {
202 if (Variables.TryGetVariable("this", out Variable v) && v.ValueObject is IRequestOrigin Origin)
203 return Origin.GetOrigin();
204 else if (Variables.TryGetVariable("QuickLoginUser", out v) && v.ValueObject is IRequestOrigin Origin2)
205 return Origin2.GetOrigin();
206 else if (Variables.TryGetVariable("User", out v) && v.ValueObject is IRequestOrigin Origin3)
207 return Origin3.GetOrigin();
208 else
209 return Task.FromResult(new RequestOrigin(Gateway.XmppClient.BareJID, null, null, null));
210 }
211
218 public static bool TryGetDataSource(string SourceId, out IDataSource Source)
219 {
220 if (!Types.TryGetModuleParameter("Sources", out object Obj) || !(Obj is IDataSource[] Sources))
221 {
222 Source = null;
223 return false;
224 }
225
226 if (Sources != sources)
227 {
228 Dictionary<string, IDataSource> SourceById = new Dictionary<string, IDataSource>();
229 Add(SourceById, Sources);
230 sourceById = SourceById;
231 sources = Sources;
232 }
233
234 return sourceById.TryGetValue(SourceId, out Source);
235 }
236
237 private static void Add(Dictionary<string, IDataSource> SourceById, IEnumerable<IDataSource> Sources)
238 {
239 foreach (IDataSource Source in Sources)
240 {
241 SourceById[Source.SourceID] = Source;
242
243 if (Source.HasChildren)
244 Add(SourceById, Source.ChildSources);
245 }
246 }
247
248 private static IDataSource[] sources = null;
249 private static Dictionary<string, IDataSource> sourceById = null;
250 }
251}
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static XmppClient XmppClient
XMPP Client connection of gateway.
Definition: Gateway.cs:3187
Gets a node object on the gateway.
Definition: GetNode.cs:18
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: GetNode.cs:87
GetNode(ScriptNode NodeId, ScriptNode SourceId, int Start, int Length, Expression Expression)
Gets a node object on the gateway.
Definition: GetNode.cs:39
GetNode(ScriptNode NodeId, int Start, int Length, Expression Expression)
Gets a node object on the gateway.
Definition: GetNode.cs:26
override string[] DefaultArgumentNames
Default Argument names
Definition: GetNode.cs:81
static Task< RequestOrigin > GetOriginOfRequest(Variables Variables)
Gets the origin of a request.
Definition: GetNode.cs:200
GetNode(ScriptNode NodeId, ScriptNode SourceId, ScriptNode Partition, ScriptNode Jid, int Start, int Length, Expression Expression)
Gets a node object on the gateway.
Definition: GetNode.cs:68
override async Task< IElement > EvaluateAsync(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: GetNode.cs:106
static bool TryGetDataSource(string SourceId, out IDataSource Source)
Tries to get a data source, from its data source ID.
Definition: GetNode.cs:218
override string FunctionName
Name of the function
Definition: GetNode.cs:76
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: GetNode.cs:95
GetNode(ScriptNode NodeId, ScriptNode SourceId, ScriptNode Partition, int Start, int Length, Expression Expression)
Gets a node object on the gateway.
Definition: GetNode.cs:53
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:14
static bool TryGetModuleParameter(string Name, out object Value)
Tries to get a module parameter value.
Definition: Types.cs:583
Class managing a script expression.
Definition: Expression.cs:39
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
static readonly ArgumentType[] argumentTypes2Scalar
Two scalar parameters.
static readonly ArgumentType[] argumentTypes3Scalar
Three scalar parameters.
static readonly ArgumentType[] argumentTypes1Scalar
One scalar parameter.
static readonly ArgumentType[] argumentTypes4Scalar
Four scalar parameters.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
override string ToString()
Definition: ScriptNode.cs:359
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:86
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
Defines the Metering Topology data source. This data source contains a tree structure of persistent r...
const string SourceID
Source ID for the metering topology data source.
Tokens available in request.
Definition: RequestOrigin.cs:9
Contains a reference to a thing
string SourceId
Optional ID of source containing node.
Basic interface for all types of elements.
Definition: IElement.cs:20
Interface for datasources that are published through the concentrator interface.
Definition: IDataSource.cs:14
bool HasChildren
If the source has any child sources.
Definition: IDataSource.cs:34
IEnumerable< IDataSource > ChildSources
Child sources. If no child sources are available, null is returned.
Definition: IDataSource.cs:50
string SourceID
ID of data source.
Definition: IDataSource.cs:19
Interface for nodes that are published through the concentrator interface.
Definition: INode.cs:49
Task< bool > CanViewAsync(RequestOrigin Caller)
If the node is visible to the caller.
Interface for requestors that can act as an origin for distributed requests.
Interface for thing references.