Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SensorNode.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
9
10namespace Waher.Things.Xmpp
11{
16 {
20 public SensorNode()
21 : base()
22 {
23 }
24
30 public override Task<string> GetTypeNameAsync(Language Language)
31 {
32 return Language.GetStringAsync(typeof(ConcentratorDevice), 12, "Sensor Node");
33 }
34
39 public async Task StartReadout(ISensorReadout Request)
40 {
41 XmppClient Client;
42
43 try
44 {
45 Client = await this.GetClient();
46 }
47 catch (Exception ex)
48 {
49 await Request.ReportErrors(true, new ThingError(this, ex.Message));
50 return;
51 }
52
54 {
55 string JID = string.Empty;
56 string NID = this.RemoteNodeID;
57 string SID = string.Empty;
58 string PID = string.Empty;
59
60 INode Loop = await this.GetParent();
61 while (!(Loop is null))
62 {
63 if (Loop is ConcentratorSourceNode SourceNode)
64 SID = SourceNode.RemoteSourceID;
65 else if (Loop is ConcentratorPartitionNode PartitionNode)
66 PID = PartitionNode.RemotePartitionID;
68 {
70 break;
71 }
72
73 if (Loop is MeteringNode MeteringNode)
74 Loop = await MeteringNode.GetParent();
75 else
76 Loop = Loop.Parent;
77 }
78
79 RosterItem Item = Client.GetRosterItem(JID);
80 if (Item is null)
81 {
82 await Request.ReportErrors(true, new ThingError(this, "JID not available in roster."));
83 return;
84 }
85
86 if (!Item.HasLastPresence || !Item.LastPresence.IsOnline)
87 {
88 await Request.ReportErrors(true, new ThingError(this, "Concentrator not online."));
89 return;
90 }
91
92 TaskCompletionSource<bool> Done = new TaskCompletionSource<bool>();
94 new IThingReference[] { new ThingReference(NID, SID, PID) }, Request.Types, Request.FieldNames,
95 Request.From, Request.To, Request.When, Request.ServiceToken, Request.DeviceToken, Request.UserToken);
96
97 Request2.OnFieldsReceived += (sender, Fields) =>
98 {
99 foreach (Field F in Fields)
100 F.Thing = this;
101
102 Request.ReportFields(false, Fields);
103 return Task.CompletedTask;
104 };
105
106 Request2.OnErrorsReceived += (sender, Errors) =>
107 {
108 List<ThingError> Errors2 = new List<ThingError>();
109
110 foreach (ThingError E in Errors)
111 Errors2.Add(new ThingError(this, E.ErrorMessage));
112
113 Request.ReportErrors(false, Errors2.ToArray());
114 return Task.CompletedTask;
115 };
116
117 Request2.OnStateChanged += (sender, State) =>
118 {
119 switch (State)
120 {
121 case SensorDataReadoutState.Cancelled:
122 Request.ReportErrors(true, new ThingError(this, "Readout was cancelled."));
123 Done.TrySetResult(false);
124 break;
125
126 case SensorDataReadoutState.Done:
127 Request.ReportFields(true);
128 Done.TrySetResult(true);
129 break;
130
131 case SensorDataReadoutState.Failure:
132 Request.ReportErrors(true, new ThingError(this, "Readout failed."));
133 Done.TrySetResult(false);
134 break;
135 }
136
137 return Task.CompletedTask;
138 };
139 }
140 else
141 await Request.ReportErrors(true, new ThingError(this, "No XMPP Sensor Client available."));
142 }
143 }
144}
Maintains information about an item in the roster.
Definition: RosterItem.cs:75
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
Implements an XMPP sensor client interface.
Definition: SensorClient.cs:21
Task< SensorDataClientRequest > RequestReadout(string Destination, FieldType Types)
Requests a sensor data readout.
Manages an XMPP client connection. Implements XMPP, as defined in https://tools.ietf....
Definition: XmppClient.cs:59
bool TryGetExtension(Type Type, out IXmppExtension Extension)
Tries to get a registered extension of a specific type from the client.
Definition: XmppClient.cs:7318
RosterItem GetRosterItem(string BareJID)
Gets a roster item.
Definition: XmppClient.cs:4522
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
Base class for all metering nodes.
Definition: MeteringNode.cs:28
async Task< INode > GetParent()
Gets the parent of the node.
Base class for all sensor data fields.
Definition: Field.cs:20
Contains information about an error on a thing
Definition: ThingError.cs:10
string ErrorMessage
Error message.
Definition: ThingError.cs:70
Node representing an XMPP concentrator.
Base class for nodes in a remote concentrator.
Node representing a partition in a data source in an XMPP concentrator.
Node representing a data source in an XMPP concentrator.
A node in a concentrator.
Definition: SensorNode.cs:16
async Task StartReadout(ISensorReadout Request)
Starts the readout of the sensor.
Definition: SensorNode.cs:39
override Task< string > GetTypeNameAsync(Language Language)
Gets the type name of the node.
Definition: SensorNode.cs:30
SensorNode()
A node in a concentrator.
Definition: SensorNode.cs:20
async Task< XmppClient > GetClient()
Gets the XMPP Client associated with node.
Definition: XmppDevice.cs:36
Interface for nodes that are published through the concentrator interface.
Definition: INode.cs:49
NodeState State
Current overall state of the node.
Definition: INode.cs:132
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.
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.
Interface for thing references.
SensorDataReadoutState
Sensor Data Readout States.