-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathOpcUaDriver.cs
More file actions
1146 lines (974 loc) · 37.2 KB
/
Copy pathOpcUaDriver.cs
File metadata and controls
1146 lines (974 loc) · 37.2 KB
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2026 Phoenix Contact GmbH & Co. KG
// Licensed under the Apache License, Version 2.0
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Runtime.Serialization;
using Microsoft.Extensions.Logging;
using Moryx.AbstractionLayer.Drivers;
using Moryx.AbstractionLayer.Drivers.InOut;
using Moryx.AbstractionLayer.Drivers.Message;
using Moryx.AbstractionLayer.Resources;
using Moryx.Configuration;
using Moryx.Drivers.OpcUa.Factories;
using Moryx.Drivers.OpcUa.Properties;
using Moryx.Drivers.OpcUa.States;
using Moryx.Serialization;
using Moryx.StateMachines;
using Moryx.Threading;
using Opc.Ua;
using Opc.Ua.Client;
namespace Moryx.Drivers.OpcUa;
/// <summary>
/// Driver to communicate via Opc Ua . It is able to write and read nodes
/// and subscribe to value changes of nodes in a session
/// </summary>
[ResourceRegistration]
[Display(Name = nameof(Strings.OpcUaDriver_DisplayName), Description = nameof(Strings.OpcUaDriver_Description), ResourceType = typeof(Strings))]
public class OpcUaDriver : Driver, IOpcUaDriver
{
private const int NodeLayersShown = 5;
/// <summary>
/// Current tate of the driver
/// </summary>
[EntrySerialize]
[Display(Name = nameof(Strings.OpcUaDriver_StateName), ResourceType = typeof(Strings))]
internal string StateName => CurrentState?.ToString() ?? "";
[EntrySerialize, ReadOnly(true)]
[Display(Name = nameof(Strings.OpcUaDriver_ServerStatus), ResourceType = typeof(Strings))]
internal ServerState ServerStatus { get; private set; }
[EntrySerialize, ReadOnly(true)]
[Display(Name = nameof(Strings.OpcUaDriver_DeviceSet), Description = nameof(Strings.OpcUaDriver_DeviceSet_Description), ResourceType = typeof(Strings))]
internal List<DeviceType> DeviceSet { get; set; } = [];
#region Configuration
/// <summary>
/// List of default subscriptions
/// </summary>
[EntrySerialize, DataMember]
[Display(Name = nameof(Strings.OpcUaDriver_DefaultSubscriptions), ResourceType = typeof(Strings))]
public List<string> DefaultSubscriptions { get; set; } = [];
[DataMember]
internal Dictionary<string, string> _nodeIdAliasDictionary; // TODO: Internal field just for tests, could be private
[EntrySerialize]
[Display(Name = nameof(Strings.OpcUaDriver_NodeIdAlias), ResourceType = typeof(Strings))]
internal List<NodeIdAlias> NodeIdAlias
{
get
{
if (_nodeIdAliasDictionary == null)
{
_nodeIdAliasDictionary = [];
return [];
}
return [.. _nodeIdAliasDictionary.Select(x => new NodeIdAlias { Alias = x.Key, NodeId = x.Value })];
}
set
{
if (value != null)
{
_nodeIdAliasDictionary = value.ToDictionary(x => x.Alias, x => x.NodeId);
}
else
{
_nodeIdAliasDictionary = [];
}
}
}
/// <summary>
/// Identifier of the driver
/// </summary>
[EntrySerialize, DataMember]
[Display(Name = nameof(Strings.OpcUaDriver_Identifier), ResourceType = typeof(Strings))]
public string Identifier { get; set; }
/// <summary>
/// Url of the OPC UA Server
/// </summary>
[EntrySerialize, DataMember]
[Display(Name = nameof(Strings.OpcUaDriver_OpcUaServerUrl), Description = nameof(Strings.OpcUaDriver_OpcUaServerUrl_Description), ResourceType = typeof(Strings))]
public string OpcUaServerUrl { get; set; }
/// <summary>
/// Username needed to authenticate on the server
/// </summary>
[EntrySerialize, DataMember]
[Display(Name = nameof(Strings.OpcUaDriver_Username), ResourceType = typeof(Strings))]
public string Username { get; set; }
/// <summary>
/// Password needed to authenticate on the server
/// </summary>
[EntrySerialize, DataMember, Password]
[Display(Name = nameof(Strings.OpcUaDriver_Password), ResourceType = typeof(Strings))]
public string Password { get; set; }
/// <summary>
/// Use encryption during communication
/// </summary>
[EntrySerialize, DataMember]
[Display(Name = nameof(Strings.OpcUaDriver_UseEncryption), Description = nameof(Strings.OpcUaDriver_UseEncryption_Description), ResourceType = typeof(Strings))]
public bool UseEncryption { get; set; }
/// <summary>
/// Path of the config file
/// </summary>
[EntrySerialize, DataMember]
[Display(Name = nameof(Strings.OpcUaDriver_FilePathClientConfig), Description = nameof(Strings.OpcUaDriver_FilePathClientConfig_Description), ResourceType = typeof(Strings))]
public string FilePathClientConfig { get; set; }
/// <summary>
/// Reconnection Period
/// </summary>
[EntrySerialize, DataMember]
[Display(Name = nameof(Strings.OpcUaDriver_ReconnectionPeriod), Description = nameof(Strings.OpcUaDriver_ReconnectionPeriod_Description), ResourceType = typeof(Strings))]
public int ReconnectionPeriod { get; set; }
// TODO: Update Publishing- and SamplingInterval without restarting the driver
/// <summary>
/// Interval, how often the server publishes notifications to the driver
/// </summary>
[EntrySerialize, DataMember]
[Display(Name = nameof(Strings.OpcUaDriver_PublishingInterval), Description = nameof(Strings.OpcUaDriver_PublishingInterval_Description), ResourceType = typeof(Strings))]
public int PublishingInterval { get; set; }
/// <summary>
/// Interval on which the changes of the monitored values are checked
/// </summary>
[EntrySerialize, DataMember]
[Display(Name = nameof(Strings.OpcUaDriver_SamplingInterval), Description = nameof(Strings.OpcUaDriver_SamplingInterval_Description), ResourceType = typeof(Strings))]
public int SamplingInterval { get; set; }
//TODO: Use selfsigned certificates for communication
#endregion
/// <summary>
/// All nodes found on the Opc Ua server
/// </summary>
[EntrySerialize, ReadOnly(true)]
[Display(Name = nameof(Strings.OpcUaDriver_Nodes), ResourceType = typeof(Strings))]
internal List<OpcUaDisplayNode> Nodes { get; private set; } = [];
/// <summary>
/// Timer used in message queue
/// </summary>
public IParallelOperations ParallelOperations { get; set; }
/// <summary>
/// The number of nodes under the driver
/// </summary>
public bool HasChannels => _nodesFlat.Count > 0;
private DriverOpcUaState State => (DriverOpcUaState)CurrentState;
/// <inheritdoc />
public IInput Input { get; set; }
/// <inheritdoc />
public IOutput Output { get; set; }
/// <inheritdoc />
public IDriver Driver => this;
private List<OpcUaNode> _nodes = [];
private readonly Dictionary<string, OpcUaNode> _nodesFlat = [];
private List<OpcUaNode> _nodesToBeSubscribed = [];
private readonly HashSet<string> _savedIds = [];
internal ISession _session; //TODO: Internal field just for tests
private SessionReconnectHandler _reconnectHandler;
private readonly Lock _lock = new();
private readonly Lock _stateLock = new();
private Subscription _subscription;
//TODO: Internal property just for tests, use xml also in tests
internal ApplicationConfigurationFactory ApplicationConfigurationFactory { get; set; } = new();
internal SubscriptionFactory SubscriptionFactory { get; set; } = new();
/// <summary>
/// Convert an OpcUaNode to an entity to be shown on the UI
/// </summary>
/// <param name="nodes"></param>
/// <returns></returns>
private static List<OpcUaDisplayNode> ConvertToDisplayNodes(List<OpcUaNode> nodes)
{
var list = new List<OpcUaDisplayNode>();
foreach (var node in nodes)
{
OpcUaDisplayNode displayNode;
if (node.NodeClass == NodeClass.Object || node.NodeClass == NodeClass.Variable)
{
var objectDisplayNode = new OpcUaObjectDisplayNode(node.NodeId)
{
Nodes = ConvertToDisplayNodes(node.Nodes)
};
displayNode = objectDisplayNode;
}
else
{
displayNode = new OpcUaDisplayNode(node.NodeId);
}
displayNode.BrowseName = node.BrowseName.ToString();
displayNode.DisplayName = node.DisplayName;
displayNode.Description = node.Description;
displayNode.ClassType = node.NodeClass;
list.Add(displayNode);
}
return list;
}
#region Lifecycle
/// <inheritdoc/>
protected override async Task OnInitializeAsync(CancellationToken cancellationToken)
{
await base.OnInitializeAsync(cancellationToken);
Input = new OpcUaInput(this);
Output = new OpcUaOutput(this);
_nodeIdAliasDictionary ??= [];
StateMachine.ForContext(this).With<DriverOpcUaState>();
ServerStatus = ServerState.Unknown;
}
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is None.</param>
/// <inheritdoc/>
protected override async Task OnStartAsync(CancellationToken cancellationToken)
{
await base.OnStartAsync(cancellationToken);
ApplicationConfigurationFactory.ApplicationName += " " + Identifier;
Connect();
}
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is None.</param>
/// <inheritdoc/>
protected override Task OnStopAsync(CancellationToken cancellationToken)
{
State.Disconnect();
return Task.CompletedTask;
}
#endregion
#region Connection Handling
private void Connect()
{
lock (_stateLock)
{
State.Connect();
}
}
/// <summary>
/// Try to connect to the Opc Ua server
/// </summary>
/// <exception cref="Exception"></exception>
internal async Task TryConnect(bool firstTry)
{
if (_session == null)
{
var result = await CreateSession(firstTry);
if (result == false)
{
return;
}
}
_session.KeepAlive += ClientKeepAlive;
lock (_stateLock)
{
State.OnConnectingCompletedAsync(true).GetAwaiter().GetResult();
}
}
private async Task<bool> CreateSession(bool firstTry)
{
var config = await ApplicationConfigurationFactory.Create(Logger, FilePathClientConfig);
if (config == null)
{
return false;
}
UriBuilder builder = null;
EndpointDescription selectedEndpoint;
try
{
builder = new UriBuilder(OpcUaServerUrl);
builder.Scheme = BuildScheme(builder);
selectedEndpoint = await CoreClientUtils.SelectEndpointAsync(config,
builder.Uri.ToString(), UseEncryption);
}
catch (Exception e)
{
if (firstTry)
{
Logger.Log(LogLevel.Error, "Failed to connect {Uri} ({Message})", builder?.Uri.ToString() ?? OpcUaServerUrl, e.Message);
}
ParallelOperations?.ScheduleExecution(TryToConnectAgain, ReconnectionPeriod, -1);
return false;
}
var endpointConfiguration = EndpointConfiguration.Create(config);
var endpoint = new ConfiguredEndpoint(null, selectedEndpoint, endpointConfiguration);
var userIdentity = new UserIdentity(Username, Password);
if (string.IsNullOrEmpty(Username))
{
userIdentity = null;
}
try
{
_session = await Session.CreateAsync(DefaultSessionFactory.Instance, config, (ITransportWaitingConnection)null, endpoint, false, false, ApplicationConfigurationFactory.ApplicationName, 60000, userIdentity, null);
}
catch (Exception ex)
{
if (firstTry)
{
Logger.Log(LogLevel.Error, "{Message}", ex.Message);
}
ParallelOperations.ScheduleExecution(TryToConnectAgain, ReconnectionPeriod, -1);
return false;
}
return true;
}
private static string BuildScheme(UriBuilder builder)
{
return IsOpcScheme(builder.Scheme) ? builder.Scheme : "opc.tcp";
}
private static bool IsOpcScheme(string scheme) => !string.IsNullOrEmpty(scheme) && scheme.Contains("opc");
private void TryToConnectAgain()
{
State.OnConnectingCompletedAsync(false);
}
private void ClientKeepAlive(ISession session, KeepAliveEventArgs e)
{
// check for events from discarded sessions.
if (!ReferenceEquals(session, _session))
{
return;
}
// start reconnect sequence on communication error.
if (ServiceResult.IsBad(e.Status))
{
ServerStatus = ServerState.Unknown;
State.OnConnectionLostAsync(e).GetAwaiter().GetResult();
}
}
internal async Task Reconnect(KeepAliveEventArgs e)
{
if (ReconnectionPeriod <= 0)
{
Logger.Log(LogLevel.Warning, "KeepAlive status {Status}, but reconnect is disabled.", e.Status);
return;
}
lock (_lock)
{
if (_reconnectHandler == null)
{
_reconnectHandler = new SessionReconnectHandler(true);
_reconnectHandler.BeginReconnect(_session, ReconnectionPeriod, ReconnectComplete);
}
else
{
Logger.Log(LogLevel.Warning, "KeepAlive status {Status}, but reconnection should have already started.", e.Status);
return;
}
}
}
/// <summary>
/// Called when the reconnect attempt was successful.
/// </summary>
private void ReconnectComplete(object sender, EventArgs e)
{
// ignore callbacks from discarded objects.
if (!ReferenceEquals(sender, _reconnectHandler))
{
return;
}
lock (_lock)
{
// if session recovered, Session property is null
if (_reconnectHandler.Session != null)
{
_session = _reconnectHandler.Session as Session;
}
_reconnectHandler.Dispose();
_reconnectHandler = null;
lock (_stateLock)
{
State.OnConnectingCompletedAsync(true).GetAwaiter().GetResult();
}
}
}
/// <summary>
/// Disconnect from the OPC UA server
/// </summary>
internal void Disconnect()
{
RemoveSubscription();
if (_session == null)
{
return;
}
_session.KeepAlive -= ClientKeepAlive;
_session?.CloseAsync().GetAwaiter().GetResult();
_session = null;
}
#endregion
/// <inheritdoc />
public IMessageChannel Channel(string identifier)
{
return State.GetNode(identifier);
}
/// <inheritdoc/>
public Task<OpcUaNode> GetNodeAsync(string nodeId, CancellationToken cancellationToken = default)
{
var expandedNodeId = OpcUaNode.CreateExpandedNodeId(GetNodeIdAsString(nodeId));
if (!_nodesFlat.TryGetValue(expandedNodeId, out var node))
{
return null;
}
return Task.FromResult(node);
}
private string GetNodeIdAsString(string identifier)
{
if (_nodeIdAliasDictionary.TryGetValue(identifier, out var nodeId))
{
return nodeId;
}
return identifier;
}
internal OpcUaNode GetNotInitializedNode(string identifier)
{
var nodeId = ExpandedNodeId.Parse(GetNodeIdAsString(identifier));
if (nodeId.NamespaceUri == null || nodeId.NamespaceUri.Equals(""))
{
return null;
}
var node = new OpcUaNode(this, Logger, identifier);
_nodesFlat.Add(node.Identifier, node);
return node;
}
/// <inheritdoc/>
public Task AddSubscriptionAsync(OpcUaNode node, CancellationToken cancellationToken = default)
{
State.AddSubscription(node);
return Task.CompletedTask;
}
internal void SaveSubscriptionToBeAdded(OpcUaNode node)
{
var duplicateNode = _nodesToBeSubscribed.FirstOrDefault(x => x.NodeId.ToString().Equals(node.NodeId.ToString()));
if (duplicateNode == null)
{
_nodesToBeSubscribed.Add(node);
}
}
internal void RemoveSubscription()
{
var subscribedNodes = _nodesFlat.Select(x => x.Value).Where(x => x.MonitoredItem != null).ToList();
foreach (var node in subscribedNodes)
{
if (node.MonitoredItem == null)
{
continue;
}
node.MonitoredItem.Notification -= OnMonitoredItemNotification;
node.MonitoredItem = null;
_nodesToBeSubscribed.Add(node);
}
_subscription?.Dispose();
_subscription = null;
}
internal async Task SubscribeSavedNodesAsync(CancellationToken cancellationToken = default)
{
_subscription = SubscriptionFactory.CreateSubscription(new Subscription(_session.DefaultSubscription)
{
PublishingEnabled = true,
PublishingInterval = PublishingInterval,
LifetimeCount = 0,
});
_session.AddSubscription(_subscription);
// Create the subscription on Server side
await _subscription.CreateAsync(cancellationToken);
//Subscribe Saved Nodes
foreach (var node in _nodesToBeSubscribed)
{
if (node.NodeClass != NodeClass.Variable)
{
Logger.Log(LogLevel.Warning, "It was tried to subscribe to the node {NodeId}. But that node is no variable node", node.NodeId);
continue;
}
var monitoredItem = CreateMonitoredItem(node);
if (monitoredItem == null)
{
continue;
}
_subscription.AddItem(monitoredItem);
}
//Subscribe default Nodes
foreach (var nodeId in DefaultSubscriptions)
{
var node = State.GetNode(nodeId);
if (node == null)
{
Logger.Log(LogLevel.Warning, "Node with the id {nodeId} was not found", nodeId);
continue;
}
if (_nodesToBeSubscribed.Contains(node))
{
continue;
}
if (node.NodeClass != NodeClass.Variable)
{
Logger.Log(LogLevel.Warning, "It was tried to subscribe to the node {nodeId}. But that node is no variable node", node.NodeId);
continue;
}
var monitoredItem = CreateMonitoredItem(node);
if (monitoredItem == null)
{
continue;
}
_subscription.AddItem(monitoredItem);
}
_nodesToBeSubscribed = [];
await _subscription.ApplyChangesAsync(cancellationToken);
await State.OnSubscriptionsInitializedAsync();
}
internal Task AddSubscriptionToSession(OpcUaNode node, CancellationToken cancellationToken = default)
{
if (node.NodeClass != NodeClass.Variable)
{
Logger.Log(LogLevel.Warning, "It was tried to subscribe to the node {NodeId}. But that node is no variable node", node.NodeId);
return Task.CompletedTask;
}
var monitoredItem = CreateMonitoredItem(node);
if (monitoredItem == null)
{
return Task.CompletedTask;
}
_subscription.AddItem(monitoredItem);
return _subscription.ApplyChangesAsync(cancellationToken);
}
private MonitoredItem CreateMonitoredItem(OpcUaNode node)
{
if (node.MonitoredItem != null)
{
return null;
}
var monitoredItem = new MonitoredItem(_subscription.DefaultItem)
{
StartNodeId = ExpandedNodeId.ToNodeId(node.NodeId, _session.NamespaceUris),
AttributeId = Attributes.Value,
DisplayName = node.DisplayName,
SamplingInterval = SamplingInterval,
QueueSize = ComputeQueueSize(PublishingInterval, SamplingInterval),
DiscardOldest = true
};
monitoredItem.Notification += OnMonitoredItemNotification;
node.MonitoredItem = monitoredItem;
return monitoredItem;
}
private uint ComputeQueueSize(int publishingInterval, int samplingInterval)
{
samplingInterval = samplingInterval > 0
? SamplingInterval
: 1;
return (uint)(publishingInterval / samplingInterval + 10);
}
private void OnMonitoredItemNotification(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
{
var nodeId = new ExpandedNodeId(monitoredItem.ResolvedNodeId,
_session.NamespaceUris.GetString(monitoredItem.ResolvedNodeId.NamespaceIndex));
var receivedObject = ((MonitoredItemNotification)e.NotificationValue).Value.Value;
OnSubscriptionChanged(nodeId, receivedObject);
}
internal void OnSubscriptionChanged(ExpandedNodeId nodeId, object value)
{
var nodeIdString = nodeId.ToString();
var msg = new OpcUaMessage()
{
Identifier = nodeIdString,
Payload = value
};
if (nodeId.IdType == IdType.Numeric && int.Parse(nodeId.Identifier.ToString(), CultureInfo.InvariantCulture) == Variables.Server_ServerStatus
&& nodeId.NamespaceIndex == 0)
{
ServerStatus = ((ServerStatusDataType)((ExtensionObject)value).Body).State;
}
var node = State.GetNode(nodeIdString);
if (node != null && node.Subscribed)
{
node.ReceivedMessage(value);
}
Received?.Invoke(this, msg);
}
#region Browse Nodes
internal async Task BrowseNodesAsync()
{
var namespaceUris = _session.NamespaceUris;
var nodes = new List<OpcUaNode>();
await BrowseNodesAsync(ObjectIds.RootFolder, namespaceUris, nodes, 0);
_nodes = nodes;
_savedIds.Clear();
Nodes = ConvertToDisplayNodes(_nodes);
lock (_stateLock)
{
State.OnBrowsingNodesCompletedAsync();
}
}
//todo: Change to BFS
private async Task BrowseNodesAsync(NodeId nodeId, NamespaceTable namespaceTable, List<OpcUaNode> list, int layer, HashSet<string> visitedNodes = null, uint referenceTypes = ReferenceTypes.HierarchicalReferences)
{
visitedNodes ??= [];
var branchNodes = new HashSet<string>(visitedNodes);
if (branchNodes.Contains(nodeId.ToString()))
{
return;
}
branchNodes.Add(nodeId.ToString());
IList<ReferenceDescriptionCollection> nextRefs;
ByteStringCollection continuationPoints;
(_, continuationPoints, nextRefs, _) = await _session.BrowseAsync(null, null, [nodeId], uint.MaxValue, BrowseDirection.Forward, new NodeId(referenceTypes), true, (uint)NodeClass.Variable | (uint)NodeClass.Object | (uint)NodeClass.Method);
var continuationPoint = continuationPoints?.FirstOrDefault();
//https://reference.opcfoundation.org/Core/Part4/v104/docs/7.6
while (continuationPoint != null)
{
IList<ReferenceDescriptionCollection> ref3;
(_, continuationPoints, ref3, _) = await _session.BrowseNextAsync(null, [continuationPoint], false);
var newContinuationPoint = continuationPoints?.FirstOrDefault();
if (ref3.Count < 1)
{
if (newContinuationPoint == null)
{
break;
}
continue;
}
foreach (var z in ref3[0])
{
nextRefs[0].Add(z);
}
continuationPoint = newContinuationPoint;
}
if (nextRefs == null)
{
return;
}
foreach (var nextRd in nextRefs[0])
{
var nextRdNodeId = OpcUaNode.CreateExpandedNodeId(nextRd.NodeId.ToString());
OpcUaNode node = null;
if (_nodesFlat.TryGetValue(nextRdNodeId, out var exisitingNode))
{
node = exisitingNode;
}
if (node == null)
{
node = ConvertToNode(nextRd, namespaceTable);
}
else
{
node.UpdateNodeId(namespaceTable);
node.DisplayName = nextRd.DisplayName.ToString();
node.NodeClass = nextRd.NodeClass;
node.BrowseName = nextRd.BrowseName;
}
if (node == null)
{
continue;
}
if (nextRd.NodeClass == NodeClass.Object || nextRd.NodeClass == NodeClass.Variable)
{
var types = nextRd.NodeClass == NodeClass.Object
? ReferenceTypes.HierarchicalReferences
: ReferenceTypes.HasComponent;
var nodesOfObject = new List<OpcUaNode>();
await BrowseNodesAsync(ExpandedNodeId.ToNodeId(nextRd.NodeId, namespaceTable), namespaceTable, nodesOfObject, layer + 1, branchNodes, types);
node.Nodes = nodesOfObject;
}
_savedIds.Add(node.Identifier);
_nodesFlat.TryAdd(node.Identifier, node);
if (layer < NodeLayersShown)
{
list.Add(node);
}
}
}
private OpcUaNode ConvertToNode(ReferenceDescription referenceDescription, NamespaceTable namespaceTable)
{
var node = new OpcUaNode(this, Logger, referenceDescription.NodeId, namespaceTable)
{
DisplayName = referenceDescription.DisplayName.ToString(),
BrowseName = referenceDescription.BrowseName
};
switch (referenceDescription.NodeClass)
{
case NodeClass.Object:
node.NodeClass = NodeClass.Object;
break;
case NodeClass.Method:
node.NodeClass = NodeClass.Method;
break;
case NodeClass.Variable:
node.NodeClass = NodeClass.Variable;
break;
default: return null;
}
return node;
}
#endregion
#region Read and write nodes
internal Task WriteNode(OpcUaNode node, object payload, CancellationToken cancellationToken = default)
{
return State.WriteNodeAsync(node, payload, cancellationToken);
}
/// <inheritdoc/>
public void WriteNode(string nodeId, object payload)
{
var node = State.GetNode(nodeId);
WriteNode(node, payload).GetAwaiter().GetResult();
}
/// <inheritdoc />
public Task WriteNodeAsync(string nodeId, object payload, CancellationToken cancellationToken = default)
{
var node = State.GetNode(nodeId);
return State.WriteNodeAsync(node, payload, cancellationToken);
}
internal async Task OnWriteNode(OpcUaNode node, object payload, CancellationToken cancellationToken = default)
{
var valueToBeWritten = new WriteValue
{
NodeId = ExpandedNodeId.ToNodeId(node.NodeId, _session.NamespaceUris),
AttributeId = Attributes.Value,
Value = new DataValue
{
Value = payload
}
};
var writeResult = await _session.WriteAsync(null, [valueToBeWritten], cancellationToken);
if (writeResult.Results != null)
{
if (writeResult.Results.First().Code != 0)
{
Logger.Log(LogLevel.Warning, "There was an error when trying to write a value to node {NodeId}", node.NodeId);
}
}
}
/// <inheritdoc/>
public object ReadNode(string nodeId)
{
return ReadNodeDataValue(nodeId).GetAwaiter().GetResult().Result.Value;
}
/// <inheritdoc />
public async Task<object> ReadNodeAsync(string nodeId, CancellationToken cancellationToken = default)
{
var result = await ReadNodeDataValue(nodeId, cancellationToken);
return result.Result.Value;
}
private async Task<DataValueResult> ReadNodeDataValue(string nodeId, CancellationToken cancellationToken = default)
{
var value = await State.ReadValueAsync(nodeId, cancellationToken);
if (!value.Success)
{
if (value.Error?.Exception != null)
{
Logger.Log(LogLevel.Error, value.Error.Exception, "Error reading node data.");
return null;
}
}
return value;
}
internal async Task<DataValueResult> OnReadValueOfNode(string identifier, CancellationToken cancellationToken)
{
var node = State.GetNode(identifier);
if (node == null)
{
return DataValueResult.WithError($"The node \"{identifier}\" was not found");
}
if (node.NodeClass != NodeClass.Variable)
{
return DataValueResult.WithError($"The node \"{identifier}\" was not of type 'variable'");
}
var nodeId = ExpandedNodeId.ToNodeId(node.NodeId, _session.NamespaceUris);
var value = await _session.ReadValueAsync(nodeId, cancellationToken);
if (StatusCode.IsGood(value.StatusCode))
{
return new DataValueResult(value);
}
return DataValueResult.WithError($"The node \"{identifier}\" was not of type 'variable'");
}
/// <summary>
/// Write a value to a node using the driver directly
/// </summary>
/// <param name="payload">Must be of the type OpcUaMessage</param>
/// <exception cref="NotImplementedException"></exception>
public void Send(object payload)
{
if (payload is not OpcUaMessage msg)
{
Logger.Log(LogLevel.Warning, "Currently it is only possible to send messages of the type OpcUaMessage " +
"using the Opc Ua Driver directly");
return;
}
var node = State.GetNode(msg.Identifier);
const string errorMsg = "When trying to read the value of the node, ";
if (node == null)
{
Logger.Log(LogLevel.Error, "{errorMsg} the node with the id {Identifier} was not found", errorMsg, msg.Identifier);
return;
}
if (node.NodeClass != NodeClass.Variable)
{
Logger.Log(LogLevel.Error, "{errorMsg} the node with the id {Identifier} was no variable node", errorMsg, msg.Identifier);
return;
}
WriteNode(node.Identifier, msg.Payload);
}
/// <inheritdoc/>
public Task SendAsync(object payload, CancellationToken cancellationToken = default)
{
if (payload is not OpcUaMessage)
{
Logger.Log(LogLevel.Warning, "Currently it is only possible to send messages of the type OpcUaMessage " +
"using the Opc Ua Driver directly");
return Task.CompletedTask;
}
throw new NotImplementedException();
}
#endregion
#region UI Methods
/// <summary>
/// Method to read nodes from the ui for testing
/// </summary>
/// <param name="nodeId"></param>
/// <returns></returns>
[EntrySerialize]
internal string ReadNodeAsString(string nodeId)
{
try
{
var value = ReadNodeDataValue(nodeId);
if (value == null)
{
return "There was an error, when trying to read the value of the node. Please look into the log for further information";
}
return value.ToString();
}
catch (Exception e)
{
Logger.LogError(e, "Failed to read node as string");
return e.Message;
}
}
/// <summary>
/// Method to write values to a node over the UI for testing
/// </summary>
/// <param name="identifier"></param>
/// <param name="valueString"></param>
/// <param name="cancellationToken"></param>
[EntrySerialize]
internal async Task WriteNode(string identifier, string valueString, CancellationToken cancellationToken = default)
{
var node = State.GetNode(identifier);
var errormsg = "When trying to read the value of the node, ";
if (node == null)
{
Logger.Log(LogLevel.Error, "{errormsg} the node with the id {identifier} was not found", errormsg, identifier);
return;
}
if (node.NodeClass != NodeClass.Variable)
{