forked from OPCFoundation/UA-CloudDashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UAPubSubMessageProcessor.cs
345 lines (308 loc) · 16.5 KB
/
UAPubSubMessageProcessor.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
namespace Opc.Ua.Cloud.Dashboard
{
using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json;
using Opc.Ua;
using Opc.Ua.PubSub;
using Opc.Ua.PubSub.Encoding;
using Opc.Ua.PubSub.PublishedData;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using UACloudDashboard.Interfaces;
public class UAPubSubMessageProcessor : IMessageProcessor
{
private StatusHubClient _hubClient;
private Dictionary<string, DataSetReaderDataType> _dataSetReaders;
public UAPubSubMessageProcessor(IHubContext<StatusHub> hubContext)
{
_hubClient = new StatusHubClient(hubContext);
_dataSetReaders = new Dictionary<string, DataSetReaderDataType>();
// add default dataset readers
AddUadpDataSetReader("default_uadp", 0, new DataSetMetaDataType());
AddJsonDataSetReader("default_json", 0, new DataSetMetaDataType());
}
public void Clear()
{
lock (_hubClient.TableEntries)
{
_hubClient.TableEntries.Clear();
}
}
public void ProcessMessage(byte[] payload, DateTime receivedTime, string contentType)
{
string message = string.Empty;
try
{
message = Encoding.UTF8.GetString(payload);
if (message != null)
{
if (((contentType != null) && (contentType == "application/json")) || message.TrimStart().StartsWith('{') || message.TrimStart().StartsWith('['))
{
if (message.TrimStart().StartsWith('['))
{
// we received an array of messages
object[] messageArray = JsonConvert.DeserializeObject<object[]>(message);
foreach (object singleMessage in messageArray)
{
DecodeMessage(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(singleMessage)), receivedTime, new JsonNetworkMessage());
}
}
else
{
DecodeMessage(payload, receivedTime, new JsonNetworkMessage());
}
}
else
{
DecodeMessage(payload, receivedTime, new UadpNetworkMessage());
}
}
}
catch (Exception ex)
{
Trace.TraceError($"Exception {ex.Message} processing message {message}");
}
}
private void AddUadpDataSetReader(string publisherId, ushort dataSetWriterId, DataSetMetaDataType metadata)
{
DataSetReaderDataType uadpDataSetReader = new DataSetReaderDataType();
uadpDataSetReader.Name = publisherId + ":" + dataSetWriterId.ToString();
uadpDataSetReader.DataSetWriterId = dataSetWriterId;
uadpDataSetReader.PublisherId = publisherId;
uadpDataSetReader.Enabled = true;
uadpDataSetReader.DataSetFieldContentMask = (uint)DataSetFieldContentMask.None;
uadpDataSetReader.KeyFrameCount = 1;
uadpDataSetReader.TransportSettings = new ExtensionObject(new BrokerDataSetReaderTransportDataType());
uadpDataSetReader.DataSetMetaData = metadata;
UadpDataSetReaderMessageDataType uadpDataSetReaderMessageSettings = new UadpDataSetReaderMessageDataType()
{
NetworkMessageContentMask = (uint)(UadpNetworkMessageContentMask.NetworkMessageNumber | UadpNetworkMessageContentMask.PublisherId | UadpNetworkMessageContentMask.DataSetClassId),
DataSetMessageContentMask = (uint)UadpDataSetMessageContentMask.None,
};
uadpDataSetReader.MessageSettings = new ExtensionObject(uadpDataSetReaderMessageSettings);
TargetVariablesDataType subscribedDataSet = new TargetVariablesDataType();
subscribedDataSet.TargetVariables = new FieldTargetDataTypeCollection();
uadpDataSetReader.SubscribedDataSet = new ExtensionObject(subscribedDataSet);
if (_dataSetReaders.ContainsKey(uadpDataSetReader.Name))
{
_dataSetReaders[uadpDataSetReader.Name] = uadpDataSetReader;
}
else
{
_dataSetReaders.Add(uadpDataSetReader.Name, uadpDataSetReader);
}
}
private void AddJsonDataSetReader(string publisherId, ushort dataSetWriterId, DataSetMetaDataType metadata)
{
DataSetReaderDataType jsonDataSetReader = new DataSetReaderDataType();
jsonDataSetReader.Name = publisherId + ":" + dataSetWriterId.ToString();
jsonDataSetReader.PublisherId = publisherId;
jsonDataSetReader.DataSetWriterId = dataSetWriterId;
jsonDataSetReader.Enabled = true;
jsonDataSetReader.DataSetFieldContentMask = (uint)DataSetFieldContentMask.None;
jsonDataSetReader.KeyFrameCount = 1;
jsonDataSetReader.TransportSettings = new ExtensionObject(new BrokerDataSetReaderTransportDataType());
jsonDataSetReader.DataSetMetaData = metadata;
JsonDataSetReaderMessageDataType jsonDataSetReaderMessageSettings = new JsonDataSetReaderMessageDataType()
{
NetworkMessageContentMask = (uint)(JsonNetworkMessageContentMask.NetworkMessageHeader | JsonNetworkMessageContentMask.DataSetMessageHeader | JsonNetworkMessageContentMask.DataSetClassId | JsonNetworkMessageContentMask.PublisherId),
DataSetMessageContentMask = (uint)JsonDataSetMessageContentMask.None,
};
jsonDataSetReader.MessageSettings = new ExtensionObject(jsonDataSetReaderMessageSettings);
TargetVariablesDataType subscribedDataSet = new TargetVariablesDataType();
subscribedDataSet.TargetVariables = new FieldTargetDataTypeCollection();
jsonDataSetReader.SubscribedDataSet = new ExtensionObject(subscribedDataSet);
if (_dataSetReaders.ContainsKey(jsonDataSetReader.Name))
{
_dataSetReaders[jsonDataSetReader.Name] = jsonDataSetReader;
}
else
{
_dataSetReaders.Add(jsonDataSetReader.Name, jsonDataSetReader);
}
}
private void DecodeMessage(byte[] payload, DateTime receivedTime, UaNetworkMessage encodedMessage)
{
encodedMessage.Decode(ServiceMessageContext.GlobalContext, payload, null);
if (encodedMessage.IsMetaDataMessage)
{
// setup dataset reader
if (encodedMessage is JsonNetworkMessage)
{
JsonNetworkMessage jsonMessage = (JsonNetworkMessage)encodedMessage;
AddJsonDataSetReader(jsonMessage.PublisherId, jsonMessage.DataSetWriterId, encodedMessage.DataSetMetaData);
}
else
{
UadpNetworkMessage uadpMessage = (UadpNetworkMessage)encodedMessage;
AddUadpDataSetReader(uadpMessage.PublisherId.ToString(), uadpMessage.DataSetWriterId, encodedMessage.DataSetMetaData);
}
}
else
{
encodedMessage.Decode(ServiceMessageContext.GlobalContext, payload, _dataSetReaders.Values.ToArray());
// reset metadata fields on default dataset readers
_dataSetReaders["default_uadp:0"].DataSetMetaData.Fields.Clear();
_dataSetReaders["default_json:0"].DataSetMetaData.Fields.Clear();
string publisherID = string.Empty;
if (encodedMessage is JsonNetworkMessage)
{
publisherID = ((JsonNetworkMessage)encodedMessage).PublisherId?.ToString();
}
else
{
publisherID = ((UadpNetworkMessage)encodedMessage).PublisherId?.ToString();
}
Dictionary<string, DataValue> flattenedPublishedNodes = new();
foreach (UaDataSetMessage datasetmessage in encodedMessage.DataSetMessages)
{
string dataSetWriterId = datasetmessage.DataSetWriterId.ToString();
string assetName = string.Empty;
if (_dataSetReaders.ContainsKey(publisherID + ":" + dataSetWriterId))
{
string name = _dataSetReaders[publisherID + ":" + dataSetWriterId].DataSetMetaData.Name;
assetName = name.Substring(0, name.LastIndexOf(';'));
}
else
{
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("IGNORE_MISSING_METADATA")))
{
// if we didn't reveice a valid asset name, we use the publisher ID instead, if configured by the user
assetName = publisherID;
}
else
{
Console.WriteLine($"No metadata message for {publisherID}:{dataSetWriterId} received yet!");
continue;
}
}
if (datasetmessage.DataSet != null)
{
for (int i = 0; i < datasetmessage.DataSet.Fields.Length; i++)
{
Field field = datasetmessage.DataSet.Fields[i];
if (field.Value != null)
{
// if the timestamp in the field is missing, use the timestamp from the dataset message instead
if (field.Value.SourceTimestamp == DateTime.MinValue)
{
field.Value.SourceTimestamp = datasetmessage.Timestamp;
}
// if we didn't receive valid metadata, we use the dataset writer ID and index into the dataset instead
string telemetryName = string.Empty;
if (field.FieldMetaData == null || string.IsNullOrEmpty(field.FieldMetaData.Name))
{
telemetryName = assetName + "_" + datasetmessage.DataSetWriterId.ToString() + "_" + i.ToString();
}
else
{
telemetryName = assetName + "_" + field.FieldMetaData.Name + "_" + field.FieldMetaData.BinaryEncodingId.ToString();
}
try
{
// check for variant array
if (field.Value.Value is Variant[])
{
// convert to string
DataValue value = new DataValue(new Variant(field.Value.ToString()), field.Value.StatusCode, field.Value.SourceTimestamp);
flattenedPublishedNodes.Add(telemetryName, value);
}
else
{
flattenedPublishedNodes.Add(telemetryName, field.Value);
}
}
catch (Exception ex)
{
Console.WriteLine($"Cannot parse field {field.Value}: {ex.Message}");
}
}
}
}
}
SendPublishedNodestoSignalRHub(flattenedPublishedNodes, receivedTime);
}
}
private void SendPublishedNodestoSignalRHub(Dictionary<string, DataValue> publishedNodes, DateTime enqueueTime)
{
Dictionary<string, string> displayNameMap = new Dictionary<string, string>(); // TODO: Add display name substitudes here!
foreach (string nodeId in publishedNodes.Keys)
{
// substitude the node Id with a custom display name, if available
string displayName = nodeId;
try
{
if (displayNameMap.Count > 0)
{
displayName = displayNameMap[nodeId];
}
}
catch
{
// keep the original node ID as the display name
}
if (publishedNodes[nodeId] != null)
{
if (publishedNodes[nodeId].SourceTimestamp == DateTime.MinValue)
{
// use the enqueued time if the OPC UA timestamp is not present
publishedNodes[nodeId].SourceTimestamp = enqueueTime;
}
try
{
string timeStamp = publishedNodes[nodeId].SourceTimestamp.ToString();
if (publishedNodes[nodeId].Value != null)
{
string value = publishedNodes[nodeId].Value.ToString();
lock (_hubClient.TableEntries)
{
if (_hubClient.TableEntries.ContainsKey(displayName))
{
_hubClient.TableEntries[displayName] = new Tuple<string, string>(value, timeStamp);
}
else
{
_hubClient.TableEntries.Add(displayName, new Tuple<string, string>(value, timeStamp));
}
float floatValue;
if (float.TryParse(value, out floatValue))
{
// create a keys array as index from our display names
List<string> keys = new List<string>();
foreach (string displayNameAsKey in _hubClient.TableEntries.Keys)
{
keys.Add(displayNameAsKey);
}
// check if we have to create an initially blank entry first
if (!_hubClient.ChartEntries.ContainsKey(timeStamp) || (keys.Count != _hubClient.ChartEntries[timeStamp].Length))
{
string[] blankValues = new string[_hubClient.TableEntries.Count];
for (int i = 0; i < blankValues.Length; i++)
{
blankValues[i] = "NaN";
}
if (_hubClient.ChartEntries.ContainsKey(timeStamp))
{
_hubClient.ChartEntries.Remove(timeStamp);
}
_hubClient.ChartEntries.Add(timeStamp, blankValues);
}
_hubClient.ChartEntries[timeStamp][keys.IndexOf(displayName)] = floatValue.ToString();
}
}
}
}
catch (Exception ex)
{
// ignore this item
Trace.TraceInformation($"Cannot add item {nodeId}: {ex.Message}");
}
}
}
}
}
}