Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ThingRegistryOrchestratorService.cs
11
13{
14 [Singleton]
15 internal class ThingRegistryOrchestratorService : LoadableService, IThingRegistryOrchestratorService
16 {
17 public ThingRegistryOrchestratorService()
18 {
19 }
20
21 public override Task Load(bool isResuming, CancellationToken cancellationToken)
22 {
23 if (this.BeginLoad(isResuming, cancellationToken))
24 this.EndLoad(true);
25
26 return Task.CompletedTask;
27 }
28
29 public override Task Unload()
30 {
31 if (this.BeginUnload())
32 this.EndUnload();
33
34 return Task.CompletedTask;
35 }
36
37 #region Event Handlers
38
39 #endregion
40
41 public Task OpenClaimDevice(string Uri)
42 {
43 MainThread.BeginInvokeOnMainThread(async () =>
44 {
46 });
47
48 return Task.CompletedTask;
49 }
50
51 public async Task OpenSearchDevices(string Uri)
52 {
53 try
54 {
55 (SearchResultThing[] Things, string? RegistryJid) = await ServiceRef.XmppService.SearchAll(Uri);
56
57 switch (Things.Length)
58 {
59 case 0:
62 break;
63
64 case 1:
65 SearchResultThing Thing = Things[0];
66
67 MainThread.BeginInvokeOnMainThread(async () =>
68 {
70
71 ContactInfo ContactInfo = await ContactInfo.FindByBareJid(Thing.Jid, Thing.Node.SourceId, Thing.Node.Partition, Thing.Node.NodeId);
72 ContactInfo ??= new ContactInfo()
73 {
74 AllowSubscriptionFrom = false,
75 BareJid = Thing.Jid,
76 IsThing = true,
77 LegalId = string.Empty,
78 LegalIdentity = null,
79 FriendlyName = ViewClaimThingViewModel.GetFriendlyName(Properties) ?? string.Empty,
80 MetaData = Properties,
81 SourceId = Thing.Node.SourceId,
82 Partition = Thing.Node.Partition,
83 NodeId = Thing.Node.NodeId,
84 Owner = false,
85 RegistryJid = RegistryJid,
86 SubscribeTo = null
87 };
88
90 MyThingsViewModel.GetNotificationEvents(ContactInfo) ?? []));
91 });
92 break;
93
94 default:
95 throw new NotImplementedException("Multiple devices were returned. Feature not implemented.");
96 // TODO: Search result list view
97 }
98 }
99 catch (Exception ex)
100 {
102 }
103 }
104
105 public async Task OpenDeviceReference(string Uri)
106 {
107 try
108 {
109 if (!ServiceRef.XmppService.TryDecodeIoTDiscoDirectURI(Uri, out string? Jid, out string? SourceId,
110 out string? NodeId, out string? PartitionId, out MetaDataTag[]? Tags))
111 {
112 throw new InvalidOperationException("Not a direct reference URI.");
113 }
114
115 Property[] Properties = ViewClaimThingViewModel.ToProperties(Tags);
116 ContactInfo Info = await ContactInfo.FindByBareJid(Jid, SourceId ?? string.Empty,
117 PartitionId ?? string.Empty, NodeId ?? string.Empty);
118
119 if (Info is null)
120 {
121 Info = new ContactInfo()
122 {
123 AllowSubscriptionFrom = false,
124 BareJid = Jid,
125 IsThing = true,
126 LegalId = string.Empty,
127 LegalIdentity = null,
128 FriendlyName = ViewClaimThingViewModel.GetFriendlyName(Properties) ?? Jid,
129 MetaData = Properties,
130 SourceId = SourceId ?? string.Empty,
131 Partition = PartitionId ?? string.Empty,
132 NodeId = NodeId ?? string.Empty,
133 Owner = false,
134 RegistryJid = ServiceRef.XmppService.RegistryServiceJid,
135 SubscribeTo = null
136 };
137
138 foreach (MetaDataTag Tag in Tags)
139 {
140 if (Tag.Name.Equals("R", StringComparison.OrdinalIgnoreCase))
141 Info.RegistryJid = Tag.StringValue;
142 }
143 }
144 else if (!(Info.IsThing ?? false))
145 {
146 Info.MetaData = Merge(Info.MetaData, Properties);
147 Info.IsThing = true;
148
149 await Database.Update(Info);
150 }
151
152 ViewThingNavigationArgs Args = new(Info, MyThingsViewModel.GetNotificationEvents(Info) ?? []);
153
154 await ServiceRef.NavigationService.GoToAsync(nameof(ViewThingPage), Args, BackMethod.CurrentPage);
155 }
156 catch (Exception ex)
157 {
159 }
160 }
161
162 private static Property[]? Merge(Property[]? Stored, Property[]? FromUri)
163 {
164 Dictionary<string, Property> Merged = [];
165 bool Changed = false;
166
167 if (Stored is not null)
168 {
169 foreach (Property P in Stored)
170 Merged[P.Name] = P;
171 }
172
173 if (FromUri is not null)
174 {
175 foreach (Property P in FromUri)
176 {
177 if (!Merged.TryGetValue(P.Name, out Property? P2) || P2.Value != P.Value)
178 {
179 Merged[P.Name] = P;
180 Changed = true;
181 }
182 }
183 }
184
185 if (!Changed)
186 return Stored;
187
188 Property[] Result = new Property[Merged.Count];
189 Merged.Values.CopyTo(Result, 0);
190
191 return Result;
192 }
193
194 }
195}
A strongly-typed resource class, for looking up localized strings, etc.
static string NoThingsFound
Looks up a localized string similar to No Things Found..
static string ErrorTitle
Looks up a localized string similar to An error has occurred.
Contains information about a contact.
Definition: ContactInfo.cs:22
Property?[] MetaData
Meta-data related to a thing.
Definition: ContactInfo.cs:211
static Task< ContactInfo > FindByBareJid(string BareJid)
Finds information about a contact, given its Bare JID.
Definition: ContactInfo.cs:221
bool BeginLoad(bool IsResuming, CancellationToken CancellationToken)
Sets the IsLoading flag if the service isn't already loading.
void EndLoad(bool isLoaded)
Sets the IsLoading and IsLoaded flags and fires an event representing the current load state of the s...
bool BeginUnload()
Sets the IsLoading flag if the service isn't already unloading.
void EndUnload()
Sets the IsLoading and IsLoaded flags and fires an event representing the current load state of the s...
Base class that references services in the app.
Definition: ServiceRef.cs:43
static IUiService UiService
Service serializing and managing UI-related tasks.
Definition: ServiceRef.cs:130
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
The view model to bind to for when displaying thing claim information.
static ? string GetFriendlyName(IEnumerable< HumanReadableTag > Tags)
Get Friendly name of thing
static Property[] ToProperties(IEnumerable< HumanReadableTag > Tags)
Converts an enumerable set of HumanReadableTag to an enumerable set of Property.
Holds navigation parameters specific to viewing things.
A page that displays information about a thing and allows the user to interact with it.
Abstract base class for all meta-data tags.
Definition: MetaDataTag.cs:10
abstract string StringValue
String-representation of meta-data tag value.
Definition: MetaDataTag.cs:31
Contains information about a thing in a search result.
ThingReference Node
Node reference. Can be equal to ThingReference.Empty if not behind a concentrator.
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
Task GoToAsync(string Route)
Navigates to the specified route and pushes the page onto the navigation stack.
Task DisplayException(Exception Exception, string? Title=null)
Displays an alert/message box to the user.
Task< bool > DisplayAlert(string Title, string Message, string? Accept=null, string? Cancel=null)
Displays an alert/message box to the user.
BackMethod
Navigation Back Method
Definition: BackMethod.cs:7