forked from OPCFoundation/UA-.NETStandard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReferenceServerUtils.cs
537 lines (478 loc) · 18 KB
/
ReferenceServerUtils.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
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
/* ========================================================================
* Copyright (c) 2005-2020 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/
using Opc.Ua;
using Opc.Ua.Server;
using System;
using System.Collections.Generic;
using System.Data;
namespace Quickstarts.ReferenceServer
{
/// <summary>
/// The interface that a server exposes to objects that it contains.
/// </summary>
public static class ServerUtils
{
private enum EventType
{
WriteValue,
CreateItem,
ModifyItem,
QueueValue,
FilterValue,
DiscardValue,
PublishValue
}
private class Event
{
public DateTime Timestamp;
public EventType EventType;
public NodeId NodeId;
public uint ServerHandle;
public DataValue Value;
public MonitoringParameters Parameters;
public MonitoringMode MonitoringMode;
}
private static Queue<Event> m_events = new Queue<Event>();
private static bool m_eventsEnabled;
/// <summary>
/// Whether event queuing is enabled.
/// </summary>
public static bool EventsEnabled
{
get { return m_eventsEnabled; }
set
{
if (m_eventsEnabled != value)
{
if (!value)
{
lock (m_events)
{
m_events.Clear();
}
}
}
m_eventsEnabled = value;
}
}
/// <summary>
/// Empties the event queue and saves it in the dataset.
/// </summary>
public static DataSet EmptyQueue(DataSet dataset)
{
if (dataset == null)
{
dataset = new DataSet();
dataset.Tables.Add("MonitoredItems");
dataset.Tables[0].Columns.Add("Id", typeof(uint));
dataset.Tables[0].Columns.Add("Timestamp", typeof(string));
dataset.Tables[0].Columns.Add("EventType", typeof(string));
dataset.Tables[0].Columns.Add("NodeId", typeof(NodeId));
dataset.Tables[0].Columns.Add("MonitoringMode", typeof(MonitoringMode));
dataset.Tables[0].Columns.Add("SamplingInterval", typeof(double));
dataset.Tables[0].Columns.Add("QueueSize", typeof(uint));
dataset.Tables[0].Columns.Add("DiscardOldest", typeof(bool));
dataset.Tables[0].Columns.Add("Filter", typeof(string));
dataset.Tables[0].Columns.Add("Value", typeof(Variant));
dataset.Tables[0].Columns.Add("StatusCode", typeof(StatusCode));
dataset.Tables[0].Columns.Add("SourceTimestamp", typeof(string));
dataset.Tables[0].Columns.Add("ServerTimestamp", typeof(string));
dataset.Tables[0].DefaultView.Sort = "Timestamp";
}
lock (m_events)
{
while (m_events.Count > 0)
{
Event e = m_events.Dequeue();
DataRow row = dataset.Tables[0].NewRow();
row[0] = e.ServerHandle;
row[1] = e.Timestamp.ToLocalTime().ToString("HH:mm:ss.ffffff");
row[2] = e.EventType.ToString();
row[3] = e.NodeId;
if (e.Parameters != null)
{
row[4] = e.MonitoringMode;
row[5] = e.Parameters.SamplingInterval;
row[6] = e.Parameters.QueueSize;
row[7] = e.Parameters.DiscardOldest;
if (e.Parameters.Filter != null)
{
row[8] = e.Parameters.Filter.ToString();
}
}
if (e.Value != null)
{
row[9] = e.Value.WrappedValue;
row[10] = e.Value.StatusCode;
row[11] = e.Value.ServerTimestamp.ToLocalTime().ToString("HH:mm:ss.fff");
row[12] = e.Value.ServerTimestamp.ToLocalTime().ToString("HH:mm:ss.fff");
}
dataset.Tables[0].Rows.Add(row);
}
}
dataset.AcceptChanges();
return dataset;
}
/// <summary>
/// Reports a value written.
/// </summary>
public static void ReportWriteValue(NodeId nodeId, DataValue value, StatusCode error)
{
if (!m_eventsEnabled)
{
return;
}
lock (m_events)
{
Event e = new Event();
e.EventType = EventType.WriteValue;
e.NodeId = nodeId;
e.ServerHandle = 0;
e.Timestamp = HiResClock.UtcNow;
e.Value = value;
e.Parameters = null;
e.MonitoringMode = MonitoringMode.Disabled;
if (StatusCode.IsBad(error))
{
e.Value = new DataValue(error);
e.Value.WrappedValue = value.WrappedValue;
}
m_events.Enqueue(e);
}
}
/// <summary>
/// Reports a value queued.
/// </summary>
public static void ReportQueuedValue(NodeId nodeId, uint serverHandle, DataValue value)
{
if (!m_eventsEnabled)
{
return;
}
lock (m_events)
{
Event e = new Event();
e.EventType = EventType.QueueValue;
e.NodeId = nodeId;
e.ServerHandle = serverHandle;
e.Timestamp = HiResClock.UtcNow;
e.Value = value;
e.Parameters = null;
e.MonitoringMode = MonitoringMode.Disabled;
m_events.Enqueue(e);
}
}
/// <summary>
/// Reports a value excluded by the filter.
/// </summary>
public static void ReportFilteredValue(NodeId nodeId, uint serverHandle, DataValue value)
{
if (!m_eventsEnabled)
{
return;
}
lock (m_events)
{
Event e = new Event();
e.EventType = EventType.FilterValue;
e.NodeId = nodeId;
e.ServerHandle = serverHandle;
e.Timestamp = HiResClock.UtcNow;
e.Value = value;
e.Parameters = null;
e.MonitoringMode = MonitoringMode.Disabled;
m_events.Enqueue(e);
}
}
/// <summary>
/// Reports a value discarded because of queue overflow.
/// </summary>
public static void ReportDiscardedValue(NodeId nodeId, uint serverHandle, DataValue value)
{
if (!m_eventsEnabled)
{
return;
}
lock (m_events)
{
Event e = new Event();
e.EventType = EventType.DiscardValue;
e.NodeId = nodeId;
e.ServerHandle = serverHandle;
e.Timestamp = HiResClock.UtcNow;
e.Value = value;
e.Parameters = null;
e.MonitoringMode = MonitoringMode.Disabled;
m_events.Enqueue(e);
}
}
/// <summary>
/// Reports a value published.
/// </summary>
public static void ReportPublishValue(NodeId nodeId, uint serverHandle, DataValue value)
{
if (!m_eventsEnabled)
{
return;
}
lock (m_events)
{
Event e = new Event();
e.EventType = EventType.PublishValue;
e.NodeId = nodeId;
e.ServerHandle = serverHandle;
e.Timestamp = HiResClock.UtcNow;
e.Value = value;
e.Parameters = null;
e.MonitoringMode = MonitoringMode.Disabled;
m_events.Enqueue(e);
}
}
/// <summary>
/// Reports a new monitored item.
/// </summary>
public static void ReportCreateMonitoredItem(
NodeId nodeId,
uint serverHandle,
double samplingInterval,
uint queueSize,
bool discardOldest,
MonitoringFilter filter,
MonitoringMode monitoringMode)
{
if (!m_eventsEnabled)
{
return;
}
lock (m_events)
{
Event e = new Event();
e.EventType = EventType.CreateItem;
e.NodeId = nodeId;
e.ServerHandle = serverHandle;
e.Timestamp = HiResClock.UtcNow;
e.Value = null;
e.Parameters = new MonitoringParameters();
e.Parameters.SamplingInterval = samplingInterval;
e.Parameters.QueueSize = queueSize;
e.Parameters.DiscardOldest = discardOldest;
e.Parameters.Filter = new ExtensionObject(filter);
e.MonitoringMode = monitoringMode;
m_events.Enqueue(e);
}
}
/// <summary>
/// Reports a modified monitored item.
/// </summary>
public static void ReportModifyMonitoredItem(
NodeId nodeId,
uint serverHandle,
double samplingInterval,
uint queueSize,
bool discardOldest,
MonitoringFilter filter,
MonitoringMode monitoringMode)
{
if (!m_eventsEnabled)
{
return;
}
lock (m_events)
{
Event e = new Event();
e.EventType = EventType.ModifyItem;
e.NodeId = nodeId;
e.ServerHandle = serverHandle;
e.Timestamp = HiResClock.UtcNow;
e.Value = null;
e.Parameters = new MonitoringParameters();
e.Parameters.SamplingInterval = samplingInterval;
e.Parameters.QueueSize = queueSize;
e.Parameters.DiscardOldest = discardOldest;
e.Parameters.Filter = new ExtensionObject(filter);
e.MonitoringMode = monitoringMode;
m_events.Enqueue(e);
}
}
#region Error and Diagnostics
/// <summary>
/// Fills in the diagnostic information after an error.
/// </summary>
public static uint CreateError(
uint code,
OperationContext context,
DiagnosticInfoCollection diagnosticInfos,
int index)
{
ServiceResult error = new ServiceResult(code);
if ((context.DiagnosticsMask & DiagnosticsMasks.OperationAll) != 0)
{
diagnosticInfos[index] = new DiagnosticInfo(error, context.DiagnosticsMask, false, context.StringTable);
}
return error.Code;
}
/// <summary>
/// Fills in the diagnostic information after an error.
/// </summary>
public static bool CreateError(
uint code,
StatusCodeCollection results,
DiagnosticInfoCollection diagnosticInfos,
OperationContext context)
{
ServiceResult error = new ServiceResult(code);
results.Add(error.Code);
if ((context.DiagnosticsMask & DiagnosticsMasks.OperationAll) != 0)
{
diagnosticInfos.Add(new DiagnosticInfo(error, context.DiagnosticsMask, false, context.StringTable));
return true;
}
return false;
}
/// <summary>
/// Fills in the diagnostic information after an error.
/// </summary>
public static bool CreateError(
uint code,
StatusCodeCollection results,
DiagnosticInfoCollection diagnosticInfos,
int index,
OperationContext context)
{
ServiceResult error = new ServiceResult(code);
results[index] = error.Code;
if ((context.DiagnosticsMask & DiagnosticsMasks.OperationAll) != 0)
{
diagnosticInfos[index] = new DiagnosticInfo(error, context.DiagnosticsMask, false, context.StringTable);
return true;
}
return false;
}
/// <summary>
/// Creates a place holder in the lists for the results.
/// </summary>
public static void CreateSuccess(
StatusCodeCollection results,
DiagnosticInfoCollection diagnosticInfos,
OperationContext context)
{
results.Add(StatusCodes.Good);
if ((context.DiagnosticsMask & DiagnosticsMasks.OperationAll) != 0)
{
diagnosticInfos.Add(null);
}
}
/// <summary>
/// Creates a collection of diagnostics from a set of errors.
/// </summary>
public static DiagnosticInfoCollection CreateDiagnosticInfoCollection(
OperationContext context,
IList<ServiceResult> errors)
{
// all done if no diagnostics requested.
if ((context.DiagnosticsMask & DiagnosticsMasks.OperationAll) == 0)
{
return null;
}
// create diagnostics.
DiagnosticInfoCollection results = new DiagnosticInfoCollection(errors.Count);
foreach (ServiceResult error in errors)
{
if (ServiceResult.IsBad(error))
{
results.Add(new DiagnosticInfo(error, context.DiagnosticsMask, false, context.StringTable));
}
else
{
results.Add(null);
}
}
return results;
}
/// <summary>
/// Creates a collection of status codes and diagnostics from a set of errors.
/// </summary>
public static StatusCodeCollection CreateStatusCodeCollection(
OperationContext context,
IList<ServiceResult> errors,
out DiagnosticInfoCollection diagnosticInfos)
{
diagnosticInfos = null;
bool noErrors = true;
StatusCodeCollection results = new StatusCodeCollection(errors.Count);
foreach (ServiceResult error in errors)
{
if (ServiceResult.IsBad(error))
{
results.Add(error.Code);
noErrors = false;
}
else
{
results.Add(StatusCodes.Good);
}
}
// only generate diagnostics if errors exist.
if (noErrors)
{
diagnosticInfos = CreateDiagnosticInfoCollection(context, errors);
}
return results;
}
/// <summary>
/// Creates the diagnostic info and translates any strings.
/// </summary>
/// <param name="server">The server.</param>
/// <param name="context">The context containing the string stable.</param>
/// <param name="error">The error to translate.</param>
/// <returns>The diagnostics with references to the strings in the context string table.</returns>
public static DiagnosticInfo CreateDiagnosticInfo(
IServerInternal server,
OperationContext context,
ServiceResult error)
{
if (error == null)
{
return null;
}
ServiceResult translatedError = error;
if ((context.DiagnosticsMask & DiagnosticsMasks.LocalizedText) != 0)
{
translatedError = server.ResourceManager.Translate(context.PreferredLocales, error);
}
DiagnosticInfo diagnosticInfo = new DiagnosticInfo(
translatedError,
context.DiagnosticsMask,
false,
context.StringTable);
return diagnosticInfo;
}
#endregion
}
}