-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathI4AASNodeManager.cs
366 lines (311 loc) · 14.6 KB
/
I4AASNodeManager.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
namespace AdminShell
{
using AAS2Nodeset;
using Opc.Ua;
using Opc.Ua.Export;
using Opc.Ua.Server;
using System;
using System.Collections.Generic;
using System.IO;
public class I4AASNodeManager : CustomNodeManager2
{
private long _lastUsedId = 0;
private string _namespaceURI = "http://opcfoundation.org/UA/AAS2Nodeset/";
public FolderState? _rootAssetAdminShells = null;
public FolderState? _rootSubmodels = null;
public FolderState? _rootConceptDescriptions = null;
public I4AASNodeManager(IServerInternal server, ApplicationConfiguration configuration)
: base(server, configuration)
{
SystemContext.NodeIdFactory = this;
List<string> namespaceUris = new();
if (!string.IsNullOrEmpty(Program.g_AASEnv?.AssetAdministrationShells[0].IdShort))
{
_namespaceURI = "http://opcfoundation.org/UA/" + Program.g_AASEnv?.AssetAdministrationShells[0].IdShort.Replace("/", "_").Replace(":", "_") + "/";
}
else if (!string.IsNullOrEmpty(Program.g_AASEnv?.AssetAdministrationShells[0].Id))
{
_namespaceURI = "http://opcfoundation.org/UA/" + Program.g_AASEnv?.AssetAdministrationShells[0].Id.Replace("/", "_").Replace(":", "_") + "/";
}
else if (!string.IsNullOrEmpty(Program.g_AASEnv?.AssetAdministrationShells[0].Identification?.Id))
{
_namespaceURI = "http://opcfoundation.org/UA/" + Program.g_AASEnv?.AssetAdministrationShells[0].Identification.Id.Replace("/", "_").Replace(":", "_") + "/";
}
namespaceUris.Add(_namespaceURI);
NamespaceUris = namespaceUris;
}
public override NodeId New(ISystemContext context, NodeState node)
{
// for new nodes we create, pick our default namespace
return new NodeId(Utils.IncrementIdentifier(ref _lastUsedId), (ushort)Server.NamespaceUris.GetIndex(_namespaceURI));
}
public void SaveNodestateCollectionAsNodeSet2(string filePath, NodeStateCollection nodesToExport)
{
if (nodesToExport.Count > 0)
{
using (var stream = new StreamWriter(filePath))
{
UANodeSet nodeSet = new()
{
LastModified = DateTime.UtcNow,
LastModifiedSpecified = true
};
foreach (NodeState node in nodesToExport)
{
// make sure we don't add duplicates
bool alreadyExists = false;
if (nodeSet.Items != null)
{
foreach (UANode existingNode in nodeSet.Items)
{
if (existingNode.NodeId == node.NodeId.ToString().Replace("ns=2", "ns=1"))
{
alreadyExists = true;
break;
}
}
}
if (!alreadyExists)
{
nodeSet.Export(SystemContext, node);
}
}
nodeSet.Write(stream.BaseStream);
stream.Flush();
}
// fixup our model definitions
string exportedContent = System.IO.File.ReadAllText(filePath);
exportedContent = exportedContent.Replace("</NamespaceUris>", "</NamespaceUris>\n" +
" <Models>\n" +
" <Model ModelUri=\"" + _namespaceURI + "\" Version=\"1.0.0\" PublicationDate=\"" + DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ") + "\"/>\n" +
" </Models>");
System.IO.File.WriteAllText(filePath, exportedContent);
}
}
public override void CreateAddressSpace(IDictionary<NodeId, IList<IReference>> externalReferences)
{
lock (Lock)
{
IList<IReference>? objectsFolderReferences = null;
if (!externalReferences.TryGetValue(ObjectIds.ObjectsFolder, out objectsFolderReferences))
{
externalReferences[ObjectIds.ObjectsFolder] = objectsFolderReferences = new List<IReference>();
}
_rootAssetAdminShells = CreateFolder(FindNodeInAddressSpace(ObjectIds.ObjectsFolder), "Asset Admin Shells");
_rootSubmodels = CreateFolder(FindNodeInAddressSpace(ObjectIds.ObjectsFolder), "Submodels");
_rootConceptDescriptions = CreateFolder(FindNodeInAddressSpace(ObjectIds.ObjectsFolder), "Concept Descriptions");
if (Program.g_AASEnv != null)
{
CreateObjects(Program.g_AASEnv);
if (!Path.Exists(Path.Combine(Directory.GetCurrentDirectory(), "NodeSets")))
{
Directory.CreateDirectory(Path.Combine(Directory.GetCurrentDirectory(), "NodeSets"));
}
string c_exportFilename;
if (Program.g_AASEnv.AssetAdministrationShells[0].IdShort != null)
{
c_exportFilename = Path.Combine(Directory.GetCurrentDirectory(), "NodeSets", Program.g_AASEnv.AssetAdministrationShells[0].IdShort.Replace("/", "_").Replace(":", "_") + ".NodeSet2.xml");
try
{
NodeStateCollection nodesToExport = new();
foreach (NodeState node in PredefinedNodes.Values)
{
// only export nodes belonging to our AAS submodel-template namespace
if (node.NodeId.NamespaceIndex == (ushort)Server.NamespaceUris.GetIndex(_namespaceURI))
{
nodesToExport.Add(node);
}
}
// export nodeset XML
Console.WriteLine($"Writing {nodesToExport.Count} nodes to file {c_exportFilename}!");
SaveNodestateCollectionAsNodeSet2(c_exportFilename, nodesToExport);
Console.WriteLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + " when exporting to {0}", c_exportFilename);
}
}
}
AddReverseReferences(externalReferences);
base.CreateAddressSpace(externalReferences);
}
}
public void CreateObjects(AssetAdministrationShellEnvironment env)
{
if (env.AssetAdministrationShells != null)
{
foreach (AssetAdministrationShell aas in env.AssetAdministrationShells)
{
// fall back to ID if no IDShort is provided
if (string.IsNullOrEmpty(aas.IdShort))
{
aas.IdShort = aas.Id;
}
// fall back to Identification if no IDShort and no ID is provided
if (string.IsNullOrEmpty(aas.IdShort))
{
aas.IdShort = aas.Identification.Id;
}
FolderState aasNode = CreateFolder(_rootAssetAdminShells, aas.IdShort);
if ((aas.AssetInformation != null) && !string.IsNullOrEmpty(aas.AssetInformation.GlobalAssetId))
{
CreateStringVariable(aasNode, aas.AssetInformation.GlobalAssetId, string.Empty);
}
if (aas.Submodels != null && aas.Submodels.Count > 0)
{
foreach (SubmodelReference reference in aas.Submodels)
{
CreateStringVariable(aasNode, reference.Keys[0].Value, string.Empty);
}
}
}
}
if (env.Submodels != null)
{
foreach (Submodel submodel in env.Submodels)
{
FolderState submodelNode = CreateFolder(_rootSubmodels, submodel.IdShort);
if (submodel.SubmodelElements.Count > 0)
{
foreach (SubmodelElementWrapper smew in submodel.SubmodelElements)
{
CreateSubmodelElement(submodelNode, smew.SubmodelElement);
}
}
}
}
if (env.ConceptDescriptions != null)
{
foreach (ConceptDescription cd in env.ConceptDescriptions)
{
CreateStringVariable(_rootConceptDescriptions, cd.Id, cd.IdShort);
}
}
}
private void CreateSubmodelElement(NodeState parent, SubmodelElement sme)
{
if (sme is SubmodelElementCollection collection)
{
if (collection.Value != null)
{
if (string.IsNullOrEmpty(sme.IdShort) && (sme.SemanticId.Keys != null) && (sme.SemanticId.Keys.Count > 0))
{
sme.IdShort = sme.SemanticId.Keys[0].Value;
}
FolderState collectionFolder = CreateFolder(parent, sme.IdShort);
foreach (SubmodelElementWrapper smew in collection.Value)
{
CreateSubmodelElement(collectionFolder, smew.SubmodelElement);
}
}
}
else
{
// fall back to SemanticID if no IDShort is provided
if (string.IsNullOrEmpty(sme.IdShort) && (sme.SemanticId.Keys != null) && (sme.SemanticId.Keys.Count > 0))
{
sme.IdShort = sme.SemanticId.Keys[0].Value;
}
if (sme is Property)
{
CreateStringVariable(parent, sme.IdShort, ((Property)sme).Value);
}
else if (sme is Blob)
{
CreateStringVariable(parent, sme.IdShort, ((Blob)sme).Value);
}
else if (sme is File)
{
CreateStringVariable(parent, sme.IdShort, ((File)sme).Value);
}
else if (sme is ReferenceElement)
{
if (string.IsNullOrEmpty(sme.IdShort) && (((ReferenceElement)sme).Value != null) && (((ReferenceElement)sme).Value.Keys.Count > 0) && (((ReferenceElement)sme).Value.Keys[0].Value != null))
{
sme.IdShort = ((ReferenceElement)sme).Value.Keys[0].Value;
CreateStringVariable(parent, sme.IdShort, string.Empty);
}
}
else
{
// use the SemanticID as the value of the variable
string value = string.Empty;
if ((sme.SemanticId.Keys != null) && (sme.SemanticId.Keys.Count > 0))
{
value = sme.SemanticId.Keys[0].Value;
}
CreateStringVariable(parent, sme.IdShort, value);
}
}
}
public FolderState CreateFolder(NodeState? parent, string browseDisplayName)
{
if (string.IsNullOrEmpty(browseDisplayName))
{
throw new ArgumentNullException("Cannot create UA folder with empty browsename!");
}
FolderState folder = new(parent)
{
BrowseName = browseDisplayName,
DisplayName = browseDisplayName,
TypeDefinitionId = ObjectTypeIds.FolderType
};
folder.NodeId = New(SystemContext, folder);
AddPredefinedNode(SystemContext, folder);
if (parent != null)
{
parent.AddChild(folder);
}
return folder;
}
public BaseObjectState CreateObject(NodeState? parent, string idShort, string nodeId)
{
if (string.IsNullOrEmpty(idShort) || string.IsNullOrEmpty(nodeId))
{
throw new ArgumentNullException("Cannot create UA object with empty browsename or type definition!");
}
BaseObjectState obj = new(parent)
{
BrowseName = idShort,
DisplayName = idShort,
TypeDefinitionId = nodeId
};
obj.NodeId = New(SystemContext, obj);
AddPredefinedNode(SystemContext, obj);
if (parent != null)
{
parent.AddChild(obj);
}
return obj;
}
public BaseDataVariableState CreateStringVariable(NodeState? parent, string browseDisplayName, string value)
{
if (string.IsNullOrEmpty(browseDisplayName))
{
throw new ArgumentNullException("Cannot create UA variable with empty browsename!");
}
BaseDataVariableState variable = new(parent)
{
SymbolicName = browseDisplayName,
BrowseName = new QualifiedName(browseDisplayName, (ushort)Server.NamespaceUris.GetIndex(_namespaceURI)),
DisplayName = new Opc.Ua.LocalizedText("en", browseDisplayName),
DataType = new NodeId(DataTypes.String),
Value = value,
ValueRank = ValueRanks.Scalar,
AccessLevel = AccessLevels.CurrentReadOrWrite,
UserAccessLevel = AccessLevels.CurrentReadOrWrite,
UserWriteMask = AttributeWriteMask.ValueForVariableType,
WriteMask = AttributeWriteMask.ValueForVariableType,
};
variable.NodeId = New(SystemContext, variable);
AddPredefinedNode(SystemContext, variable);
if (parent != null)
{
parent.AddChild(variable);
}
return variable;
}
}
}