-
-
Notifications
You must be signed in to change notification settings - Fork 509
Expand file tree
/
Copy pathsample_client_with_callback.js
More file actions
154 lines (142 loc) · 5.25 KB
/
sample_client_with_callback.js
File metadata and controls
154 lines (142 loc) · 5.25 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
const { OPCUAClient, makeBrowsePath, AttributeIds, resolveNodeId, TimestampsToReturn } = require("node-opcua");
const async = require("async");
// const endpointUrl = "opc.tcp://<hostname>:4334/UA/MyLittleServer";
const endpointUrl = "opc.tcp://" + require("os").hostname() + ":4334/UA/MyLittleServer";
const client = OPCUAClient.create({
endpointMustExist: false
});
client.on("backoff", (retry, delay) =>
console.log("still trying to connect to ", endpointUrl, ": retry =", retry, "next attempt in ", delay / 1000, "seconds")
);
let the_session, the_subscription;
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) {
return callback(err);
}
the_session = session;
callback();
});
},
// step 3 : browse
function (callback) {
the_session.browse("RootFolder", function (err, browseResult) {
if (!err) {
console.log("Browsing rootfolder: ");
for (let reference of browseResult.references) {
console.log(reference.browseName.toString(), reference.nodeId.toString());
}
}
callback(err);
});
},
// step 4 : read a variable with readVariableValue
function (callback) {
the_session.read({ nodeId: "ns=1;s=free_memory", attributeId: AttributeIds.Value }, (err, dataValue) => {
if (!err) {
console.log(" free mem % = ", dataValue.toString());
}
callback(err);
});
},
// step 4' : read a variable with read
function (callback) {
const maxAge = 0;
const nodeToRead = {
nodeId: "ns=1;s=free_memory",
attributeId: AttributeIds.Value
};
the_session.read(nodeToRead, maxAge, function (err, dataValue) {
if (!err) {
console.log(" free mem % = ", dataValue.toString());
}
callback(err);
});
},
// step 5: install a subscription and install a monitored item for 10 seconds
function (callback) {
const subscriptionOptions = {
maxNotificationsPerPublish: 1000,
publishingEnabled: true,
requestedLifetimeCount: 100,
requestedMaxKeepAliveCount: 10,
requestedPublishingInterval: 1000
};
the_session.createSubscription2(subscriptionOptions, (err, subscription) => {
if (err) {
return callback(err);
}
the_subscription = subscription;
the_subscription
.on("started", () => {
console.log("subscription started for 2 seconds - subscriptionId=", the_subscription.subscriptionId);
})
.on("keepalive", function () {
console.log("subscription keepalive");
})
.on("terminated", function () {
console.log("terminated");
});
callback();
});
},
function (callback) {
// install monitored item
const itemToMonitor = {
nodeId: resolveNodeId("ns=1;s=free_memory"),
attributeId: AttributeIds.Value
};
const monitoringParamaters = {
samplingInterval: 100,
discardOldest: true,
queueSize: 10
};
the_subscription.monitor(itemToMonitor, monitoringParamaters, TimestampsToReturn.Both, (err, monitoredItem) => {
monitoredItem.on("changed", function (dataValue) {
console.log("monitored item changed: % free mem = ", dataValue.value.value);
});
callback();
});
console.log("-------------------------------------");
},
function (callback) {
// wait a little bit : 10 seconds
setTimeout(() => callback(), 10 * 1000);
},
// terminate session
function (callback) {
the_subscription.terminate(callback);
},
// close session
function (callback) {
the_session.close(function (err) {
if (err) {
console.log("closing session failed ?");
}
callback();
});
}
],
function (err) {
if (err) {
console.log(" failure ", err);
} else {
console.log("done!");
}
client.disconnect(function () {});
}
);