node-opcua-client-dynamic-extension-object 2.70.1 → 2.71.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/dist/client_dynamic_extension_object.d.ts +22 -0
  2. package/dist/client_dynamic_extension_object.js +1012 -0
  3. package/dist/client_dynamic_extension_object.js.map +1 -0
  4. package/dist/convert_data_type_definition_to_structuretype_schema.d.ts +12 -12
  5. package/dist/convert_data_type_definition_to_structuretype_schema.js +343 -331
  6. package/dist/convert_data_type_definition_to_structuretype_schema.js.map +1 -1
  7. package/dist/extra_data_type_manager.d.ts +16 -16
  8. package/dist/extra_data_type_manager.js +75 -75
  9. package/dist/get_extension_object_constructor.d.ts +7 -7
  10. package/dist/get_extension_object_constructor.js +37 -37
  11. package/dist/get_extra_data_type_manager.d.ts +3 -3
  12. package/dist/get_extra_data_type_manager.js +51 -51
  13. package/dist/index.d.ts +12 -12
  14. package/dist/index.js +28 -28
  15. package/dist/populate_data_type_manager.d.ts +3 -3
  16. package/dist/populate_data_type_manager.js +25 -25
  17. package/dist/private/find_encodings.d.ts +4 -4
  18. package/dist/private/find_encodings.js +55 -55
  19. package/dist/private/populate_data_type_manager_103.d.ts +9 -9
  20. package/dist/private/populate_data_type_manager_103.js +601 -601
  21. package/dist/private/populate_data_type_manager_104.d.ts +9 -9
  22. package/dist/private/populate_data_type_manager_104.js +182 -182
  23. package/dist/private/populate_data_type_manager_104.js.map +1 -1
  24. package/dist/promote_opaque_structure.d.ts +6 -6
  25. package/dist/promote_opaque_structure.js +41 -41
  26. package/dist/promote_opaque_structure_in_notification_data.d.ts +3 -3
  27. package/dist/promote_opaque_structure_in_notification_data.js +51 -51
  28. package/dist/resolve_dynamic_extension_object.d.ts +4 -4
  29. package/dist/resolve_dynamic_extension_object.js +105 -105
  30. package/dist/resolve_dynamic_extension_object.js.map +1 -1
  31. package/package.json +16 -16
  32. package/source/convert_data_type_definition_to_structuretype_schema.ts +43 -30
  33. package/source/private/populate_data_type_manager_104.ts +1 -0
  34. package/source/resolve_dynamic_extension_object.ts +13 -10
@@ -1,332 +1,344 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.convertDataTypeDefinitionToStructureTypeSchema = void 0;
13
- const node_opcua_assert_1 = require("node-opcua-assert");
14
- const node_opcua_data_model_1 = require("node-opcua-data-model");
15
- const node_opcua_debug_1 = require("node-opcua-debug");
16
- const node_opcua_factory_1 = require("node-opcua-factory");
17
- const node_opcua_nodeid_1 = require("node-opcua-nodeid");
18
- const node_opcua_pseudo_session_1 = require("node-opcua-pseudo-session");
19
- const node_opcua_status_code_1 = require("node-opcua-status-code");
20
- const node_opcua_types_1 = require("node-opcua-types");
21
- const node_opcua_extension_object_1 = require("node-opcua-extension-object");
22
- //
23
- const find_encodings_1 = require("./private/find_encodings");
24
- const debugLog = (0, node_opcua_debug_1.make_debugLog)(__filename);
25
- const errorLog = (0, node_opcua_debug_1.make_errorLog)(__filename);
26
- function findSuperType(session, dataTypeNodeId) {
27
- return __awaiter(this, void 0, void 0, function* () {
28
- const nodeToBrowse3 = {
29
- browseDirection: node_opcua_data_model_1.BrowseDirection.Inverse,
30
- includeSubtypes: false,
31
- nodeClassMask: node_opcua_data_model_1.NodeClassMask.DataType,
32
- nodeId: dataTypeNodeId,
33
- referenceTypeId: (0, node_opcua_nodeid_1.resolveNodeId)("HasSubtype"),
34
- resultMask: (0, node_opcua_data_model_1.makeResultMask)("NodeId | ReferenceType | BrowseName | NodeClass")
35
- };
36
- const result3 = yield (0, node_opcua_pseudo_session_1.browseAll)(session, nodeToBrowse3);
37
- /* istanbul ignore next */
38
- if (result3.statusCode !== node_opcua_status_code_1.StatusCodes.Good) {
39
- throw new Error("Cannot find superType for " + dataTypeNodeId.toString());
40
- }
41
- result3.references = result3.references || [];
42
- /* istanbul ignore next */
43
- if (result3.references.length !== 1) {
44
- console.log(result3.toString());
45
- throw new Error("Invalid dataType with more than one superType " + dataTypeNodeId.toString());
46
- }
47
- return result3.references[0].nodeId;
48
- });
49
- }
50
- function findDataTypeCategory(session, cache, dataTypeNodeId) {
51
- return __awaiter(this, void 0, void 0, function* () {
52
- const subTypeNodeId = yield findSuperType(session, dataTypeNodeId);
53
- debugLog("subTypeNodeId of ", dataTypeNodeId.toString(), " is ", subTypeNodeId.toString());
54
- const key = subTypeNodeId.toString();
55
- if (cache[key]) {
56
- return cache[key].category;
57
- }
58
- let category;
59
- if (subTypeNodeId.namespace === 0 && subTypeNodeId.value <= 29) {
60
- // well known node ID !
61
- switch (subTypeNodeId.value) {
62
- case 22 /* Structure */:
63
- category = node_opcua_factory_1.FieldCategory.complex;
64
- break;
65
- case 29 /* Enumeration */:
66
- category = node_opcua_factory_1.FieldCategory.enumeration;
67
- break;
68
- default:
69
- category = node_opcua_factory_1.FieldCategory.basic;
70
- break;
71
- }
72
- return category;
73
- }
74
- // must drill down ...
75
- return yield findDataTypeCategory(session, cache, subTypeNodeId);
76
- });
77
- }
78
- function findDataTypeBasicType(session, cache, dataTypeNodeId) {
79
- return __awaiter(this, void 0, void 0, function* () {
80
- const subTypeNodeId = yield findSuperType(session, dataTypeNodeId);
81
- debugLog("subTypeNodeId of ", dataTypeNodeId.toString(), " is ", subTypeNodeId.toString());
82
- const key = subTypeNodeId.toString();
83
- if (cache[key]) {
84
- return cache[key].schema;
85
- }
86
- if (subTypeNodeId.namespace === 0 && subTypeNodeId.value < 29) {
87
- switch (subTypeNodeId.value) {
88
- case 22: /* Structure */
89
- case 29 /* Enumeration */:
90
- throw new Error("Not expecting Structure or Enumeration");
91
- default:
92
- break;
93
- }
94
- const nameDataValue = yield session.read({
95
- attributeId: node_opcua_data_model_1.AttributeIds.BrowseName,
96
- nodeId: subTypeNodeId
97
- });
98
- const name = nameDataValue.value.value.name;
99
- return (0, node_opcua_factory_1.getBuildInType)(name);
100
- }
101
- // must drill down ...
102
- return yield findDataTypeBasicType(session, cache, subTypeNodeId);
103
- });
104
- }
105
- function readBrowseName(session, nodeId) {
106
- return __awaiter(this, void 0, void 0, function* () {
107
- const dataValue = yield session.read({ nodeId, attributeId: node_opcua_data_model_1.AttributeIds.BrowseName });
108
- if (dataValue.statusCode !== node_opcua_status_code_1.StatusCodes.Good) {
109
- const message = "cannot extract BrowseName of nodeId = " + nodeId.toString() + " statusCode = " + dataValue.statusCode.toString();
110
- debugLog(message);
111
- throw new Error(message);
112
- }
113
- return dataValue.value.value.name;
114
- });
115
- }
116
- function resolve2(session, dataTypeNodeId, dataTypeFactory, fieldTypeName, cache) {
117
- return __awaiter(this, void 0, void 0, function* () {
118
- const category = yield findDataTypeCategory(session, cache, dataTypeNodeId);
119
- debugLog(" type " + fieldTypeName + " has not been seen yet, let resolve it => (category = ", category, " )");
120
- let schema = undefined;
121
- switch (category) {
122
- case node_opcua_factory_1.FieldCategory.basic:
123
- schema = yield findDataTypeBasicType(session, cache, dataTypeNodeId);
124
- /* istanbul ignore next */
125
- if (!schema) {
126
- errorLog("Cannot find basic type " + fieldTypeName);
127
- }
128
- break;
129
- default:
130
- case node_opcua_factory_1.FieldCategory.enumeration:
131
- case node_opcua_factory_1.FieldCategory.complex:
132
- {
133
- const dataTypeDefinitionDataValue = yield session.read({
134
- attributeId: node_opcua_data_model_1.AttributeIds.DataTypeDefinition,
135
- nodeId: dataTypeNodeId
136
- });
137
- /* istanbul ignore next */
138
- if (dataTypeDefinitionDataValue.statusCode !== node_opcua_status_code_1.StatusCodes.Good) {
139
- throw new Error(" Cannot find dataType Definition ! with nodeId =" + dataTypeNodeId.toString());
140
- }
141
- const definition = dataTypeDefinitionDataValue.value.value;
142
- const convertIn64ToInteger = (a) => a[1];
143
- const convert = (fields) => {
144
- const retVal = {};
145
- fields && fields.forEach((field) => (retVal[field.name || ""] = convertIn64ToInteger(field.value)));
146
- return retVal;
147
- };
148
- if (category === node_opcua_factory_1.FieldCategory.enumeration) {
149
- if (definition instanceof node_opcua_types_1.EnumDefinition) {
150
- const e = new node_opcua_factory_1.EnumerationDefinitionSchema({
151
- enumValues: convert(definition.fields),
152
- name: fieldTypeName
153
- });
154
- dataTypeFactory.registerEnumeration(e);
155
- schema = e;
156
- }
157
- }
158
- else {
159
- schema = yield convertDataTypeDefinitionToStructureTypeSchema(session, dataTypeNodeId, fieldTypeName, definition, dataTypeFactory, cache);
160
- }
161
- // xx const schema1 = dataTypeFactory.getStructuredTypeSchema(fieldTypeName);
162
- }
163
- break;
164
- }
165
- return { schema, category };
166
- });
167
- }
168
- // eslint-disable-next-line max-statements
169
- function resolveFieldType(session, dataTypeNodeId, dataTypeFactory, cache) {
170
- return __awaiter(this, void 0, void 0, function* () {
171
- if (dataTypeNodeId.namespace === 0 && dataTypeNodeId.value === 22) {
172
- // ERN return null;
173
- const category = node_opcua_factory_1.FieldCategory.complex;
174
- const fieldTypeName = "Structure";
175
- const schema = node_opcua_extension_object_1.ExtensionObject.schema;
176
- return {
177
- category,
178
- fieldTypeName,
179
- schema
180
- };
181
- }
182
- const key = dataTypeNodeId.toString();
183
- const v = cache[key];
184
- if (v) {
185
- return v;
186
- }
187
- if (dataTypeNodeId.value === 0) {
188
- const v3 = {
189
- category: node_opcua_factory_1.FieldCategory.basic,
190
- fieldTypeName: "Variant",
191
- schema: dataTypeFactory.getSimpleType("Variant")
192
- };
193
- cache[key] = v3;
194
- return v3;
195
- }
196
- const fieldTypeName = yield readBrowseName(session, dataTypeNodeId);
197
- let schema;
198
- let category = node_opcua_factory_1.FieldCategory.enumeration;
199
- if (dataTypeFactory.hasStructuredType(fieldTypeName)) {
200
- schema = dataTypeFactory.getStructuredTypeSchema(fieldTypeName);
201
- category = node_opcua_factory_1.FieldCategory.complex;
202
- }
203
- else if (dataTypeFactory.hasSimpleType(fieldTypeName)) {
204
- category = node_opcua_factory_1.FieldCategory.basic;
205
- schema = dataTypeFactory.getSimpleType(fieldTypeName);
206
- }
207
- else if (dataTypeFactory.hasEnumeration(fieldTypeName)) {
208
- category = node_opcua_factory_1.FieldCategory.enumeration;
209
- schema = dataTypeFactory.getEnumeration(fieldTypeName);
210
- }
211
- else {
212
- debugLog(" type " + fieldTypeName + " has not been seen yet, let resolve it");
213
- const res = yield resolve2(session, dataTypeNodeId, dataTypeFactory, fieldTypeName, cache);
214
- schema = res.schema;
215
- category = res.category;
216
- }
217
- /* istanbul ignore next */
218
- if (!schema) {
219
- throw new Error("expecting a schema here fieldTypeName=" + fieldTypeName + " " + dataTypeNodeId.toString() + " category = " + category);
220
- }
221
- const v2 = {
222
- category,
223
- fieldTypeName,
224
- schema
225
- };
226
- cache[key] = v2;
227
- return v2;
228
- });
229
- }
230
- function _setupEncodings(session, dataTypeNodeId, schema) {
231
- return __awaiter(this, void 0, void 0, function* () {
232
- // read abstract flag
233
- const isAbstractDV = yield session.read({ nodeId: dataTypeNodeId, attributeId: node_opcua_data_model_1.AttributeIds.IsAbstract });
234
- schema.dataTypeNodeId = dataTypeNodeId;
235
- schema.id = dataTypeNodeId;
236
- if (isAbstractDV.statusCode === node_opcua_status_code_1.StatusCodes.Good && isAbstractDV.value.value === false) {
237
- const encodings = yield (0, find_encodings_1._findEncodings)(session, dataTypeNodeId);
238
- schema.encodingDefaultBinary = (0, node_opcua_nodeid_1.makeExpandedNodeId)(encodings.binaryEncodingNodeId);
239
- schema.encodingDefaultXml = (0, node_opcua_nodeid_1.makeExpandedNodeId)(encodings.xmlEncodingNodeId);
240
- schema.encodingDefaultJson = (0, node_opcua_nodeid_1.makeExpandedNodeId)(encodings.jsonEncodingNodeId);
241
- }
242
- return schema;
243
- });
244
- }
245
- // eslint-disable-next-line max-statements
246
- function convertDataTypeDefinitionToStructureTypeSchema(session, dataTypeNodeId, name, definition, dataTypeFactory, cache) {
247
- return __awaiter(this, void 0, void 0, function* () {
248
- if (definition instanceof node_opcua_types_1.StructureDefinition) {
249
- const fields = [];
250
- const isUnion = definition.structureType === node_opcua_types_1.StructureType.Union;
251
- switch (definition.structureType) {
252
- case node_opcua_types_1.StructureType.Union:
253
- // xx console.log("Union Found : ", name);
254
- fields.push({
255
- fieldType: "UInt32",
256
- name: "SwitchField"
257
- });
258
- break;
259
- case node_opcua_types_1.StructureType.Structure:
260
- case node_opcua_types_1.StructureType.StructureWithOptionalFields:
261
- break;
262
- }
263
- let switchValue = 1;
264
- let switchBit = 0;
265
- const bitFields = isUnion ? undefined : [];
266
- const postActions = [];
267
- for (const fieldD of definition.fields) {
268
- let field;
269
- ({ field, switchBit, switchValue } = createField(fieldD, switchBit, bitFields, isUnion, switchValue));
270
- if (fieldD.dataType.value === dataTypeNodeId.value && fieldD.dataType.namespace === dataTypeNodeId.namespace) {
271
- // this is a structure with a field of the same type
272
- // push an empty placeholder that we will fill later
273
- const fieldTypeName = yield readBrowseName(session, dataTypeNodeId);
274
- (field.fieldType = fieldTypeName), (field.category = node_opcua_factory_1.FieldCategory.complex);
275
- fields.push(field);
276
- const capturedField = field;
277
- postActions.push((schema) => {
278
- capturedField.schema = schema;
279
- });
280
- continue;
281
- }
282
- const rt = (yield resolveFieldType(session, fieldD.dataType, dataTypeFactory, cache));
283
- if (!rt) {
284
- errorLog("convertDataTypeDefinitionToStructureTypeSchema cannot handle field", fieldD.name, "in", name, "because " + fieldD.dataType.toString() + " cannot be resolved");
285
- continue;
286
- }
287
- const { schema, category, fieldTypeName } = rt;
288
- (field.fieldType = fieldTypeName), (field.category = category);
289
- field.schema = schema;
290
- fields.push(field);
291
- }
292
- const a = yield resolveFieldType(session, definition.baseDataType, dataTypeFactory, cache);
293
- const baseType = a ? a.fieldTypeName : "ExtensionObject";
294
- const os = new node_opcua_factory_1.StructuredTypeSchema({
295
- baseType,
296
- bitFields,
297
- fields,
298
- id: 0,
299
- name
300
- });
301
- const structuredTypeSchema = yield _setupEncodings(session, dataTypeNodeId, os);
302
- postActions.forEach((action) => action(structuredTypeSchema));
303
- return structuredTypeSchema;
304
- }
305
- throw new Error("Not Implemented");
306
- function createField(fieldD, switchBit, bitFields, isUnion, switchValue) {
307
- const field = {
308
- fieldType: "",
309
- name: fieldD.name,
310
- schema: null
311
- };
312
- if (fieldD.isOptional) {
313
- field.switchBit = switchBit++;
314
- bitFields === null || bitFields === void 0 ? void 0 : bitFields.push({ name: fieldD.name + "Specified", length: 1 });
315
- }
316
- if (isUnion) {
317
- field.switchValue = switchValue;
318
- switchValue += 1;
319
- }
320
- (0, node_opcua_assert_1.assert)(fieldD.valueRank === -1 || fieldD.valueRank === 1 || fieldD.valueRank === 0);
321
- if (fieldD.valueRank === 1) {
322
- field.isArray = true;
323
- }
324
- else {
325
- field.isArray = false;
326
- }
327
- return { field, switchBit, switchValue };
328
- }
329
- });
330
- }
331
- exports.convertDataTypeDefinitionToStructureTypeSchema = convertDataTypeDefinitionToStructureTypeSchema;
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.convertDataTypeDefinitionToStructureTypeSchema = void 0;
13
+ const node_opcua_assert_1 = require("node-opcua-assert");
14
+ const node_opcua_data_model_1 = require("node-opcua-data-model");
15
+ const node_opcua_debug_1 = require("node-opcua-debug");
16
+ const node_opcua_factory_1 = require("node-opcua-factory");
17
+ const node_opcua_nodeid_1 = require("node-opcua-nodeid");
18
+ const node_opcua_pseudo_session_1 = require("node-opcua-pseudo-session");
19
+ const node_opcua_status_code_1 = require("node-opcua-status-code");
20
+ const node_opcua_types_1 = require("node-opcua-types");
21
+ const node_opcua_extension_object_1 = require("node-opcua-extension-object");
22
+ //
23
+ const node_opcua_variant_1 = require("node-opcua-variant");
24
+ const find_encodings_1 = require("./private/find_encodings");
25
+ const debugLog = (0, node_opcua_debug_1.make_debugLog)(__filename);
26
+ const errorLog = (0, node_opcua_debug_1.make_errorLog)(__filename);
27
+ function findSuperType(session, dataTypeNodeId) {
28
+ return __awaiter(this, void 0, void 0, function* () {
29
+ const nodeToBrowse3 = {
30
+ browseDirection: node_opcua_data_model_1.BrowseDirection.Inverse,
31
+ includeSubtypes: false,
32
+ nodeClassMask: node_opcua_data_model_1.NodeClassMask.DataType,
33
+ nodeId: dataTypeNodeId,
34
+ referenceTypeId: (0, node_opcua_nodeid_1.resolveNodeId)("HasSubtype"),
35
+ resultMask: (0, node_opcua_data_model_1.makeResultMask)("NodeId | ReferenceType | BrowseName | NodeClass")
36
+ };
37
+ const result3 = yield (0, node_opcua_pseudo_session_1.browseAll)(session, nodeToBrowse3);
38
+ /* istanbul ignore next */
39
+ if (result3.statusCode !== node_opcua_status_code_1.StatusCodes.Good) {
40
+ throw new Error("Cannot find superType for " + dataTypeNodeId.toString());
41
+ }
42
+ result3.references = result3.references || [];
43
+ /* istanbul ignore next */
44
+ if (result3.references.length !== 1) {
45
+ console.log(result3.toString());
46
+ throw new Error("Invalid dataType with more than one superType " + dataTypeNodeId.toString());
47
+ }
48
+ return result3.references[0].nodeId;
49
+ });
50
+ }
51
+ function findDataTypeCategory(session, cache, dataTypeNodeId) {
52
+ return __awaiter(this, void 0, void 0, function* () {
53
+ const subTypeNodeId = yield findSuperType(session, dataTypeNodeId);
54
+ debugLog("subTypeNodeId of ", dataTypeNodeId.toString(), " is ", subTypeNodeId.toString());
55
+ const key = subTypeNodeId.toString();
56
+ if (cache[key]) {
57
+ return cache[key].category;
58
+ }
59
+ let category;
60
+ if (subTypeNodeId.namespace === 0 && subTypeNodeId.value <= 29) {
61
+ // well known node ID !
62
+ switch (subTypeNodeId.value) {
63
+ case 22 /* Structure */:
64
+ category = node_opcua_factory_1.FieldCategory.complex;
65
+ break;
66
+ case 29 /* Enumeration */:
67
+ category = node_opcua_factory_1.FieldCategory.enumeration;
68
+ break;
69
+ default:
70
+ category = node_opcua_factory_1.FieldCategory.basic;
71
+ break;
72
+ }
73
+ return category;
74
+ }
75
+ // must drill down ...
76
+ return yield findDataTypeCategory(session, cache, subTypeNodeId);
77
+ });
78
+ }
79
+ function findDataTypeBasicType(session, cache, dataTypeNodeId) {
80
+ return __awaiter(this, void 0, void 0, function* () {
81
+ const subTypeNodeId = yield findSuperType(session, dataTypeNodeId);
82
+ debugLog("subTypeNodeId of ", dataTypeNodeId.toString(), " is ", subTypeNodeId.toString());
83
+ const key = subTypeNodeId.toString();
84
+ if (cache[key]) {
85
+ return cache[key].schema;
86
+ }
87
+ if (subTypeNodeId.namespace === 0 && subTypeNodeId.value < 29) {
88
+ switch (subTypeNodeId.value) {
89
+ case 22: /* Structure */
90
+ case 29 /* Enumeration */:
91
+ throw new Error("Not expecting Structure or Enumeration");
92
+ default:
93
+ break;
94
+ }
95
+ const nameDataValue = yield session.read({
96
+ attributeId: node_opcua_data_model_1.AttributeIds.BrowseName,
97
+ nodeId: subTypeNodeId
98
+ });
99
+ const name = nameDataValue.value.value.name;
100
+ return (0, node_opcua_factory_1.getBuildInType)(name);
101
+ }
102
+ // must drill down ...
103
+ return yield findDataTypeBasicType(session, cache, subTypeNodeId);
104
+ });
105
+ }
106
+ function readBrowseName(session, nodeId) {
107
+ return __awaiter(this, void 0, void 0, function* () {
108
+ const dataValue = yield session.read({ nodeId, attributeId: node_opcua_data_model_1.AttributeIds.BrowseName });
109
+ if (dataValue.statusCode !== node_opcua_status_code_1.StatusCodes.Good) {
110
+ const message = "cannot extract BrowseName of nodeId = " + nodeId.toString() + " statusCode = " + dataValue.statusCode.toString();
111
+ debugLog(message);
112
+ throw new Error(message);
113
+ }
114
+ return dataValue.value.value.name;
115
+ });
116
+ }
117
+ function resolve2(session, dataTypeNodeId, dataTypeFactory, fieldTypeName, cache) {
118
+ return __awaiter(this, void 0, void 0, function* () {
119
+ const category = yield findDataTypeCategory(session, cache, dataTypeNodeId);
120
+ debugLog(" type " + fieldTypeName + " has not been seen yet, let resolve it => (category = ", category, " )");
121
+ let schema = undefined;
122
+ switch (category) {
123
+ case node_opcua_factory_1.FieldCategory.basic:
124
+ schema = yield findDataTypeBasicType(session, cache, dataTypeNodeId);
125
+ /* istanbul ignore next */
126
+ if (!schema) {
127
+ errorLog("Cannot find basic type " + fieldTypeName);
128
+ }
129
+ break;
130
+ default:
131
+ case node_opcua_factory_1.FieldCategory.enumeration:
132
+ case node_opcua_factory_1.FieldCategory.complex:
133
+ {
134
+ const dataTypeDefinitionDataValue = yield session.read({
135
+ attributeId: node_opcua_data_model_1.AttributeIds.DataTypeDefinition,
136
+ nodeId: dataTypeNodeId
137
+ });
138
+ /* istanbul ignore next */
139
+ if (dataTypeDefinitionDataValue.statusCode !== node_opcua_status_code_1.StatusCodes.Good) {
140
+ throw new Error(" Cannot find dataType Definition ! with nodeId =" + dataTypeNodeId.toString());
141
+ }
142
+ const definition = dataTypeDefinitionDataValue.value.value;
143
+ const convertIn64ToInteger = (a) => a[1];
144
+ const convert = (fields) => {
145
+ const retVal = {};
146
+ fields && fields.forEach((field) => (retVal[field.name || ""] = convertIn64ToInteger(field.value)));
147
+ return retVal;
148
+ };
149
+ if (category === node_opcua_factory_1.FieldCategory.enumeration) {
150
+ if (definition instanceof node_opcua_types_1.EnumDefinition) {
151
+ const e = new node_opcua_factory_1.EnumerationDefinitionSchema({
152
+ enumValues: convert(definition.fields),
153
+ name: fieldTypeName
154
+ });
155
+ dataTypeFactory.registerEnumeration(e);
156
+ schema = e;
157
+ }
158
+ }
159
+ else {
160
+ schema = yield convertDataTypeDefinitionToStructureTypeSchema(session, dataTypeNodeId, fieldTypeName, definition, dataTypeFactory, cache);
161
+ }
162
+ // xx const schema1 = dataTypeFactory.getStructuredTypeSchema(fieldTypeName);
163
+ }
164
+ break;
165
+ }
166
+ return { schema, category };
167
+ });
168
+ }
169
+ // eslint-disable-next-line max-statements
170
+ function resolveFieldType(session, dataTypeNodeId, dataTypeFactory, cache) {
171
+ return __awaiter(this, void 0, void 0, function* () {
172
+ if (dataTypeNodeId.namespace === 0 && dataTypeNodeId.value === 22) {
173
+ // ERN return null;
174
+ const category = node_opcua_factory_1.FieldCategory.complex;
175
+ const fieldTypeName = "Structure";
176
+ const schema = node_opcua_extension_object_1.ExtensionObject.schema;
177
+ return {
178
+ category,
179
+ fieldTypeName,
180
+ schema
181
+ };
182
+ }
183
+ const key = dataTypeNodeId.toString();
184
+ const v = cache[key];
185
+ if (v) {
186
+ return v;
187
+ }
188
+ if (dataTypeNodeId.value === 0) {
189
+ const v3 = {
190
+ category: node_opcua_factory_1.FieldCategory.basic,
191
+ fieldTypeName: "Variant",
192
+ schema: dataTypeFactory.getSimpleType("Variant")
193
+ };
194
+ cache[key] = v3;
195
+ return v3;
196
+ }
197
+ const fieldTypeName = yield readBrowseName(session, dataTypeNodeId);
198
+ let schema;
199
+ let category = node_opcua_factory_1.FieldCategory.enumeration;
200
+ if (dataTypeFactory.hasStructuredType(fieldTypeName)) {
201
+ schema = dataTypeFactory.getStructuredTypeSchema(fieldTypeName);
202
+ category = node_opcua_factory_1.FieldCategory.complex;
203
+ }
204
+ else if (dataTypeFactory.hasSimpleType(fieldTypeName)) {
205
+ category = node_opcua_factory_1.FieldCategory.basic;
206
+ schema = dataTypeFactory.getSimpleType(fieldTypeName);
207
+ }
208
+ else if (dataTypeFactory.hasEnumeration(fieldTypeName)) {
209
+ category = node_opcua_factory_1.FieldCategory.enumeration;
210
+ schema = dataTypeFactory.getEnumeration(fieldTypeName);
211
+ }
212
+ else {
213
+ debugLog(" type " + fieldTypeName + " has not been seen yet, let resolve it");
214
+ const res = yield resolve2(session, dataTypeNodeId, dataTypeFactory, fieldTypeName, cache);
215
+ schema = res.schema;
216
+ category = res.category;
217
+ }
218
+ /* istanbul ignore next */
219
+ if (!schema) {
220
+ throw new Error("expecting a schema here fieldTypeName=" + fieldTypeName + " " + dataTypeNodeId.toString() + " category = " + category);
221
+ }
222
+ const v2 = {
223
+ category,
224
+ fieldTypeName,
225
+ schema
226
+ };
227
+ cache[key] = v2;
228
+ return v2;
229
+ });
230
+ }
231
+ function _setupEncodings(session, dataTypeNodeId, schema) {
232
+ return __awaiter(this, void 0, void 0, function* () {
233
+ // read abstract flag
234
+ const isAbstractDV = yield session.read({ nodeId: dataTypeNodeId, attributeId: node_opcua_data_model_1.AttributeIds.IsAbstract });
235
+ schema.dataTypeNodeId = dataTypeNodeId;
236
+ schema.id = dataTypeNodeId;
237
+ if (isAbstractDV.statusCode === node_opcua_status_code_1.StatusCodes.Good && isAbstractDV.value.value === false) {
238
+ const encodings = yield (0, find_encodings_1._findEncodings)(session, dataTypeNodeId);
239
+ schema.encodingDefaultBinary = (0, node_opcua_nodeid_1.makeExpandedNodeId)(encodings.binaryEncodingNodeId);
240
+ schema.encodingDefaultXml = (0, node_opcua_nodeid_1.makeExpandedNodeId)(encodings.xmlEncodingNodeId);
241
+ schema.encodingDefaultJson = (0, node_opcua_nodeid_1.makeExpandedNodeId)(encodings.jsonEncodingNodeId);
242
+ }
243
+ return schema;
244
+ });
245
+ }
246
+ // eslint-disable-next-line max-statements
247
+ function convertDataTypeDefinitionToStructureTypeSchema(session, dataTypeNodeId, name, definition, dataTypeFactory, cache) {
248
+ var _a;
249
+ return __awaiter(this, void 0, void 0, function* () {
250
+ if (definition instanceof node_opcua_types_1.StructureDefinition) {
251
+ let fieldCountToIgnore = 0;
252
+ let base = (_a = dataTypeFactory.getConstructorForDataType(definition.baseDataType)) === null || _a === void 0 ? void 0 : _a.schema;
253
+ while (base && !(base.dataTypeNodeId.value === node_opcua_variant_1.DataType.ExtensionObject && base.dataTypeNodeId.namespace === 0)) {
254
+ fieldCountToIgnore += base._possibleFields.length;
255
+ base = base._baseSchema;
256
+ }
257
+ const fields = [];
258
+ const isUnion = definition.structureType === node_opcua_types_1.StructureType.Union;
259
+ switch (definition.structureType) {
260
+ case node_opcua_types_1.StructureType.Union:
261
+ // xx console.log("Union Found : ", name);
262
+ fields.push({
263
+ fieldType: "UInt32",
264
+ name: "SwitchField"
265
+ });
266
+ break;
267
+ case node_opcua_types_1.StructureType.Structure:
268
+ case node_opcua_types_1.StructureType.StructureWithOptionalFields:
269
+ break;
270
+ }
271
+ let switchValue = 1;
272
+ let switchBit = 0;
273
+ const bitFields = isUnion ? undefined : [];
274
+ const postActions = [];
275
+ if (definition.fields) {
276
+ for (let i = fieldCountToIgnore; i < definition.fields.length; i++) {
277
+ const fieldD = definition.fields[i];
278
+ // we need to skip fields that have already been handled in base class
279
+ let field;
280
+ ({ field, switchBit, switchValue } = createField(fieldD, switchBit, bitFields, isUnion, switchValue));
281
+ if (fieldD.dataType.value === dataTypeNodeId.value && fieldD.dataType.namespace === dataTypeNodeId.namespace) {
282
+ // this is a structure with a field of the same type
283
+ // push an empty placeholder that we will fill later
284
+ const fieldTypeName = yield readBrowseName(session, dataTypeNodeId);
285
+ (field.fieldType = fieldTypeName), (field.category = node_opcua_factory_1.FieldCategory.complex);
286
+ fields.push(field);
287
+ const capturedField = field;
288
+ postActions.push((schema) => {
289
+ capturedField.schema = schema;
290
+ });
291
+ continue;
292
+ }
293
+ const rt = (yield resolveFieldType(session, fieldD.dataType, dataTypeFactory, cache));
294
+ if (!rt) {
295
+ errorLog("convertDataTypeDefinitionToStructureTypeSchema cannot handle field", fieldD.name, "in", name, "because " + fieldD.dataType.toString() + " cannot be resolved");
296
+ continue;
297
+ }
298
+ const { schema, category, fieldTypeName } = rt;
299
+ (field.fieldType = fieldTypeName), (field.category = category);
300
+ field.schema = schema;
301
+ fields.push(field);
302
+ }
303
+ }
304
+ const a = yield resolveFieldType(session, definition.baseDataType, dataTypeFactory, cache);
305
+ const baseType = a ? a.fieldTypeName : "ExtensionObject";
306
+ const os = new node_opcua_factory_1.StructuredTypeSchema({
307
+ baseType,
308
+ bitFields,
309
+ fields,
310
+ id: 0,
311
+ name
312
+ });
313
+ const structuredTypeSchema = yield _setupEncodings(session, dataTypeNodeId, os);
314
+ postActions.forEach((action) => action(structuredTypeSchema));
315
+ return structuredTypeSchema;
316
+ }
317
+ throw new Error("Not Implemented");
318
+ function createField(fieldD, switchBit, bitFields, isUnion, switchValue) {
319
+ const field = {
320
+ fieldType: "",
321
+ name: fieldD.name,
322
+ schema: null
323
+ };
324
+ if (fieldD.isOptional) {
325
+ field.switchBit = switchBit++;
326
+ bitFields === null || bitFields === void 0 ? void 0 : bitFields.push({ name: fieldD.name + "Specified", length: 1 });
327
+ }
328
+ if (isUnion) {
329
+ field.switchValue = switchValue;
330
+ switchValue += 1;
331
+ }
332
+ (0, node_opcua_assert_1.assert)(fieldD.valueRank === -1 || fieldD.valueRank === 1 || fieldD.valueRank === 0);
333
+ if (fieldD.valueRank === 1) {
334
+ field.isArray = true;
335
+ }
336
+ else {
337
+ field.isArray = false;
338
+ }
339
+ return { field, switchBit, switchValue };
340
+ }
341
+ });
342
+ }
343
+ exports.convertDataTypeDefinitionToStructureTypeSchema = convertDataTypeDefinitionToStructureTypeSchema;
332
344
  //# sourceMappingURL=convert_data_type_definition_to_structuretype_schema.js.map