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