/
UANodeSetHelpers.cs
1205 lines (1000 loc) · 40.9 KB
/
UANodeSetHelpers.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
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) 1996-2016, OPC Foundation. All rights reserved.
The source code in this file is covered under a dual-license scenario:
- RCL: for OPC Foundation members in good-standing
- GPL V2: everybody else
RCL license terms accompanied with this source code. See http://opcfoundation.org/License/RCL/1.00/
GNU General Public License as published by the Free Software Foundation;
version 2 of the License are accompanied with this source code. See http://opcfoundation.org/License/GPLv2
This source code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace Opc.Ua.Export
{
/// <summary>
/// A set of nodes in an address space.
/// </summary>
public partial class UANodeSet
{
#region Constructors
/// <summary>
/// Creates an empty nodeset.
/// </summary>
public UANodeSet()
{
}
/// <summary>
/// Loads a nodeset from a stream.
/// </summary>
/// <param name="istrm">The input stream.</param>
/// <returns>The set of nodes</returns>
public static UANodeSet Read(Stream istrm)
{
StreamReader reader = new StreamReader(istrm);
try
{
XmlSerializer serializer = new XmlSerializer(typeof(UANodeSet));
return serializer.Deserialize(reader) as UANodeSet;
}
finally
{
reader.Dispose();
}
}
/// <summary>
/// Write a nodeset to a stream.
/// </summary>
/// <param name="istrm">The input stream.</param>
public void Write(Stream istrm)
{
StreamWriter writer = new StreamWriter(istrm, Encoding.UTF8);
try
{
XmlSerializer serializer = new XmlSerializer(typeof(UANodeSet));
serializer.Serialize(writer, this);
}
finally
{
writer.Flush();
writer.Dispose();
}
}
#endregion
#region Public Methods
/// <summary>
/// Adds an alias to the node set.
/// </summary>
public void AddAlias(ISystemContext context, string alias, Opc.Ua.NodeId nodeId)
{
int count = 1;
if (this.Aliases != null)
{
for (int ii = 0; ii < this.Aliases.Length; ii++)
{
if (this.Aliases[ii].Alias == alias)
{
this.Aliases[ii].Value = Export(nodeId, context.NamespaceUris);
return;
}
}
count += this.Aliases.Length;
}
NodeIdAlias[] aliases = new NodeIdAlias[count];
if (this.Aliases != null)
{
Array.Copy(this.Aliases, aliases, this.Aliases.Length);
}
aliases[count-1] = new NodeIdAlias() { Alias = alias, Value = Export(nodeId, context.NamespaceUris) };
this.Aliases = aliases;
}
/// <summary>
/// Imports a node from the set.
/// </summary>
public void Import(ISystemContext context, NodeStateCollection nodes)
{
for (int ii = 0; ii < this.Items.Length; ii++)
{
UANode node = this.Items[ii];
NodeState importedNode = Import(context, node);
nodes.Add(importedNode);
}
}
/// <summary>
/// Adds a node to the set.
/// </summary>
public void Export(ISystemContext context, NodeState node)
{
if (node == null) throw new ArgumentNullException("node");
if (Opc.Ua.NodeId.IsNull(node.NodeId))
{
throw new ArgumentException("A non-null NodeId must be specified.");
}
UANode exportedNode = null;
switch (node.NodeClass)
{
case NodeClass.Object:
{
BaseObjectState o = (BaseObjectState)node;
UAObject value = new UAObject();
value.EventNotifier = o.EventNotifier;
if (o.Parent != null)
{
value.ParentNodeId = ExportAlias(o.Parent.NodeId, context.NamespaceUris);
}
exportedNode = value;
break;
}
case NodeClass.Variable:
{
BaseVariableState o = (BaseVariableState)node;
UAVariable value = new UAVariable();
value.DataType = ExportAlias(o.DataType, context.NamespaceUris);
value.ValueRank = o.ValueRank;
value.ArrayDimensions = Export(o.ArrayDimensions);
value.AccessLevel = o.AccessLevel;
value.UserAccessLevel = o.UserAccessLevel;
value.MinimumSamplingInterval = o.MinimumSamplingInterval;
value.Historizing = o.Historizing;
if (o.Parent != null)
{
value.ParentNodeId = ExportAlias(o.Parent.NodeId, context.NamespaceUris);
}
if (o.Value != null)
{
XmlEncoder encoder = CreateEncoder(context);
Variant variant = new Variant(o.Value);
encoder.WriteVariantContents(variant.Value, variant.TypeInfo);
XmlDocument document = new XmlDocument();
document.InnerXml = encoder.Close();
value.Value = document.DocumentElement;
}
exportedNode = value;
break;
}
case NodeClass.Method:
{
MethodState o = (MethodState)node;
UAMethod value = new UAMethod();
value.Executable = o.Executable;
value.UserExecutable = o.UserExecutable;
if (o.Parent != null)
{
value.ParentNodeId = ExportAlias(o.Parent.NodeId, context.NamespaceUris);
}
exportedNode = value;
break;
}
case NodeClass.View:
{
ViewState o = (ViewState)node;
UAView value = new UAView();
value.ContainsNoLoops = o.ContainsNoLoops;
exportedNode = value;
break;
}
case NodeClass.ObjectType:
{
BaseObjectTypeState o = (BaseObjectTypeState)node;
UAObjectType value = new UAObjectType();
value.IsAbstract = o.IsAbstract;
exportedNode = value;
break;
}
case NodeClass.VariableType:
{
BaseVariableTypeState o = (BaseVariableTypeState)node;
UAVariableType value = new UAVariableType();
value.IsAbstract = o.IsAbstract;
value.DataType = ExportAlias(o.DataType, context.NamespaceUris);
value.ValueRank = o.ValueRank;
value.ArrayDimensions = Export(o.ArrayDimensions);
if (o.Value != null)
{
XmlEncoder encoder = CreateEncoder(context);
Variant variant = new Variant(o.Value);
encoder.WriteVariantContents(variant.Value, variant.TypeInfo);
XmlDocument document = new XmlDocument();
document.InnerXml = encoder.Close();
value.Value = document.DocumentElement;
}
exportedNode = value;
break;
}
case NodeClass.DataType:
{
DataTypeState o = (DataTypeState)node;
UADataType value = new UADataType();
value.IsAbstract = o.IsAbstract;
value.Definition = Export(o.Definition, context.NamespaceUris);
exportedNode = value;
break;
}
case NodeClass.ReferenceType:
{
ReferenceTypeState o = (ReferenceTypeState)node;
UAReferenceType value = new UAReferenceType();
value.IsAbstract = o.IsAbstract;
value.InverseName = Export(new Opc.Ua.LocalizedText[] { o.InverseName });
value.Symmetric = o.Symmetric;
exportedNode = value;
break;
}
}
exportedNode.NodeId = Export(node.NodeId, context.NamespaceUris);
exportedNode.BrowseName = Export(node.BrowseName, context.NamespaceUris);
exportedNode.DisplayName = Export(new Opc.Ua.LocalizedText[] { node.DisplayName });
exportedNode.Description = Export(new Opc.Ua.LocalizedText[] { node.Description });
exportedNode.WriteMask = (uint)node.WriteMask;
exportedNode.UserWriteMask = (uint)node.UserWriteMask;
if (!String.IsNullOrEmpty(node.SymbolicName) && node.SymbolicName != node.BrowseName.Name)
{
exportedNode.SymbolicName = node.SymbolicName;
}
// export references.
INodeBrowser browser = node.CreateBrowser(context, null, null, true, BrowseDirection.Both, null, null, true);
List<Reference> exportedReferences = new List<Reference>();
IReference reference = browser.Next();
while (reference != null)
{
Reference exportedReference = new Reference();
exportedReference.ReferenceType = ExportAlias(reference.ReferenceTypeId, context.NamespaceUris);
exportedReference.IsForward = !reference.IsInverse;
exportedReference.Value = Export(reference.TargetId, context.NamespaceUris, context.ServerUris);
exportedReferences.Add(exportedReference);
reference = browser.Next();
}
exportedNode.References = exportedReferences.ToArray();
// add node to list.
UANode[] nodes = null;
int count = 1;
if (this.Items == null)
{
nodes = new UANode[count];
}
else
{
count += this.Items.Length;
nodes = new UANode[count];
Array.Copy(this.Items, nodes, this.Items.Length);
}
nodes[count-1] = exportedNode;
this.Items = nodes;
// recusively process children.
List<BaseInstanceState> children = new List<BaseInstanceState>();
node.GetChildren(context, children);
for (int ii = 0; ii < children.Count; ii++)
{
Export(context, children[ii]);
}
}
#endregion
#region Private Members
/// <summary>
/// Creates an encoder to save Variant values.
/// </summary>
private XmlEncoder CreateEncoder(ISystemContext context)
{
ServiceMessageContext messageContext = new ServiceMessageContext();
messageContext.NamespaceUris = context.NamespaceUris;
messageContext.ServerUris = context.ServerUris;
messageContext.Factory = context.EncodeableFactory;
XmlEncoder encoder = new XmlEncoder(messageContext);
NamespaceTable namespaceUris = new NamespaceTable();
if (NamespaceUris != null)
{
for (int ii = 0; ii < NamespaceUris.Length; ii++)
{
namespaceUris.Append(NamespaceUris[ii]);
}
}
StringTable serverUris = new StringTable();
if (ServerUris != null)
{
serverUris.Append(context.ServerUris.GetString(0));
for (int ii = 0; ii < ServerUris.Length; ii++)
{
serverUris.Append(ServerUris[ii]);
}
}
encoder.SetMappingTables(namespaceUris, serverUris);
return encoder;
}
/// <summary>
/// Creates an decoder to restore Variant values.
/// </summary>
private XmlDecoder CreateDecoder(ISystemContext context, XmlElement source)
{
ServiceMessageContext messageContext = new ServiceMessageContext();
messageContext.NamespaceUris = context.NamespaceUris;
messageContext.ServerUris = context.ServerUris;
messageContext.Factory = context.EncodeableFactory;
XmlDecoder decoder = new XmlDecoder(source, messageContext);
NamespaceTable namespaceUris = new NamespaceTable();
if (NamespaceUris != null)
{
for (int ii = 0; ii < NamespaceUris.Length; ii++)
{
namespaceUris.Append(NamespaceUris[ii]);
}
}
StringTable serverUris = new StringTable();
if (ServerUris != null)
{
serverUris.Append(context.ServerUris.GetString(0));
for (int ii = 0; ii < ServerUris.Length; ii++)
{
serverUris.Append(ServerUris[ii]);
}
}
decoder.SetMappingTables(namespaceUris, serverUris);
return decoder;
}
/// <summary>
/// Imports a node from the set.
/// </summary>
private NodeState Import(ISystemContext context, UANode node)
{
NodeState importedNode = null;
NodeClass nodeClass = NodeClass.Unspecified;
if (node is UAObject) nodeClass = NodeClass.Object;
else if (node is UAVariable) nodeClass = NodeClass.Variable;
else if (node is UAMethod) nodeClass = NodeClass.Method;
else if (node is UAObjectType) nodeClass = NodeClass.ObjectType;
else if (node is UAVariableType) nodeClass = NodeClass.VariableType;
else if (node is UADataType) nodeClass = NodeClass.DataType;
else if (node is UAReferenceType) nodeClass = NodeClass.ReferenceType;
else if (node is UAView) nodeClass = NodeClass.View;
switch (nodeClass)
{
case NodeClass.Object:
{
UAObject o = (UAObject)node;
BaseObjectState value = new BaseObjectState(null);
value.EventNotifier = o.EventNotifier;
importedNode = value;
break;
}
case NodeClass.Variable:
{
UAVariable o = (UAVariable)node;
NodeId typeDefinitionId = null;
if (node.References != null)
{
for (int ii = 0; ii < node.References.Length; ii++)
{
Opc.Ua.NodeId referenceTypeId = ImportNodeId(node.References[ii].ReferenceType, context.NamespaceUris, true);
bool isInverse = !node.References[ii].IsForward;
Opc.Ua.ExpandedNodeId targetId = ImportExpandedNodeId(node.References[ii].Value, context.NamespaceUris, context.ServerUris);
if (referenceTypeId == ReferenceTypeIds.HasTypeDefinition && !isInverse)
{
typeDefinitionId = Opc.Ua.ExpandedNodeId.ToNodeId(targetId, context.NamespaceUris);
break;
}
}
}
BaseVariableState value = null;
if (typeDefinitionId == Opc.Ua.VariableTypeIds.PropertyType)
{
value = new PropertyState(null);
}
else
{
value = new BaseDataVariableState(null);
}
value.DataType = ImportNodeId(o.DataType, context.NamespaceUris, true);
value.ValueRank = o.ValueRank;
value.ArrayDimensions = ImportArrayDimensions(o.ArrayDimensions);
value.AccessLevel = o.AccessLevel;
value.UserAccessLevel = o.UserAccessLevel;
value.MinimumSamplingInterval = o.MinimumSamplingInterval;
value.Historizing = o.Historizing;
if (o.Value != null)
{
XmlDecoder decoder = CreateDecoder(context, o.Value);
TypeInfo typeInfo = null;
value.Value = decoder.ReadVariantContents(out typeInfo);
decoder.Close();
}
importedNode = value;
break;
}
case NodeClass.Method:
{
UAMethod o = (UAMethod)node;
MethodState value = new MethodState(null);
value.Executable = o.Executable;
value.UserExecutable = o.UserExecutable;
importedNode = value;
break;
}
case NodeClass.View:
{
UAView o = (UAView)node;
ViewState value = new ViewState();
value.ContainsNoLoops = o.ContainsNoLoops;
importedNode = value;
break;
}
case NodeClass.ObjectType:
{
UAObjectType o = (UAObjectType)node;
BaseObjectTypeState value = new BaseObjectTypeState();
value.IsAbstract = o.IsAbstract;
importedNode = value;
break;
}
case NodeClass.VariableType:
{
UAVariableType o = (UAVariableType)node;
BaseVariableTypeState value = new BaseDataVariableTypeState();
value.IsAbstract = o.IsAbstract;
value.DataType = ImportNodeId(o.DataType, context.NamespaceUris, true);
value.ValueRank = o.ValueRank;
value.ArrayDimensions = ImportArrayDimensions(o.ArrayDimensions);
if (o.Value != null)
{
XmlDecoder decoder = CreateDecoder(context, o.Value);
TypeInfo typeInfo = null;
value.Value = decoder.ReadVariantContents(out typeInfo);
decoder.Close();
}
importedNode = value;
break;
}
case NodeClass.DataType:
{
UADataType o = (UADataType)node;
DataTypeState value = new DataTypeState();
value.IsAbstract = o.IsAbstract;
value.Definition = Import(o.Definition, context.NamespaceUris);
importedNode = value;
break;
}
case NodeClass.ReferenceType:
{
UAReferenceType o = (UAReferenceType)node;
ReferenceTypeState value = new ReferenceTypeState();
value.IsAbstract = o.IsAbstract;
value.InverseName = Import(o.InverseName);
value.Symmetric = o.Symmetric;
importedNode = value;
break;
}
}
importedNode.NodeId = ImportNodeId(node.NodeId, context.NamespaceUris, false);
importedNode.BrowseName = ImportQualifiedName(node.BrowseName, context.NamespaceUris);
importedNode.DisplayName = Import(node.DisplayName);
importedNode.Description = Import(node.Description);
importedNode.WriteMask = (AttributeWriteMask)node.WriteMask;
importedNode.UserWriteMask = (AttributeWriteMask)node.UserWriteMask;
if (!String.IsNullOrEmpty(node.SymbolicName))
{
importedNode.SymbolicName = node.SymbolicName;
}
if (node.References != null)
{
BaseInstanceState instance = importedNode as BaseInstanceState;
BaseTypeState type = importedNode as BaseTypeState;
for (int ii = 0; ii < node.References.Length; ii++)
{
Opc.Ua.NodeId referenceTypeId = ImportNodeId(node.References[ii].ReferenceType, context.NamespaceUris, true);
bool isInverse = !node.References[ii].IsForward;
Opc.Ua.ExpandedNodeId targetId = ImportExpandedNodeId(node.References[ii].Value, context.NamespaceUris, context.ServerUris);
if (instance != null)
{
if (referenceTypeId == ReferenceTypeIds.HasModellingRule && !isInverse)
{
instance.ModellingRuleId = Opc.Ua.ExpandedNodeId.ToNodeId(targetId, context.NamespaceUris);
continue;
}
if (referenceTypeId == ReferenceTypeIds.HasTypeDefinition && !isInverse)
{
instance.TypeDefinitionId = Opc.Ua.ExpandedNodeId.ToNodeId(targetId, context.NamespaceUris);
continue;
}
}
if (type != null)
{
if (referenceTypeId == ReferenceTypeIds.HasSubtype && isInverse)
{
type.SuperTypeId = Opc.Ua.ExpandedNodeId.ToNodeId(targetId, context.NamespaceUris);
continue;
}
}
importedNode.AddReference(referenceTypeId, isInverse, targetId);
}
}
return importedNode;
}
/// <summary>
/// Exports a NodeId as an alias.
/// </summary>
private string ExportAlias(Opc.Ua.NodeId source, NamespaceTable namespaceUris)
{
string nodeId = Export(source, namespaceUris);
if (!String.IsNullOrEmpty(nodeId))
{
if (this.Aliases != null)
{
for (int ii = 0; ii < this.Aliases.Length; ii++)
{
if (this.Aliases[ii].Value == nodeId)
{
return this.Aliases[ii].Alias;
}
}
}
}
return nodeId;
}
/// <summary>
/// Exports a NodeId
/// </summary>
private string Export(Opc.Ua.NodeId source, NamespaceTable namespaceUris)
{
if (Opc.Ua.NodeId.IsNull(source))
{
return String.Empty;
}
if (source.NamespaceIndex > 0)
{
ushort namespaceIndex = ExportNamespaceIndex(source.NamespaceIndex, namespaceUris);
source = new Opc.Ua.NodeId(source.Identifier, namespaceIndex);
}
return source.ToString();
}
/// <summary>
/// Imports a NodeId
/// </summary>
private Opc.Ua.NodeId ImportNodeId(string source, NamespaceTable namespaceUris, bool lookupAlias)
{
if (String.IsNullOrEmpty(source))
{
return Opc.Ua.NodeId.Null;
}
// lookup alias.
if (lookupAlias && this.Aliases != null)
{
for (int ii = 0; ii < this.Aliases.Length; ii++)
{
if (this.Aliases[ii].Alias == source)
{
source = this.Aliases[ii].Value;
break;
}
}
}
// parse the string.
Opc.Ua.NodeId nodeId = Opc.Ua.NodeId.Parse(source);
if (nodeId.NamespaceIndex > 0)
{
ushort namespaceIndex = ImportNamespaceIndex(nodeId.NamespaceIndex, namespaceUris);
nodeId = new Opc.Ua.NodeId(nodeId.Identifier, namespaceIndex);
}
return nodeId;
}
/// <summary>
/// Exports a ExpandedNodeId
/// </summary>
private string Export(Opc.Ua.ExpandedNodeId source, NamespaceTable namespaceUris, StringTable serverUris)
{
if (Opc.Ua.NodeId.IsNull(source))
{
return String.Empty;
}
if (source.ServerIndex <= 0 && source.NamespaceIndex <= 0 && String.IsNullOrEmpty(source.NamespaceUri))
{
return source.ToString();
}
ushort namespaceIndex = 0;
if (String.IsNullOrEmpty(source.NamespaceUri))
{
namespaceIndex = ExportNamespaceIndex(source.NamespaceIndex, namespaceUris);
}
else
{
namespaceIndex = ExportNamespaceUri(source.NamespaceUri, namespaceUris);
}
uint serverIndex = ExportServerIndex(source.ServerIndex, serverUris);
source = new Opc.Ua.ExpandedNodeId(source.Identifier, namespaceIndex, null, serverIndex);
return source.ToString();
}
/// <summary>
/// Imports a ExpandedNodeId
/// </summary>
private Opc.Ua.ExpandedNodeId ImportExpandedNodeId(string source, NamespaceTable namespaceUris, StringTable serverUris)
{
if (String.IsNullOrEmpty(source))
{
return Opc.Ua.ExpandedNodeId.Null;
}
// parse the node.
Opc.Ua.ExpandedNodeId nodeId = Opc.Ua.ExpandedNodeId.Parse(source);
if (nodeId.ServerIndex <= 0 && nodeId.NamespaceIndex <= 0 && String.IsNullOrEmpty(nodeId.NamespaceUri))
{
return nodeId;
}
uint serverIndex = ImportServerIndex(nodeId.ServerIndex, serverUris);
ushort namespaceIndex = ImportNamespaceIndex(nodeId.NamespaceIndex, namespaceUris);
if (serverIndex > 0)
{
string namespaceUri = nodeId.NamespaceUri;
if (String.IsNullOrEmpty(nodeId.NamespaceUri))
{
namespaceUri = namespaceUris.GetString(namespaceIndex);
}
nodeId = new Opc.Ua.ExpandedNodeId(nodeId.Identifier, 0, namespaceUri, serverIndex);
return nodeId;
}
nodeId = new Opc.Ua.ExpandedNodeId(nodeId.Identifier, namespaceIndex, null, 0);
return nodeId;
}
/// <summary>
/// Exports a QualifiedName
/// </summary>
private string Export(Opc.Ua.QualifiedName source, NamespaceTable namespaceUris)
{
if (Opc.Ua.QualifiedName.IsNull(source))
{
return String.Empty;
}
if (source.NamespaceIndex > 0)
{
ushort namespaceIndex = ExportNamespaceIndex(source.NamespaceIndex, namespaceUris);
source = new Opc.Ua.QualifiedName(source.Name, namespaceIndex);
}
return source.ToString();
}
/// <summary>
/// Exports a DataTypeDefinition
/// </summary>
private Opc.Ua.Export.DataTypeDefinition Export(Opc.Ua.UADataTypeDefinition source, NamespaceTable namespaceUris)
{
if (source == null)
{
return null;
}
DataTypeDefinition definition = new DataTypeDefinition();
definition.Name = Export(source.Name, namespaceUris);
definition.SymbolicName = source.SymbolicName;
definition.BaseType = Export(source.BaseType, namespaceUris);
if (source.Fields != null)
{
List<Opc.Ua.Export.DataTypeField> fields = new List<DataTypeField>();
foreach (DataTypeDefinitionField field in source.Fields)
{
Opc.Ua.Export.DataTypeField output = new Opc.Ua.Export.DataTypeField();
output.Name = field.Name;
output.SymbolicName = field.SymbolicName;
output.Description = Export(new Opc.Ua.LocalizedText[] { field.Description });
if (NodeId.IsNull(field.DataType))
{
output.DataType = Export(DataTypeIds.BaseDataType, namespaceUris);
}
else
{
output.DataType = Export(field.DataType, namespaceUris);
}
output.ValueRank = field.ValueRank;
output.Value = field.Value;
output.Definition = Export(field.Definition, namespaceUris);
fields.Add(output);
}
definition.Field = fields.ToArray();
}
return definition;
}
/// <summary>
/// Imports a DataTypeDefinition
/// </summary>
private Opc.Ua.UADataTypeDefinition Import(Opc.Ua.Export.DataTypeDefinition source, NamespaceTable namespaceUris)
{
if (source == null)
{
return null;
}
Opc.Ua.UADataTypeDefinition definition = new Opc.Ua.UADataTypeDefinition();
definition.Name = ImportQualifiedName(source.Name, namespaceUris);
definition.SymbolicName = source.SymbolicName;
definition.BaseType = ImportQualifiedName(source.BaseType, namespaceUris);
if (source.Field != null)
{
List<Opc.Ua.DataTypeDefinitionField> fields = new List<Opc.Ua.DataTypeDefinitionField>();
foreach (DataTypeField field in source.Field)
{
Opc.Ua.DataTypeDefinitionField output = new Opc.Ua.DataTypeDefinitionField();
output.Name = field.Name;
output.SymbolicName = field.SymbolicName;
output.Description = Import(field.Description);
output.DataType = ImportNodeId(field.DataType, namespaceUris, true);
output.ValueRank = field.ValueRank;
output.Value = field.Value;
output.Definition = Import(field.Definition, namespaceUris);
fields.Add(output);
}
definition.Fields = fields;
}
return definition;
}
/// <summary>
/// Imports a QualifiedName
/// </summary>
private Opc.Ua.QualifiedName ImportQualifiedName(string source, NamespaceTable namespaceUris)
{
if (String.IsNullOrEmpty(source))
{
return Opc.Ua.QualifiedName.Null;
}
Opc.Ua.QualifiedName qname = Opc.Ua.QualifiedName.Parse(source);
if (qname.NamespaceIndex > 0)
{
ushort namespaceIndex = ImportNamespaceIndex(qname.NamespaceIndex, namespaceUris);
qname = new Opc.Ua.QualifiedName(qname.Name, namespaceIndex);
}
return qname;
}
/// <summary>
/// Exports the array dimensions.
/// </summary>
private string Export(IList<uint> arrayDimensions)
{
if (arrayDimensions == null)
{
return String.Empty;
}
StringBuilder buffer = new StringBuilder();
for (int ii = 0; ii < arrayDimensions.Count; ii++)
{
if (buffer.Length > 0)
{
buffer.Append(',');
}
buffer.Append(arrayDimensions[ii]);
}
return buffer.ToString();
}
/// <summary>
/// Imports the array dimensions.
/// </summary>
private uint[] ImportArrayDimensions(string arrayDimensions)
{
if (String.IsNullOrEmpty(arrayDimensions))
{
return null;
}
string[] fields = arrayDimensions.Split(',');
uint[] dimensions = new uint[fields.Length];
for (int ii = 0; ii < fields.Length; ii++)
{
try
{
dimensions[ii] = Convert.ToUInt32(fields[ii]);
}
catch
{
dimensions[ii] = 0;
}
}
return dimensions;
}
/// <summary>
/// Exports localized text.
/// </summary>
private Opc.Ua.Export.LocalizedText[] Export(Opc.Ua.LocalizedText[] input)
{
if (input == null)
{
return null;
}
List<Opc.Ua.Export.LocalizedText> output = new List<LocalizedText>();
for (int ii = 0; ii < input.Length; ii++)
{
if (input[ii] != null)
{
Opc.Ua.Export.LocalizedText text = new LocalizedText();
text.Locale = input[ii].Locale;
text.Value = input[ii].Text;
output.Add(text);
}
}
return output.ToArray();
}
/// <summary>
/// Exports localized text.
/// </summary>
private Opc.Ua.Export.LocalizedText Export(Opc.Ua.LocalizedText input)
{
if (input == null)
{
return null;
}
Opc.Ua.Export.LocalizedText text = new LocalizedText();
text.Locale = input.Locale;
text.Value = input.Text;
return text;
}
/// <summary>
/// Imports localized text.
/// </summary>
private Opc.Ua.LocalizedText Import(params Opc.Ua.Export.LocalizedText[] input)
{
if (input == null)
{
return null;
}
for (int ii = 0; ii < input.Length; ii++)