This repository was archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_client.js
More file actions
226 lines (192 loc) · 6.78 KB
/
simple_client.js
File metadata and controls
226 lines (192 loc) · 6.78 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Simple OPC UA client using Node-OPCUA
// Modified by Jeff Codling for proof of concept
// value to read from server
var readValue = "ns=2;s=Sonic";
var readValue2 = "ns=2;s=PumpSpeed";
var readValue3 = "ns=4;s=TemperatureAnalogItem";
// data store for subscribed variables
var values = [];
var values2 = [];
var values3 = [];
var opcua = require("node-opcua");
var async = require("async");
var client = new opcua.OPCUAClient();
// Endpoint currently set to access Open OPC UA server running in Win8 VM
// var endpointUrl = "opc.tcp://" + require("os").hostname() + ":4841";
// Endpoint for Windows 8.1 VM
// var endpointUrl = "opc.tcp://10.211.55.3:16664";
// Endpoint for Raspberry Pi
var endpointUrl = "opc.tcp://192.168.1.116:26543/UA/Server";
// var endpointUrl = "opc.tcp://192.168.1.116:26543";
var the_session = 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) {
the_session = session;
console.log("Session successfully created");
}
callback(err);
});
},
// step 3 : browse
function(callback) {
the_session.browse("RootFolder", function(err,browse_result,diagnostics){
if(!err) {
browse_result[0].references.forEach(function(reference) {
console.log( reference.browseName);
});
}
callback(err);
});
},
// step 4 : read a variable
function(callback) {
the_session.readVariableValue(readValue, function(err,dataValues,diagnostics) {
if (!err) {
console.log("\n " + readValue + " = " + dataValues[0] + "\n");
}
callback(err);
})
},
// step 5: install a subscription and monitored item
//
// -----------------------------------------
// create subscription
function(callback) {
the_subscription=new opcua.ClientSubscription(the_session,{
requestedPublishingInterval: 250,
requestedLifetimeCount: 10,
requestedMaxKeepAliveCount: 2,
maxNotificationsPerPublish: 10,
publishingEnabled: true,
priority: 10
});
the_subscription.on("started",function(){
console.log("subscription started for 2 seconds - subscriptionId=",the_subscription.subscriptionId);
}).on("keepalive",function(){
console.log("keepalive");
}).on("terminated",function(){
callback();
});
setTimeout(function(){
the_subscription.terminate();
},10000);
// install monitored item
//
var monitoredItem = the_subscription.monitor({
nodeId: opcua.resolveNodeId(readValue),
attributeId: 13
//, dataEncoding: { namespaceIndex: 0, name:null }
},
{
samplingInterval: 100,
discardOldest: true,
queueSize: 10
});
console.log("Monitor-----------------1-------------------");
// subscription.on("item_added",function(monitoredItem){
//xx monitoredItem.on("initialized",function(){ });
//xx monitoredItem.on("terminated",function(value){ });
var monitoredItem2 = the_subscription.monitor({
nodeId: opcua.resolveNodeId(readValue2),
attributeId: 13
},
{
sampleInterval: 100,
discardOldest: true,
queueSize: 10
});
console.log("Monitor-----------------2-------------------");
var monitoredItem3 = the_subscription.monitor({
nodeId: opcua.resolveNodeId(readValue3),
attributeId: 13
},
{
sampleInterval: 100,
discardOldest: true,
queueSize: 10
});
console.log("Monitor-----------------3-------------------");
monitoredItem.on("changed",function(value){
console.log("==========" + readValue.cyan);
console.log(value.value.value);
values.push(value.value.value);
});
monitoredItem2.on("changed",function(value){
console.log("----------" + readValue2.red);
console.log(value.value.value);
values2.push(value.value.value);
});
monitoredItem3.on("changed",function(value){
console.log("++++++++++" + readValue3.green);
console.log(value.value.value);
values3.push(value.value.value);
});
},
// ------------------------------------------------
// closing session
//
function(callback) {
console.log("-closing session".yellow);
the_session.close(function(err){
console.log("-session closed".red);
callback();
});
}
],
function(err) {
if (err) {
console.log("-failure ".red,err);
} else {
console.log("done!".green)
var average = 0,
min = 9999999999,
max = 0,
i = 0;
console.log("\nStats".white);
for(i = 0; i < values.length; i++) {
average += values[i];
};
average /= values.length;
min = Math.min.apply(Math, values);
max = Math.max.apply(Math, values);
console.log("");
console.log(readValue.cyan);
console.log("Len=" + values.length + "\tMax=" + max + "\tMin=" + min + "\tAvg=" + average);
console.log("");
average = min = max = 0;
for(i = 0; i < values2.length; i++) {
average += values2[i];
};
average /= values2.length;
min = Math.min.apply(Math, values2);
max = Math.max.apply(Math, values2);
console.log(readValue2.red);
console.log("Len=" + values2.length + "\tMax=" + max + "\tMin=" + min + "\tAvg=" + average);
console.log("");
average = min = max = 0;
for(i = 0; i < values3.length; i++) {
average += values3[i];
};
average /= values3.length;
min = Math.min.apply(Math, values3);
max = Math.max.apply(Math, values3);
console.log(readValue3.green);
console.log("Len=" + values3.length + "\tMax=" + max + "\tMin=" + min + "\tAvg=" + average);
}
client.disconnect(function(){});
}
);