Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
XmppOther.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
7
9{
13 public class XmppOther : XmppContact
14 {
15 public XmppOther(TreeNode Parent, XmppClient Client, string BareJid, bool SupportsRdp)
16 : base(Parent, Client, BareJid, SupportsRdp)
17 {
18 }
19
20 public override string TypeName
21 {
22 get { return "Other"; }
23 }
24
25 public override bool CanReadSensorData
26 {
27 get
28 {
30 if (Node is null)
31 return false;
32
33 XmppClient Client = Node.Client;
34 RosterItem Item = Client[this.BareJID];
35
36 return !(Item is null) && Item.HasLastPresence && Item.LastPresence.IsOnline;
37 }
38 }
39
40 public override Task<SensorDataClientRequest> StartSensorDataFullReadout()
41 {
42 return this.DoReadout(Waher.Things.SensorData.FieldType.All);
43 }
44
45 public override Task<SensorDataClientRequest> StartSensorDataMomentaryReadout()
46 {
47 return this.DoReadout(Waher.Things.SensorData.FieldType.Momentary);
48 }
49
50 private async Task<SensorDataClientRequest> DoReadout(Waher.Things.SensorData.FieldType Types)
51 {
52 XmppClient Client = this.XmppAccountNode.Client;
53 string Id = Guid.NewGuid().ToString();
54
55 RosterItem Item = Client[this.BareJID];
56 string Jid = Item.LastPresenceFullJid;
57
58 CustomSensorDataClientRequest Request = new CustomSensorDataClientRequest(Id, string.Empty, string.Empty, null,
59 Types, null, DateTime.MinValue, DateTime.MaxValue, DateTime.Now, string.Empty, string.Empty, string.Empty);
60
61 await Request.Accept(false);
62 await Request.Started();
63
64 await Client.SendServiceDiscoveryRequest(Jid, (Sender, e) =>
65 {
66 if (e.Ok)
67 {
68 List<Waher.Things.SensorData.Field> Fields = new List<Waher.Things.SensorData.Field>();
69 DateTime Now = DateTime.Now;
70
71 foreach (KeyValuePair<string, bool> Feature in e.Features)
72 {
73 Fields.Add(new Waher.Things.SensorData.BooleanField(Waher.Things.ThingReference.Empty, Now,
74 Feature.Key, Feature.Value, Waher.Things.SensorData.FieldType.Momentary, Waher.Things.SensorData.FieldQoS.AutomaticReadout));
75 }
76
77 bool VersionDone = false;
78
79 if ((Types & Waher.Things.SensorData.FieldType.Identity) != 0)
80 {
81 foreach (Identity Identity in e.Identities)
82 {
83 Fields.Add(new Waher.Things.SensorData.StringField(Waher.Things.ThingReference.Empty, Now,
84 Identity.Type, Identity.Category + (string.IsNullOrEmpty(Identity.Name) ? string.Empty : " (" + Identity.Name + ")"),
85 Waher.Things.SensorData.FieldType.Identity,
86 Waher.Things.SensorData.FieldQoS.AutomaticReadout));
87 }
88
89 if (e.HasFeature(XmppClient.NamespaceSoftwareVersion))
90 {
91 Client.SendSoftwareVersionRequest(Jid, (sender2, e2) =>
92 {
93 Now = DateTime.Now;
94
95 if (e2.Ok)
96 {
97 Request.LogFields(new Waher.Things.SensorData.Field[]
98 {
99 new Waher.Things.SensorData.StringField(Waher.Things.ThingReference.Empty, Now, "Client, Name", e2.Name,
100 Waher.Things.SensorData.FieldType.Identity, Waher.Things.SensorData.FieldQoS.AutomaticReadout),
101 new Waher.Things.SensorData.StringField(Waher.Things.ThingReference.Empty, Now, "Client, OS", e2.OS,
102 Waher.Things.SensorData.FieldType.Identity, Waher.Things.SensorData.FieldQoS.AutomaticReadout),
103 new Waher.Things.SensorData.StringField(Waher.Things.ThingReference.Empty, Now, "Client, Version", e2.Version,
104 Waher.Things.SensorData.FieldType.Identity, Waher.Things.SensorData.FieldQoS.AutomaticReadout),
105 });
106 }
107 else
108 {
109 Request.LogErrors(new Waher.Things.ThingError[]
110 {
111 new Waher.Things.ThingError(Waher.Things.ThingReference.Empty, Now, "Unable to read software version.")
112 });
113 }
114
115 VersionDone = true;
116
117 if (VersionDone)
118 Request.Done();
119
120 return Task.CompletedTask;
121
122 }, null);
123 }
124 else
125 VersionDone = true;
126 }
127 else
128 VersionDone = true;
129
130 Request.LogFields(Fields);
131
132 if (VersionDone)
133 Request.Done();
134 }
135 else
136 Request.Fail("Unable to perform a service discovery.");
137
138 return Task.CompletedTask;
139
140 }, null);
141
142 return Request;
143 }
144
145 public override bool CanRecycle
146 {
147 get
148 {
149 if (!this.XmppAccountNode.IsOnline)
150 return false;
151
152 RosterItem Item = this.XmppAccountNode.Client[this.BareJID];
153 if (Item is null || !Item.HasLastPresence || !Item.LastPresence.IsOnline)
154 return false;
155
156 return true;
157 }
158 }
159
160 public override Task Recycle(MainWindow Window)
161 {
162 if (!this.XmppAccountNode.IsOnline)
163 return Task.CompletedTask;
164
165 RosterItem Item = this.XmppAccountNode.Client[this.BareJID];
166 if (Item is null || !Item.HasLastPresence || !Item.LastPresence.IsOnline)
167 return Task.CompletedTask;
168
169 this.XmppAccountNode.CheckType(this, Item.LastPresenceFullJid);
170
171 return Task.CompletedTask;
172 }
173
174 }
175}
Interaction logic for xaml
Abstract base class for tree nodes in the connection view.
Definition: TreeNode.cs:24
TreeNode Parent
Parent node. May be null if a root node.
Definition: TreeNode.cs:107
Class representing a normal XMPP account.
Represents an XMPP contact whose capabilities have not been measured.
Definition: XmppContact.cs:24
Represents an unspecialized XMPP contact.
Definition: XmppOther.cs:14
override Task< SensorDataClientRequest > StartSensorDataMomentaryReadout()
Starts readout of momentary sensor data values.
Definition: XmppOther.cs:45
override Task Recycle(MainWindow Window)
Is called when the user wants to recycle the node.
Definition: XmppOther.cs:160
override Task< SensorDataClientRequest > StartSensorDataFullReadout()
Starts readout of all sensor data values.
Definition: XmppOther.cs:40
Maintains information about an item in the roster.
Definition: RosterItem.cs:75
new Task LogFields(IEnumerable< Field > Fields)
Fields logged.
Contains information about an item of an entity.
Definition: Item.cs:11
Manages an XMPP client connection. Implements XMPP, as defined in https://tools.ietf....
Definition: XmppClient.cs:59
const string NamespaceSoftwareVersion
jabber:iq:version
Definition: XmppClient.cs:128
Task SendServiceDiscoveryRequest(string To, EventHandlerAsync< ServiceDiscoveryEventArgs > Callback, object State)
Sends a service discovery request
Definition: XmppClient.cs:5806
Definition: App.xaml.cs:4