-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopcua-read.js
46 lines (35 loc) · 1.44 KB
/
opcua-read.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
module.exports = function (RED) {
var core = require('./core');
var opcua = require('node-opcua');
function opcUaReadNode(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;
// Read Input Arg node
node.on('input', function (msg) {
if(existingClient.clientState == "reconnecting") return;
if(existingClient.clientState == "disconnected") return;
if (existingClient.session == undefined) {
node.error("Session not found");
return;
}
// Override nodeId from incoming node if not defined on read node
if (!args.nodeId && msg.nodeId) node.nodeId = msg.nodeId;
readNode();
});
async function readNode() {
const nodeToRead = {
nodeId: node.nodeId,
attributeId: opcua.AttributeIds.Value
};
const dataValue = await existingClient.session.read(nodeToRead);
const dataValueString = JSON.stringify(dataValue);
const dataValueObj = JSON.parse(dataValueString);
node.send({ payload: dataValueObj });
}
}
RED.nodes.registerType("opcua-read", opcUaReadNode);
}