-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.opc-client.js
95 lines (82 loc) · 2.53 KB
/
server.opc-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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
var opcua = require("node-opcua");
var async = require("async");
const client = new opcua.OPCUAClient();
const subscriptionConfig = {
requestedPublishingInterval: 100,
requestedLifetimeCount: 10,
requestedMaxKeepAliveCount: 2,
maxNotificationsPerPublish: 10,
publishingEnabled: true,
priority: 10
};
const monitoredItemConfig = {
samplingInterval: 1,
discardOldest: true,
queueSize: 10
};
//const endpointUrl = "opc.tcp://opcuaserver.com:48010";
let session;
let init = endpointUrl => async.series([
// step 1 : connect to
function (callback) {
client.connect(endpointUrl, function (err) {
if (err) {
console.log(" cannot connect to endpoint :", endpointUrl);
} else {
console.log("connected ! " + endpointUrl);
}
callback(err);
});
},
// step 2 : createSession
function (callback) {
client.createSession(function (err, s) {
if (!err) {
session = s;
}
callback(err);
});
}
],
function (err) {
if (err) {
console.log(" failure ", err);
} else {
//console.log("done!");
}
//client.disconnect(function () { });
});
// step 3: install a subscription and install a monitored item for 10 seconds
let monitorNodes = function (nodes, onDataChange) {
let subscription = new opcua.ClientSubscription(session, subscriptionConfig);
subscription.on("started", function () {
console.log("subscription started for subscriptionId=", subscription.subscriptionId);
}).on("keepalive", function () {
console.log("keepalive");
}).on("terminated", function () {
});
// install monitored item
nodes.forEach(n => {
var monitoredItem = subscription.monitor({
nodeId: opcua.resolveNodeId(n),
attributeId: opcua.AttributeIds.Value
},
monitoredItemConfig,
opcua.read_service.TimestampsToReturn.Both
);
console.log("-------------------------------------");
monitoredItem.on("changed", onDataChange);
});
return subscription;
}
module.exports = {
init,
monitorNodes,
// todo: save all subscriptions in an array and iterate over it for termination
// note: session is lazy init
//close: () => session.close(),
terminateSubscription: subscription => {
console.info("terminating subscription: " + subscription);
subscription.terminate();
}
}