node-opcua-client-dynamic-extension-object 2.113.0 → 2.114.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.
@@ -1,13 +1,4 @@
1
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
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.convertDataTypeDefinitionToStructureTypeSchema = void 0;
13
4
  const node_opcua_data_model_1 = require("node-opcua-data-model");
@@ -25,159 +16,149 @@ const find_encodings_1 = require("./private/find_encodings");
25
16
  const debugLog = (0, node_opcua_debug_1.make_debugLog)(__filename);
26
17
  const errorLog = (0, node_opcua_debug_1.make_errorLog)(__filename);
27
18
  const warningLog = (0, node_opcua_debug_1.make_warningLog)(__filename);
28
- function findSuperType(session, dataTypeNodeId) {
29
- return __awaiter(this, void 0, void 0, function* () {
30
- if (dataTypeNodeId.namespace === 0 && dataTypeNodeId.value === 24) {
31
- // BaseDataType !
32
- return (0, node_opcua_nodeid_2.coerceNodeId)(0);
33
- }
34
- const nodeToBrowse3 = {
35
- browseDirection: node_opcua_data_model_1.BrowseDirection.Inverse,
36
- includeSubtypes: false,
37
- nodeClassMask: node_opcua_data_model_1.NodeClassMask.DataType,
38
- nodeId: dataTypeNodeId,
39
- referenceTypeId: (0, node_opcua_nodeid_2.resolveNodeId)("HasSubtype"),
40
- resultMask: (0, node_opcua_data_model_1.makeResultMask)("NodeId | ReferenceType | BrowseName | NodeClass")
41
- };
42
- const result3 = yield (0, node_opcua_pseudo_session_1.browseAll)(session, nodeToBrowse3);
43
- /* istanbul ignore next */
44
- if (result3.statusCode.isNotGood()) {
45
- throw new Error("Cannot find superType for " + dataTypeNodeId.toString());
46
- }
47
- result3.references = result3.references || [];
48
- /* istanbul ignore next */
49
- if (result3.references.length !== 1) {
50
- errorLog("Invalid dataType with more than one (or 0) superType", result3.toString());
51
- throw new Error("Invalid dataType with more than one (or 0) superType " + dataTypeNodeId.toString() + " l=" + result3.references.length);
52
- }
53
- return result3.references[0].nodeId;
54
- });
19
+ async function findSuperType(session, dataTypeNodeId) {
20
+ if (dataTypeNodeId.namespace === 0 && dataTypeNodeId.value === 24) {
21
+ // BaseDataType !
22
+ return (0, node_opcua_nodeid_2.coerceNodeId)(0);
23
+ }
24
+ const nodeToBrowse3 = {
25
+ browseDirection: node_opcua_data_model_1.BrowseDirection.Inverse,
26
+ includeSubtypes: false,
27
+ nodeClassMask: node_opcua_data_model_1.NodeClassMask.DataType,
28
+ nodeId: dataTypeNodeId,
29
+ referenceTypeId: (0, node_opcua_nodeid_2.resolveNodeId)("HasSubtype"),
30
+ resultMask: (0, node_opcua_data_model_1.makeResultMask)("NodeId | ReferenceType | BrowseName | NodeClass")
31
+ };
32
+ const result3 = await (0, node_opcua_pseudo_session_1.browseAll)(session, nodeToBrowse3);
33
+ /* istanbul ignore next */
34
+ if (result3.statusCode.isNotGood()) {
35
+ throw new Error("Cannot find superType for " + dataTypeNodeId.toString());
36
+ }
37
+ result3.references = result3.references || [];
38
+ /* istanbul ignore next */
39
+ if (result3.references.length !== 1) {
40
+ errorLog("Invalid dataType with more than one (or 0) superType", result3.toString());
41
+ throw new Error("Invalid dataType with more than one (or 0) superType " + dataTypeNodeId.toString() + " l=" + result3.references.length);
42
+ }
43
+ return result3.references[0].nodeId;
55
44
  }
56
- function findDataTypeCategory(session, cache, dataTypeNodeId) {
57
- return __awaiter(this, void 0, void 0, function* () {
58
- const subTypeNodeId = yield findSuperType(session, dataTypeNodeId);
59
- debugLog("subTypeNodeId of ", dataTypeNodeId.toString(), " is ", subTypeNodeId.toString());
60
- const key = subTypeNodeId.toString();
61
- if (cache[key]) {
62
- return cache[key].category;
63
- }
64
- let category;
65
- const n = subTypeNodeId;
66
- if (n.identifierType === node_opcua_nodeid_1.NodeIdType.NUMERIC && n.namespace === 0 && n.value <= 29) {
67
- // well known node ID !
68
- switch (n.value) {
69
- case 22 /* Structure */:
70
- category = node_opcua_factory_1.FieldCategory.complex;
71
- break;
72
- case 29 /* Enumeration */:
73
- category = node_opcua_factory_1.FieldCategory.enumeration;
74
- break;
75
- default:
76
- category = node_opcua_factory_1.FieldCategory.basic;
77
- break;
78
- }
79
- return category;
45
+ async function findDataTypeCategory(session, cache, dataTypeNodeId) {
46
+ const subTypeNodeId = await findSuperType(session, dataTypeNodeId);
47
+ debugLog("subTypeNodeId of ", dataTypeNodeId.toString(), " is ", subTypeNodeId.toString());
48
+ const key = subTypeNodeId.toString();
49
+ if (cache[key]) {
50
+ return cache[key].category;
51
+ }
52
+ let category;
53
+ const n = subTypeNodeId;
54
+ if (n.identifierType === node_opcua_nodeid_1.NodeIdType.NUMERIC && n.namespace === 0 && n.value <= 29) {
55
+ // well known node ID !
56
+ switch (n.value) {
57
+ case 22 /* Structure */:
58
+ category = node_opcua_factory_1.FieldCategory.complex;
59
+ break;
60
+ case 29 /* Enumeration */:
61
+ category = node_opcua_factory_1.FieldCategory.enumeration;
62
+ break;
63
+ default:
64
+ category = node_opcua_factory_1.FieldCategory.basic;
65
+ break;
80
66
  }
81
- // must drill down ...
82
- return yield findDataTypeCategory(session, cache, subTypeNodeId);
83
- });
67
+ return category;
68
+ }
69
+ // must drill down ...
70
+ return await findDataTypeCategory(session, cache, subTypeNodeId);
84
71
  }
85
- function findDataTypeBasicType(session, cache, dataTypeNodeId) {
86
- return __awaiter(this, void 0, void 0, function* () {
87
- const subTypeNodeId = yield findSuperType(session, dataTypeNodeId);
88
- debugLog("subTypeNodeId of ", dataTypeNodeId.toString(), " is ", subTypeNodeId.toString());
89
- const key = subTypeNodeId.toString();
90
- if (cache[key]) {
91
- return cache[key].schema;
92
- }
93
- const n = subTypeNodeId;
94
- if (n.identifierType === node_opcua_nodeid_1.NodeIdType.NUMERIC && n.namespace === 0 && n.value < 29) {
95
- switch (n.value) {
96
- case 22: /* Structure */
97
- case 29 /* Enumeration */:
98
- throw new Error("Not expecting Structure or Enumeration");
99
- default:
100
- break;
101
- }
102
- const nameDataValue = yield session.read({
103
- attributeId: node_opcua_data_model_1.AttributeIds.BrowseName,
104
- nodeId: subTypeNodeId
105
- });
106
- const name = nameDataValue.value.value.name;
107
- return (0, node_opcua_factory_1.getBuiltInType)(name);
72
+ async function findDataTypeBasicType(session, cache, dataTypeNodeId) {
73
+ const subTypeNodeId = await findSuperType(session, dataTypeNodeId);
74
+ debugLog("subTypeNodeId of ", dataTypeNodeId.toString(), " is ", subTypeNodeId.toString());
75
+ const key = subTypeNodeId.toString();
76
+ if (cache[key]) {
77
+ return cache[key].schema;
78
+ }
79
+ const n = subTypeNodeId;
80
+ if (n.identifierType === node_opcua_nodeid_1.NodeIdType.NUMERIC && n.namespace === 0 && n.value < 29) {
81
+ switch (n.value) {
82
+ case 22: /* Structure */
83
+ case 29 /* Enumeration */:
84
+ throw new Error("Not expecting Structure or Enumeration");
85
+ default:
86
+ break;
108
87
  }
109
- // must drill down ...
110
- return yield findDataTypeBasicType(session, cache, subTypeNodeId);
111
- });
88
+ const nameDataValue = await session.read({
89
+ attributeId: node_opcua_data_model_1.AttributeIds.BrowseName,
90
+ nodeId: subTypeNodeId
91
+ });
92
+ const name = nameDataValue.value.value.name;
93
+ return (0, node_opcua_factory_1.getBuiltInType)(name);
94
+ }
95
+ // must drill down ...
96
+ return await findDataTypeBasicType(session, cache, subTypeNodeId);
112
97
  }
113
- function readBrowseName(session, nodeId) {
114
- return __awaiter(this, void 0, void 0, function* () {
115
- const dataValue = yield session.read({ nodeId, attributeId: node_opcua_data_model_1.AttributeIds.BrowseName });
116
- if (dataValue.statusCode.isNotGood()) {
117
- const message = "cannot extract BrowseName of nodeId = " + nodeId.toString() + " statusCode = " + dataValue.statusCode.toString();
118
- debugLog(message);
119
- throw new Error(message);
120
- }
121
- return dataValue.value.value.name;
122
- });
98
+ async function readBrowseName(session, nodeId) {
99
+ const dataValue = await session.read({ nodeId, attributeId: node_opcua_data_model_1.AttributeIds.BrowseName });
100
+ if (dataValue.statusCode.isNotGood()) {
101
+ const message = "cannot extract BrowseName of nodeId = " + nodeId.toString() + " statusCode = " + dataValue.statusCode.toString();
102
+ debugLog(message);
103
+ throw new Error(message);
104
+ }
105
+ return dataValue.value.value.name;
123
106
  }
124
- function resolve2(session, dataTypeNodeId, dataTypeFactory, fieldTypeName, cache) {
125
- return __awaiter(this, void 0, void 0, function* () {
126
- const category = yield findDataTypeCategory(session, cache, dataTypeNodeId);
127
- debugLog(" type " + fieldTypeName + " has not been seen yet, let resolve it => (category = ", category, " )");
128
- let schema = undefined;
129
- switch (category) {
130
- case node_opcua_factory_1.FieldCategory.basic:
131
- schema = yield findDataTypeBasicType(session, cache, dataTypeNodeId);
107
+ async function resolve2(session, dataTypeNodeId, dataTypeFactory, fieldTypeName, cache) {
108
+ const category = await findDataTypeCategory(session, cache, dataTypeNodeId);
109
+ debugLog(" type " + fieldTypeName + " has not been seen yet, let resolve it => (category = ", category, " )");
110
+ let schema = undefined;
111
+ switch (category) {
112
+ case node_opcua_factory_1.FieldCategory.basic:
113
+ schema = await findDataTypeBasicType(session, cache, dataTypeNodeId);
114
+ /* istanbul ignore next */
115
+ if (!schema) {
116
+ errorLog("Cannot find basic type " + fieldTypeName);
117
+ }
118
+ break;
119
+ default:
120
+ case node_opcua_factory_1.FieldCategory.complex:
121
+ {
122
+ const dataTypeDefinitionDataValue = await session.read({
123
+ attributeId: node_opcua_data_model_1.AttributeIds.DataTypeDefinition,
124
+ nodeId: dataTypeNodeId
125
+ });
132
126
  /* istanbul ignore next */
133
- if (!schema) {
134
- errorLog("Cannot find basic type " + fieldTypeName);
127
+ if (dataTypeDefinitionDataValue.statusCode.isNotGood()) {
128
+ throw new Error(" Cannot find dataType Definition ! with nodeId =" + dataTypeNodeId.toString());
135
129
  }
136
- break;
137
- default:
138
- case node_opcua_factory_1.FieldCategory.complex:
139
- {
140
- const dataTypeDefinitionDataValue = yield session.read({
141
- attributeId: node_opcua_data_model_1.AttributeIds.DataTypeDefinition,
142
- nodeId: dataTypeNodeId
143
- });
144
- /* istanbul ignore next */
145
- if (dataTypeDefinitionDataValue.statusCode.isNotGood()) {
146
- throw new Error(" Cannot find dataType Definition ! with nodeId =" + dataTypeNodeId.toString());
147
- }
148
- const definition = dataTypeDefinitionDataValue.value.value;
149
- const convertIn64ToInteger = (a) => a[1];
150
- const convert = (fields) => {
151
- const retVal = {};
152
- fields && fields.forEach((field) => (retVal[field.name || ""] = convertIn64ToInteger(field.value)));
153
- return retVal;
154
- };
155
- if (category === node_opcua_factory_1.FieldCategory.enumeration) {
156
- if (definition instanceof node_opcua_types_1.EnumDefinition) {
157
- const e = new node_opcua_factory_1.EnumerationDefinitionSchema(dataTypeNodeId, {
158
- enumValues: convert(definition.fields),
159
- name: fieldTypeName
160
- });
161
- dataTypeFactory.registerEnumeration(e);
162
- schema = e;
163
- }
164
- }
165
- else {
166
- const isAbstract = false;
167
- schema = yield convertDataTypeDefinitionToStructureTypeSchema(session, dataTypeNodeId, fieldTypeName, definition, dataTypeFactory, isAbstract, cache);
130
+ const definition = dataTypeDefinitionDataValue.value.value;
131
+ const convertIn64ToInteger = (a) => a[1];
132
+ const convert = (fields) => {
133
+ const retVal = {};
134
+ fields && fields.forEach((field) => (retVal[field.name || ""] = convertIn64ToInteger(field.value)));
135
+ return retVal;
136
+ };
137
+ if (category === node_opcua_factory_1.FieldCategory.enumeration) {
138
+ if (definition instanceof node_opcua_types_1.EnumDefinition) {
139
+ const e = new node_opcua_factory_1.EnumerationDefinitionSchema(dataTypeNodeId, {
140
+ enumValues: convert(definition.fields),
141
+ name: fieldTypeName
142
+ });
143
+ dataTypeFactory.registerEnumeration(e);
144
+ schema = e;
168
145
  }
169
- // xx const schema1 = dataTypeFactory.getStructuredTypeSchema(fieldTypeName);
170
146
  }
171
- break;
172
- }
173
- return { schema, category };
174
- });
147
+ else {
148
+ const isAbstract = false;
149
+ schema = await convertDataTypeDefinitionToStructureTypeSchema(session, dataTypeNodeId, fieldTypeName, definition, dataTypeFactory, isAbstract, cache);
150
+ }
151
+ // xx const schema1 = dataTypeFactory.getStructuredTypeSchema(fieldTypeName);
152
+ }
153
+ break;
154
+ }
155
+ return { schema, category };
175
156
  }
176
- const isExtensionObject = (session, dataTypeNodeId) => __awaiter(void 0, void 0, void 0, function* () {
157
+ const isExtensionObject = async (session, dataTypeNodeId) => {
177
158
  if (dataTypeNodeId.namespace === 0 && dataTypeNodeId.value === node_opcua_variant_1.DataType.ExtensionObject) {
178
159
  return true;
179
160
  }
180
- const baseDataType = yield findSuperType(session, dataTypeNodeId);
161
+ const baseDataType = await findSuperType(session, dataTypeNodeId);
181
162
  const bn = baseDataType;
182
163
  if (bn.identifierType === node_opcua_nodeid_1.NodeIdType.NUMERIC) {
183
164
  if (bn.namespace === 0 && bn.value === node_opcua_variant_1.DataType.ExtensionObject) {
@@ -187,224 +168,218 @@ const isExtensionObject = (session, dataTypeNodeId) => __awaiter(void 0, void 0,
187
168
  return false;
188
169
  }
189
170
  }
190
- return yield isExtensionObject(session, baseDataType);
191
- });
171
+ return await isExtensionObject(session, baseDataType);
172
+ };
192
173
  // eslint-disable-next-line max-statements
193
- function resolveFieldType(session, dataTypeNodeId, dataTypeFactory, cache) {
194
- return __awaiter(this, void 0, void 0, function* () {
195
- if (dataTypeNodeId.namespace === 0 && dataTypeNodeId.value === 22) {
196
- // ERN return null;
197
- const category = node_opcua_factory_1.FieldCategory.complex;
198
- const fieldTypeName = "Structure";
199
- const schema = node_opcua_extension_object_1.ExtensionObject.schema;
200
- return {
201
- category,
202
- fieldTypeName,
203
- schema,
174
+ async function resolveFieldType(session, dataTypeNodeId, dataTypeFactory, cache) {
175
+ if (dataTypeNodeId.namespace === 0 && dataTypeNodeId.value === 22) {
176
+ // ERN return null;
177
+ const category = node_opcua_factory_1.FieldCategory.complex;
178
+ const fieldTypeName = "Structure";
179
+ const schema = node_opcua_extension_object_1.ExtensionObject.schema;
180
+ return {
181
+ category,
182
+ fieldTypeName,
183
+ schema,
184
+ allowSubType: true,
185
+ dataType: (0, node_opcua_nodeid_2.coerceNodeId)(node_opcua_variant_1.DataType.ExtensionObject)
186
+ };
187
+ }
188
+ const key = dataTypeNodeId.toString();
189
+ const v = cache[key];
190
+ if (v) {
191
+ return v;
192
+ }
193
+ if (dataTypeNodeId.value === 0) {
194
+ const v3 = {
195
+ category: node_opcua_factory_1.FieldCategory.basic,
196
+ fieldTypeName: "Variant",
197
+ schema: dataTypeFactory.getBuiltInType("Variant")
198
+ };
199
+ cache[key] = v3;
200
+ return v3;
201
+ }
202
+ const isAbstract = (await session.read({ nodeId: dataTypeNodeId, attributeId: node_opcua_data_model_1.AttributeIds.IsAbstract })).value.value;
203
+ const fieldTypeName = await readBrowseName(session, dataTypeNodeId);
204
+ if (isAbstract) {
205
+ const _isExtensionObject = await isExtensionObject(session, dataTypeNodeId);
206
+ debugLog(" dataType " + dataTypeNodeId.toString() + " " + fieldTypeName + " is abstract => extObj ?= " + _isExtensionObject);
207
+ if (_isExtensionObject) {
208
+ // we could have complex => Structure
209
+ const v3 = {
210
+ category: node_opcua_factory_1.FieldCategory.complex,
211
+ fieldTypeName: fieldTypeName,
212
+ schema: node_opcua_extension_object_1.ExtensionObject.schema,
204
213
  allowSubType: true,
205
- dataType: (0, node_opcua_nodeid_2.coerceNodeId)(node_opcua_variant_1.DataType.ExtensionObject)
214
+ dataType: dataTypeNodeId
206
215
  };
216
+ cache[key] = v3;
217
+ return v3;
207
218
  }
208
- const key = dataTypeNodeId.toString();
209
- const v = cache[key];
210
- if (v) {
211
- return v;
212
- }
213
- if (dataTypeNodeId.value === 0) {
219
+ else {
220
+ // we could have basic => Variant
214
221
  const v3 = {
215
222
  category: node_opcua_factory_1.FieldCategory.basic,
216
- fieldTypeName: "Variant",
217
- schema: dataTypeFactory.getBuiltInType("Variant")
223
+ fieldTypeName: fieldTypeName,
224
+ schema: dataTypeFactory.getBuiltInType("Variant"),
225
+ allowSubType: true,
226
+ dataType: dataTypeNodeId
218
227
  };
219
228
  cache[key] = v3;
220
229
  return v3;
221
230
  }
222
- const isAbstract = (yield session.read({ nodeId: dataTypeNodeId, attributeId: node_opcua_data_model_1.AttributeIds.IsAbstract })).value.value;
223
- const fieldTypeName = yield readBrowseName(session, dataTypeNodeId);
224
- if (isAbstract) {
225
- const _isExtensionObject = yield isExtensionObject(session, dataTypeNodeId);
226
- debugLog(" dataType " + dataTypeNodeId.toString() + " " + fieldTypeName + " is abstract => extObj ?= " + _isExtensionObject);
227
- if (_isExtensionObject) {
228
- // we could have complex => Structure
229
- const v3 = {
230
- category: node_opcua_factory_1.FieldCategory.complex,
231
- fieldTypeName: fieldTypeName,
232
- schema: node_opcua_extension_object_1.ExtensionObject.schema,
233
- allowSubType: true,
234
- dataType: dataTypeNodeId
235
- };
236
- cache[key] = v3;
237
- return v3;
238
- }
239
- else {
240
- // we could have basic => Variant
241
- const v3 = {
242
- category: node_opcua_factory_1.FieldCategory.basic,
243
- fieldTypeName: fieldTypeName,
244
- schema: dataTypeFactory.getBuiltInType("Variant"),
245
- allowSubType: true,
246
- dataType: dataTypeNodeId
247
- };
248
- cache[key] = v3;
249
- return v3;
250
- }
251
- }
252
- let schema;
253
- let category = node_opcua_factory_1.FieldCategory.enumeration;
254
- if (dataTypeFactory.hasStructureByTypeName(fieldTypeName)) {
255
- schema = dataTypeFactory.getStructuredTypeSchema(fieldTypeName);
256
- category = node_opcua_factory_1.FieldCategory.complex;
257
- }
258
- else if (dataTypeFactory.hasBuiltInType(fieldTypeName)) {
259
- category = node_opcua_factory_1.FieldCategory.basic;
260
- schema = dataTypeFactory.getBuiltInType(fieldTypeName);
261
- }
262
- else if (dataTypeFactory.hasEnumeration(fieldTypeName)) {
263
- category = node_opcua_factory_1.FieldCategory.enumeration;
264
- schema = dataTypeFactory.getEnumeration(fieldTypeName);
265
- }
266
- else {
267
- debugLog(" type " + fieldTypeName + " has not been seen yet, let resolve it");
268
- const res = yield resolve2(session, dataTypeNodeId, dataTypeFactory, fieldTypeName, cache);
269
- schema = res.schema;
270
- category = res.category;
271
- }
272
- /* istanbul ignore next */
273
- if (!schema) {
274
- throw new Error("expecting a schema here fieldTypeName=" + fieldTypeName + " " + dataTypeNodeId.toString() + " category = " + category);
275
- }
276
- const v2 = {
277
- category,
278
- fieldTypeName,
279
- schema
280
- };
281
- cache[key] = v2;
282
- return v2;
283
- });
231
+ }
232
+ let schema;
233
+ let category = node_opcua_factory_1.FieldCategory.enumeration;
234
+ if (dataTypeFactory.hasStructureByTypeName(fieldTypeName)) {
235
+ schema = dataTypeFactory.getStructuredTypeSchema(fieldTypeName);
236
+ category = node_opcua_factory_1.FieldCategory.complex;
237
+ }
238
+ else if (dataTypeFactory.hasBuiltInType(fieldTypeName)) {
239
+ category = node_opcua_factory_1.FieldCategory.basic;
240
+ schema = dataTypeFactory.getBuiltInType(fieldTypeName);
241
+ }
242
+ else if (dataTypeFactory.hasEnumeration(fieldTypeName)) {
243
+ category = node_opcua_factory_1.FieldCategory.enumeration;
244
+ schema = dataTypeFactory.getEnumeration(fieldTypeName);
245
+ }
246
+ else {
247
+ debugLog(" type " + fieldTypeName + " has not been seen yet, let resolve it");
248
+ const res = await resolve2(session, dataTypeNodeId, dataTypeFactory, fieldTypeName, cache);
249
+ schema = res.schema;
250
+ category = res.category;
251
+ }
252
+ /* istanbul ignore next */
253
+ if (!schema) {
254
+ throw new Error("expecting a schema here fieldTypeName=" + fieldTypeName + " " + dataTypeNodeId.toString() + " category = " + category);
255
+ }
256
+ const v2 = {
257
+ category,
258
+ fieldTypeName,
259
+ schema
260
+ };
261
+ cache[key] = v2;
262
+ return v2;
284
263
  }
285
- function _setupEncodings(session, dataTypeNodeId, schema) {
286
- return __awaiter(this, void 0, void 0, function* () {
287
- // read abstract flag
288
- const isAbstractDV = yield session.read({ nodeId: dataTypeNodeId, attributeId: node_opcua_data_model_1.AttributeIds.IsAbstract });
289
- schema.dataTypeNodeId = dataTypeNodeId;
290
- if (isAbstractDV.statusCode.isGood() && isAbstractDV.value.value === false) {
291
- const encodings = yield (0, find_encodings_1._findEncodings)(session, dataTypeNodeId);
292
- schema.encodingDefaultBinary = (0, node_opcua_nodeid_2.makeExpandedNodeId)(encodings.binaryEncodingNodeId);
293
- schema.encodingDefaultXml = (0, node_opcua_nodeid_2.makeExpandedNodeId)(encodings.xmlEncodingNodeId);
294
- schema.encodingDefaultJson = (0, node_opcua_nodeid_2.makeExpandedNodeId)(encodings.jsonEncodingNodeId);
295
- }
296
- return schema;
297
- });
264
+ async function _setupEncodings(session, dataTypeNodeId, schema) {
265
+ // read abstract flag
266
+ const isAbstractDV = await session.read({ nodeId: dataTypeNodeId, attributeId: node_opcua_data_model_1.AttributeIds.IsAbstract });
267
+ schema.dataTypeNodeId = dataTypeNodeId;
268
+ if (isAbstractDV.statusCode.isGood() && isAbstractDV.value.value === false) {
269
+ const encodings = await (0, find_encodings_1._findEncodings)(session, dataTypeNodeId);
270
+ schema.encodingDefaultBinary = (0, node_opcua_nodeid_2.makeExpandedNodeId)(encodings.binaryEncodingNodeId);
271
+ schema.encodingDefaultXml = (0, node_opcua_nodeid_2.makeExpandedNodeId)(encodings.xmlEncodingNodeId);
272
+ schema.encodingDefaultJson = (0, node_opcua_nodeid_2.makeExpandedNodeId)(encodings.jsonEncodingNodeId);
273
+ }
274
+ return schema;
298
275
  }
299
276
  // eslint-disable-next-line max-statements
300
- function convertDataTypeDefinitionToStructureTypeSchema(session, dataTypeNodeId, name, definition, dataTypeFactory, isAbstract, cache) {
301
- return __awaiter(this, void 0, void 0, function* () {
302
- if (definition instanceof node_opcua_types_1.StructureDefinition) {
303
- let fieldCountToIgnore = 0;
304
- const structureInfo = dataTypeFactory.getStructureInfoForDataType(definition.baseDataType);
305
- const baseSchema = structureInfo === null || structureInfo === void 0 ? void 0 : structureInfo.schema;
306
- if (baseSchema) {
307
- const possibleFields = (0, node_opcua_factory_1.extractAllPossibleFields)(baseSchema);
308
- fieldCountToIgnore += possibleFields.length;
309
- }
310
- // while (base && !(base.dataTypeNodeId.value === DataType.ExtensionObject && base.dataTypeNodeId.namespace === 0)) {
311
- // fieldCountToIgnore += base..length;
312
- // base = base.getBaseSchema();
313
- // }
314
- const fields = [];
315
- const isUnion = definition.structureType === node_opcua_types_1.StructureType.Union;
316
- switch (definition.structureType) {
317
- case node_opcua_types_1.StructureType.Union:
318
- fields.push({
319
- fieldType: "UInt32",
320
- name: "SwitchField"
321
- });
322
- break;
323
- case node_opcua_types_1.StructureType.Structure:
324
- case node_opcua_types_1.StructureType.StructureWithOptionalFields:
325
- break;
326
- }
327
- let switchValue = 1;
328
- let switchBit = 0;
329
- const bitFields = isUnion ? undefined : [];
330
- const postActions = [];
331
- if (definition.fields) {
332
- for (let i = fieldCountToIgnore; i < definition.fields.length; i++) {
333
- const fieldD = definition.fields[i];
334
- // we need to skip fields that have already been handled in base class
335
- let field;
336
- ({ field, switchBit, switchValue } = createField(fieldD, switchBit, bitFields, isUnion, switchValue));
337
- if (fieldD.dataType.value === dataTypeNodeId.value && fieldD.dataType.namespace === dataTypeNodeId.namespace) {
338
- // this is a structure with a field of the same type
339
- // push an empty placeholder that we will fill later
340
- const fieldTypeName = yield readBrowseName(session, dataTypeNodeId);
341
- (field.fieldType = fieldTypeName), (field.category = node_opcua_factory_1.FieldCategory.complex);
342
- fields.push(field);
343
- const capturedField = field;
344
- postActions.push((schema) => {
345
- capturedField.schema = schema;
346
- });
347
- continue;
348
- }
349
- const rt = (yield resolveFieldType(session, fieldD.dataType, dataTypeFactory, cache));
350
- if (!rt) {
351
- errorLog("convertDataTypeDefinitionToStructureTypeSchema cannot handle field", fieldD.name, "in", name, "because " + fieldD.dataType.toString() + " cannot be resolved");
352
- continue;
353
- }
354
- const { schema, category, fieldTypeName, dataType, allowSubType } = rt;
355
- field.fieldType = fieldTypeName;
356
- field.category = category;
357
- field.schema = schema;
358
- field.dataType = dataType || fieldD.dataType;
359
- field.allowSubType = allowSubType || false;
360
- field.basicDataType = yield (0, node_opcua_pseudo_session_1.findBasicDataType)(session, field.dataType);
277
+ async function convertDataTypeDefinitionToStructureTypeSchema(session, dataTypeNodeId, name, definition, dataTypeFactory, isAbstract, cache) {
278
+ if (definition instanceof node_opcua_types_1.StructureDefinition) {
279
+ let fieldCountToIgnore = 0;
280
+ const structureInfo = dataTypeFactory.getStructureInfoForDataType(definition.baseDataType);
281
+ const baseSchema = structureInfo?.schema;
282
+ if (baseSchema) {
283
+ const possibleFields = (0, node_opcua_factory_1.extractAllPossibleFields)(baseSchema);
284
+ fieldCountToIgnore += possibleFields.length;
285
+ }
286
+ // while (base && !(base.dataTypeNodeId.value === DataType.ExtensionObject && base.dataTypeNodeId.namespace === 0)) {
287
+ // fieldCountToIgnore += base..length;
288
+ // base = base.getBaseSchema();
289
+ // }
290
+ const fields = [];
291
+ const isUnion = definition.structureType === node_opcua_types_1.StructureType.Union;
292
+ switch (definition.structureType) {
293
+ case node_opcua_types_1.StructureType.Union:
294
+ fields.push({
295
+ fieldType: "UInt32",
296
+ name: "SwitchField"
297
+ });
298
+ break;
299
+ case node_opcua_types_1.StructureType.Structure:
300
+ case node_opcua_types_1.StructureType.StructureWithOptionalFields:
301
+ break;
302
+ }
303
+ let switchValue = 1;
304
+ let switchBit = 0;
305
+ const bitFields = isUnion ? undefined : [];
306
+ const postActions = [];
307
+ if (definition.fields) {
308
+ for (let i = fieldCountToIgnore; i < definition.fields.length; i++) {
309
+ const fieldD = definition.fields[i];
310
+ // we need to skip fields that have already been handled in base class
311
+ let field;
312
+ ({ field, switchBit, switchValue } = createField(fieldD, switchBit, bitFields, isUnion, switchValue));
313
+ if (fieldD.dataType.value === dataTypeNodeId.value && fieldD.dataType.namespace === dataTypeNodeId.namespace) {
314
+ // this is a structure with a field of the same type
315
+ // push an empty placeholder that we will fill later
316
+ const fieldTypeName = await readBrowseName(session, dataTypeNodeId);
317
+ (field.fieldType = fieldTypeName), (field.category = node_opcua_factory_1.FieldCategory.complex);
361
318
  fields.push(field);
319
+ const capturedField = field;
320
+ postActions.push((schema) => {
321
+ capturedField.schema = schema;
322
+ });
323
+ continue;
362
324
  }
325
+ const rt = (await resolveFieldType(session, fieldD.dataType, dataTypeFactory, cache));
326
+ if (!rt) {
327
+ errorLog("convertDataTypeDefinitionToStructureTypeSchema cannot handle field", fieldD.name, "in", name, "because " + fieldD.dataType.toString() + " cannot be resolved");
328
+ continue;
329
+ }
330
+ const { schema, category, fieldTypeName, dataType, allowSubType } = rt;
331
+ field.fieldType = fieldTypeName;
332
+ field.category = category;
333
+ field.schema = schema;
334
+ field.dataType = dataType || fieldD.dataType;
335
+ field.allowSubType = allowSubType || false;
336
+ field.basicDataType = await (0, node_opcua_pseudo_session_1.findBasicDataType)(session, field.dataType);
337
+ fields.push(field);
363
338
  }
364
- /// some server may provide definition.baseDataType to be i=22 (ExtensionObject)
365
- /// instead of 12756 Union;
366
- if (isUnion && (0, node_opcua_nodeid_1.sameNodeId)(definition.baseDataType, (0, node_opcua_nodeid_2.coerceNodeId)("i=22"))) {
367
- definition.baseDataType = (0, node_opcua_nodeid_2.resolveNodeId)("i=1276"); // aka DataTypeIds.Union
368
- }
369
- const a = yield resolveFieldType(session, definition.baseDataType, dataTypeFactory, cache);
370
- const baseType = a ? a.fieldTypeName : isUnion ? "Union" : "ExtensionObject";
371
- const os = new node_opcua_factory_1.StructuredTypeSchema({
372
- baseType,
373
- bitFields,
374
- fields,
375
- name,
376
- dataTypeFactory
377
- });
378
- const structuredTypeSchema = yield _setupEncodings(session, dataTypeNodeId, os);
379
- postActions.forEach((action) => action(structuredTypeSchema));
380
- return structuredTypeSchema;
381
339
  }
382
- throw new Error("Not Implemented");
383
- function createField(fieldD, switchBit, bitFields, isUnion, switchValue) {
384
- const field = {
385
- fieldType: "",
386
- name: fieldD.name,
387
- schema: undefined
388
- };
389
- if (fieldD.isOptional) {
390
- field.switchBit = switchBit++;
391
- bitFields === null || bitFields === void 0 ? void 0 : bitFields.push({ name: fieldD.name + "Specified", length: 1 });
392
- }
393
- if (isUnion) {
394
- field.switchValue = switchValue;
395
- switchValue += 1;
396
- }
397
- // (fieldD.valueRank === -1 || fieldD.valueRank === 1 || fieldD.valueRank === 0);
398
- if (fieldD.valueRank >= 1) {
399
- field.valueRank = fieldD.valueRank;
400
- field.isArray = true;
401
- }
402
- else {
403
- field.isArray = false;
404
- }
405
- return { field, switchBit, switchValue };
340
+ /// some server may provide definition.baseDataType to be i=22 (ExtensionObject)
341
+ /// instead of 12756 Union;
342
+ if (isUnion && (0, node_opcua_nodeid_1.sameNodeId)(definition.baseDataType, (0, node_opcua_nodeid_2.coerceNodeId)("i=22"))) {
343
+ definition.baseDataType = (0, node_opcua_nodeid_2.resolveNodeId)("i=1276"); // aka DataTypeIds.Union
344
+ }
345
+ const a = await resolveFieldType(session, definition.baseDataType, dataTypeFactory, cache);
346
+ const baseType = a ? a.fieldTypeName : isUnion ? "Union" : "ExtensionObject";
347
+ const os = new node_opcua_factory_1.StructuredTypeSchema({
348
+ baseType,
349
+ bitFields,
350
+ fields,
351
+ name,
352
+ dataTypeFactory
353
+ });
354
+ const structuredTypeSchema = await _setupEncodings(session, dataTypeNodeId, os);
355
+ postActions.forEach((action) => action(structuredTypeSchema));
356
+ return structuredTypeSchema;
357
+ }
358
+ throw new Error("Not Implemented");
359
+ function createField(fieldD, switchBit, bitFields, isUnion, switchValue) {
360
+ const field = {
361
+ fieldType: "",
362
+ name: fieldD.name,
363
+ schema: undefined
364
+ };
365
+ if (fieldD.isOptional) {
366
+ field.switchBit = switchBit++;
367
+ bitFields?.push({ name: fieldD.name + "Specified", length: 1 });
368
+ }
369
+ if (isUnion) {
370
+ field.switchValue = switchValue;
371
+ switchValue += 1;
372
+ }
373
+ // (fieldD.valueRank === -1 || fieldD.valueRank === 1 || fieldD.valueRank === 0);
374
+ if (fieldD.valueRank >= 1) {
375
+ field.valueRank = fieldD.valueRank;
376
+ field.isArray = true;
406
377
  }
407
- });
378
+ else {
379
+ field.isArray = false;
380
+ }
381
+ return { field, switchBit, switchValue };
382
+ }
408
383
  }
409
384
  exports.convertDataTypeDefinitionToStructureTypeSchema = convertDataTypeDefinitionToStructureTypeSchema;
410
385
  //# sourceMappingURL=convert_data_type_definition_to_structuretype_schema.js.map