Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CanReadNotificationEvent.cs
8using Waher.Things;
10
12{
17 {
22 : base()
23 {
24 }
25
30 public CanReadNotificationEvent(CanReadEventArgs e)
31 : base(e)
32 {
33 this.Fields = e.Fields;
34 this.FieldTypes = e.FieldTypes;
35 }
36
40 public string[]? Fields { get; set; }
41
45 public string[]? AllFields { get; set; }
46
50 public FieldType FieldTypes { get; set; }
51
55 public override async Task<string> GetDescription()
56 {
57 string ThingName = await ContactInfo.GetFriendlyName(this.BareJid, this.SourceId ?? string.Empty,
58 this.PartitionId ?? string.Empty, this.NodeId ?? string.Empty);
59
60 string RemoteName = await ContactInfo.GetFriendlyName(this.RemoteJid);
61
62 return ServiceRef.Localizer[nameof(AppResources.ReadoutRequestText), RemoteName, ThingName];
63 }
64
68 public override async Task Open()
69 {
70 string ThingName = await ContactInfo.GetFriendlyName(this.BareJid);
71 string RemoteName = await ContactInfo.GetFriendlyName(this.RemoteJid);
72
73 await ServiceRef.UiService.GoToAsync(nameof(CanReadPage), new CanReadNavigationArgs(this, ThingName, RemoteName));
74 }
75
79 public override async Task Prepare()
80 {
81 await base.Prepare();
82
83 string[]? Fields = await GetAvailableFieldNames(this.BareJid, new ThingReference(this.NodeId, this.SourceId, this.PartitionId));
84
85 if (Fields is not null)
86 {
87 this.AllFields = Fields;
88 await Database.Update(this);
89 }
90 }
91
98 public static async Task<string[]?> GetAvailableFieldNames(string BareJid, ThingReference Thing)
99 {
100 try
101 {
102 RosterItem? Item = ServiceRef.XmppService.GetRosterItem(BareJid);
103 if (Item is null || !Item.HasLastPresence || !Item.LastPresence.IsOnline)
104 return null;
105
106 SortedDictionary<string, bool> Fields = [];
107 SensorDataClientRequest Request = ServiceRef.XmppService.RequestSensorReadout(Item.LastPresenceFullJid, [ Thing ], FieldType.All);
108 TaskCompletionSource<bool> Done = new();
109
110 Request.OnFieldsReceived += (sender, NewFields) =>
111 {
112 foreach (Field Field in NewFields)
113 Fields[Field.Name] = true;
114
115 return Task.CompletedTask;
116 };
117
118 Request.OnStateChanged += (Sender, NewState) =>
119 {
120 switch (NewState)
121 {
122 case SensorDataReadoutState.Done:
123 Done.TrySetResult(true);
124 break;
125
126 case SensorDataReadoutState.Cancelled:
127 case SensorDataReadoutState.Failure:
128 Done.TrySetResult(false);
129 break;
130 }
131
132 return Task.CompletedTask;
133 };
134
135 Task _ = Task.Delay(30000).ContinueWith((_) => Done.TrySetResult(false));
136
137 if (await Done.Task)
138 {
139 string[] Fields2 = new string[Fields.Count];
140 Fields.Keys.CopyTo(Fields2, 0);
141
142 return Fields2;
143 }
144 else
145 return null;
146 }
147 catch (Exception)
148 {
149 return null;
150 }
151 }
152
153 }
154}
Contains information about a contact.
Definition: ContactInfo.cs:21
static async Task< string > GetFriendlyName(CaseInsensitiveString RemoteId)
Gets the friendly name of a remote identity (Legal ID or Bare JID).
Definition: ContactInfo.cs:257
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:31
static IUiService UiService
Service serializing and managing UI-related tasks.
Definition: ServiceRef.cs:55
static IStringLocalizer Localizer
Localization service
Definition: ServiceRef.cs:235
static IXmppService XmppService
The XMPP service for XMPP communication.
Definition: ServiceRef.cs:67
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.
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:19
static async Task Update(object Object)
Updates an object in the database.
Definition: Database.cs:626
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
SensorDataReadoutState
Sensor Data Readout States.
FieldType
Field Type flags
Definition: FieldType.cs:10