Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CanReadNotificationEvent.cs
9using Waher.Things;
11
13{
18 {
23 : base()
24 {
25 }
26
32 : base(e)
33 {
34 this.Fields = e.Fields;
35 this.FieldTypes = e.FieldTypes;
36 }
37
41 public string[]? Fields { get; set; }
42
46 public string[]? AllFields { get; set; }
47
51 public FieldType FieldTypes { get; set; }
52
56 public override async Task<string> GetDescription()
57 {
58 string ThingName = await ContactInfo.GetFriendlyName(this.BareJid, this.SourceId ?? string.Empty,
59 this.PartitionId ?? string.Empty, this.NodeId ?? string.Empty);
60
61 string RemoteName = await ContactInfo.GetFriendlyName(this.RemoteJid);
62
63 return ServiceRef.Localizer[nameof(AppResources.ReadoutRequestText), RemoteName, ThingName];
64 }
65
69 public override async Task Open()
70 {
71 string ThingName = await ContactInfo.GetFriendlyName(this.BareJid);
72 string RemoteName = await ContactInfo.GetFriendlyName(this.RemoteJid);
73
74 await ServiceRef.NavigationService.GoToAsync(nameof(CanReadPage), new CanReadNavigationArgs(this, ThingName, RemoteName));
75 }
76
80 public override async Task Prepare()
81 {
82 await base.Prepare();
83
84 string[]? Fields = await GetAvailableFieldNames(this.BareJid, new ThingReference(this.NodeId, this.SourceId, this.PartitionId));
85
86 if (Fields is not null)
87 {
88 this.AllFields = Fields;
89 await Database.Update(this);
90 }
91 }
92
99 public static async Task<string[]?> GetAvailableFieldNames(string BareJid, ThingReference Thing)
100 {
101 try
102 {
103 RosterItem? Item = ServiceRef.XmppService.GetRosterItem(BareJid);
104 if (Item is null || !Item.HasLastPresence || !Item.LastPresence.IsOnline)
105 return null;
106
107 SortedDictionary<string, bool> Fields = [];
108 SensorDataClientRequest Request = await ServiceRef.XmppService.RequestSensorReadout(Item.LastPresenceFullJid, [ Thing ], FieldType.All);
109 TaskCompletionSource<bool> Done = new();
110
111 Request.OnFieldsReceived += (sender, NewFields) =>
112 {
113 foreach (Field Field in NewFields)
114 Fields[Field.Name] = true;
115
116 return Task.CompletedTask;
117 };
118
119 Request.OnStateChanged += (Sender, NewState) =>
120 {
121 switch (NewState)
122 {
123 case SensorDataReadoutState.Done:
124 Done.TrySetResult(true);
125 break;
126
127 case SensorDataReadoutState.Cancelled:
128 case SensorDataReadoutState.Failure:
129 Done.TrySetResult(false);
130 break;
131 }
132
133 return Task.CompletedTask;
134 };
135
136 Task _ = Task.Delay(30000).ContinueWith((_) => Done.TrySetResult(false));
137
138 if (await Done.Task)
139 {
140 string[] Fields2 = new string[Fields.Count];
141 Fields.Keys.CopyTo(Fields2, 0);
142
143 return Fields2;
144 }
145 else
146 return null;
147 }
148 catch (Exception)
149 {
150 return null;
151 }
152 }
153
154 }
155}
A strongly-typed resource class, for looking up localized strings, etc.
static string ReadoutRequestText
Looks up a localized string similar to {0} wants to read data from your device {1}....
Contains information about a contact.
Definition: ContactInfo.cs:22
static async Task< string > GetFriendlyName(CaseInsensitiveString RemoteId)
Gets the friendly name of a remote identity (Legal ID or Bare JID).
Definition: ContactInfo.cs:258
CanReadNotificationEvent(CanReadEventArgs e)
Contains information about a request to read a thing.
CanReadNotificationEvent()
Contains information about a request to read a thing.
static async Task< string[]?> GetAvailableFieldNames(string BareJid, ThingReference Thing)
Gets available fields for a thing.
override async Task Prepare()
Performs perparatory tasks, that will simplify opening the notification.
override async Task< string > GetDescription()
Gets a descriptive text for the event.
Base class that references services in the app.
Definition: ServiceRef.cs:43
static INavigationService NavigationService
The navigation service for navigating between pages.
Definition: ServiceRef.cs:178
static IReportingStringLocalizer Localizer
Localization service
Definition: ServiceRef.cs:370
static IXmppService XmppService
The XMPP service for XMPP communication.
Definition: ServiceRef.cs:190
Holds navigation parameters specific to displaying the can-read provisioning question.
A page that asks the user if a remote entity is allowed to read the device.
FieldType FieldTypes
Any field types available in the original request.
string[] Fields
Any field specifications of the original request. If null, all fields are requested.
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
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
static async Task Update(object Object)
Updates an object in the database.
Definition: Database.cs:1211
Base class for all sensor data fields.
Definition: Field.cs:20
string Name
Unlocalized field name.
Definition: Field.cs:279
Contains a reference to a thing
Definition: ImplTypes.g.cs:58
SensorDataReadoutState
Sensor Data Readout States.
FieldType
Field Type flags
Definition: FieldType.cs:10