forked from barnstee/UA-CloudPublisher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MQTTClient.cs
363 lines (315 loc) · 17.1 KB
/
MQTTClient.cs
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
namespace Opc.Ua.Cloud.Publisher.Configuration
{
using Microsoft.Extensions.Logging;
using MQTTnet;
using MQTTnet.Adapter;
using MQTTnet.Client;
using MQTTnet.Packets;
using MQTTnet.Protocol;
using Newtonsoft.Json;
using Opc.Ua.Cloud.Dashboard;
using Opc.Ua.Cloud.Publisher.Interfaces;
using Opc.Ua.Cloud.Publisher.Models;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
public class MQTTClient : IBrokerClient
{
private IMqttClient _client = null;
private bool _isAltBroker = false;
private readonly ILogger _logger;
private readonly ICommandProcessor _commandProcessor;
private readonly IUAApplication _uAApplication;
private CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
public MQTTClient(ILoggerFactory loggerFactory, ICommandProcessor commandProcessor, IUAApplication uAApplication)
{
_logger = loggerFactory.CreateLogger("MQTTClient");
_commandProcessor = commandProcessor;
_uAApplication = uAApplication;
}
public class MqttClientCertificatesProvider : IMqttClientCertificatesProvider
{
private readonly IUAApplication _uAApplication;
public MqttClientCertificatesProvider(IUAApplication uAApplication)
{
_uAApplication = uAApplication;
}
X509CertificateCollection IMqttClientCertificatesProvider.GetCertificates()
{
X509Certificate2 appCert = null;
if (Settings.Instance.UseCustomCertAuth)
{
string filePath = Path.Combine(Directory.GetCurrentDirectory(), "customclientcert");
appCert = new X509Certificate2(filePath);
}
else
{
appCert = _uAApplication.UAApplicationInstance.ApplicationConfiguration.SecurityConfiguration.ApplicationCertificate.Certificate;
}
if (appCert == null)
{
throw new Exception($"Cannot access OPC UA application certificate!");
}
return new X509CertificateCollection() { appCert };
}
}
public void Connect(bool altBroker = false)
{
_isAltBroker = altBroker;
try
{
// disconnect if still connected
if ((_client != null) && _client.IsConnected)
{
_client.DisconnectAsync().GetAwaiter().GetResult();
_cancellationTokenSource.Cancel();
Diagnostics.Singleton.Info.ConnectedToBroker = false;
}
// read our settings
string brokerUrl = altBroker ? Settings.Instance.AltBrokerUrl : Settings.Instance.BrokerUrl;
uint brokerPort = altBroker ? Settings.Instance.AltBrokerPort : Settings.Instance.BrokerPort;
string publisherName = Settings.Instance.PublisherName;
string username = altBroker ? Settings.Instance.AltBrokerUsername : Settings.Instance.BrokerUsername;
string password = altBroker ? Settings.Instance.AltBrokerPassword : Settings.Instance.BrokerPassword;
string receiveTopic = altBroker ? Settings.Instance.BrokerDataReceivedTopic : Settings.Instance.BrokerCommandTopic;
if (string.IsNullOrEmpty(brokerUrl))
{
// no broker URL configured = nothing to connect to!
_logger.LogError("Broker URL not configured. Cannot connect to broker!");
return;
}
// create MQTT password
if (Settings.Instance.CreateBrokerSASToken)
{
// create SAS token as password
TimeSpan sinceEpoch = DateTime.UtcNow - new DateTime(1970, 1, 1);
int week = 60 * 60 * 24 * 7;
string expiry = Convert.ToString((int)sinceEpoch.TotalSeconds + week);
string stringToSign = HttpUtility.UrlEncode(brokerUrl + "/devices/" + publisherName) + "\n" + expiry;
HMACSHA256 hmac = new HMACSHA256(Convert.FromBase64String(password));
string signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
password = "SharedAccessSignature sr=" + HttpUtility.UrlEncode(brokerUrl + "/devices/" + publisherName) + "&sig=" + HttpUtility.UrlEncode(signature) + "&se=" + expiry;
}
// create MQTT client
_client = new MqttFactory().CreateMqttClient();
_client.ApplicationMessageReceivedAsync += msg => HandleMessageAsync(msg);
MqttClientOptionsBuilder clientOptions = new MqttClientOptionsBuilder()
.WithTcpServer(brokerUrl, (int?)brokerPort)
.WithClientId(publisherName)
.WithTlsOptions(new MqttClientTlsOptions { UseTls = Settings.Instance.UseTLS })
.WithProtocolVersion(MQTTnet.Formatter.MqttProtocolVersion.V311)
.WithTimeout(TimeSpan.FromSeconds(10))
.WithKeepAlivePeriod(TimeSpan.FromSeconds(100))
.WithCleanSession(true) // clear existing subscriptions
.WithCredentials(username, password);
if (brokerPort == 443)
{
clientOptions = new MqttClientOptionsBuilder()
.WithWebSocketServer(o => o.WithUri(brokerUrl))
.WithClientId(publisherName)
.WithTlsOptions(new MqttClientTlsOptions { UseTls = Settings.Instance.UseTLS })
.WithProtocolVersion(MQTTnet.Formatter.MqttProtocolVersion.V311)
.WithTimeout(TimeSpan.FromSeconds(10))
.WithKeepAlivePeriod(TimeSpan.FromSeconds(100))
.WithCleanSession(true) // clear existing subscriptions
.WithCredentials(username, password);
}
if (Settings.Instance.UseUACertAuth || Settings.Instance.UseCustomCertAuth)
{
clientOptions = new MqttClientOptionsBuilder()
.WithTcpServer(brokerUrl)
.WithClientId(publisherName)
.WithTlsOptions(new MqttClientTlsOptions
{
UseTls = true,
AllowUntrustedCertificates = true,
IgnoreCertificateChainErrors = true,
ClientCertificatesProvider = new MqttClientCertificatesProvider(_uAApplication)
})
.WithProtocolVersion(MQTTnet.Formatter.MqttProtocolVersion.V500)
.WithTimeout(TimeSpan.FromSeconds(10))
.WithKeepAlivePeriod(TimeSpan.FromSeconds(100))
.WithCleanSession(true) // clear existing subscriptions
.WithCredentials(publisherName, string.Empty);
}
// setup disconnection handling
_client.DisconnectedAsync += disconnectArgs =>
{
_logger.LogWarning($"Disconnected from MQTT broker: {disconnectArgs.Reason}");
// wait a 5 seconds, then simply reconnect again, if needed
Task.Delay(TimeSpan.FromSeconds(5)).GetAwaiter().GetResult();
MqttClientConnectResult connectResult = _client.ConnectAsync(clientOptions.Build(), _cancellationTokenSource.Token).GetAwaiter().GetResult();
if (connectResult.ResultCode != MqttClientConnectResultCode.Success)
{
string status = GetStatus(connectResult.UserProperties)?.ToString("x4");
throw new Exception($"Connection to MQTT broker failed. Status: {connectResult.ResultCode}; status: {status}");
}
return Task.CompletedTask;
};
try
{
_cancellationTokenSource.Dispose();
_cancellationTokenSource = new CancellationTokenSource();
MqttClientConnectResult connectResult = _client.ConnectAsync(clientOptions.Build(), _cancellationTokenSource.Token).GetAwaiter().GetResult();
if (connectResult.ResultCode != MqttClientConnectResultCode.Success)
{
string status = GetStatus(connectResult.UserProperties)?.ToString("x4");
throw new Exception($"Connection to MQTT broker failed. Status: {connectResult.ResultCode}; status: {status}");
}
if (!string.IsNullOrEmpty(receiveTopic))
{
MqttClientSubscribeResult subscribeResult = _client.SubscribeAsync(
new MqttTopicFilter
{
Topic = receiveTopic,
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
}).GetAwaiter().GetResult();
// make sure subscriptions were successful
if (subscribeResult.Items.Count != 1 || subscribeResult.Items.ElementAt(0).ResultCode != MqttClientSubscribeResultCode.GrantedQoS0)
{
throw new ApplicationException("Failed to subscribe");
}
}
Diagnostics.Singleton.Info.ConnectedToBroker = true;
_logger.LogInformation("Connected to MQTT broker.");
}
catch (MqttConnectingFailedException ex)
{
_logger.LogCritical($"Failed to connect with reason {ex.ResultCode} and message: {ex.Message}");
if (ex.Result?.UserProperties != null)
{
foreach (var prop in ex.Result.UserProperties)
{
_logger.LogCritical($"{prop.Name}: {prop.Value}");
}
}
}
}
catch (Exception ex)
{
_logger.LogCritical("Failed to connect to MQTT broker: " + ex.Message);
}
}
public void Publish(byte[] payload)
{
MqttApplicationMessage message = new MqttApplicationMessageBuilder()
.WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce)
.WithTopic(Settings.Instance.BrokerMessageTopic)
.WithPayload(payload)
.Build();
_client.PublishAsync(message, _cancellationTokenSource.Token).GetAwaiter().GetResult();
}
public void PublishMetadata(byte[] payload)
{
MqttApplicationMessage message = new MqttApplicationMessageBuilder()
.WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce)
.WithTopic(Settings.Instance.BrokerMetadataTopic)
.WithPayload(payload)
.Build();
_client.PublishAsync(message, _cancellationTokenSource.Token).GetAwaiter().GetResult();
}
private MqttApplicationMessage BuildResponse(string status, string id, byte[] payload)
{
string responseTopic = Settings.Instance.BrokerResponseTopic;
return new MqttApplicationMessageBuilder()
.WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce)
.WithTopic($"{responseTopic}/{status}/{id}")
.WithPayload(payload)
.Build();
}
// parses status from packet properties
private int? GetStatus(List<MqttUserProperty> properties)
{
var status = properties.FirstOrDefault(up => up.Name == "status");
if (status == null)
{
return null;
}
return int.Parse(status.Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
}
// handles all incoming messages
private async Task HandleMessageAsync(MqttApplicationMessageReceivedEventArgs args)
{
string requestID = string.Empty;
ResponseModel response = new()
{
TimeStamp = DateTime.UtcNow,
};
try
{
// check if this should be a data message (and we are the alternative broker) or a command
if (_isAltBroker)
{
new UAPubSubBinaryMessageDecoder(_uAApplication, new LoggerFactory()).ProcessMessage(args.ApplicationMessage.PayloadSegment.ToArray(), DateTime.UtcNow, args.ApplicationMessage.ContentType);
}
else
{
_logger.LogInformation($"Received cloud command with topic: {args.ApplicationMessage.Topic} and payload: {args.ApplicationMessage.ConvertPayloadToString()}");
string requestTopic = Settings.Instance.BrokerCommandTopic;
requestID = args.ApplicationMessage.Topic.Substring(args.ApplicationMessage.Topic.IndexOf("?"));
string requestPayload = args.ApplicationMessage.ConvertPayloadToString();
// parse the message
RequestModel request = JsonConvert.DeserializeObject<RequestModel>(requestPayload);
// discard messages that are older than 15 seconds
if (request.TimeStamp < DateTime.UtcNow.AddSeconds(-15))
{
_logger.LogInformation($"Discarding old message with timestamp {request.TimeStamp}");
return;
}
response.CorrelationId = request.CorrelationId;
// route this to the right handler
if (args.ApplicationMessage.Topic.StartsWith(requestTopic.TrimEnd('#') + "PublishNodes"))
{
response.Status = Encoding.UTF8.GetString(_commandProcessor.PublishNodes(requestPayload));
response.Success = true;
}
else if (args.ApplicationMessage.Topic.StartsWith(requestTopic.TrimEnd('#') + "UnpublishNodes"))
{
response.Status = Encoding.UTF8.GetString(_commandProcessor.UnpublishNodes(requestPayload));
response.Success = true;
}
else if (args.ApplicationMessage.Topic.StartsWith(requestTopic.TrimEnd('#') + "UnpublishAllNodes"))
{
response.Status = Encoding.UTF8.GetString(_commandProcessor.UnpublishAllNodes());
response.Success = true;
}
else if (args.ApplicationMessage.Topic.StartsWith(requestTopic.TrimEnd('#') + "GetPublishedNodes"))
{
response.Status = Encoding.UTF8.GetString(_commandProcessor.GetPublishedNodes());
response.Success = true;
}
else if (args.ApplicationMessage.Topic.StartsWith(requestTopic.TrimEnd('#') + "GetInfo"))
{
response.Status = Encoding.UTF8.GetString(_commandProcessor.GetInfo());
response.Success = true;
}
else
{
_logger.LogError("Unknown command received: " + args.ApplicationMessage.Topic);
response.Status = "Unkown command " + args.ApplicationMessage.Topic;
response.Success = false;
}
// send reponse to MQTT broker
await _client.PublishAsync(BuildResponse("200", requestID, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(response))), _cancellationTokenSource.Token).ConfigureAwait(false);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "HandleMessageAsync");
response.Status = ex.Message;
response.Success = false;
// send error to MQTT broker
await _client.PublishAsync(BuildResponse("500", requestID, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(response))), _cancellationTokenSource.Token).ConfigureAwait(false);
}
}
}
}