-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopcua-client.js
55 lines (41 loc) · 1.5 KB
/
opcua-client.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
module.exports = function (RED) {
var core = require('./core');
var opcua = require('node-opcua');
// constructor
function opcUaClientNode(args) {
RED.nodes.createNode(this, args);
let node = this;
node.connectionId = args.id;
node.name = args.name; //unique name identifier
node.host = args.host; //opc.tcp
// Setup client
const authOption = {
securityPolicy: opcua.SecurityPolicy[args.securitypolicy],
securityMode: opcua.MessageSecurityMode[args.messagesecurity],
}
node.debug('Setup opc client for ' + node.name);
const opcClient = core.createOpcUaClient(node.connectionId, node.name, authOption);
// Connect client
connect();
node.on('close', onNodeClosed);
node.on('error', onNodeError);
//#region Methods
async function connect() {
const userOption = {
type: args.anonymous ? opcua.UserTokenType.Anonymous : opcua.UserTokenType.UserName,
userName: args.username,
password: args.password
}
await core.connect(node.connectionId, node.host, userOption);
}
async function onNodeClosed(done){
await core.close(node.connectionId);
done();
}
function onNodeError(){
core.close(node.connectionId);
}
//#endregion
}
RED.nodes.registerType("opcua-client", opcUaClientNode);
}