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;
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 ConfigurationNode[] Children
132 {
133 get
134 {
135 lock (this.children)
136 {
137 return this.children.ToArray();
138 }
139 }
140 }
141
142 internal IEnumerable<ConfigurationNode> Siblings => this.parent?.children;
143
149 public Task<bool> CanViewAsync(RequestOrigin Caller)
150 {
151 return Task.FromResult(Caller.HasPrivilege("Source." + GatewayConfigSource.SourceID + ".Node.View"));
152 }
153
159 public virtual Task<bool> CanEditAsync(RequestOrigin Caller)
160 {
161 return Task.FromResult(Caller.HasPrivilege("Source." + GatewayConfigSource.SourceID + ".Node.Edit"));
162 }
163
169 public virtual Task<bool> CanAddAsync(RequestOrigin Caller)
170 {
171 return Task.FromResult(Caller.HasPrivilege("Source." + GatewayConfigSource.SourceID + ".Node.Add"));
172 }
173
179 public virtual Task<bool> CanDestroyAsync(RequestOrigin Caller)
180 {
181 return Task.FromResult(Caller.HasPrivilege("Source." + GatewayConfigSource.SourceID + ".Node.Destroy"));
182 }
183
190 public virtual Task<IEnumerable<Parameter>> GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
191 {
192 return Task.FromResult<IEnumerable<Parameter>>(Array.Empty<Parameter>());
193 }
194
199 public virtual Task<IEnumerable<Message>> GetMessagesAsync(RequestOrigin Caller) => Task.FromResult<IEnumerable<Message>>(null);
200
206 public virtual Task<bool> MoveUpAsync(RequestOrigin Caller)
207 {
208 if (this.parent is null || !this.parent.ChildrenOrdered)
209 Task.FromResult(false);
210
211 lock (this.parent.children)
212 {
213 int i = this.parent.children.IndexOf(this);
214 if (i <= 0)
215 return Task.FromResult(false);
216
217 this.parent.children.RemoveAt(i);
218 this.parent.children.Insert(i - 1, this);
219
220 return Task.FromResult(true);
221 }
222 }
223
229 public virtual Task<bool> MoveDownAsync(RequestOrigin Caller)
230 {
231 if (this.parent is null || !this.parent.ChildrenOrdered)
232 Task.FromResult(false);
233
234 lock (this.parent.children)
235 {
236 int i = this.parent.children.IndexOf(this);
237 if (i < 0 || i >= this.parent.children.Count - 1)
238 return Task.FromResult(false);
239
240 this.parent.children.RemoveAt(i);
241 this.parent.children.Insert(i + 1, this);
242
243 return Task.FromResult(true);
244 }
245 }
246
252 public virtual Task<bool> AcceptsParentAsync(INode Parent) => Task.FromResult(false);
253
259 public virtual Task<bool> AcceptsChildAsync(INode Child) => Task.FromResult(false);
260
265 public virtual Task AddAsync(INode Child)
266 {
267 if (!(Child is ConfigurationNode ConfigurationNode))
268 throw new InvalidOperationException("Incompatible type.");
269
270 this.AddInternal(ConfigurationNode);
271
272 return this.source?.NodeAdded(Child, false) ?? Task.CompletedTask;
273 }
274
275 internal void AddInternal(ConfigurationNode Child)
276 {
277 lock (this.children)
278 {
279 this.children.Add(Child);
280 }
281
282 Child.source = this.source;
283 Child.parent = this;
284 }
285
289 public virtual Task UpdateAsync() => this.source?.NodeUpdated(this, false) ?? Task.CompletedTask;
290
296 public virtual Task<bool> RemoveAsync(INode Child)
297 {
298 if (!(Child is ConfigurationNode ConfigurationNode))
299 return Task.FromResult(false);
300
301 lock (this.children)
302 {
303 return Task.FromResult(this.children.Remove(ConfigurationNode));
304 }
305 }
306
310 public virtual Task DestroyAsync() => this.source?.NodeDeleted(this, false) ?? Task.CompletedTask;
311
315 public virtual Task<IEnumerable<ICommand>> Commands => Task.FromResult<IEnumerable<ICommand>>(null);
316 }
317}
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 SourceID
Source ID for the reports data source.
Base class for all node parameters.
Definition: Parameter.cs:10
Tokens available in request.
Definition: RequestOrigin.cs:9
bool HasPrivilege(string Privilege)
If the origin has a given privilege.
Interface for nodes that are published through the concentrator interface.
Definition: INode.cs:49
NodeState
State of a node.
Definition: INode.cs:13