@vitormnm/node-red-simple-opcua 1.4.2 → 1.4.3

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,330 +1,330 @@
1
- "use strict";
2
-
3
- const {
4
- AttributeIds,
5
- BrowseDirection,
6
- DataType,
7
- NodeClass,
8
- coerceNodeId,
9
- makeNodeId
10
- } = require("node-opcua");
11
-
12
- async function browseNode(session, root) {
13
- const nodeID = normalizeNodeId(root.nodeID || root.nodeId || ROOT_NODE_ID);
14
- const result = {
15
- name: root.name || await readBrowseName(session, nodeID, "RootFolder"),
16
- nodeID,
17
- browse: []
18
- };
19
-
20
- const browseResult = await session.browse({
21
- nodeId: nodeID,
22
- browseDirection: BrowseDirection.Forward,
23
- includeSubtypes: true,
24
- resultMask: 63
25
- });
26
-
27
- const references = browseResult?.references ?? [];
28
- if (!references.length) return result;
29
-
30
- // Monta lista de todos os atributos de todos os nós de uma vez
31
- const nodeIds = references.map(ref => normalizeNodeId(ref.nodeId));
32
- const attributesToRead = nodeIds.flatMap(nodeId => [
33
- { nodeId, attributeId: AttributeIds.Description },
34
- { nodeId, attributeId: AttributeIds.DataType },
35
- { nodeId, attributeId: AttributeIds.Value },
36
- ]);
37
-
38
- // UMA única chamada para todos os nós e atributos
39
- const dataValues = await session.read(attributesToRead);
40
-
41
- // Distribui os resultados por nó (3 atributos por nó)
42
- result.browse = await Promise.all(references.map(async (reference, i) => {
43
- const childNodeId = nodeIds[i];
44
- const nodeClass = resolveNodeClassName(reference.nodeClass);
45
- const browseName = extractBrowseName(reference.browseName, childNodeId);
46
- const displayName = extractDisplayName(reference.displayName, browseName);
47
-
48
- const descValue = dataValues[i * 3]?.value?.value;
49
- const description = typeof descValue === "string"
50
- ? descValue
51
- : (descValue?.text ?? "");
52
-
53
- const item = { nodeID: childNodeId, nodeClass, browseName, displayName, description };
54
-
55
- if (nodeClass === "Variable") {
56
- const dataTypeValue = dataValues[i * 3 + 1]?.value?.value;
57
- const rawValue = dataValues[i * 3 + 2]?.value?.value;
58
-
59
- item.dataType = dataTypeValue?.namespace === 0 && typeof dataTypeValue?.value === "number"
60
- ? (DataType[dataTypeValue.value] || dataTypeValue.toString())
61
- : (dataTypeValue?.toString() ?? "");
62
-
63
- item.value = rawValue ?? "";
64
- }
65
-
66
- if (nodeClass === "Method") {
67
- const definition = await readMethodArguments(session, childNodeId);
68
- item.inputArguments = definition.inputArguments;
69
- item.outputArguments = definition.outputArguments;
70
- }
71
-
72
- return item;
73
- }));
74
-
75
- return result;
76
- }
77
- function normalizeBrowseRoots(payload) {
78
- if (payload === undefined || payload === null) {
79
- return [{ name: "RootFolder", nodeID: ROOT_NODE_ID }];
80
- }
81
-
82
- if (!Array.isArray(payload)) {
83
- throw new Error("OPC UA browse expects msg.payload as an array");
84
- }
85
-
86
- if (!payload.length) {
87
- return [{ name: "RootFolder", nodeID: ROOT_NODE_ID }];
88
- }
89
-
90
- return payload.map((item) => {
91
- if (!item || typeof item !== "object" || Array.isArray(item)) {
92
- throw new Error("Each browse item must be an object");
93
- }
94
-
95
- return {
96
- name: typeof item.name === "string" && item.name.trim() ? item.name.trim() : "",
97
- nodeID: normalizeNodeId(item.nodeID || item.nodeId)
98
- };
99
- });
100
- }
101
-
102
- async function mapReference(session, reference) {
103
- const childNodeId = normalizeNodeId(reference.nodeId);
104
- const nodeClass = resolveNodeClassName(reference.nodeClass);
105
- const browseName = extractBrowseName(reference.browseName, childNodeId);
106
- const item = {
107
- nodeID: childNodeId,
108
- nodeClass,
109
- browseName,
110
- displayName: extractDisplayName(reference.displayName, browseName),
111
- description: await readDescription(session, childNodeId)
112
- };
113
-
114
- const hasTypeDefinition = await readHasTypeDefinition(session, childNodeId);
115
- if (hasTypeDefinition) {
116
- item.hasTypeDefinition = hasTypeDefinition;
117
- }
118
-
119
- if (nodeClass === "Variable") {
120
- item.value = await readValue(session, childNodeId);
121
- item.dataType = await readDataType(session, childNodeId);
122
- return item;
123
- }
124
-
125
- if (nodeClass === "Method") {
126
- const definition = await readMethodArguments(session, childNodeId);
127
- item.inputArguments = definition.inputArguments;
128
- item.outputArguments = definition.outputArguments;
129
- return item;
130
- }
131
-
132
- return item;
133
- }
134
-
135
- async function readHasTypeDefinition(session, nodeId) {
136
- try {
137
- const browseResult = await session.browse({
138
- nodeId,
139
- browseDirection: BrowseDirection.Forward,
140
- referenceTypeId: makeNodeId(40, 0), // HasTypeDefinition
141
- includeSubtypes: false,
142
- resultMask: 63
143
- });
144
- const references = browseResult && Array.isArray(browseResult.references)
145
- ? browseResult.references
146
- : [];
147
- if (!references.length) {
148
- return null;
149
- }
150
-
151
- const reference = references[0];
152
- const typeNodeId = normalizeNodeId(reference.nodeId);
153
- return {
154
- nodeID: typeNodeId,
155
- browseName: extractBrowseName(reference.browseName, typeNodeId),
156
- displayName: extractDisplayName(reference.displayName, typeNodeId)
157
- };
158
- } catch (error) {
159
- return null;
160
- }
161
- }
162
-
163
- async function readBrowseName(session, nodeId, fallback) {
164
- try {
165
- const dataValue = await session.read({
166
- nodeId,
167
- attributeId: AttributeIds.BrowseName
168
- });
169
- const value = dataValue && dataValue.value ? dataValue.value.value : null;
170
- if (value && value.name) {
171
- return String(value.name);
172
- }
173
- } catch (error) {
174
- // Use fallback below.
175
- }
176
-
177
- return String(fallback || nodeId);
178
- }
179
-
180
- async function readDescription(session, nodeId) {
181
- try {
182
- const dataValue = await session.read({
183
- nodeId,
184
- attributeId: AttributeIds.Description
185
- });
186
- const value = dataValue && dataValue.value ? dataValue.value.value : null;
187
-
188
- if (typeof value === "string") {
189
- return value;
190
- }
191
-
192
- if (value && typeof value.text === "string") {
193
- return value.text;
194
- }
195
- } catch (error) {
196
- // Return empty description when unavailable.
197
- }
198
-
199
- return "";
200
- }
201
-
202
- async function readValue(session, nodeId) {
203
- try {
204
- const dataValue = await session.read({
205
- nodeId,
206
- attributeId: AttributeIds.Value
207
- });
208
-
209
- if (!dataValue || !dataValue.value) {
210
- return "";
211
- }
212
-
213
- const value = dataValue.value.value;
214
- return value === undefined || value === null ? "" : value;
215
- } catch (error) {
216
- return "";
217
- }
218
- }
219
-
220
- async function readDataType(session, nodeId) {
221
- try {
222
- const dataValue = await session.read({
223
- nodeId,
224
- attributeId: AttributeIds.DataType
225
- });
226
-
227
- const value = dataValue && dataValue.value ? dataValue.value.value : null;
228
- if (!value) {
229
- return "";
230
- }
231
-
232
- if (value.namespace === 0 && typeof value.value === "number") {
233
- return DataType[value.value] || value.toString();
234
- }
235
-
236
- return value.toString();
237
- } catch (error) {
238
- return "";
239
- }
240
- }
241
-
242
- async function readMethodArguments(session, nodeId) {
243
- try {
244
- const definition = await session.getArgumentDefinition(nodeId);
245
- return {
246
- inputArguments: normalizeMethodArguments(definition && definition.inputArguments),
247
- outputArguments: normalizeMethodArguments(definition && definition.outputArguments)
248
- };
249
- } catch (error) {
250
- return {
251
- inputArguments: [],
252
- outputArguments: []
253
- };
254
- }
255
- }
256
-
257
- function normalizeNodeId(nodeId) {
258
- return coerceNodeId(nodeId).toString();
259
- }
260
-
261
- function resolveNodeClassName(nodeClass) {
262
- if (!nodeClass && nodeClass !== 0) {
263
- return "";
264
- }
265
-
266
- if (typeof nodeClass === "object" && typeof nodeClass.key === "string") {
267
- return nodeClass.key;
268
- }
269
-
270
- if (typeof nodeClass === "string") {
271
- return nodeClass;
272
- }
273
-
274
- return NodeClass[nodeClass] || String(nodeClass);
275
- }
276
-
277
- function extractBrowseName(browseName, fallback) {
278
- if (browseName && typeof browseName.name === "string" && browseName.name) {
279
- return browseName.name;
280
- }
281
-
282
- return String(fallback || "");
283
- }
284
-
285
- function extractDisplayName(displayName, fallback) {
286
- if (displayName && typeof displayName.text === "string" && displayName.text) {
287
- return displayName.text;
288
- }
289
-
290
- if (typeof displayName === "string" && displayName) {
291
- return displayName;
292
- }
293
-
294
- return String(fallback || "");
295
- }
296
-
297
- function normalizeMethodArguments(argumentsList) {
298
- if (!Array.isArray(argumentsList)) {
299
- return [];
300
- }
301
-
302
- return argumentsList.map((argument, index) => ({
303
- name: argument && argument.name ? String(argument.name) : "arg" + (index + 1),
304
- dataType: resolveArgumentDataType(argument && argument.dataType),
305
- description: argument && argument.description && typeof argument.description.text === "string"
306
- ? argument.description.text
307
- : ""
308
- }));
309
- }
310
-
311
- function resolveArgumentDataType(dataType) {
312
- if (!dataType) {
313
- return "";
314
- }
315
-
316
- if (dataType.namespace === 0 && typeof dataType.value === "number") {
317
- return DataType[dataType.value] || dataType.toString();
318
- }
319
-
320
- return dataType.toString();
321
- }
322
-
323
- const ROOT_NODE_ID = "i=84";
324
-
325
- module.exports = {
326
- browseNode,
327
- normalizeBrowseRoots,
328
- normalizeNodeId,
329
- ROOT_NODE_ID
330
- };
1
+ "use strict";
2
+
3
+ const {
4
+ AttributeIds,
5
+ BrowseDirection,
6
+ DataType,
7
+ NodeClass,
8
+ coerceNodeId,
9
+ makeNodeId
10
+ } = require("node-opcua");
11
+
12
+ async function browseNode(session, root) {
13
+ const nodeID = normalizeNodeId(root.nodeID || root.nodeId || ROOT_NODE_ID);
14
+ const result = {
15
+ name: root.name || await readBrowseName(session, nodeID, "RootFolder"),
16
+ nodeID,
17
+ browse: []
18
+ };
19
+
20
+ const browseResult = await session.browse({
21
+ nodeId: nodeID,
22
+ browseDirection: BrowseDirection.Forward,
23
+ includeSubtypes: true,
24
+ resultMask: 63
25
+ });
26
+
27
+ const references = browseResult?.references ?? [];
28
+ if (!references.length) return result;
29
+
30
+ // Monta lista de todos os atributos de todos os nós de uma vez
31
+ const nodeIds = references.map(ref => normalizeNodeId(ref.nodeId));
32
+ const attributesToRead = nodeIds.flatMap(nodeId => [
33
+ { nodeId, attributeId: AttributeIds.Description },
34
+ { nodeId, attributeId: AttributeIds.DataType },
35
+ { nodeId, attributeId: AttributeIds.Value },
36
+ ]);
37
+
38
+ // UMA única chamada para todos os nós e atributos
39
+ const dataValues = await session.read(attributesToRead);
40
+
41
+ // Distribui os resultados por nó (3 atributos por nó)
42
+ result.browse = await Promise.all(references.map(async (reference, i) => {
43
+ const childNodeId = nodeIds[i];
44
+ const nodeClass = resolveNodeClassName(reference.nodeClass);
45
+ const browseName = extractBrowseName(reference.browseName, childNodeId);
46
+ const displayName = extractDisplayName(reference.displayName, browseName);
47
+
48
+ const descValue = dataValues[i * 3]?.value?.value;
49
+ const description = typeof descValue === "string"
50
+ ? descValue
51
+ : (descValue?.text ?? "");
52
+
53
+ const item = { nodeID: childNodeId, nodeClass, browseName, displayName, description };
54
+
55
+ if (nodeClass === "Variable") {
56
+ const dataTypeValue = dataValues[i * 3 + 1]?.value?.value;
57
+ const rawValue = dataValues[i * 3 + 2]?.value?.value;
58
+
59
+ item.dataType = dataTypeValue?.namespace === 0 && typeof dataTypeValue?.value === "number"
60
+ ? (DataType[dataTypeValue.value] || dataTypeValue.toString())
61
+ : (dataTypeValue?.toString() ?? "");
62
+
63
+ item.value = rawValue ?? "";
64
+ }
65
+
66
+ if (nodeClass === "Method") {
67
+ const definition = await readMethodArguments(session, childNodeId);
68
+ item.inputArguments = definition.inputArguments;
69
+ item.outputArguments = definition.outputArguments;
70
+ }
71
+
72
+ return item;
73
+ }));
74
+
75
+ return result;
76
+ }
77
+ function normalizeBrowseRoots(payload) {
78
+ if (payload === undefined || payload === null) {
79
+ return [{ name: "RootFolder", nodeID: ROOT_NODE_ID }];
80
+ }
81
+
82
+ if (!Array.isArray(payload)) {
83
+ throw new Error("OPC UA browse expects msg.payload as an array");
84
+ }
85
+
86
+ if (!payload.length) {
87
+ return [{ name: "RootFolder", nodeID: ROOT_NODE_ID }];
88
+ }
89
+
90
+ return payload.map((item) => {
91
+ if (!item || typeof item !== "object" || Array.isArray(item)) {
92
+ throw new Error("Each browse item must be an object");
93
+ }
94
+
95
+ return {
96
+ name: typeof item.name === "string" && item.name.trim() ? item.name.trim() : "",
97
+ nodeID: normalizeNodeId(item.nodeID || item.nodeId)
98
+ };
99
+ });
100
+ }
101
+
102
+ async function mapReference(session, reference) {
103
+ const childNodeId = normalizeNodeId(reference.nodeId);
104
+ const nodeClass = resolveNodeClassName(reference.nodeClass);
105
+ const browseName = extractBrowseName(reference.browseName, childNodeId);
106
+ const item = {
107
+ nodeID: childNodeId,
108
+ nodeClass,
109
+ browseName,
110
+ displayName: extractDisplayName(reference.displayName, browseName),
111
+ description: await readDescription(session, childNodeId)
112
+ };
113
+
114
+ const hasTypeDefinition = await readHasTypeDefinition(session, childNodeId);
115
+ if (hasTypeDefinition) {
116
+ item.hasTypeDefinition = hasTypeDefinition;
117
+ }
118
+
119
+ if (nodeClass === "Variable") {
120
+ item.value = await readValue(session, childNodeId);
121
+ item.dataType = await readDataType(session, childNodeId);
122
+ return item;
123
+ }
124
+
125
+ if (nodeClass === "Method") {
126
+ const definition = await readMethodArguments(session, childNodeId);
127
+ item.inputArguments = definition.inputArguments;
128
+ item.outputArguments = definition.outputArguments;
129
+ return item;
130
+ }
131
+
132
+ return item;
133
+ }
134
+
135
+ async function readHasTypeDefinition(session, nodeId) {
136
+ try {
137
+ const browseResult = await session.browse({
138
+ nodeId,
139
+ browseDirection: BrowseDirection.Forward,
140
+ referenceTypeId: makeNodeId(40, 0), // HasTypeDefinition
141
+ includeSubtypes: false,
142
+ resultMask: 63
143
+ });
144
+ const references = browseResult && Array.isArray(browseResult.references)
145
+ ? browseResult.references
146
+ : [];
147
+ if (!references.length) {
148
+ return null;
149
+ }
150
+
151
+ const reference = references[0];
152
+ const typeNodeId = normalizeNodeId(reference.nodeId);
153
+ return {
154
+ nodeID: typeNodeId,
155
+ browseName: extractBrowseName(reference.browseName, typeNodeId),
156
+ displayName: extractDisplayName(reference.displayName, typeNodeId)
157
+ };
158
+ } catch (error) {
159
+ return null;
160
+ }
161
+ }
162
+
163
+ async function readBrowseName(session, nodeId, fallback) {
164
+ try {
165
+ const dataValue = await session.read({
166
+ nodeId,
167
+ attributeId: AttributeIds.BrowseName
168
+ });
169
+ const value = dataValue && dataValue.value ? dataValue.value.value : null;
170
+ if (value && value.name) {
171
+ return String(value.name);
172
+ }
173
+ } catch (error) {
174
+ // Use fallback below.
175
+ }
176
+
177
+ return String(fallback || nodeId);
178
+ }
179
+
180
+ async function readDescription(session, nodeId) {
181
+ try {
182
+ const dataValue = await session.read({
183
+ nodeId,
184
+ attributeId: AttributeIds.Description
185
+ });
186
+ const value = dataValue && dataValue.value ? dataValue.value.value : null;
187
+
188
+ if (typeof value === "string") {
189
+ return value;
190
+ }
191
+
192
+ if (value && typeof value.text === "string") {
193
+ return value.text;
194
+ }
195
+ } catch (error) {
196
+ // Return empty description when unavailable.
197
+ }
198
+
199
+ return "";
200
+ }
201
+
202
+ async function readValue(session, nodeId) {
203
+ try {
204
+ const dataValue = await session.read({
205
+ nodeId,
206
+ attributeId: AttributeIds.Value
207
+ });
208
+
209
+ if (!dataValue || !dataValue.value) {
210
+ return "";
211
+ }
212
+
213
+ const value = dataValue.value.value;
214
+ return value === undefined || value === null ? "" : value;
215
+ } catch (error) {
216
+ return "";
217
+ }
218
+ }
219
+
220
+ async function readDataType(session, nodeId) {
221
+ try {
222
+ const dataValue = await session.read({
223
+ nodeId,
224
+ attributeId: AttributeIds.DataType
225
+ });
226
+
227
+ const value = dataValue && dataValue.value ? dataValue.value.value : null;
228
+ if (!value) {
229
+ return "";
230
+ }
231
+
232
+ if (value.namespace === 0 && typeof value.value === "number") {
233
+ return DataType[value.value] || value.toString();
234
+ }
235
+
236
+ return value.toString();
237
+ } catch (error) {
238
+ return "";
239
+ }
240
+ }
241
+
242
+ async function readMethodArguments(session, nodeId) {
243
+ try {
244
+ const definition = await session.getArgumentDefinition(nodeId);
245
+ return {
246
+ inputArguments: normalizeMethodArguments(definition && definition.inputArguments),
247
+ outputArguments: normalizeMethodArguments(definition && definition.outputArguments)
248
+ };
249
+ } catch (error) {
250
+ return {
251
+ inputArguments: [],
252
+ outputArguments: []
253
+ };
254
+ }
255
+ }
256
+
257
+ function normalizeNodeId(nodeId) {
258
+ return coerceNodeId(nodeId).toString();
259
+ }
260
+
261
+ function resolveNodeClassName(nodeClass) {
262
+ if (!nodeClass && nodeClass !== 0) {
263
+ return "";
264
+ }
265
+
266
+ if (typeof nodeClass === "object" && typeof nodeClass.key === "string") {
267
+ return nodeClass.key;
268
+ }
269
+
270
+ if (typeof nodeClass === "string") {
271
+ return nodeClass;
272
+ }
273
+
274
+ return NodeClass[nodeClass] || String(nodeClass);
275
+ }
276
+
277
+ function extractBrowseName(browseName, fallback) {
278
+ if (browseName && typeof browseName.name === "string" && browseName.name) {
279
+ return browseName.name;
280
+ }
281
+
282
+ return String(fallback || "");
283
+ }
284
+
285
+ function extractDisplayName(displayName, fallback) {
286
+ if (displayName && typeof displayName.text === "string" && displayName.text) {
287
+ return displayName.text;
288
+ }
289
+
290
+ if (typeof displayName === "string" && displayName) {
291
+ return displayName;
292
+ }
293
+
294
+ return String(fallback || "");
295
+ }
296
+
297
+ function normalizeMethodArguments(argumentsList) {
298
+ if (!Array.isArray(argumentsList)) {
299
+ return [];
300
+ }
301
+
302
+ return argumentsList.map((argument, index) => ({
303
+ name: argument && argument.name ? String(argument.name) : "arg" + (index + 1),
304
+ dataType: resolveArgumentDataType(argument && argument.dataType),
305
+ description: argument && argument.description && typeof argument.description.text === "string"
306
+ ? argument.description.text
307
+ : ""
308
+ }));
309
+ }
310
+
311
+ function resolveArgumentDataType(dataType) {
312
+ if (!dataType) {
313
+ return "";
314
+ }
315
+
316
+ if (dataType.namespace === 0 && typeof dataType.value === "number") {
317
+ return DataType[dataType.value] || dataType.toString();
318
+ }
319
+
320
+ return dataType.toString();
321
+ }
322
+
323
+ const ROOT_NODE_ID = "i=84";
324
+
325
+ module.exports = {
326
+ browseNode,
327
+ normalizeBrowseRoots,
328
+ normalizeNodeId,
329
+ ROOT_NODE_ID
330
+ };