-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathThingRegistryClient.cs
More file actions
1760 lines (1532 loc) · 61.4 KB
/
ThingRegistryClient.cs
File metadata and controls
1760 lines (1532 loc) · 61.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Threading.Tasks;
using Waher.Content;
using Waher.Content.Xml;
using Waher.Events;
using Waher.Networking.XMPP.Events;
using Waher.Networking.XMPP.Provisioning.Events;
using Waher.Networking.XMPP.Provisioning.SearchOperators;
using Waher.Networking.XMPP.ServiceDiscovery;
using Waher.Things;
namespace Waher.Networking.XMPP.Provisioning
{
/// <summary>
/// Implements an XMPP thing registry client interface.
///
/// The interface is defined in XEP-0347:
/// http://xmpp.org/extensions/xep-0347.html
/// </summary>
public class ThingRegistryClient : XmppExtension
{
private readonly string thingRegistryAddress;
/// <summary>
/// urn:ieee:iot:disco:1.0
/// </summary>
public const string NamespaceDiscoveryIeeeV1 = "urn:ieee:iot:disco:1.0";
/// <summary>
/// urn:nf:iot:disco:1.0
/// </summary>
public const string NamespaceDiscoveryNeuroFoundationV1 = "urn:nf:iot:disco:1.0";
/// <summary>
/// Current namespace for IoT Discovery
/// </summary>
public const string NamespaceDiscoveryCurrent = NamespaceDiscoveryNeuroFoundationV1;
/// <summary>
/// Namespaces supported for discovery.
/// </summary>
public static readonly string[] NamespacesDiscovery = new string[]
{
NamespaceDiscoveryIeeeV1,
NamespaceDiscoveryNeuroFoundationV1
};
/// <summary>
/// Implements an XMPP provisioning client interface.
///
/// The interface is defined in the Neuro-Foundation XMPP IoT extensions:
/// https://neuro-foundation.io
/// </summary>
/// <param name="Client">XMPP Client</param>
/// <param name="ThingRegistryAddress">Thing Registry XMPP address.</param>
public ThingRegistryClient(XmppClient Client, string ThingRegistryAddress)
: base(Client)
{
this.thingRegistryAddress = ThingRegistryAddress;
#region Neuro-Foundation V1
this.client.RegisterIqSetHandler("claimed", NamespaceDiscoveryNeuroFoundationV1, this.ClaimedHandler, true);
this.client.RegisterIqSetHandler("removed", NamespaceDiscoveryNeuroFoundationV1, this.RemovedHandler, false);
this.client.RegisterIqSetHandler("disowned", NamespaceDiscoveryNeuroFoundationV1, this.DisownedHandler, false);
#endregion
#region IEEE V1
this.client.RegisterIqSetHandler("claimed", NamespaceDiscoveryIeeeV1, this.ClaimedHandler, true);
this.client.RegisterIqSetHandler("removed", NamespaceDiscoveryIeeeV1, this.RemovedHandler, false);
this.client.RegisterIqSetHandler("disowned", NamespaceDiscoveryIeeeV1, this.DisownedHandler, false);
#endregion
}
/// <inheritdoc/>
public override void Dispose()
{
#region Neuro-Foundation V1
this.client.UnregisterIqSetHandler("claimed", NamespaceDiscoveryNeuroFoundationV1, this.ClaimedHandler, true);
this.client.UnregisterIqSetHandler("removed", NamespaceDiscoveryNeuroFoundationV1, this.RemovedHandler, false);
this.client.UnregisterIqSetHandler("disowned", NamespaceDiscoveryNeuroFoundationV1, this.DisownedHandler, false);
#endregion
#region IEEE V1
this.client.UnregisterIqSetHandler("claimed", NamespaceDiscoveryIeeeV1, this.ClaimedHandler, true);
this.client.UnregisterIqSetHandler("removed", NamespaceDiscoveryIeeeV1, this.RemovedHandler, false);
this.client.UnregisterIqSetHandler("disowned", NamespaceDiscoveryIeeeV1, this.DisownedHandler, false);
#endregion
base.Dispose();
}
/// <summary>
/// Implemented extensions.
/// </summary>
public override string[] Extensions => new string[] { "XEP-0347" };
/// <summary>
/// Thing Registry XMPP address.
/// </summary>
public string ThingRegistryAddress => this.thingRegistryAddress;
/// <summary>
/// Registers a thing in the Thing Registry. Only things that does not have an owner can register with the Thing Registry.
/// Things that have an owner should call <see cref="UpdateThing(MetaDataTag[], EventHandlerAsync{UpdateEventArgs}, object)"/> to update
/// its meta-data in the Thing Registry, if the meta-data has changed.
/// </summary>
/// <param name="MetaDataTags">Meta-data tags to register with the registry.</param>
/// <param name="Callback">Callback method to call when response is returned.</param>
/// <param name="State">State object to pass on to the callback method.</param>
public Task RegisterThing(MetaDataTag[] MetaDataTags, EventHandlerAsync<RegistrationEventArgs> Callback, object State)
{
return this.RegisterThing(false, string.Empty, string.Empty, string.Empty, MetaDataTags, Callback, State);
}
/// <summary>
/// Registers a thing in the Thing Registry. Only things that does not have an owner can register with the Thing Registry.
/// Things that have an owner should call <see cref="UpdateThing(string, MetaDataTag[], EventHandlerAsync{UpdateEventArgs}, object)"/> to
/// update its meta-data in the Thing Registry, if the meta-data has changed.
/// </summary>
/// <param name="NodeId">Node ID of thing, if behind a concentrator.</param>
/// <param name="MetaDataTags">Meta-data tags to register with the registry.</param>
/// <param name="Callback">Callback method to call when response is returned.</param>
/// <param name="State">State object to pass on to the callback method.</param>
public Task RegisterThing(string NodeId, MetaDataTag[] MetaDataTags, EventHandlerAsync<RegistrationEventArgs> Callback, object State)
{
return this.RegisterThing(false, NodeId, string.Empty, string.Empty, MetaDataTags, Callback, State);
}
/// <summary>
/// Registers a thing in the Thing Registry. Only things that does not have an owner can register with the Thing Registry.
/// Things that have an owner should call <see cref="UpdateThing(string, string, MetaDataTag[], EventHandlerAsync{UpdateEventArgs}, object)"/> to
/// update its meta-data in the Thing Registry, if the meta-data has changed.
/// </summary>
/// <param name="NodeId">Node ID of thing, if behind a concentrator.</param>
/// <param name="SourceId">Source ID of thing, if behind a concentrator.</param>
/// <param name="MetaDataTags">Meta-data tags to register with the registry.</param>
/// <param name="Callback">Callback method to call when response is returned.</param>
/// <param name="State">State object to pass on to the callback method.</param>
public Task RegisterThing(string NodeId, string SourceId, MetaDataTag[] MetaDataTags,
EventHandlerAsync<RegistrationEventArgs> Callback, object State)
{
return this.RegisterThing(false, NodeId, SourceId, string.Empty, MetaDataTags, Callback, State);
}
/// <summary>
/// Registers a thing in the Thing Registry. Only things that does not have an owner can register with the Thing Registry.
/// Things that have an owner should call <see cref="UpdateThing(string, string, string, MetaDataTag[], EventHandlerAsync{UpdateEventArgs}, object)"/>
/// to update its meta-data in the Thing Registry, if the meta-data has changed.
/// </summary>
/// <param name="NodeId">Node ID of thing, if behind a concentrator.</param>
/// <param name="SourceId">Source ID of thing, if behind a concentrator.</param>
/// <param name="Partition">Partition of thing, if behind a concentrator.</param>
/// <param name="MetaDataTags">Meta-data tags to register with the registry.</param>
/// <param name="Callback">Callback method to call when response is returned.</param>
/// <param name="State">State object to pass on to the callback method.</param>
public Task RegisterThing(string NodeId, string SourceId, string Partition, MetaDataTag[] MetaDataTags,
EventHandlerAsync<RegistrationEventArgs> Callback, object State)
{
return this.RegisterThing(false, NodeId, SourceId, Partition, MetaDataTags, Callback, State);
}
/// <summary>
/// Registers a thing in the Thing Registry. Only things that does not have an owner can register with the Thing Registry.
/// Things that have an owner should call <see cref="UpdateThing(MetaDataTag[], EventHandlerAsync{UpdateEventArgs}, object)"/> to
/// update its meta-data in the Thing Registry, if the meta-data has changed.
/// </summary>
/// <param name="SelfOwned">If the thing is owned by itself.</param>
/// <param name="MetaDataTags">Meta-data tags to register with the registry.</param>
/// <param name="Callback">Callback method to call when response is returned.</param>
/// <param name="State">State object to pass on to the callback method.</param>
public Task RegisterThing(bool SelfOwned, MetaDataTag[] MetaDataTags, EventHandlerAsync<RegistrationEventArgs> Callback, object State)
{
return this.RegisterThing(SelfOwned, string.Empty, string.Empty, string.Empty, MetaDataTags, Callback, State);
}
/// <summary>
/// Registers a thing in the Thing Registry. Only things that does not have an owner can register with the Thing Registry.
/// Things that have an owner should call <see cref="UpdateThing(string, MetaDataTag[], EventHandlerAsync{UpdateEventArgs}, object)"/>
/// to update its meta-data in the Thing Registry, if the meta-data has changed.
/// </summary>
/// <param name="SelfOwned">If the thing is owned by itself.</param>
/// <param name="NodeId">Node ID of thing, if behind a concentrator.</param>
/// <param name="MetaDataTags">Meta-data tags to register with the registry.</param>
/// <param name="Callback">Callback method to call when response is returned.</param>
/// <param name="State">State object to pass on to the callback method.</param>
public Task RegisterThing(bool SelfOwned, string NodeId, MetaDataTag[] MetaDataTags,
EventHandlerAsync<RegistrationEventArgs> Callback, object State)
{
return this.RegisterThing(SelfOwned, NodeId, string.Empty, string.Empty, MetaDataTags, Callback, State);
}
/// <summary>
/// Registers a thing in the Thing Registry. Only things that does not have an owner can register with the Thing Registry.
/// Things that have an owner should call <see cref="UpdateThing(string, string, MetaDataTag[], EventHandlerAsync{UpdateEventArgs}, object)"/>
/// to update its meta-data in the Thing Registry, if the meta-data has changed.
/// </summary>
/// <param name="SelfOwned">If the thing is owned by itself.</param>
/// <param name="NodeId">Node ID of thing, if behind a concentrator.</param>
/// <param name="SourceId">Source ID of thing, if behind a concentrator.</param>
/// <param name="MetaDataTags">Meta-data tags to register with the registry.</param>
/// <param name="Callback">Callback method to call when response is returned.</param>
/// <param name="State">State object to pass on to the callback method.</param>
public Task RegisterThing(bool SelfOwned, string NodeId, string SourceId, MetaDataTag[] MetaDataTags,
EventHandlerAsync<RegistrationEventArgs> Callback, object State)
{
return this.RegisterThing(SelfOwned, NodeId, SourceId, string.Empty, MetaDataTags, Callback, State);
}
/// <summary>
/// Registers a thing in the Thing Registry. Only things that does not have an owner can register with the Thing Registry.
/// Things that have an owner should call <see cref="UpdateThing(string, string, string, MetaDataTag[], EventHandlerAsync{UpdateEventArgs}, object)"/>
/// to update its meta-data in the Thing Registry, if the meta-data has changed.
/// </summary>
/// <param name="SelfOwned">If the thing is owned by itself.</param>
/// <param name="NodeId">Node ID of thing, if behind a concentrator.</param>
/// <param name="SourceId">Source ID of thing, if behind a concentrator.</param>
/// <param name="Partition">Partition of thing, if behind a concentrator.</param>
/// <param name="MetaDataTags">Meta-data tags to register with the registry.</param>
/// <param name="Callback">Callback method to call when response is returned.</param>
/// <param name="State">State object to pass on to the callback method.</param>
public Task RegisterThing(bool SelfOwned, string NodeId, string SourceId, string Partition, MetaDataTag[] MetaDataTags,
EventHandlerAsync<RegistrationEventArgs> Callback, object State)
{
StringBuilder Request = new StringBuilder();
Request.Append("<register xmlns='");
Request.Append(NamespaceDiscoveryCurrent);
this.AddNodeInfo(Request, NodeId, SourceId, Partition);
if (SelfOwned)
Request.Append("' selfOwned='true");
Request.Append("'>");
string RegistryAddress = this.AddTags(Request, MetaDataTags, this.thingRegistryAddress);
Request.Append("</register>");
return this.client.SendIqSet(RegistryAddress, Request.ToString(), async (Sender, e) =>
{
XmlElement E = e.FirstElement;
string OwnerJid = string.Empty;
bool IsPublic = false;
if (e.Ok && !(E is null) && E.LocalName == "claimed")
{
OwnerJid = XML.Attribute(E, "jid");
IsPublic = XML.Attribute(E, "public", false);
if (string.IsNullOrEmpty(NodeId) && string.IsNullOrEmpty(SourceId) && string.IsNullOrEmpty(Partition) &&
this.client.TryGetExtension(out ProvisioningClient ProvisioningClient))
{
ProvisioningClient.OwnerJid = OwnerJid;
}
}
await Callback.Raise(this, new RegistrationEventArgs(e, State, OwnerJid, IsPublic));
}, State);
}
private void AddNodeInfo(StringBuilder Request, string NodeId, string SourceId, string Partition)
{
if (!string.IsNullOrEmpty(NodeId))
{
Request.Append("' id='");
Request.Append(XML.Encode(NodeId));
}
if (!string.IsNullOrEmpty(SourceId))
{
Request.Append("' src='");
Request.Append(XML.Encode(SourceId));
}
if (!string.IsNullOrEmpty(Partition))
{
Request.Append("' pt='");
Request.Append(XML.Encode(Partition));
}
}
private string AddTags(StringBuilder Request, MetaDataTag[] MetaDataTags, string RegistryAddress)
{
foreach (MetaDataTag Tag in MetaDataTags)
{
if (Tag is MetaDataStringTag)
{
if (Tag.Name == "R")
RegistryAddress = Tag.StringValue;
else
{
Request.Append("<str name='");
Request.Append(XML.Encode(Tag.Name));
Request.Append("' value='");
Request.Append(XML.Encode(Tag.StringValue));
Request.Append("'/>");
}
}
else if (Tag is MetaDataNumericTag)
{
Request.Append("<num name='");
Request.Append(XML.Encode(Tag.Name));
Request.Append("' value='");
Request.Append(Tag.StringValue);
Request.Append("'/>");
}
}
return RegistryAddress;
}
/// <summary>
/// Claims a thing.
/// </summary>
/// <param name="MetaDataTags">Meta-data describing the thing.</param>
/// <param name="Callback">Method to call when response to claim is returned.</param>
/// <param name="State">State object passed on to the callback method.</param>
public Task Mine(MetaDataTag[] MetaDataTags, EventHandlerAsync<NodeResultEventArgs> Callback, object State)
{
return this.Mine(true, MetaDataTags, Callback, State);
}
/// <summary>
/// Claims a thing.
/// </summary>
/// <param name="Public">If the thing should be left as a public thing that is searchable.</param>
/// <param name="MetaDataTags">Meta-data describing the thing.</param>
/// <param name="Callback">Method to call when response to claim is returned.</param>
/// <param name="State">State object passed on to the callback method.</param>
public Task Mine(bool Public, MetaDataTag[] MetaDataTags, EventHandlerAsync<NodeResultEventArgs> Callback, object State)
{
StringBuilder Request = new StringBuilder();
Request.Append("<mine xmlns='");
Request.Append(NamespaceDiscoveryCurrent);
Request.Append("' public='");
Request.Append(CommonTypes.Encode(Public));
Request.Append("'>");
string RegistryAddress = this.AddTags(Request, MetaDataTags, this.thingRegistryAddress);
Request.Append("</mine>");
return this.client.SendIqSet(RegistryAddress, Request.ToString(), async (Sender, e) =>
{
XmlElement E = e.FirstElement;
string NodeJid = string.Empty;
ThingReference Node = ThingReference.Empty;
if (e.Ok && !(E is null) && E.LocalName == "claimed")
{
string NodeId = XML.Attribute(E, "id");
string SourceId = XML.Attribute(E, "src");
string Partition = XML.Attribute(E, "pt");
NodeJid = XML.Attribute(E, "jid");
if (!string.IsNullOrEmpty(NodeId) || !string.IsNullOrEmpty(SourceId) || !string.IsNullOrEmpty(Partition))
Node = new ThingReference(NodeId, SourceId, Partition);
}
await Callback.Raise(this, new NodeResultEventArgs(e, State, NodeJid, Node));
}, State);
}
private async Task ClaimedHandler(object Sender, IqEventArgs e)
{
XmlElement E = e.Query;
string OwnerJid = XML.Attribute(E, "jid");
string NodeId = XML.Attribute(E, "id");
string SourceId = XML.Attribute(E, "src");
string Partition = XML.Attribute(E, "pt");
bool Public = XML.Attribute(E, "public", false);
ThingReference Node;
if (string.IsNullOrEmpty(NodeId) && string.IsNullOrEmpty(SourceId) && string.IsNullOrEmpty(Partition))
{
Node = ThingReference.Empty;
if (this.client.TryGetExtension(out ProvisioningClient ProvisioningClient))
ProvisioningClient.OwnerJid = OwnerJid;
}
else
Node = new ThingReference(NodeId, SourceId, Partition);
ClaimedEventArgs e2 = new ClaimedEventArgs(e, Node, OwnerJid, Public);
await this.Claimed.Raise(this, e2);
await e.IqResult(string.Empty);
}
/// <summary>
/// Event raised when a node has been claimed.
/// </summary>
public event EventHandlerAsync<ClaimedEventArgs> Claimed = null;
/// <summary>
/// Removes a publicly claimed thing from the thing registry, so that it does not appear in search results.
/// </summary>
/// <param name="ThingJid">JID of thing to disown.</param>
/// <param name="Callback">Method to call when response is received.</param>
/// <param name="State">State object to pass on to the callback method.</param>
public Task Remove(string ThingJid, EventHandlerAsync<IqResultEventArgs> Callback, object State)
{
return this.Remove(ThingJid, string.Empty, string.Empty, string.Empty, Callback, State);
}
/// <summary>
/// Removes a publicly claimed thing from the thing registry, so that it does not appear in search results.
/// </summary>
/// <param name="ThingJid">JID of thing to disown.</param>
/// <param name="NodeId">Optional Node ID of thing.</param>
/// <param name="Callback">Method to call when response is received.</param>
/// <param name="State">State object to pass on to the callback method.</param>
public Task Remove(string ThingJid, string NodeId, EventHandlerAsync<IqResultEventArgs> Callback, object State)
{
return this.Remove(ThingJid, NodeId, string.Empty, string.Empty, Callback, State);
}
/// <summary>
/// Removes a publicly claimed thing from the thing registry, so that it does not appear in search results.
/// </summary>
/// <param name="ThingJid">JID of thing to disown.</param>
/// <param name="NodeId">Optional Node ID of thing.</param>
/// <param name="SourceId">Optional Source ID of thing.</param>
/// <param name="Callback">Method to call when response is received.</param>
/// <param name="State">State object to pass on to the callback method.</param>
public Task Remove(string ThingJid, string NodeId, string SourceId, EventHandlerAsync<IqResultEventArgs> Callback, object State)
{
return this.Remove(ThingJid, NodeId, SourceId, string.Empty, Callback, State);
}
/// <summary>
/// Removes a publicly claimed thing from the thing registry, so that it does not appear in search results.
/// </summary>
/// <param name="ThingJid">JID of thing to disown.</param>
/// <param name="NodeId">Optional Node ID of thing.</param>
/// <param name="SourceId">Optional Source ID of thing.</param>
/// <param name="Partition">Optional Partition of thing.</param>
/// <param name="Callback">Method to call when response is received.</param>
/// <param name="State">State object to pass on to the callback method.</param>
public Task Remove(string ThingJid, string NodeId, string SourceId, string Partition, EventHandlerAsync<IqResultEventArgs> Callback, object State)
{
return this.Remove(this.thingRegistryAddress, ThingJid, NodeId, SourceId, Partition, Callback, State);
}
/// <summary>
/// Removes a publicly claimed thing from the thing registry, so that it does not appear in search results.
/// </summary>
/// <param name="RegistryJid">JID of registry service.</param>
/// <param name="ThingJid">JID of thing to disown.</param>
/// <param name="NodeId">Optional Node ID of thing.</param>
/// <param name="SourceId">Optional Source ID of thing.</param>
/// <param name="Partition">Optional Partition of thing.</param>
/// <param name="Callback">Method to call when response is received.</param>
/// <param name="State">State object to pass on to the callback method.</param>
public Task Remove(string RegistryJid, string ThingJid, string NodeId, string SourceId, string Partition, EventHandlerAsync<IqResultEventArgs> Callback, object State)
{
StringBuilder Request = new StringBuilder();
Request.Append("<remove xmlns='");
Request.Append(NamespaceDiscoveryCurrent);
Request.Append("' jid='");
Request.Append(XML.Encode(ThingJid));
this.AddNodeInfo(Request, NodeId, SourceId, Partition);
Request.Append("'/>");
return this.client.SendIqSet(RegistryJid, Request.ToString(), (Sender, e) => Callback.Raise(this, e), State);
}
private async Task RemovedHandler(object Sender, IqEventArgs e)
{
XmlElement E = e.Query;
string NodeId = XML.Attribute(E, "id");
string SourceId = XML.Attribute(E, "src");
string Partition = XML.Attribute(E, "pt");
ThingReference Node;
if (string.IsNullOrEmpty(NodeId) && string.IsNullOrEmpty(SourceId) && string.IsNullOrEmpty(Partition))
Node = ThingReference.Empty;
else
Node = new ThingReference(NodeId, SourceId, Partition);
NodeEventArgs e2 = new NodeEventArgs(e, Node);
await this.Removed.Raise(this, e2);
await e.IqResult(string.Empty);
}
/// <summary>
/// Event raised when a node has been removed from the registry.
/// </summary>
public event EventHandlerAsync<NodeEventArgs> Removed = null;
/// <summary>
/// Updates the meta-data about a thing in the Thing Registry. Only public things that have an owner can update its meta-data.
/// Things that do not have an owner should call <see cref="RegisterThing(MetaDataTag[], EventHandlerAsync{RegistrationEventArgs}, object)"/> to
/// update its meta-data in the Thing Registry.
///
/// Note: Meta information updated in this way will only overwrite tags provided in the request, and leave other tags previously
/// reported as is.
/// </summary>
/// <param name="MetaDataTags">Meta-data tags to register with the registry.</param>
/// <param name="Callback">Callback method.</param>
/// <param name="State">State object passed on to callback method.</param>
public Task UpdateThing(MetaDataTag[] MetaDataTags, EventHandlerAsync<UpdateEventArgs> Callback, object State)
{
return this.UpdateThing(string.Empty, string.Empty, string.Empty, string.Empty, MetaDataTags, Callback, State);
}
/// <summary>
/// Updates the meta-data about a thing in the Thing Registry. Only public things that have an owner can update its meta-data.
/// Things that do not have an owner should call <see cref="RegisterThing(string, MetaDataTag[], EventHandlerAsync{RegistrationEventArgs}, object)"/> to
/// update its meta-data in the Thing Registry.
///
/// Note: Meta information updated in this way will only overwrite tags provided in the request, and leave other tags previously
/// reported as is.
/// </summary>
/// <param name="NodeId">Node ID of thing, if behind a concentrator.</param>
/// <param name="MetaDataTags">Meta-data tags to register with the registry.</param>
/// <param name="Callback">Callback method.</param>
/// <param name="State">State object passed on to callback method.</param>
public Task UpdateThing(string NodeId, MetaDataTag[] MetaDataTags, EventHandlerAsync<UpdateEventArgs> Callback, object State)
{
return this.UpdateThing(NodeId, string.Empty, string.Empty, string.Empty, MetaDataTags, Callback, State);
}
/// <summary>
/// Updates the meta-data about a thing in the Thing Registry. Only public things that have an owner can update its meta-data.
/// Things that do not have an owner should call <see cref="RegisterThing(string, string, MetaDataTag[], EventHandlerAsync{RegistrationEventArgs}, object)"/>
/// to update its meta-data in the Thing Registry.
///
/// Note: Meta information updated in this way will only overwrite tags provided in the request, and leave other tags previously
/// reported as is.
/// </summary>
/// <param name="NodeId">Node ID of thing, if behind a concentrator.</param>
/// <param name="SourceId">Source ID of thing, if behind a concentrator.</param>
/// <param name="MetaDataTags">Meta-data tags to register with the registry.</param>
/// <param name="Callback">Callback method.</param>
/// <param name="State">State object passed on to callback method.</param>
public Task UpdateThing(string NodeId, string SourceId, MetaDataTag[] MetaDataTags, EventHandlerAsync<UpdateEventArgs> Callback, object State)
{
return this.UpdateThing(NodeId, SourceId, string.Empty, string.Empty, MetaDataTags, Callback, State);
}
/// <summary>
/// Updates the meta-data about a thing in the Thing Registry. Only public things that have an owner can update its meta-data.
/// Things that do not have an owner should call <see cref="RegisterThing(string, string, string, MetaDataTag[], EventHandlerAsync{RegistrationEventArgs}, object)"/> to
/// update its meta-data in the Thing Registry.
///
/// Note: Meta information updated in this way will only overwrite tags provided in the request, and leave other tags previously
/// reported as is.
/// </summary>
/// <param name="NodeId">Node ID of thing, if behind a concentrator.</param>
/// <param name="SourceId">Source ID of thing, if behind a concentrator.</param>
/// <param name="Partition">Partition of thing, if behind a concentrator.</param>
/// <param name="MetaDataTags">Meta-data tags to register with the registry.</param>
/// <param name="Callback">Callback method.</param>
/// <param name="State">State object passed on to callback method.</param>
public Task UpdateThing(string NodeId, string SourceId, string Partition, MetaDataTag[] MetaDataTags,
EventHandlerAsync<UpdateEventArgs> Callback, object State)
{
return this.UpdateThing(NodeId, SourceId, Partition, string.Empty, MetaDataTags, Callback, State);
}
/// <summary>
/// Allows an owner to update the meta-data about one of its things in the Thing Registry.
///
/// Note: Meta information updated in this way will only overwrite tags provided in the request, and leave other tags previously
/// reported as is.
/// </summary>
/// <param name="NodeId">Node ID of thing, if behind a concentrator.</param>
/// <param name="SourceId">Source ID of thing, if behind a concentrator.</param>
/// <param name="Partition">Partition of thing, if behind a concentrator.</param>
/// <param name="ThingJid">JID of thing. Required if an owner wants to update the meta-data about one of its things. Leave empty,
/// if the thing wants to update its own meta-data.</param>
/// <param name="MetaDataTags">Meta-data tags to register with the registry.</param>
/// <param name="Callback">Callback method.</param>
/// <param name="State">State object passed on to callback method.</param>
public Task UpdateThing(string NodeId, string SourceId, string Partition, string ThingJid, MetaDataTag[] MetaDataTags,
EventHandlerAsync<UpdateEventArgs> Callback, object State)
{
StringBuilder Request = new StringBuilder();
Request.Append("<update xmlns='");
Request.Append(NamespaceDiscoveryCurrent);
if (!string.IsNullOrEmpty(ThingJid))
{
Request.Append("' jid='");
Request.Append(XML.Encode(ThingJid));
}
this.AddNodeInfo(Request, NodeId, SourceId, Partition);
Request.Append("'>");
string RegistryAddress = this.AddTags(Request, MetaDataTags, this.thingRegistryAddress);
Request.Append("</update>");
return this.client.SendIqSet(RegistryAddress, Request.ToString(), async (Sender, e) =>
{
XmlElement E = e.FirstElement;
bool Disowned = false;
if (e.Ok && !(E is null) && E.LocalName == "disowned")
{
Disowned = true;
if (string.IsNullOrEmpty(NodeId) && string.IsNullOrEmpty(SourceId) && string.IsNullOrEmpty(Partition) &&
this.client.TryGetExtension(out ProvisioningClient ProvisioningClient))
{
ProvisioningClient.OwnerJid = string.Empty;
}
}
await Callback.Raise(this, new UpdateEventArgs(e, State, Disowned));
}, State);
}
/// <summary>
/// Unregisters a thing from the thing registry.
/// </summary>
/// <param name="Callback">Callback method.</param>
/// <param name="State">State object passed on to callback method.</param>
public Task Unregister(EventHandlerAsync<IqResultEventArgs> Callback, object State)
{
return this.Unregister(string.Empty, string.Empty, string.Empty, Callback, State);
}
/// <summary>
/// Unregisters a thing from the thing registry.
/// </summary>
/// <param name="NodeId">Node ID of thing, if behind a concentrator.</param>
/// <param name="Callback">Callback method.</param>
/// <param name="State">State object passed on to callback method.</param>
public Task Unregister(string NodeId, EventHandlerAsync<IqResultEventArgs> Callback, object State)
{
return this.Unregister(NodeId, string.Empty, string.Empty, Callback, State);
}
/// <summary>
/// Unregisters a thing from the thing registry.
/// </summary>
/// <param name="NodeId">Node ID of thing, if behind a concentrator.</param>
/// <param name="SourceId">Source ID of thing, if behind a concentrator.</param>
/// <param name="Callback">Callback method.</param>
/// <param name="State">State object passed on to callback method.</param>
public Task Unregister(string NodeId, string SourceId, EventHandlerAsync<IqResultEventArgs> Callback, object State)
{
return this.Unregister(NodeId, SourceId, string.Empty, Callback, State);
}
/// <summary>
/// Unregisters a thing from the thing registry.
/// </summary>
/// <param name="NodeId">Node ID of thing, if behind a concentrator.</param>
/// <param name="SourceId">Source ID of thing, if behind a concentrator.</param>
/// <param name="Partition">Partition of thing, if behind a concentrator.</param>
/// <param name="Callback">Callback method.</param>
/// <param name="State">State object passed on to callback method.</param>
public Task Unregister(string NodeId, string SourceId, string Partition, EventHandlerAsync<IqResultEventArgs> Callback, object State)
{
return this.Unregister(this.thingRegistryAddress, NodeId, SourceId, Partition, Callback, State);
}
/// <summary>
/// Unregisters a thing from the thing registry.
/// </summary>
/// <param name="RegistryJid">JID of registry service.</param>
/// <param name="NodeId">Node ID of thing, if behind a concentrator.</param>
/// <param name="SourceId">Source ID of thing, if behind a concentrator.</param>
/// <param name="Partition">Partition of thing, if behind a concentrator.</param>
/// <param name="Callback">Callback method.</param>
/// <param name="State">State object passed on to callback method.</param>
public Task Unregister(string RegistryJid, string NodeId, string SourceId, string Partition, EventHandlerAsync<IqResultEventArgs> Callback, object State)
{
StringBuilder Request = new StringBuilder();
Request.Append("<unregister xmlns='");
Request.Append(NamespaceDiscoveryCurrent);
this.AddNodeInfo(Request, NodeId, SourceId, Partition);
Request.Append("'/>");
return this.client.SendIqSet(RegistryJid, Request.ToString(), (Sender, e) => Callback.Raise(this, e), State);
}
/// <summary>
/// Disowns a thing, so that it can be claimed by another.
/// </summary>
/// <param name="ThingJid">JID of thing to disown.</param>
/// <param name="Callback">Method to call when response is received.</param>
/// <param name="State">State object to pass on to the callback method.</param>
public Task Disown(string ThingJid, EventHandlerAsync<IqResultEventArgs> Callback, object State)
{
return this.Disown(ThingJid, string.Empty, string.Empty, string.Empty, Callback, State);
}
/// <summary>
/// Disowns a thing, so that it can be claimed by another.
/// </summary>
/// <param name="ThingJid">JID of thing to disown.</param>
/// <param name="NodeId">Optional Node ID of thing.</param>
/// <param name="Callback">Method to call when response is received.</param>
/// <param name="State">State object to pass on to the callback method.</param>
public Task Disown(string ThingJid, string NodeId, EventHandlerAsync<IqResultEventArgs> Callback, object State)
{
return this.Disown(ThingJid, NodeId, string.Empty, string.Empty, Callback, State);
}
/// <summary>
/// Disowns a thing, so that it can be claimed by another.
/// </summary>
/// <param name="ThingJid">JID of thing to disown.</param>
/// <param name="NodeId">Optional Node ID of thing.</param>
/// <param name="SourceId">Optional Source ID of thing.</param>
/// <param name="Callback">Method to call when response is received.</param>
/// <param name="State">State object to pass on to the callback method.</param>
public Task Disown(string ThingJid, string NodeId, string SourceId, EventHandlerAsync<IqResultEventArgs> Callback, object State)
{
return this.Disown(ThingJid, NodeId, SourceId, string.Empty, Callback, State);
}
/// <summary>
/// Disowns a thing, so that it can be claimed by another.
/// </summary>
/// <param name="ThingJid">JID of thing to disown.</param>
/// <param name="NodeId">Optional Node ID of thing.</param>
/// <param name="SourceId">Optional Source ID of thing.</param>
/// <param name="Partition">Optional Partition of thing.</param>
/// <param name="Callback">Method to call when response is received.</param>
/// <param name="State">State object to pass on to the callback method.</param>
public Task Disown(string ThingJid, string NodeId, string SourceId, string Partition, EventHandlerAsync<IqResultEventArgs> Callback, object State)
{
return this.Disown(this.thingRegistryAddress, ThingJid, NodeId, SourceId, Partition, Callback, State);
}
/// <summary>
/// Disowns a thing, so that it can be claimed by another.
/// </summary>
/// <param name="RegistryJid">JID of registry service.</param>
/// <param name="ThingJid">JID of thing to disown.</param>
/// <param name="NodeId">Optional Node ID of thing.</param>
/// <param name="SourceId">Optional Source ID of thing.</param>
/// <param name="Partition">Optional Partition of thing.</param>
/// <param name="Callback">Method to call when response is received.</param>
/// <param name="State">State object to pass on to the callback method.</param>
public Task Disown(string RegistryJid, string ThingJid, string NodeId, string SourceId, string Partition, EventHandlerAsync<IqResultEventArgs> Callback, object State)
{
StringBuilder Request = new StringBuilder();
Request.Append("<disown xmlns='");
Request.Append(NamespaceDiscoveryCurrent);
Request.Append("' jid='");
Request.Append(XML.Encode(ThingJid));
this.AddNodeInfo(Request, NodeId, SourceId, Partition);
Request.Append("'/>");
return this.client.SendIqSet(RegistryJid, Request.ToString(), (Sender, e) => Callback.Raise(this, e), State);
}
private async Task DisownedHandler(object Sender, IqEventArgs e)
{
XmlElement E = e.Query;
string NodeId = XML.Attribute(E, "id");
string SourceId = XML.Attribute(E, "src");
string Partition = XML.Attribute(E, "pt");
ThingReference Node;
if (string.IsNullOrEmpty(NodeId) && string.IsNullOrEmpty(SourceId) && string.IsNullOrEmpty(Partition))
{
Node = ThingReference.Empty;
if (this.client.TryGetExtension(out ProvisioningClient ProvisioningClient))
ProvisioningClient.OwnerJid = string.Empty;
}
else
Node = new ThingReference(NodeId, SourceId, Partition);
NodeEventArgs e2 = new NodeEventArgs(e, Node);
await this.Disowned.Raise(this, e2);
await e.IqResult(string.Empty);
}
/// <summary>
/// Event raised when a node has been disowned.
/// </summary>
public event EventHandlerAsync<NodeEventArgs> Disowned = null;
/// <summary>
/// Searches for publically available things in the thing registry.
/// </summary>
/// <param name="Offset">Search offset.</param>
/// <param name="MaxCount">Maximum number of things to return.</param>
/// <param name="SearchOperators">Search operators to use in search.</param>
/// <param name="Callback">Method to call when result has been received.</param>
/// <param name="State">State object to pass on to the callback method.</param>
public Task Search(int Offset, int MaxCount, SearchOperator[] SearchOperators,
EventHandlerAsync<SearchResultEventArgs> Callback, object State)
{
return this.Search(this.thingRegistryAddress, Offset, MaxCount, SearchOperators, Callback, State);
}
/// <summary>
/// Searches for publically available things in the thing registry.
/// </summary>
/// <param name="RegistryJid">JID of registry service.</param>
/// <param name="Offset">Search offset.</param>
/// <param name="MaxCount">Maximum number of things to return.</param>
/// <param name="SearchOperators">Search operators to use in search.</param>
/// <param name="Callback">Method to call when result has been received.</param>
/// <param name="State">State object to pass on to the callback method.</param>
public Task Search(string RegistryJid, int Offset, int MaxCount, SearchOperator[] SearchOperators,
EventHandlerAsync<SearchResultEventArgs> Callback, object State)
{
StringBuilder Request = new StringBuilder();
Request.Append("<search xmlns='");
Request.Append(NamespaceDiscoveryCurrent);
Request.Append("' offset='");
Request.Append(Offset.ToString());
Request.Append("' maxCount='");
Request.Append(MaxCount.ToString());
Request.Append("'>");
foreach (SearchOperator Operator in SearchOperators)
Operator.Serialize(Request);
Request.Append("</search>");
return this.client.SendIqGet(RegistryJid, Request.ToString(), (Sender, e) =>
{
return ParseResultSet(Offset, MaxCount, this, e, Callback, State);
}, State);
}
/// <summary>
/// Searches for publically available things in the thing registry.
/// </summary>
/// <param name="Offset">Search offset.</param>
/// <param name="MaxCount">Maximum number of things to return.</param>
/// <param name="SearchOperators">Search operators to use in search.</param>
/// <returns>Search result.</returns>
public Task<SearchResultEventArgs> SearchAsync(int Offset, int MaxCount, params SearchOperator[] SearchOperators)
{
return this.SearchAsync(this.thingRegistryAddress, Offset, MaxCount, SearchOperators);
}
/// <summary>
/// Searches for publically available things in the thing registry.
/// </summary>
/// <param name="RegistryJid">JID of registry service.</param>
/// <param name="Offset">Search offset.</param>
/// <param name="MaxCount">Maximum number of things to return.</param>
/// <param name="SearchOperators">Search operators to use in search.</param>
/// <returns>Search result.</returns>
public async Task<SearchResultEventArgs> SearchAsync(string RegistryJid, int Offset, int MaxCount,
params SearchOperator[] SearchOperators)
{
TaskCompletionSource<SearchResultEventArgs> Result = new TaskCompletionSource<SearchResultEventArgs>();
await this.Search(RegistryJid, Offset, MaxCount, SearchOperators, (_, e) =>
{
if (e.Ok)
Result.TrySetResult(e);
else
Result.TrySetException(e.StanzaError ?? new Exception("Unable to perform search operation."));
return Task.CompletedTask;
}, null);
return await Result.Task;
}
internal static async Task ParseResultSet(int Offset, int MaxCount, object Sender, IqResultEventArgs e, EventHandlerAsync<SearchResultEventArgs> Callback, object State)
{
List<SearchResultThing> Things = new List<SearchResultThing>();
List<MetaDataTag> MetaData = new List<MetaDataTag>();
ThingReference Node;
XmlElement E = e.FirstElement;
XmlElement E2, E3;
string Jid;
string NodeId;
string SourceId;
string Partition;
string Name;
bool More = false;
if (e.Ok && !(E is null) && E.LocalName == "found")
{
More = XML.Attribute(E, "more", false);
foreach (XmlNode N in E.ChildNodes)
{
E2 = N as XmlElement;
if (E2.LocalName == "thing")
{
Jid = XML.Attribute(E2, "jid");
NodeId = XML.Attribute(E2, "id");
SourceId = XML.Attribute(E2, "src");
Partition = XML.Attribute(E2, "pt");
if (string.IsNullOrEmpty(NodeId) && string.IsNullOrEmpty(SourceId) && string.IsNullOrEmpty(Partition))
Node = ThingReference.Empty;
else
Node = new ThingReference(NodeId, SourceId, Partition);
MetaData.Clear();
foreach (XmlNode N2 in E2.ChildNodes)
{
E3 = N2 as XmlElement;
if (E3 is null)
continue;
Name = XML.Attribute(E3, "name");
switch (E3.LocalName)
{
case "str":
MetaData.Add(new MetaDataStringTag(Name, XML.Attribute(E3, "value")));
break;
case "num":
MetaData.Add(new MetaDataNumericTag(Name, XML.Attribute(E3, "value", 0.0)));
break;
}
}
Things.Add(new SearchResultThing(Jid, Node, MetaData.ToArray()));
}
}
}
SearchResultEventArgs e2 = new SearchResultEventArgs(e, State, Offset, MaxCount, More, Things.ToArray());
await Callback.Raise(Sender, e2);
}
/// <summary>
/// Generates an IOTDISCO URI from the meta-data provided in <paramref name="MetaData"/>.
///
/// For more information about the IOTDISCO URI scheme, see: http://www.iana.org/assignments/uri-schemes/prov/iotdisco.pdf
/// </summary>
/// <param name="MetaData">Meta-data to encode.</param>
/// <returns>IOTDISCO URI encoding the meta-data.</returns>
public string EncodeAsIoTDiscoURI(params MetaDataTag[] MetaData)
{
StringBuilder Result = new StringBuilder("iotdisco:");
bool First = true;
bool ContainsR = false;
foreach (MetaDataTag Tag in MetaData)
{
if (First)
First = false;
else
Result.Append(';');
if (Tag is MetaDataNumericTag)
Result.Append('#');
Result.Append(Uri.EscapeDataString(Tag.Name));
Result.Append('=');
Result.Append(Uri.EscapeDataString(Tag.StringValue));
if (!ContainsR && Tag.Name == "R")
ContainsR = true;
}
if (!ContainsR)
{
if (!First)
Result.Append(';');
Result.Append("R=");
Result.Append(Uri.EscapeDataString(this.thingRegistryAddress));
}