-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopcua-read.js
65 lines (51 loc) · 1.89 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module.exports = function (RED) {
const {
GetClient,
IsValidNodeId
} = require('./core');
const {
AttributeIds,
StatusCodes,
StatusCode
} = require('node-opcua');
function opcUaReadNode(args) {
RED.nodes.createNode(this, args);
const opcuaclientnode = RED.nodes.getNode(args.client);
const opcClient = GetClient(opcuaclientnode.connectionId);
var node = this;
node.name = args.name;
node.nodeId = args.nodeid;
// Read Input Arg node
node.on('input', function (msg) {
if(opcClient.clientState == "reconnecting") return;
if(opcClient.clientState == "disconnected") return;
if (opcClient.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;
const isValid = IsValidNodeId(node.nodeId);
if(!isValid){
node.error(node.nodeId + " is not a valid NodeId");
return;
}
readNode();
});
async function readNode() {
const nodeToRead = {
nodeId: node.nodeId,
attributeId: AttributeIds.Value
};
const dataValue = await opcClient.session.read(nodeToRead);
if(!dataValue.statusCode.isGood()){
node.error("Something went wrong on read NodeId " + node.nodeId);
return;
}
const dataValueString = JSON.stringify(dataValue);
const dataValueObj = JSON.parse(dataValueString);
node.send({ payload: dataValueObj });
}
}
RED.nodes.registerType("opcua-read", opcUaReadNode);
}