Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SensorDevice.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
8
9namespace Waher.Things.Xmpp
10{
15 {
19 public SensorDevice()
20 : base()
21 {
22 }
23
29 public override Task<string> GetTypeNameAsync(Language Language)
30 {
31 return Language.GetStringAsync(typeof(ConcentratorDevice), 58, "XMPP Sensor Device");
32 }
33
38 public async Task StartReadout(ISensorReadout Request)
39 {
40 XmppClient Client;
41
42 try
43 {
44 Client = await this.GetClient();
45 }
46 catch (Exception ex)
47 {
48 await Request.ReportErrors(true, new ThingError(this, ex.Message));
49 return;
50 }
51
53 {
54 RosterItem Item = Client.GetRosterItem(this.JID);
55 if (Item is null)
56 {
57 await Request.ReportErrors(true, new ThingError(this, "JID not available in roster."));
58 return;
59 }
60
61 if (!Item.HasLastPresence || !Item.LastPresence.IsOnline)
62 {
63 await Request.ReportErrors(true, new ThingError(this, "Device not online."));
64 return;
65 }
66
67 TaskCompletionSource<bool> Done = new TaskCompletionSource<bool>();
69 new IThingReference[] { ThingReference.Empty }, Request.Types, Request.FieldNames,
70 Request.From, Request.To, Request.When, Request.ServiceToken, Request.DeviceToken, Request.UserToken);
71
72 Request2.OnFieldsReceived += (sender, Fields) =>
73 {
74 foreach (Field F in Fields)
75 F.Thing = this;
76
77 Request.ReportFields(false, Fields);
78 return Task.CompletedTask;
79 };
80
81 Request2.OnErrorsReceived += (sender, Errors) =>
82 {
83 List<ThingError> Errors2 = new List<ThingError>();
84
85 foreach (ThingError E in Errors)
86 Errors2.Add(new ThingError(this, E.ErrorMessage));
87
88 Request.ReportErrors(false, Errors2.ToArray());
89 return Task.CompletedTask;
90 };
91
92 Request2.OnStateChanged += (sender, State) =>
93 {
94 switch (State)
95 {
96 case SensorDataReadoutState.Cancelled:
97 Request.ReportErrors(true, new ThingError(this, "Readout was cancelled."));
98 Done.TrySetResult(false);
99 break;
100
101 case SensorDataReadoutState.Done:
102 Request.ReportFields(true);
103 Done.TrySetResult(true);
104 break;
105
106 case SensorDataReadoutState.Failure:
107 Request.ReportErrors(true, new ThingError(this, "Readout failed."));
108 Done.TrySetResult(false);
109 break;
110 }
111
112 return Task.CompletedTask;
113 };
114 }
115 else
116 await Request.ReportErrors(true, new ThingError(this, "No XMPP Sensor Client available."));
117 }
118 }
119}
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 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.
Node representing a device that is connected to XMPP.
A connected standalone sensor.
Definition: SensorDevice.cs:15
async Task StartReadout(ISensorReadout Request)
Starts the readout of the sensor.
Definition: SensorDevice.cs:38
override Task< string > GetTypeNameAsync(Language Language)
Gets the type name of the node.
Definition: SensorDevice.cs:29
SensorDevice()
A connected standalone sensor.
Definition: SensorDevice.cs:19
async Task< XmppClient > GetClient()
Gets the XMPP Client associated with node.
Definition: XmppDevice.cs:36
NodeState State
Current overall state of the node.
Definition: INode.cs:132
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.