-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathI4AASNodeManager.cs
376 lines (317 loc) · 14.1 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
367
368
369
370
371
372
373
374
375
376
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
{
// AAS type nodeId constants from I4AAS Companion Spec
const int c_assetAdministrationShellTypeNodeId = 1002;
const int c_referenceTypeNodeId = 1004;
const int c_submodelTypeNodeId = 1006;
const int c_conceptDescriptionTypeNodeId = 1007;
const int c_assetTypeNodeId = 1005;
private long _lastUsedId = 0;
private string _namespaceURI = "http://opcfoundation.org/UA/" + Program.g_AASEnv?.AssetAdministrationShells[0].IdShort + "/";
public NodeState? _rootAssetAdminShells = null;
public NodeState? _rootSubmodels = null;
public NodeState? _rootConceptDescriptions = null;
public I4AASNodeManager(IServerInternal server, ApplicationConfiguration configuration)
: base(server, configuration)
{
SystemContext.NodeIdFactory = this;
List<string> namespaceUris = new()
{
_namespaceURI
};
LoadNamespaceUrisFromNodesetXml(namespaceUris, "I4AAS.NodeSet2.xml");
NamespaceUris = namespaceUris;
}
private void LoadNamespaceUrisFromNodesetXml(List<string> namespaceUris, string nodesetFile)
{
using (FileStream stream = new(nodesetFile, FileMode.Open, FileAccess.Read))
{
UANodeSet nodeSet = UANodeSet.Read(stream);
if ((nodeSet.NamespaceUris != null) && (nodeSet.NamespaceUris.Length > 0))
{
foreach (string ns in nodeSet.NamespaceUris)
{
if (!namespaceUris.Contains(ns))
{
namespaceUris.Add(ns);
}
}
}
}
}
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" +
" <RequiredModel ModelUri=\"http://opcfoundation.org/UA/I4AAS/\" Version=\"5.0.0\" PublicationDate=\"2021-06-04T00:00:00Z\"/>\n" +
" </Model>\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>();
}
AddNodesFromNodesetXml("I4AAS.NodeSet2.xml");
_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 + ".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);
}
}
private void AddNodesFromNodesetXml(string nodesetFile)
{
using (Stream stream = new FileStream(nodesetFile, FileMode.Open))
{
UANodeSet nodeSet = UANodeSet.Read(stream);
NodeStateCollection predefinedNodes = new NodeStateCollection();
nodeSet.Import(SystemContext, predefinedNodes);
for (int i = 0; i < predefinedNodes.Count; i++)
{
try
{
AddPredefinedNode(SystemContext, predefinedNodes[i]);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message, ex);
}
}
}
}
public void CreateObjects(AssetAdministrationShellEnvironment env)
{
string typeNodeId = "ns=" + Server.NamespaceUris.GetIndex("http://opcfoundation.org/UA/I4AAS/").ToString() + ";i=";
if (env.AssetAdministrationShells != null)
{
foreach (AssetAdministrationShell aas in env.AssetAdministrationShells)
{
BaseObjectState aasNode = CreateObject(_rootAssetAdminShells, aas.IdShort, typeNodeId + c_assetAdministrationShellTypeNodeId.ToString());
if (aas.AssetInformation != null)
{
CreateObject(aasNode, aas.AssetInformation.GlobalAssetId, typeNodeId + c_assetTypeNodeId.ToString());
}
if (aas.Submodels != null && aas.Submodels.Count > 0)
{
foreach (SubmodelReference reference in aas.Submodels)
{
CreateObject(aasNode, reference.Keys[0].Value, typeNodeId + c_referenceTypeNodeId.ToString());
}
}
}
}
if (env.Submodels != null)
{
foreach (Submodel submodel in env.Submodels)
{
BaseObjectState submodelNode = CreateObject(_rootSubmodels, submodel.IdShort, typeNodeId + c_submodelTypeNodeId.ToString());
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)
{
CreateObject(_rootConceptDescriptions, cd.Id + ";" + cd.IdShort, typeNodeId + c_conceptDescriptionTypeNodeId.ToString());
}
}
}
private void CreateSubmodelElement(NodeState parent, SubmodelElement sme)
{
string typeNodeId = "ns=" + Server.NamespaceUris.GetIndex("http://opcfoundation.org/UA/I4AAS/").ToString() + ";i=";
if (sme is SubmodelElementCollection collection)
{
if (collection.Value != null)
{
NodeState collectionFolder = CreateFolder(parent, "Submodel Elements");
foreach (SubmodelElementWrapper smew in collection.Value)
{
CreateSubmodelElement(collectionFolder, smew.SubmodelElement);
}
}
}
else
{
string id;
if (sme.SemanticId.Keys != null && sme.SemanticId.Keys.Count > 0)
{
id = sme.SemanticId.Keys[0].Value;
}
else
{
id = sme.IdShort;
}
if (sme is Property)
{
CreateStringVariable(parent, id, ((Property)sme).Value);
}
else if (sme is Blob)
{
CreateStringVariable(parent, id, ((Blob)sme).Value);
}
else if (sme is File)
{
CreateStringVariable(parent, id, ((File)sme).Value);
}
else
{
if (sme.Description != null && sme.Description.Count > 0)
{
CreateStringVariable(parent, id, sme.Description[0].Text);
}
else
{
CreateStringVariable(parent, id, string.Empty);
}
}
}
}
public FolderState CreateFolder(NodeState? parent, string browseDisplayName)
{
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)
{
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)
{
BaseDataVariableState variable = new(parent)
{
BrowseName = browseDisplayName,
DisplayName = browseDisplayName,
Description = new Opc.Ua.LocalizedText("en", browseDisplayName),
DataType = new NodeId(DataTypes.String),
TypeDefinitionId = VariableTypeIds.BaseDataVariableType,
Value = value
};
variable.NodeId = New(SystemContext, variable);
AddPredefinedNode(SystemContext, variable);
if (parent != null)
{
parent.AddChild(variable);
}
return variable;
}
}
}