Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ExternalNode.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
9using Waher.Things;
11
13{
17 public class ExternalNode : INode, ISensor
18 {
19 private readonly string nodeId;
20 private readonly string sourceId;
21 private readonly string paritionId;
22 private readonly CaseInsensitiveString jid;
23
31 public ExternalNode(string NodeId, string SourceId, string ParitionId, CaseInsensitiveString Jid)
32 {
33 this.nodeId = NodeId;
34 this.sourceId = SourceId;
35 this.paritionId = ParitionId;
36 this.jid = Jid;
37 }
38
42 public string LocalId => this.nodeId;
43
47 public string LogId
48 {
49 get
50 {
51 StringBuilder sb = new StringBuilder();
52
53 sb.Append(this.nodeId);
54
55 if (!string.IsNullOrEmpty(this.sourceId) || !string.IsNullOrEmpty(this.paritionId))
56 {
57 sb.Append('/');
58 sb.Append(this.sourceId);
59
60 if (!string.IsNullOrEmpty(this.paritionId))
61 {
62 sb.Append('/');
63 sb.Append(this.paritionId);
64 }
65 }
66
68 {
69 sb.Append('@');
70 sb.Append(this.jid.Value);
71 }
72
73 return sb.ToString();
74 }
75 }
76
80 public bool HasChildren => false;
81
85 public bool ChildrenOrdered => false;
86
90 public bool IsReadable => true;
91
95 public bool IsControllable => true;
96
100 public bool HasCommands => false;
101
105 public INode Parent => null;
106
110 public DateTime LastChanged => DateTime.MinValue;
111
115 public NodeState State => NodeState.None;
116
120 public Task<IEnumerable<INode>> ChildNodes => Task.FromResult<IEnumerable<INode>>(null);
121
125 public string NodeId => this.nodeId;
126
130 public string SourceId => this.sourceId;
131
135 public string Partition => this.paritionId;
136
140 public CaseInsensitiveString Jid => this.jid;
141
147 public Task<bool> AcceptsChildAsync(INode Child)
148 {
149 return Task.FromResult(false);
150 }
151
157 public Task<bool> AcceptsParentAsync(INode Parent)
158 {
159 return Task.FromResult(false);
160 }
161
166 public Task AddAsync(INode Child)
167 {
168 throw new NotSupportedException("This node cannot accept child nodes.");
169 }
170
176 public Task<bool> CanAddAsync(RequestOrigin Caller)
177 {
178 return Task.FromResult(false);
179 }
180
186 public Task<bool> CanDestroyAsync(RequestOrigin Caller)
187 {
188 return Task.FromResult(false);
189 }
190
196 public Task<bool> CanEditAsync(RequestOrigin Caller)
197 {
198 return Task.FromResult(false);
199 }
200
206 public Task<bool> CanViewAsync(RequestOrigin Caller)
207 {
208 return Task.FromResult(true);
209 }
210
214 public Task DestroyAsync()
215 {
216 return Task.CompletedTask;
217 }
218
225 public Task<IEnumerable<Parameter>> GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
226 {
227 return Task.FromResult<IEnumerable<Parameter>>(new Parameter[0]);
228 }
229
234 public Task<IEnumerable<Message>> GetMessagesAsync(RequestOrigin Caller)
235 {
236 return Task.FromResult<IEnumerable<Message>>(new Message[0]);
237 }
238
243 public Task<string> GetTypeNameAsync(Language Language)
244 {
245 return Task.FromResult(string.Empty);
246 }
247
253 public Task<bool> MoveDownAsync(RequestOrigin Caller)
254 {
255 return Task.FromResult(false);
256 }
257
263 public Task<bool> MoveUpAsync(RequestOrigin Caller)
264 {
265 return Task.FromResult(false);
266 }
267
273 public Task<bool> RemoveAsync(INode Child)
274 {
275 return Task.FromResult(false);
276 }
277
281 public Task UpdateAsync()
282 {
283 return Task.CompletedTask;
284 }
285
289 public Task<IEnumerable<ICommand>> Commands => Task.FromResult<IEnumerable<ICommand>>(null);
290
295 public async Task StartReadout(ISensorReadout Request)
296 {
297 RosterItem Item = Gateway.XmppClient[this.jid.Value];
298
299 if (Item is null ||
300 (Item.State != SubscriptionState.Both &&
301 Item.State != SubscriptionState.To))
302 {
303 await Gateway.XmppClient.RequestPresenceSubscription(this.jid.Value);
304 await Request.ReportErrors(true, new ThingError(this, "No presence subscription approved by " + this.jid.Value + ". New subscription request sent."));
305 return;
306 }
307
308 if (!Item.HasLastPresence || !Item.LastPresence.IsOnline)
309 {
310 await Request.ReportErrors(true, new ThingError(this, "Sensor host not online: " + this.jid.Value));
311 return;
312 }
313
314 INode[] Nodes;
315
316 if (string.IsNullOrEmpty(this.nodeId))
317 Nodes = null;
318 else
319 Nodes = new INode[] { this };
320
322 Nodes, Request.Types, Request.FieldNames, Request.From, Request.To, Request.When,
323 Request.ServiceToken, Request.DeviceToken, Request.UserToken);
324
325 SensorDataRequest.OnFieldsReceived += (sender, Fields) =>
326 {
327 Request.ReportFields(false, Fields);
328 return Task.CompletedTask;
329 };
330
331 SensorDataRequest.OnErrorsReceived += (sender, Errors) =>
332 {
333 Request.ReportErrors(false, Errors);
334 return Task.CompletedTask;
335 };
336
337 SensorDataRequest.OnStateChanged += (sender, State) =>
338 {
339 switch (State)
340 {
341 case SensorDataReadoutState.Cancelled:
342 Request.ReportErrors(true, new ThingError(this, "Readout was cancelled."));
343 break;
344
345 case SensorDataReadoutState.Done:
346 Request.ReportFields(true);
347 break;
348
349 case SensorDataReadoutState.Failure:
350 Request.ReportErrors(true, new ThingError(this, "Readout failed."));
351 break;
352 }
353
354 return Task.CompletedTask;
355 };
356 }
357 }
358}
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static SensorClient SensorClient
XMPP Sensor Client.
Definition: Gateway.cs:3227
static XmppClient XmppClient
XMPP Client connection of gateway.
Definition: Gateway.cs:3187
Task< bool > CanEditAsync(RequestOrigin Caller)
If the node can be edited by the caller.
NodeState State
Current overall state of the node.
bool HasChildren
If the source has any child sources.
Definition: ExternalNode.cs:80
string LocalId
If provided, an ID for the node, but unique locally between siblings. Can be null,...
Definition: ExternalNode.cs:42
Task< bool > CanViewAsync(RequestOrigin Caller)
If the node is visible to the caller.
Task< bool > MoveDownAsync(RequestOrigin Caller)
Tries to move the node down.
bool ChildrenOrdered
If the children of the node have an intrinsic order (true), or if the order is not important (false).
Definition: ExternalNode.cs:85
Task UpdateAsync()
Updates the node (in persisted storage).
string LogId
If provided, an ID for the node, as it would appear or be used in system logs. Can be null,...
Definition: ExternalNode.cs:48
bool HasCommands
If the node has registered commands or not.
DateTime LastChanged
When the node was last updated.
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 ...
Task< bool > RemoveAsync(INode Child)
Removes a child from the node.
Task< string > GetTypeNameAsync(Language Language)
Gets the type name of the node.
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...
Task< bool > MoveUpAsync(RequestOrigin Caller)
Tries to move the node up.
Task< IEnumerable< INode > > ChildNodes
Child nodes. If no child nodes are available, null is returned.
bool IsControllable
If the node can be controlled.
Definition: ExternalNode.cs:95
string SourceId
Optional ID of source containing node.
ExternalNode(string NodeId, string SourceId, string ParitionId, CaseInsensitiveString Jid)
Represents an external node.
Definition: ExternalNode.cs:31
Task< IEnumerable< Parameter > > GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
Gets displayable parameters.
string Partition
Optional partition in which the Node ID is unique.
Task AddAsync(INode Child)
Adds a new child to the node.
Task< bool > CanDestroyAsync(RequestOrigin Caller)
If the node can be destroyed to by the caller.
CaseInsensitiveString Jid
JID of remote entity.
Task< IEnumerable< ICommand > > Commands
Available command objects. If no commands are available, null is returned.
INode Parent
Parent Node, or null if a root node.
Task DestroyAsync()
Destroys the node. If it is a child to a parent node, it is removed from the parent first.
Task< IEnumerable< Message > > GetMessagesAsync(RequestOrigin Caller)
Gets messages logged on the node.
async Task StartReadout(ISensorReadout Request)
Starts the readout of the sensor.
Task< bool > CanAddAsync(RequestOrigin Caller)
If the node can be added to by the caller.
Maintains information about an item in the roster.
Definition: RosterItem.cs:75
SubscriptionState State
roup Current subscription state.
Definition: RosterItem.cs:268
bool HasLastPresence
If the roster item has received presence from an online resource having the given bare JID.
Definition: RosterItem.cs:425
string LastPresenceFullJid
Full JID of last resource sending online presence.
Definition: RosterItem.cs:343
PresenceEventArgs LastPresence
Last presence received from a resource having this bare JID.
Definition: RosterItem.cs:356
Task< SensorDataClientRequest > RequestReadout(string Destination, FieldType Types)
Requests a sensor data readout.
Base class for sensor data requests.
Task RequestPresenceSubscription(string BareJid)
Requests subscription of presence information from a contact.
Definition: XmppClient.cs:4919
Represents a case-insensitive string.
static bool IsNullOrEmpty(CaseInsensitiveString value)
Indicates whether the specified string is null or an CaseInsensitiveString.Empty string.
Contains information about a language.
Definition: Language.cs:17
Contains information about a message logged on a node.
Definition: Message.cs:32
Base class for all node parameters.
Definition: Parameter.cs:10
Tokens available in request.
Definition: RequestOrigin.cs:9
Contains information about an error on a thing
Definition: ThingError.cs:10
Interface for nodes that are published through the concentrator interface.
Definition: INode.cs:49
Interface for sensor nodes.
Definition: ISensor.cs:9
Interface for classes managing sensor data readouts.
string ServiceToken
Optional service token.
DateTime When
When the readout is to be made. Use DateTime.MinValue to start the readout immediately.
string[] FieldNames
Names of fields to read.
Task ReportErrors(bool Done, params ThingError[] Errors)
Report error states to the client.
FieldType Types
Field Types to read.
DateTime To
To what time readout is to be made. Use DateTime.MaxValue to specify no upper limit.
string DeviceToken
Optional device token.
Task ReportFields(bool Done, params Field[] Fields)
Report read fields to the client.
DateTime From
From what time readout is to be made. Use DateTime.MinValue to specify no lower limit.
string UserToken
Optional user token.
SensorDataReadoutState
Sensor Data Readout States.
SubscriptionState
State of a presence subscription.
Definition: RosterItem.cs:16
NodeState
State of a node.
Definition: INode.cs:13