@tier0/opcua-client 0.6.3 → 0.6.4
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.
- package/README.md +15 -3
- package/lib/diagnostics.js +55 -1
- package/nodes/browse.js +165 -160
- package/nodes/connection.js +2 -8
- package/nodes/read.js +130 -125
- package/nodes/subscribe.js +806 -800
- package/nodes/write.js +72 -67
- package/package.json +1 -1
package/nodes/write.js
CHANGED
|
@@ -1,67 +1,72 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const { createDiagnostics } = require('../lib/diagnostics');
|
|
4
|
-
|
|
5
|
-
const { messageWritePoints } = require('../lib/points');
|
|
6
|
-
|
|
7
|
-
module.exports = function registerWriteNode(RED) {
|
|
8
|
-
function OpcUaWriteNode(config) {
|
|
9
|
-
RED.nodes.createNode(this, config);
|
|
10
|
-
const node = this;
|
|
11
|
-
node.connection = RED.nodes.getNode(config.connection);
|
|
12
|
-
const diagnostics = createDiagnostics(node, config, 'write');
|
|
13
|
-
const consumer = node.connection?.registerConsumer?.('write', {
|
|
14
|
-
nodeId: config.id, name: config.name,
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
node.on('input', async (msg, send, done) => {
|
|
18
|
-
if (!node.connection) {
|
|
19
|
-
const error = new Error('OPCUA_CONNECTION_NOT_CONFIGURED');
|
|
20
|
-
diagnostics.failure(error, send);
|
|
21
|
-
return done(error);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
msg.
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { createDiagnostics } = require('../lib/diagnostics');
|
|
4
|
+
|
|
5
|
+
const { messageWritePoints } = require('../lib/points');
|
|
6
|
+
|
|
7
|
+
module.exports = function registerWriteNode(RED) {
|
|
8
|
+
function OpcUaWriteNode(config) {
|
|
9
|
+
RED.nodes.createNode(this, config);
|
|
10
|
+
const node = this;
|
|
11
|
+
node.connection = RED.nodes.getNode(config.connection);
|
|
12
|
+
const diagnostics = createDiagnostics(node, config, 'write');
|
|
13
|
+
const consumer = node.connection?.registerConsumer?.('write', {
|
|
14
|
+
nodeId: config.id, name: config.name,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
node.on('input', async (msg, send, done) => {
|
|
18
|
+
if (!node.connection) {
|
|
19
|
+
const error = new Error('OPCUA_CONNECTION_NOT_CONFIGURED');
|
|
20
|
+
diagnostics.failure(error, send);
|
|
21
|
+
return done(error);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
diagnostics.beginInput();
|
|
25
|
+
let inputError;
|
|
26
|
+
try {
|
|
27
|
+
consumer?.markUsed('write');
|
|
28
|
+
const points = messageWritePoints(msg, config);
|
|
29
|
+
node.status({ fill: 'blue', shape: 'dot', text: `writing ${points.length}` });
|
|
30
|
+
const results = await node.connection.client.writeMany(
|
|
31
|
+
points,
|
|
32
|
+
Number(config.batchSize) || 500
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
const failed = diagnostics.results(results, send, new Map(points.map((point) => [point.nodeId, point])));
|
|
36
|
+
if ((config.outputMode || 'batch') === 'split') {
|
|
37
|
+
results.forEach((result) => send({ ...msg, topic: result.nodeId, opcua: result }));
|
|
38
|
+
} else {
|
|
39
|
+
msg.payload = results;
|
|
40
|
+
msg.opcua = {
|
|
41
|
+
...(msg.opcua || {}),
|
|
42
|
+
operation: 'write',
|
|
43
|
+
total: results.length,
|
|
44
|
+
good: results.filter((item) => item.statusCode === 'Good').length,
|
|
45
|
+
clientDiagnostics: node.connection.getDiagnostics?.() ||
|
|
46
|
+
node.connection.client.getDiagnostics(),
|
|
47
|
+
};
|
|
48
|
+
send(msg);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
node.status({ fill: failed ? 'yellow' : 'green', shape: failed ? 'ring' : 'dot',
|
|
52
|
+
text: `${results.length - failed}/${results.length} written` + (failed ? ` / ${failed} failed` : '') });
|
|
53
|
+
done();
|
|
54
|
+
} catch (error) {
|
|
55
|
+
inputError = error;
|
|
56
|
+
node.status({ fill: 'red', shape: 'ring', text: error.message });
|
|
57
|
+
diagnostics.failure(error, send);
|
|
58
|
+
done(error);
|
|
59
|
+
} finally {
|
|
60
|
+
diagnostics.endInput(inputError);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
node.on('close', (removed, done) => {
|
|
65
|
+
if (typeof removed === 'function') done = removed;
|
|
66
|
+
consumer?.release();
|
|
67
|
+
done?.();
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
RED.nodes.registerType('tier0-opcua-write', OpcUaWriteNode);
|
|
72
|
+
};
|