Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ConfigurationNode.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
5using Waher.Things;
7
9{
13 public abstract class ConfigurationNode : INode
14 {
15 private readonly List<ConfigurationNode> children = new List<ConfigurationNode>();
16 private GatewayConfigSource source;
17 private ConfigurationNode parent;
18
25 {
26 this.source = null;
27 this.parent = null;
28 }
29
36 {
37 this.source = Source;
38 this.parent = Parent;
39 }
40
44 public GatewayConfigSource Source => this.source;
45
49 public abstract string NodeId { get; }
50
55
59 public string Partition => string.Empty;
60
64 public virtual string LocalId => this.NodeId;
65
69 public string LogId => this.NodeId;
70
75 public abstract Task<string> GetTypeNameAsync(Language Language);
76
80 public virtual bool HasChildren
81 {
82 get
83 {
84 lock (this.children)
85 {
86 return this.children.Count > 0;
87 }
88 }
89 }
90
94 public virtual bool ChildrenOrdered => false;
95
99 public bool IsReadable => false;
100
104 public bool IsControllable => false;
105
109 public virtual bool HasCommands => false;
110
114 public virtual INode Parent => this.parent;
115
119 public DateTime LastChanged => GatewayConfigSource.nodesTimestamp;
120
124 public NodeState State => NodeState.None;
125
129 public virtual Task<IEnumerable<INode>> ChildNodes => Task.FromResult<IEnumerable<INode>>(this.children);
130
131 internal INode[] Children
132 {
133 get
134 {
135 lock (this.children)
136 {
137 return this.children.ToArray();
138 }
139 }
140 }
141
147 public Task<bool> CanViewAsync(RequestOrigin Caller) => XmppServerModule.IsAdmin(Caller.From);
148
154 public virtual Task<bool> CanEditAsync(RequestOrigin Caller) => XmppServerModule.IsAdmin(Caller.From);
155
161 public virtual Task<bool> CanAddAsync(RequestOrigin Caller) => Task.FromResult(true);
162
168 public virtual Task<bool> CanDestroyAsync(RequestOrigin Caller) => XmppServerModule.IsAdmin(Caller.From);
169
176 public virtual Task<IEnumerable<Parameter>> GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
177 {
178 return Task.FromResult<IEnumerable<Parameter>>(new Parameter[0]);
179 }
180
185 public virtual Task<IEnumerable<Message>> GetMessagesAsync(RequestOrigin Caller) => Task.FromResult<IEnumerable<Message>>(null);
186
192 public virtual Task<bool> MoveUpAsync(RequestOrigin Caller)
193 {
194 if (this.parent is null || !this.parent.ChildrenOrdered)
195 Task.FromResult(false);
196
197 lock (this.parent.children)
198 {
199 int i = this.parent.children.IndexOf(this);
200 if (i <= 0)
201 return Task.FromResult(false);
202
203 this.parent.children.RemoveAt(i);
204 this.parent.children.Insert(i - 1, this);
205
206 return Task.FromResult(true);
207 }
208 }
209
215 public virtual Task<bool> MoveDownAsync(RequestOrigin Caller)
216 {
217 if (this.parent is null || !this.parent.ChildrenOrdered)
218 Task.FromResult(false);
219
220 lock (this.parent.children)
221 {
222 int i = this.parent.children.IndexOf(this);
223 if (i < 0 || i >= this.parent.children.Count - 1)
224 return Task.FromResult(false);
225
226 this.parent.children.RemoveAt(i);
227 this.parent.children.Insert(i + 1, this);
228
229 return Task.FromResult(true);
230 }
231 }
232
238 public virtual Task<bool> AcceptsParentAsync(INode Parent) => Task.FromResult(false);
239
245 public virtual Task<bool> AcceptsChildAsync(INode Child) => Task.FromResult(false);
246
251 public virtual Task AddAsync(INode Child)
252 {
253 if (!(Child is ConfigurationNode ConfigurationNode))
254 throw new InvalidOperationException("Incompatible type.");
255
256 this.AddInternal(ConfigurationNode);
257
258 return this.source?.NodeAdded(Child, false) ?? Task.CompletedTask;
259 }
260
261 internal void AddInternal(ConfigurationNode Child)
262 {
263 lock (this.children)
264 {
265 this.children.Add(Child);
266 }
267
268 Child.source = this.source;
269 Child.parent = this;
270 }
271
275 public virtual Task UpdateAsync() => this.source?.NodeUpdated(this, false) ?? Task.CompletedTask;
276
282 public virtual Task<bool> RemoveAsync(INode Child)
283 {
284 if (!(Child is ConfigurationNode ConfigurationNode))
285 return Task.FromResult(false);
286
287 lock (this.children)
288 {
289 return Task.FromResult(this.children.Remove(ConfigurationNode));
290 }
291 }
292
296 public virtual Task DestroyAsync() => this.source?.NodeDeleted(this, false) ?? Task.CompletedTask;
297
301 public virtual Task<IEnumerable<ICommand>> Commands => Task.FromResult<IEnumerable<ICommand>>(null);
302 }
303}
Contains information about a language.
Definition: Language.cs:17
Abstract base class for gateway configuration nodes.
virtual Task UpdateAsync()
Updates the node (in persisted storage).
virtual bool ChildrenOrdered
If the children of the node have an intrinsic order (true), or if the order is not important (false).
virtual Task< IEnumerable< Parameter > > GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
Gets displayable parameters.
virtual 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...
virtual Task< IEnumerable< ICommand > > Commands
Available command objects. If no commands are available, null is returned.
virtual Task< bool > CanEditAsync(RequestOrigin Caller)
If the node can be edited by the caller.
virtual Task< bool > MoveUpAsync(RequestOrigin Caller)
Tries to move the node up.
ConfigurationNode()
Abstract base class for gateway configuration nodes.
abstract Task< string > GetTypeNameAsync(Language Language)
Gets the type name of the node.
virtual Task< IEnumerable< INode > > ChildNodes
Child nodes. If no child nodes are available, null is returned.
virtual Task< bool > RemoveAsync(INode Child)
Removes a child from the node.
virtual bool HasCommands
If the node has registered commands or not.
virtual Task< bool > MoveDownAsync(RequestOrigin Caller)
Tries to move the node down.
string LogId
If provided, an ID for the node, as it would appear or be used in system logs. Can be null,...
virtual Task< bool > CanDestroyAsync(RequestOrigin Caller)
If the node can be destroyed to by the caller.
virtual Task AddAsync(INode Child)
Adds a new child to the node.
virtual Task< IEnumerable< Message > > GetMessagesAsync(RequestOrigin Caller)
Gets messages logged on the node.
Task< bool > CanViewAsync(RequestOrigin Caller)
If the node is visible to the caller.
string Partition
Optional partition in which the Node ID is unique.
virtual INode Parent
Parent Node, or null if a root node.
virtual 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 ...
ConfigurationNode(GatewayConfigSource Source, ConfigurationNode Parent)
Abstract base class for gateway configuration nodes.
GatewayConfigSource Source
Source hosting the node.
virtual Task< bool > CanAddAsync(RequestOrigin Caller)
If the node can be added to by the caller.
virtual bool HasChildren
If the source has any child sources.
virtual Task DestroyAsync()
Destroys the node. If it is a child to a parent node, it is removed from the parent first.
virtual string LocalId
If provided, an ID for the node, but unique locally between siblings. Can be null,...
string SourceId
Optional ID of source containing node.
const string GatewayConfigSourceID
Data source mirroring the Gateway.config file.
Service Module hosting the XMPP broker and its components.
Base class for all node parameters.
Definition: Parameter.cs:10
Tokens available in request.
Definition: RequestOrigin.cs:9
Interface for nodes that are published through the concentrator interface.
Definition: INode.cs:49
NodeState
State of a node.
Definition: INode.cs:13