Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CanControlNotificationEvent.cs
9using Waher.Things;
10
12{
17 {
22 : base()
23 {
24 }
25
31 : base(e)
32 {
33 this.Parameters = e.Parameters;
34 }
35
39 public string[]? Parameters { get; set; }
40
44 public string[]? AllParameters { get; set; }
45
49 public override async Task<string> GetDescription()
50 {
51 string ThingName = await ContactInfo.GetFriendlyName(this.BareJid, this.SourceId ?? string.Empty,
52 this.PartitionId ?? string.Empty, this.NodeId ?? string.Empty);
53
54 string RemoteName = await ContactInfo.GetFriendlyName(this.RemoteJid);
55
56 return ServiceRef.Localizer[nameof(AppResources.ControlRequestText), RemoteName, ThingName];
57 }
58
62 public override async Task Open()
63 {
64 string ThingName = await ContactInfo.GetFriendlyName(this.BareJid);
65 string RemoteName = await ContactInfo.GetFriendlyName(this.RemoteJid);
66
67 await ServiceRef.NavigationService.GoToAsync(nameof(CanControlPage), new CanControlNavigationArgs(this, ThingName, RemoteName));
68 }
69
73 public override async Task Prepare()
74 {
75 await base.Prepare();
76
77 string[]? Parameters = await GetAvailableParameterNames(this.BareJid,
78 new ThingReference(this.NodeId, this.SourceId, this.PartitionId));
79
80 if (Parameters is not null)
81 {
82 this.AllParameters = Parameters;
83 await Database.Update(this);
84 }
85 }
86
93 public static async Task<string[]?> GetAvailableParameterNames(string BareJid, ThingReference Thing)
94 {
95 try
96 {
97 RosterItem? Item = ServiceRef.XmppService.GetRosterItem(BareJid);
98 if (Item is null || !Item.HasLastPresence || !Item.LastPresence.IsOnline)
99 return null;
100
101 TaskCompletionSource<DataForm?> FormTask = new();
102
103 ServiceRef.XmppService.GetControlForm(Item.LastPresenceFullJid,
104 System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName,
105 (sender, e) =>
106 {
107 if (e.Ok)
108 FormTask.TrySetResult(e.Form);
109 else
110 FormTask.TrySetResult(null);
111
112 return Task.CompletedTask;
113 }, null, [ Thing ]);
114
115 Task _ = Task.Delay(30000).ContinueWith((_) => FormTask.TrySetResult(null));
116
117 DataForm? Form = await FormTask.Task;
118
119 if (Form is not null)
120 {
121 SortedDictionary<string, bool> Parameters = [];
122
123 foreach (Field Field in Form.Fields)
124 {
125 if (!Field.ReadOnly && !string.IsNullOrEmpty(Field.Var))
126 Parameters[Field.Var] = true;
127 }
128
129 string[] Parameters2 = new string[Parameters.Count];
130 Parameters.Keys.CopyTo(Parameters2, 0);
131
132 return Parameters2;
133 }
134 else
135 return null;
136 }
137 catch (Exception)
138 {
139 return null;
140 }
141 }
142
143 }
144}
A strongly-typed resource class, for looking up localized strings, etc.
static string ControlRequestText
Looks up a localized string similar to {0} wants to control parameters on 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
CanControlNotificationEvent()
Contains information about a request to read a thing.
static async Task< string[]?> GetAvailableParameterNames(string BareJid, ThingReference Thing)
Gets available fields for a thing.
override async Task< string > GetDescription()
Gets a descriptive text for the event.
override async Task Prepare()
Performs perparatory tasks, that will simplify opening the notification.
CanControlNotificationEvent(CanControlEventArgs e)
Contains information about a request to read a thing.
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-control provisioning question.
A page that asks the user if a remote entity is allowed to control the device.
Implements support for data forms. Data Forms are defined in the following XEPs:
Definition: DataForm.cs:42
Field[] Fields
Fields in the form.
Definition: DataForm.cs:715
Base class for form fields
Definition: Field.cs:17
string Var
Variable name
Definition: Field.cs:82
bool ReadOnly
Flags the field as being read-only.
Definition: Field.cs:146
string[] Parameters
Any parameter name specifications of the original request.
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
Contains a reference to a thing
Definition: ImplTypes.g.cs:58