-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathVc2OpcUaBridge.cs
510 lines (431 loc) · 19.5 KB
/
Vc2OpcUaBridge.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
/*
* Bridge between Visual Components and OPCUA
*/
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Caliburn.Micro;
using Opc.Ua;
using Opc.Ua.Sample;
using Opc.Ua.Server;
using VisualComponents.Create3D;
namespace vc2opcua
{
class Vc2OpcUaBridge
{
[Import]
IApplication _application = null;
VcUtils _vcUtils = new VcUtils();
IServerInternal uaServer;
public UaNodeManager nodeManager;
private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#region Constructors
public Vc2OpcUaBridge(IServerInternal server,
ApplicationConfiguration configuration)
{
uaServer = server;
nodeManager = new UaNodeManager(uaServer, configuration);
nodeManager.AddressSpaceCreated += NodeManager_AddressSpaceCreated;
_application = IoC.Get<IApplication>();
// Subscribe to added/removing component events
_application.World.ComponentAdded += World_ComponentAdded;
_application.World.ComponentRemoving += World_ComponentRemoving;
}
#endregion
#region Properties
// Correlates OPCUA BrowseNames to corresponding component names
public Dictionary<string, string> UaBrowseName2VcComponentName { get; set; } = new Dictionary<string, string>();
// Correlates types between VC Signal Types and OPCUA types
Dictionary<BehaviorType, NodeId> VcSignal2OpcuaType { get; } = new Dictionary<BehaviorType, NodeId>
{
{ BehaviorType.StringSignal, new NodeId(DataTypeIds.String) },
{ BehaviorType.BooleanSignal, new NodeId(DataTypeIds.Boolean) },
{ BehaviorType.RealSignal, new NodeId(DataTypeIds.Double) },
{ BehaviorType.IntegerSignal, new NodeId(DataTypeIds.Integer) },
{ BehaviorType.ComponentSignal, new NodeId(DataTypeIds.String) }
};
// Correlates types between VC Property Types and OPCUA types
Dictionary<Type, NodeId> VcProperty2OpcuaType { get; } = new Dictionary<Type, NodeId>
{
{ typeof(String), new NodeId(DataTypeIds.String) },
{ typeof(Boolean), new NodeId(DataTypeIds.Boolean) },
{ typeof(Double), new NodeId(DataTypeIds.Double) },
{ typeof(Int32), new NodeId(DataTypeIds.Integer) }
};
#endregion
#region Methods
/// <summary>
/// Gets the components active in simulation when server is started
/// </summary>
private List<NodeState> GetInitialNodes()
{
List<NodeState> initialNodes = new List<NodeState>();
foreach(ISimComponent vcComponent in _vcUtils.GetComponents())
{
initialNodes.Add(CreateNodeTree(vcComponent));
}
return initialNodes;
}
/// <summary>
/// Returns a node tree with a component and all its signals
/// </summary>
private ComponentState CreateNodeTree(ISimComponent simComponent)
{
ComponentState componentNode = CreateComponentNode(simComponent.Name);
VcComponent vcComponent = new VcComponent(simComponent);
AddSignalNodes(componentNode, vcComponent);
AddPropertyNodes(componentNode, vcComponent);
return componentNode;
}
/// <summary>
/// Add signals from VC component to component OPCUA node
/// </summary>
private void AddSignalNodes(ComponentState uaComponentNode, VcComponent vcComponent)
{
foreach (ISignal vcSignal in vcComponent.GetSignals())
{
// Add signal node
BaseDataVariableState uaSignalNode = CreateVariableNode(uaComponentNode.Signals, vcSignal.Name, VcSignal2OpcuaType[vcSignal.Type]);
uaComponentNode.Signals.AddChild(uaSignalNode);
// Store names in UaBrowseName2VcComponentName
UaBrowseName2VcComponentName.Add(uaSignalNode.BrowseName.Name, vcComponent.component.Name);
// Subscribe to signal triggered events
vcSignal.SignalTrigger += vc_SignalTriggered;
uaSignalNode.StateChanged += ua_SignalTriggered;
}
}
/// <summary>
/// Add properties from VC component to component OPCUA node
/// </summary>
private void AddPropertyNodes(ComponentState uaComponentNode, VcComponent vcComponent)
{
foreach (IProperty vcProperty in vcComponent.component.Properties)
{
try
{
// Add signal node
BaseDataVariableState uaPropertyNode = CreateVariableNode(uaComponentNode.Properties, vcProperty.Name, VcProperty2OpcuaType[vcProperty.PropertyType]);
uaComponentNode.Properties.AddChild(uaPropertyNode);
// Store names in UaBrowseName2VcComponentName
UaBrowseName2VcComponentName.Add(uaPropertyNode.BrowseName.Name, vcComponent.component.Name);
uaPropertyNode.Value = vcProperty.Value;
// Subscribe to property changed events
vcProperty.PropertyChanged += vc_PropertyChanged;
uaPropertyNode.StateChanged += ua_PropertyChanged;
}
catch (Exception ex)
{
logger.Warn("Error adding property", ex);
}
}
}
/// <summary>
/// Creates node for Component in Visual Components.
/// </summary>
private ComponentState CreateComponentNode(string nodeName)
{
string namespaceUri = Namespaces.vc2opcua;
ComponentState componentNode = new ComponentState(null);
NodeId componentNodeId = NodeId.Create(nodeName, namespaceUri, uaServer.NamespaceUris);
NodeId signalsNodeId = NodeId.Create("Signals-" + nodeName, namespaceUri, uaServer.NamespaceUris);
NodeId propertiesNodeId = NodeId.Create("Properties-" + nodeName, namespaceUri, uaServer.NamespaceUris);
componentNode.Create(
nodeManager.context,
componentNodeId,
new QualifiedName(nodeName, (ushort)uaServer.NamespaceUris.GetIndex(namespaceUri)),
null,
true);
componentNode.Signals.NodeId = signalsNodeId;
componentNode.Properties.NodeId = propertiesNodeId;
nodeManager.baseFolder.AddReference(ReferenceTypeIds.Organizes, false, componentNode.NodeId);
componentNode.AddReference(ReferenceTypeIds.Organizes, true, nodeManager.baseFolder.NodeId);
return componentNode;
}
/// <summary>
/// Creates node for Signal associated to Component in Visual Components.
/// </summary>
private BaseDataVariableState CreateVariableNode(FolderState parentFolder, string nodeName, NodeId nodeDataType)
{
string namespaceUri = Namespaces.vc2opcua;
string nodeNameParent = String.Format("{0}-{1}", nodeName, parentFolder.Parent.DisplayName);
BaseDataVariableState variableNode = new BaseDataVariableState(parentFolder);
NodeId nodeId = NodeId.Create(nodeNameParent, namespaceUri, uaServer.NamespaceUris);
variableNode.Create(
nodeManager.context,
nodeId,
new QualifiedName(nodeNameParent, (ushort)uaServer.NamespaceUris.GetIndex(namespaceUri)),
new LocalizedText(nodeName),
true);
variableNode.DataType = nodeDataType;
parentFolder.AddReference(ReferenceTypeIds.Organizes, false, variableNode.NodeId);
variableNode.AddReference(ReferenceTypeIds.Organizes, true, parentFolder.NodeId);
return variableNode;
}
#endregion
#region Event Handlers
private void NodeManager_AddressSpaceCreated(object source, EventArgs args)
{
foreach (NodeState node in GetInitialNodes())
{
nodeManager.AddNode(node);
}
}
private void World_ComponentAdded(object sender, ComponentAddedEventArgs e)
{
// Add component node
ComponentState componentNode = CreateNodeTree(e.Component);
nodeManager.AddNode(componentNode);
_vcUtils.VcWriteWarningMsg("Component added: " + e.Component.Name);
}
private void World_ComponentRemoving(object sender, ComponentRemovingEventArgs e)
{
NodeId componentId = NodeId.Create(e.Component.Name, Namespaces.vc2opcua, uaServer.NamespaceUris);
ComponentState componentNode = (ComponentState)nodeManager.FindPredefinedNode(componentId,
typeof(ComponentState));
if (componentNode != null)
{
nodeManager.RemoveNode(nodeManager.baseFolder, componentNode, ReferenceTypeIds.Organizes);
}
// Remove signals from SignalComponents property
VcComponent vcComponent = new VcComponent(e.Component);
foreach (ISignal signal in vcComponent.GetSignals())
{
string nodeNameParent = String.Format("{0}-{1}", signal.Name, vcComponent.component.Name);
UaBrowseName2VcComponentName.Remove(nodeNameParent);
}
_vcUtils.VcWriteWarningMsg("Component removed: " + e.Component.Name);
}
/// <summary>
/// Sets value of OPCUA node signal to corresponding VC signal
/// </summary>
private void ua_SignalTriggered(ISystemContext context, NodeState node, NodeStateChangeMasks changes)
{
if (!UaBrowseName2VcComponentName.ContainsKey(node.BrowseName.Name))
{
_vcUtils.VcWriteWarningMsg(String.Format("Component with signal {0} not found", node.BrowseName.Name));
return;
}
// Cast signal OPCUA node to BaseDataVariableState type
BaseDataVariableState uaSignal = (BaseDataVariableState)node;
// Get signal component as VC object
ISimComponent vcComponent = _vcUtils.GetComponent(UaBrowseName2VcComponentName[node.BrowseName.Name]);
ISignal vcSignal = (ISignal)vcComponent.FindBehavior(node.DisplayName.ToString());
if (uaSignal.DataType == new NodeId(DataTypeIds.String))
{
if ((string)uaSignal.Value == (string)vcSignal.Value)
{
return;
}
vcSignal.Value = (string)uaSignal.Value;
}
else if (uaSignal.DataType == new NodeId(DataTypeIds.Boolean))
{
if ((bool)uaSignal.Value == (bool)vcSignal.Value)
{
return;
}
vcSignal.Value = (bool)uaSignal.Value;
}
else if (uaSignal.DataType == new NodeId(DataTypeIds.Double))
{
if ((double)uaSignal.Value == (double)vcSignal.Value)
{
return;
}
vcSignal.Value = (double)uaSignal.Value;
}
else if (uaSignal.DataType == new NodeId(DataTypeIds.Integer))
{
if ((int)uaSignal.Value == (int)vcSignal.Value)
{
return;
}
vcSignal.Value = (int)uaSignal.Value;
}
else
{
_vcUtils.VcWriteWarningMsg("OPCUA signal type not supported" + uaSignal.DataType.ToString());
return;
}
}
/// <summary>
/// Sets value of VC signal to OPCUA node
/// </summary>
private void vc_SignalTriggered(object sender, SignalTriggerEventArgs e)
{
// Get OPCUA node that will get its value updated
string nodeNameParent = String.Format("{0}-{1}", e.Signal.Name, e.Signal.Node.Name);
NodeId nodeId = NodeId.Create(nodeNameParent, Namespaces.vc2opcua, uaServer.NamespaceUris);
BaseDataVariableState uaSignal = (BaseDataVariableState)nodeManager.FindPredefinedNode(nodeId, typeof(BaseDataVariableState));
if (uaSignal == null)
{
// Unsubscribe to events
e.Signal.SignalTrigger -= vc_SignalTriggered;
return;
}
if (e.Signal.Type == BehaviorType.StringSignal)
{
if ((string)uaSignal.Value == (string)e.Signal.Value)
{
return;
}
uaSignal.Value = (string)e.Signal.Value;
}
else if (e.Signal.Type == BehaviorType.ComponentSignal)
{
ISimComponent component = (ISimComponent)e.Signal.Value;
if ((string)uaSignal.Value == component.Name)
{
return;
}
uaSignal.Value = component.Name;
}
else if (e.Signal.Type == BehaviorType.BooleanSignal)
{
if ((bool)uaSignal.Value == (bool)e.Signal.Value)
{
return;
}
uaSignal.Value = (bool)e.Signal.Value;
}
else if (e.Signal.Type == BehaviorType.RealSignal)
{
if ((double)uaSignal.Value == (double)e.Signal.Value)
{
return;
}
uaSignal.Value = (double)e.Signal.Value;
}
else if (e.Signal.Type == BehaviorType.IntegerSignal)
{
if ((int)uaSignal.Value == (int)e.Signal.Value)
{
return;
}
uaSignal.Value = (int)e.Signal.Value;
}
else
{
_vcUtils.VcWriteWarningMsg("VC signal type not supported" + e.Signal.Type.ToString());
return;
}
uaSignal.Timestamp = DateTime.UtcNow;
uaSignal.ClearChangeMasks(nodeManager.context, true);
}
/// <summary>
/// Sets value of OPCUA node property to corresponding VC property
/// </summary>
private void ua_PropertyChanged(ISystemContext context, NodeState node, NodeStateChangeMasks changes)
{
if (!UaBrowseName2VcComponentName.ContainsKey(node.BrowseName.Name))
{
_vcUtils.VcWriteWarningMsg(String.Format("Component with property {0} not found", node.BrowseName.Name));
return;
}
// Cast property OPCUA node to BaseDataVariableState type
BaseDataVariableState uaProperty = (BaseDataVariableState)node;
// Get property component as VC object
ISimComponent vcComponent = _vcUtils.GetComponent(UaBrowseName2VcComponentName[node.BrowseName.Name]);
IProperty vcProperty = vcComponent.GetProperty(node.DisplayName.ToString());
if (uaProperty.DataType == new NodeId(DataTypeIds.String))
{
if ((string)uaProperty.Value == (string)vcProperty.Value)
{
return;
}
vcProperty.Value = (string)uaProperty.Value;
}
else if (uaProperty.DataType == new NodeId(DataTypeIds.Boolean))
{
if ((bool)uaProperty.Value == (bool)vcProperty.Value)
{
return;
}
vcProperty.Value = (bool)uaProperty.Value;
}
else if (uaProperty.DataType == new NodeId(DataTypeIds.Double))
{
if ((double)uaProperty.Value == (double)vcProperty.Value)
{
return;
}
vcProperty.Value = (double)uaProperty.Value;
}
else if (uaProperty.DataType == new NodeId(DataTypeIds.Integer))
{
if ((int)uaProperty.Value == (int)vcProperty.Value)
{
return;
}
vcProperty.Value = (int)uaProperty.Value;
}
else
{
_vcUtils.VcWriteWarningMsg("OPCUA property type not supported" + uaProperty.DataType.ToString());
return;
}
}
/// <summary>
/// Sets value of VC property to OPCUA node
/// </summary>
private void vc_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
IProperty vcProperty = (IProperty)sender;
// Get OPCUA node that will get its value updated
string nodeNameParent = String.Format("{0}-{1}", vcProperty.Name, vcProperty.Container);
NodeId nodeId = NodeId.Create(nodeNameParent, Namespaces.vc2opcua, uaServer.NamespaceUris);
BaseDataVariableState uaProperty = (BaseDataVariableState)nodeManager.FindPredefinedNode(nodeId, typeof(BaseDataVariableState));
if (uaProperty == null)
{
// Unsubscribe to events
vcProperty.PropertyChanged -= vc_PropertyChanged;
return;
}
if (vcProperty.PropertyType == typeof(String))
{
if ((string)uaProperty.Value == (string)vcProperty.Value)
{
return;
}
uaProperty.Value = (string)vcProperty.Value;
}
else if (vcProperty.PropertyType == typeof(Boolean))
{
if ((bool)uaProperty.Value == (bool)vcProperty.Value)
{
return;
}
uaProperty.Value = (bool)vcProperty.Value;
}
else if (vcProperty.PropertyType == typeof(Double))
{
if ((double)uaProperty.Value == (double)vcProperty.Value)
{
return;
}
uaProperty.Value = (double)vcProperty.Value;
}
else if (vcProperty.PropertyType == typeof(Int32))
{
if ((int)uaProperty.Value == (int)vcProperty.Value)
{
return;
}
uaProperty.Value = (int)vcProperty.Value;
}
else
{
_vcUtils.VcWriteWarningMsg("VC property type not supported" + vcProperty.PropertyType.ToString());
return;
}
uaProperty.Timestamp = DateTime.UtcNow;
uaProperty.ClearChangeMasks(nodeManager.context, true);
}
#endregion
}
}