-
Notifications
You must be signed in to change notification settings - Fork 0
/
opcua-write.js
48 lines (36 loc) · 1.63 KB
/
opcua-write.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// See example https://github.com/node-opcua/node-opcua/blob/af235f19e9e353fa748c11009b1300f76026fb28/packages/node-opcua-service-write/test/test_service_write.js
module.exports = function (RED) {
var core = require('./core');
var opcua = require('node-opcua');
function opcUaWriteNode(args) {
RED.nodes.createNode(this, args);
const opcuaclientnode = RED.nodes.getNode(args.client);
const existingClient = core.opcClients[opcuaclientnode.connectionId];
var node = this;
node.name = args.name;
node.nodeId = args.nodeid;
node.on('input', function (msg) {
if(existingClient.clientState == "reconnecting") return;
if(existingClient.clientState == "disconnected") return;
// Override nodeId from incoming node if not defined on read node
//if (!args.nodeId && msg.nodeId) node.nodeId = msg.nodeId;
const value = msg.payload.value;
write(value);
});
async function write(requestValue) {
const dataType = await existingClient.session.getBuiltInDataType(node.nodeId);
const resultStatus = await existingClient.session.write({
nodeId: node.nodeId,
attributeId: opcua.AttributeIds.Value,
value: new opcua.DataValue({
value: new opcua.Variant({
dataType: dataType,
value: requestValue
})
})
});
node.debug(resultStatus.description);
}
}
RED.nodes.registerType("opcua-write", opcUaWriteNode);
}