forked from node-opcua/node-opcua-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_client.js
144 lines (125 loc) · 3.81 KB
/
simple_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const {
OPCUAClient,
AttributeIds,
ClientSubscription,
TimestampsToReturn
} = require("node-opcua");
const async = require("async");
const client = OPCUAClient.create({ endpoint_must_exist: false });
const endpointUrl = "opc.tcp://opcuademo.sterfive.com:26543";
const nodeId = "ns=7;s=Scalar_Simulation_Double";
/** @type ClientSession */
let theSession = null;
/** @type ClientSubscription */
let theSubscription = null;
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 !");
}
callback(err);
});
},
// step 2 : createSession
function(callback) {
client.createSession(function(err, session) {
if (!err) {
theSession = session;
}
callback(err);
});
},
// step 3 : browse
function(callback) {
theSession.browse("RootFolder", function(err, browse_result) {
if (!err) {
browse_result.references.forEach(function(reference) {
console.log(reference.browseName);
});
}
callback(err);
});
},
// step 4 : read a variable
function(callback) {
theSession.read({
nodeId,
attributeId: AttributeIds.Value
}, (err, dataValue) => {
if (!err) {
console.log(" read value = ", dataValue.toString());
}
callback(err);
})
},
// step 5: install a subscription and monitored item
//
// -----------------------------------------
// create subscription
function(callback) {
theSession.createSubscription2({
requestedPublishingInterval: 1000,
requestedLifetimeCount: 1000,
requestedMaxKeepAliveCount: 20,
maxNotificationsPerPublish: 10,
publishingEnabled: true,
priority: 10
}, function(err, subscription) {
if (err) { return callback(err); }
theSubscription = subscription;
theSubscription.on("keepalive", function() {
console.log("keepalive");
}).on("terminated", function() {
});
callback();
});
}, function(callback) {
// install monitored item
//
theSubscription.monitor({
nodeId,
attributeId: AttributeIds.Value
},
{
samplingInterval: 100,
discardOldest: true,
queueSize: 10
}, TimestampsToReturn.Both,
(err, monitoredItem) => {
console.log("-------------------------------------");
monitoredItem
.on("changed", function(value) {
console.log(" New Value = ", value.toString());
})
.on("err", (err) => {
console.log("MonitoredItem err =", err.message);
});
callback(err);
});
}, function(callback) {
console.log("Waiting 5 seconds")
setTimeout(() => {
theSubscription.terminate();
callback();
}, 5000);
}, function(callback) {
console.log(" closing session");
theSession.close(function(err) {
console.log(" session closed");
callback();
});
},
],
function(err) {
if (err) {
console.log(" failure ", err);
process.exit(0);
} else {
console.log("done!");
}
client.disconnect(function() { });
});