Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ThingNotificationEvent.cs
1using System.Security.Cryptography.X509Certificates;
6
8{
13 {
18 : base()
19 {
20 }
21
25 public ThingNotificationEvent(NodeQuestionEventArgs e)
26 : base(e)
27 {
28 this.NodeId = e.NodeId;
29 this.PartitionId = e.Partition;
30 this.SourceId = e.SourceId;
31 this.ServiceTokens = Convert(e.ServiceTokens, TokenType.Service);
32 this.DeviceTokens = Convert(e.DeviceTokens, TokenType.Device);
33 this.UserTokens = Convert(e.UserTokens, TokenType.User);
34 this.Category = ContactInfo.GetThingNotificationCategoryKey(this.BareJid, this.NodeId, this.SourceId, this.PartitionId);
35 }
36
37 private static ProvisioningToken[]? Convert(string[] Tokens, TokenType Type)
38 {
39 if (Tokens is null)
40 return null;
41
42 int i, c = Tokens.Length;
43 ProvisioningToken[] Result = new ProvisioningToken[c];
44
45 for (i = 0; i < c; i++)
46 Result[i] = new ProvisioningToken(Tokens[i], Type);
47
48 return Result;
49 }
50
54 [DefaultValueStringEmpty]
55 public string? NodeId { get; set; }
56
60 [DefaultValueStringEmpty]
61 public string? SourceId { get; set; }
62
66 [DefaultValueStringEmpty]
67 public string? PartitionId { get; set; }
68
72 [DefaultValueNull]
73 public ProvisioningToken[]? UserTokens { get; set; }
74
78 [DefaultValueNull]
79 public ProvisioningToken[]? ServiceTokens { get; set; }
80
84 [DefaultValueNull]
85 public ProvisioningToken[]? DeviceTokens { get; set; }
86
90 public override async Task Prepare()
91 {
92 await base.Prepare();
93
94 await this.Prepare(this.UserTokens);
95 await this.Prepare(this.DeviceTokens);
96 await this.Prepare(this.ServiceTokens);
97 }
98
99 private async Task Prepare(ProvisioningToken[]? Tokens)
100 {
101 if (Tokens is null)
102 return;
103
104 bool Updated = false;
105
106 foreach (ProvisioningToken Token in Tokens)
107 {
108 if (!string.IsNullOrEmpty(Token.FriendlyName))
109 continue;
110
111 lock (certificatesyByToken)
112 {
113 if (certificatesyByToken.TryGetValue(Token.Token, out X509Certificate2? Certificate))
114 {
115 Token.FriendlyName = Certificate.FriendlyName;
116 Token.Certificate = Certificate.RawData;
117 Updated = true;
118 continue;
119 }
120 }
121
122 ServiceRef.XmppService.GetCertificate(Token.Token, this.CertificateResponse, Token);
123 }
124
125 if (Updated)
126 await Database.Update(this);
127 }
128
129 private static readonly Dictionary<string, X509Certificate2> certificatesyByToken = [];
130
131 private async Task CertificateResponse(object? Sender, CertificateEventArgs e)
132 {
133 if (e.Ok && e.State is ProvisioningToken Token)
134 {
135 lock (certificatesyByToken)
136 {
137 certificatesyByToken[Token.Token] = e.Certificate;
138 }
139
140 Token.FriendlyName = e.Certificate.FriendlyName;
141 Token.Certificate = e.Certificate.RawData;
142
143 await Database.Update(this);
144 }
145 }
146 }
147}
Contains information about a contact.
Definition: ContactInfo.cs:21
static string GetThingNotificationCategoryKey(string BareJid, string? NodeId, string? SourceId, string? Partition)
Category key used for thing notifications.
Definition: ContactInfo.cs:710
ThingNotificationEvent(NodeQuestionEventArgs e)
Abstract base class of thing notification events.
ThingNotificationEvent()
Abstract base class of thing notification events.
override async Task Prepare()
Performs perparatory tasks, that will simplify opening the notification.
Base class that references services in the app.
Definition: ServiceRef.cs:31
static IXmppService XmppService
The XMPP service for XMPP communication.
Definition: ServiceRef.cs:67
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