-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopcua-browse.js
51 lines (37 loc) · 1.52 KB
/
opcua-browse.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
module.exports = function (RED) {
var core = require('./core');
function opcUaBrowseNode(args) {
RED.nodes.createNode(this, args);
const opcuaclientnode = RED.nodes.getNode(args.client);
const existingClient = core.opcClients[opcuaclientnode.connectionId];
let node = this;
node.name = args.name;
node.nodeId = args.nodeid;
// Read Input Arg node
node.on('input', function (msg) {
if (!existingClient) {
node.error("OPC UA Client not defined");
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;
browseNode(existingClient);
});
async function browseNode(opcClient) {
const browseResult = await opcClient.session.browse(node.nodeId);
items = [browseResult.references.length];
for (const index in browseResult.references) {
const reference = browseResult.references[index];
const browseItemString = JSON.stringify(reference);
const browseItemObj = JSON.parse(browseItemString);
items[index] = browseItemObj;
}
node.send({ payload: items });
}
}
RED.nodes.registerType("opcua-browse", opcUaBrowseNode);
}