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;
7
9{
14 {
19 : base()
20 {
21 }
22
27 : base(e)
28 {
29 this.NodeId = e.NodeId;
30 this.PartitionId = e.Partition;
31 this.SourceId = e.SourceId;
32 this.ServiceTokens = Convert(e.ServiceTokens, TokenType.Service);
33 this.DeviceTokens = Convert(e.DeviceTokens, TokenType.Device);
34 this.UserTokens = Convert(e.UserTokens, TokenType.User);
35 this.Category = ContactInfo.GetThingNotificationCategoryKey(this.BareJid, this.NodeId, this.SourceId, this.PartitionId);
36 }
37
38 private static ProvisioningToken[]? Convert(string[] Tokens, TokenType Type)
39 {
40 if (Tokens is null)
41 return null;
42
43 int i, c = Tokens.Length;
44 ProvisioningToken[] Result = new ProvisioningToken[c];
45
46 for (i = 0; i < c; i++)
47 Result[i] = new ProvisioningToken(Tokens[i], Type);
48
49 return Result;
50 }
51
55 [DefaultValueStringEmpty]
56 public string? NodeId { get; set; }
57
61 [DefaultValueStringEmpty]
62 public string? SourceId { get; set; }
63
67 [DefaultValueStringEmpty]
68 public string? PartitionId { get; set; }
69
73 [DefaultValueNull]
74 public ProvisioningToken[]? UserTokens { get; set; }
75
79 [DefaultValueNull]
80 public ProvisioningToken[]? ServiceTokens { get; set; }
81
85 [DefaultValueNull]
86 public ProvisioningToken[]? DeviceTokens { get; set; }
87
91 public override async Task Prepare()
92 {
93 await base.Prepare();
94
95 await this.Prepare(this.UserTokens);
96 await this.Prepare(this.DeviceTokens);
97 await this.Prepare(this.ServiceTokens);
98 }
99
100 private async Task Prepare(ProvisioningToken[]? Tokens)
101 {
102 if (Tokens is null)
103 return;
104
105 bool Updated = false;
106
107 foreach (ProvisioningToken Token in Tokens)
108 {
109 if (!string.IsNullOrEmpty(Token.FriendlyName))
110 continue;
111
112 lock (certificatesyByToken)
113 {
114 if (certificatesyByToken.TryGetValue(Token.Token, out X509Certificate2? Certificate))
115 {
116 Token.FriendlyName = Certificate.FriendlyName;
117 Token.Certificate = Certificate.RawData;
118 Updated = true;
119 continue;
120 }
121 }
122
123 ServiceRef.XmppService.GetCertificate(Token.Token, this.CertificateResponse, Token);
124 }
125
126 if (Updated)
127 await Database.Update(this);
128 }
129
130 private static readonly Dictionary<string, X509Certificate2> certificatesyByToken = [];
131
132 private async Task CertificateResponse(object? Sender, CertificateEventArgs e)
133 {
134 if (e.Ok && e.State is ProvisioningToken Token)
135 {
136 lock (certificatesyByToken)
137 {
138 certificatesyByToken[Token.Token] = e.Certificate;
139 }
140
141 Token.FriendlyName = e.Certificate.FriendlyName;
142 Token.Certificate = e.Certificate.RawData;
143
144 await Database.Update(this);
145 }
146 }
147 }
148}
Contains information about a contact.
Definition: ContactInfo.cs:22
static string GetThingNotificationCategoryKey(string BareJid, string? NodeId, string? SourceId, string? Partition)
Category key used for thing notifications.
Definition: ContactInfo.cs:711
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:43
static IXmppService XmppService
The XMPP service for XMPP communication.
Definition: ServiceRef.cs:190
bool Ok
If the response is an OK result response (true), or an error response (false).
object State
State object passed to the original request.
X509Certificate2 Certificate
Certificate corresponding to the given token.
Abstract base class containing event arguments for node question events.
string[] ServiceTokens
Any service tokens available in the request.
string[] UserTokens
And user tokens available in the request.
string[] DeviceTokens
And device tokens available in the request.
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