Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CanControlNotificationEvent.cs
8using Waher.Things;
9
11{
16 {
21 : base()
22 {
23 }
24
29 public CanControlNotificationEvent(CanControlEventArgs e)
30 : base(e)
31 {
32 this.Parameters = e.Parameters;
33 }
34
38 public string[]? Parameters { get; set; }
39
43 public string[]? AllParameters { get; set; }
44
48 public override async Task<string> GetDescription()
49 {
50 string ThingName = await ContactInfo.GetFriendlyName(this.BareJid, this.SourceId ?? string.Empty,
51 this.PartitionId ?? string.Empty, this.NodeId ?? string.Empty);
52
53 string RemoteName = await ContactInfo.GetFriendlyName(this.RemoteJid);
54
55 return ServiceRef.Localizer[nameof(AppResources.ControlRequestText), RemoteName, ThingName];
56 }
57
61 public override async Task Open()
62 {
63 string ThingName = await ContactInfo.GetFriendlyName(this.BareJid);
64 string RemoteName = await ContactInfo.GetFriendlyName(this.RemoteJid);
65
66 await ServiceRef.UiService.GoToAsync(nameof(CanControlPage), new CanControlNavigationArgs(this, ThingName, RemoteName));
67 }
68
72 public override async Task Prepare()
73 {
74 await base.Prepare();
75
76 string[]? Parameters = await GetAvailableParameterNames(this.BareJid,
77 new ThingReference(this.NodeId, this.SourceId, this.PartitionId));
78
79 if (Parameters is not null)
80 {
81 this.AllParameters = Parameters;
82 await Database.Update(this);
83 }
84 }
85
92 public static async Task<string[]?> GetAvailableParameterNames(string BareJid, ThingReference Thing)
93 {
94 try
95 {
96 RosterItem? Item = ServiceRef.XmppService.GetRosterItem(BareJid);
97 if (Item is null || !Item.HasLastPresence || !Item.LastPresence.IsOnline)
98 return null;
99
100 TaskCompletionSource<DataForm?> FormTask = new();
101
102 ServiceRef.XmppService.GetControlForm(Item.LastPresenceFullJid,
103 System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName,
104 (sender, e) =>
105 {
106 if (e.Ok)
107 FormTask.TrySetResult(e.Form);
108 else
109 FormTask.TrySetResult(null);
110
111 return Task.CompletedTask;
112 }, null, [ Thing ]);
113
114 Task _ = Task.Delay(30000).ContinueWith((_) => FormTask.TrySetResult(null));
115
116 DataForm? Form = await FormTask.Task;
117
118 if (Form is not null)
119 {
120 SortedDictionary<string, bool> Parameters = [];
121
122 foreach (Field Field in Form.Fields)
123 {
124 if (!Field.ReadOnly && !string.IsNullOrEmpty(Field.Var))
125 Parameters[Field.Var] = true;
126 }
127
128 string[] Parameters2 = new string[Parameters.Count];
129 Parameters.Keys.CopyTo(Parameters2, 0);
130
131 return Parameters2;
132 }
133 else
134 return null;
135 }
136 catch (Exception)
137 {
138 return null;
139 }
140 }
141
142 }
143}
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
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: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-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:716
Base class for form fields
Definition: Field.cs:16
string Var
Variable name
Definition: Field.cs:81
bool ReadOnly
Flags the field as being read-only.
Definition: Field.cs:145
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
Contains a reference to a thing