node-opcua-samples 2.183.1 → 2.184.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/client_example.cjs +711 -0
- package/dist/client_example_ts.d.ts +1 -0
- package/dist/client_example_ts.d.ts.map +1 -0
- package/dist/client_example_ts.js +89 -125
- package/dist/client_example_ts.js.map +1 -1
- package/dist/get_endpoints.d.ts +1 -0
- package/dist/get_endpoints.d.ts.map +1 -0
- package/dist/get_endpoints.js +36 -39
- package/dist/get_endpoints.js.map +1 -1
- package/dist/mini_server.d.ts +1 -0
- package/dist/mini_server.d.ts.map +1 -0
- package/dist/mini_server.js +23 -28
- package/dist/mini_server.js.map +1 -1
- package/dist/mini_server_on_port_0.d.ts +1 -0
- package/dist/mini_server_on_port_0.d.ts.map +1 -0
- package/dist/mini_server_on_port_0.js +8 -13
- package/dist/mini_server_on_port_0.js.map +1 -1
- package/dist/reverse_connect_client.d.ts +1 -0
- package/dist/reverse_connect_client.d.ts.map +1 -0
- package/dist/reverse_connect_client.js +13 -18
- package/dist/reverse_connect_client.js.map +1 -1
- package/dist/reverse_connect_server.d.ts +1 -0
- package/dist/reverse_connect_server.d.ts.map +1 -0
- package/dist/reverse_connect_server.js +17 -22
- package/dist/reverse_connect_server.js.map +1 -1
- package/dist/server_with_changing_password.d.ts +1 -0
- package/dist/server_with_changing_password.d.ts.map +1 -0
- package/dist/server_with_changing_password.js +10 -12
- package/dist/server_with_changing_password.js.map +1 -1
- package/dist/server_with_push_certificate.d.ts +1 -0
- package/dist/server_with_push_certificate.d.ts.map +1 -0
- package/dist/server_with_push_certificate.js +29 -37
- package/dist/server_with_push_certificate.js.map +1 -1
- package/dist/simple_findservers.d.ts +1 -0
- package/dist/simple_findservers.d.ts.map +1 -0
- package/dist/simple_findservers.js +13 -18
- package/dist/simple_findservers.js.map +1 -1
- package/dist/simple_secure_server.d.ts +1 -0
- package/dist/simple_secure_server.d.ts.map +1 -0
- package/dist/simple_secure_server.js +26 -31
- package/dist/simple_secure_server.js.map +1 -1
- package/dist/simple_server_with_custom_extension_objects.d.ts +1 -0
- package/dist/simple_server_with_custom_extension_objects.d.ts.map +1 -0
- package/dist/simple_server_with_custom_extension_objects.js +15 -23
- package/dist/simple_server_with_custom_extension_objects.js.map +1 -1
- package/dist/simple_server_with_hsm_style_key.d.ts +1 -0
- package/dist/simple_server_with_hsm_style_key.d.ts.map +1 -0
- package/dist/simple_server_with_hsm_style_key.js +13 -18
- package/dist/simple_server_with_hsm_style_key.js.map +1 -1
- package/dist/stressing_client.d.ts +1 -0
- package/dist/stressing_client.d.ts.map +1 -0
- package/dist/stressing_client.js +4 -6
- package/dist/stressing_client.js.map +1 -1
- package/dist/tiny_client.d.ts +1 -0
- package/dist/tiny_client.d.ts.map +1 -0
- package/dist/tiny_client.js +2 -4
- package/dist/tiny_client.js.map +1 -1
- package/package.json +17 -16
- /package/bin/{di_server.js → di_server.cjs} +0 -0
- /package/bin/{interactive_client.js → interactive_client.cjs} +0 -0
- /package/bin/{simple_server.js → simple_server.cjs} +0 -0
|
@@ -0,0 +1,711 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const path = require("path");
|
|
4
|
+
const util = require("util");
|
|
5
|
+
const treeify = require("treeify");
|
|
6
|
+
const chalk = require("chalk");
|
|
7
|
+
const Table = require("easy-table");
|
|
8
|
+
const {
|
|
9
|
+
ApplicationType,
|
|
10
|
+
assert,
|
|
11
|
+
AttributeIds,
|
|
12
|
+
BrowseDirection,
|
|
13
|
+
callConditionRefresh,
|
|
14
|
+
ClientMonitoredItem,
|
|
15
|
+
coerceMessageSecurityMode,
|
|
16
|
+
coerceNodeId,
|
|
17
|
+
coerceSecurityPolicy,
|
|
18
|
+
constructEventFilter,
|
|
19
|
+
dumpEvent,
|
|
20
|
+
hexDump,
|
|
21
|
+
makeExpandedNodeId,
|
|
22
|
+
makeNodeId,
|
|
23
|
+
MessageSecurityMode,
|
|
24
|
+
NodeClassMask,
|
|
25
|
+
NodeId,
|
|
26
|
+
ObjectTypeIds,
|
|
27
|
+
OPCUAClient,
|
|
28
|
+
resolveNodeId,
|
|
29
|
+
SecurityPolicy,
|
|
30
|
+
VariableIds,
|
|
31
|
+
ObjectIds,
|
|
32
|
+
UserTokenType
|
|
33
|
+
} = require("node-opcua");
|
|
34
|
+
const { toPem } = require("node-opcua-crypto");
|
|
35
|
+
|
|
36
|
+
//node bin/client_example.js --endpoint opc.tcp://localhost:53530/OPCUA/SimulationServer --node "ns=5;s=Sinusoid1"
|
|
37
|
+
const commandLineArgs = require("command-line-args");
|
|
38
|
+
const commandLineUsage = require("command-line-usage");
|
|
39
|
+
|
|
40
|
+
const optionDefinitions = [
|
|
41
|
+
{ name: "endpoint", alias: "e", type: String, description: "the end point to connect to " },
|
|
42
|
+
{ name: "securityMode", alias: "s", type: String, defaultValue: "None", description: "the security mode ( None Sign SignAndEncrypt )" },
|
|
43
|
+
{
|
|
44
|
+
name: "securityPolicy",
|
|
45
|
+
alias: "P",
|
|
46
|
+
type: String,
|
|
47
|
+
defaultValue: "None",
|
|
48
|
+
description: "the policy mode : (" + Object.keys(SecurityPolicy).join(" - ") + ")"
|
|
49
|
+
},
|
|
50
|
+
{ name: "userName", alias: "u", type: String, description: "specify the user name of a UserNameIdentityToken" },
|
|
51
|
+
{ name: "password", alias: "p", type: String, description: "specify the password of a UserNameIdentityToken" },
|
|
52
|
+
{ name: "node", alias: "n", type: String, description: "the nodeId of the value to monitor" },
|
|
53
|
+
{ name: "timeout", alias: "t", type: Number, description: " the timeout of the session in second => (-1 for infinity)" },
|
|
54
|
+
{ name: "debug", alias: "d", type: Boolean, description: " display more verbose information" },
|
|
55
|
+
{ name: "history", alias: "h", type: Boolean, description: "make an historical read" },
|
|
56
|
+
{
|
|
57
|
+
name: "discovery",
|
|
58
|
+
alias: "D",
|
|
59
|
+
type: String,
|
|
60
|
+
description: "specify the endpoint uri of discovery server (by default same as server endpoint uri)"
|
|
61
|
+
}
|
|
62
|
+
];
|
|
63
|
+
const sections = [
|
|
64
|
+
{ header: "client_example", content: "connect to a server, browse it and monitor a node" },
|
|
65
|
+
{ header: "Options", optionList: optionDefinitions },
|
|
66
|
+
{
|
|
67
|
+
header: "Examples",
|
|
68
|
+
content: [
|
|
69
|
+
"simple_client --endpoint opc.tcp://localhost:49230 -P=Basic256Rsa256 -s=Sign",
|
|
70
|
+
"simple_client -e opc.tcp://localhost:49230 -P=Basic256Sha256 -s=Sign -u JoeDoe -p P@338@rd ",
|
|
71
|
+
'simple_client --endpoint opc.tcp://localhost:49230 -n="ns=0;i=2258"'
|
|
72
|
+
]
|
|
73
|
+
}
|
|
74
|
+
];
|
|
75
|
+
const argv = commandLineArgs(optionDefinitions);
|
|
76
|
+
|
|
77
|
+
const securityMode = coerceMessageSecurityMode(argv.securityMode);
|
|
78
|
+
if (securityMode === MessageSecurityMode.Invalid) {
|
|
79
|
+
throw new Error("Invalid Security mode");
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const securityPolicy = coerceSecurityPolicy(argv.securityPolicy);
|
|
83
|
+
if (securityPolicy === SecurityPolicy.Invalid) {
|
|
84
|
+
throw new Error("Invalid securityPolicy");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const timeout = argv.timeout * 1000 || 20000;
|
|
88
|
+
|
|
89
|
+
const monitored_node = coerceNodeId(argv.node || makeNodeId(VariableIds.Server_ServerStatus_CurrentTime));
|
|
90
|
+
|
|
91
|
+
console.log(chalk.cyan("securityMode = "), securityMode.toString());
|
|
92
|
+
console.log(chalk.cyan("securityPolicy = "), securityPolicy.toString());
|
|
93
|
+
console.log(chalk.cyan("timeout = "), timeout ? timeout : " Infinity ");
|
|
94
|
+
console.log(" monitoring node id = ", monitored_node);
|
|
95
|
+
|
|
96
|
+
const endpointUrl = argv.endpoint;
|
|
97
|
+
|
|
98
|
+
if (!endpointUrl) {
|
|
99
|
+
console.log(commandLineUsage(sections));
|
|
100
|
+
process.exit(0);
|
|
101
|
+
}
|
|
102
|
+
const discoveryUrl = argv.discovery ? argv.discovery : endpointUrl;
|
|
103
|
+
|
|
104
|
+
const doHistory = !!argv.history;
|
|
105
|
+
|
|
106
|
+
function w(str, l) {
|
|
107
|
+
return str.padEnd(l).substring(0, l);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async function enumerateAllConditionTypes(session) {
|
|
111
|
+
const tree = {};
|
|
112
|
+
|
|
113
|
+
const conditionEventTypes = {};
|
|
114
|
+
|
|
115
|
+
async function findAllNodeOfType(tree1, typeNodeId1, browseName) {
|
|
116
|
+
const browseDesc1 = {
|
|
117
|
+
nodeId: typeNodeId1,
|
|
118
|
+
referenceTypeId: resolveNodeId("HasSubtype"),
|
|
119
|
+
|
|
120
|
+
browseDirection: BrowseDirection.Forward,
|
|
121
|
+
includeSubtypes: true,
|
|
122
|
+
resultMask: 63
|
|
123
|
+
};
|
|
124
|
+
const browseDesc2 = {
|
|
125
|
+
nodeId: typeNodeId1,
|
|
126
|
+
referenceTypeId: resolveNodeId("HasTypeDefinition"),
|
|
127
|
+
|
|
128
|
+
browseDirection: BrowseDirection.Inverse,
|
|
129
|
+
includeSubtypes: true,
|
|
130
|
+
resultMask: 63
|
|
131
|
+
};
|
|
132
|
+
const browseDesc3 = {
|
|
133
|
+
nodeId: typeNodeId1,
|
|
134
|
+
referenceTypeId: resolveNodeId("HasTypeDefinition"),
|
|
135
|
+
|
|
136
|
+
browseDirection: BrowseDirection.Forward,
|
|
137
|
+
includeSubtypes: true,
|
|
138
|
+
resultMask: 63
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const nodesToBrowse = [browseDesc1, browseDesc2, browseDesc3];
|
|
142
|
+
const browseResults = await session.browse(nodesToBrowse);
|
|
143
|
+
|
|
144
|
+
tree1[browseName] = {};
|
|
145
|
+
browseResults[0].references = browseResults[0].references || [];
|
|
146
|
+
const promises = [];
|
|
147
|
+
for (const reference of browseResults[0].references) {
|
|
148
|
+
conditionEventTypes[reference.nodeId.toString()] = reference.browseName.toString();
|
|
149
|
+
promises.push(findAllNodeOfType(tree1[browseName], reference.nodeId, reference.browseName.toString()));
|
|
150
|
+
}
|
|
151
|
+
await Promise.all(promises);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const typeNodeId = resolveNodeId("ConditionType");
|
|
155
|
+
|
|
156
|
+
await findAllNodeOfType(tree, typeNodeId, "ConditionType");
|
|
157
|
+
|
|
158
|
+
return tree;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async function enumerateAllAlarmAndConditionInstances(session) {
|
|
162
|
+
const conditions = {};
|
|
163
|
+
|
|
164
|
+
const found = [];
|
|
165
|
+
|
|
166
|
+
function isConditionEventType(nodeId) {
|
|
167
|
+
return Object.prototype.hasOwnProperty.call(conditions,nodeId.toString());
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async function exploreForObjectOfType(session1, nodeId) {
|
|
171
|
+
async function worker(element) {
|
|
172
|
+
const nodeToBrowse = {
|
|
173
|
+
nodeId: element.nodeId,
|
|
174
|
+
referenceTypeId: resolveNodeId("HierarchicalReferences"),
|
|
175
|
+
|
|
176
|
+
browseDirection: BrowseDirection.Forward,
|
|
177
|
+
includeSubtypes: true,
|
|
178
|
+
nodeClassMask: 0x1, // Objects
|
|
179
|
+
resultMask: 63
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
const browseResult = await session1.browse(nodeToBrowse);
|
|
183
|
+
|
|
184
|
+
for (const ref of browseResult.references) {
|
|
185
|
+
if (isConditionEventType(ref.typeDefinition)) {
|
|
186
|
+
//
|
|
187
|
+
const alarm = {
|
|
188
|
+
parent: element.nodeId,
|
|
189
|
+
|
|
190
|
+
alarmNodeId: ref.nodeId,
|
|
191
|
+
browseName: ref.browseName,
|
|
192
|
+
typeDefinition: ref.typeDefinition,
|
|
193
|
+
typeDefinitionName: conditions[ref.typeDefinition.toString()]
|
|
194
|
+
};
|
|
195
|
+
found.push(alarm);
|
|
196
|
+
} else {
|
|
197
|
+
await worker(ref.nodeId);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
await worker(nodeId);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
await enumerateAllConditionTypes(session);
|
|
206
|
+
|
|
207
|
+
await exploreForObjectOfType(session, resolveNodeId("RootFolder"));
|
|
208
|
+
|
|
209
|
+
return Object.values(conditions);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async function _getAllEventTypes(session, baseNodeId, tree) {
|
|
213
|
+
const browseDesc1 = {
|
|
214
|
+
nodeId: baseNodeId,
|
|
215
|
+
referenceTypeId: resolveNodeId("HasSubtype"),
|
|
216
|
+
|
|
217
|
+
browseDirection: BrowseDirection.Forward,
|
|
218
|
+
includeSubtypes: true,
|
|
219
|
+
nodeClassMask: NodeClassMask.ObjectType, // Objects
|
|
220
|
+
resultMask: 63
|
|
221
|
+
};
|
|
222
|
+
const browseResult = await session.browse(browseDesc1);
|
|
223
|
+
|
|
224
|
+
// to do continuation points
|
|
225
|
+
for (const reference of browseResult.references) {
|
|
226
|
+
const subtree = { nodeId: reference.nodeId.toString() };
|
|
227
|
+
tree[reference.browseName.toString()] = subtree;
|
|
228
|
+
await _getAllEventTypes(session, reference.nodeId, subtree);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* getAllEventType recursively
|
|
234
|
+
*/
|
|
235
|
+
async function getAllEventTypes(session) {
|
|
236
|
+
const baseNodeId = makeNodeId(ObjectTypeIds.BaseEventType);
|
|
237
|
+
const result = {};
|
|
238
|
+
await _getAllEventTypes(session, baseNodeId, result);
|
|
239
|
+
return result;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
async function monitorAlarm(session, subscription) {
|
|
243
|
+
try {
|
|
244
|
+
await callConditionRefresh(session, subscription);
|
|
245
|
+
} catch (err) {
|
|
246
|
+
console.log(" monitorAlarm failed , may be your server doesn't support A&E", err.message);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function getTick() {
|
|
251
|
+
return Date.now();
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
async function exploreEndpoint(endpointUrl) {
|
|
255
|
+
const optionsInitial = {
|
|
256
|
+
securityMode,
|
|
257
|
+
securityPolicy,
|
|
258
|
+
|
|
259
|
+
endpointMustExist: false,
|
|
260
|
+
keepSessionAlive: true,
|
|
261
|
+
|
|
262
|
+
connectionStrategy: {
|
|
263
|
+
initialDelay: 2000,
|
|
264
|
+
maxDelay: 10 * 1000,
|
|
265
|
+
maxRetry: 10
|
|
266
|
+
},
|
|
267
|
+
|
|
268
|
+
discoveryUrl
|
|
269
|
+
};
|
|
270
|
+
const client = OPCUAClient.create(optionsInitial);
|
|
271
|
+
|
|
272
|
+
client.on("backoff", (retry, delay) => {
|
|
273
|
+
console.log(chalk.bgWhite.yellow("backoff attempt #"), retry, " retrying in ", delay / 1000.0, " seconds");
|
|
274
|
+
});
|
|
275
|
+
console.log(" connecting to ", chalk.cyan.bold(endpointUrl));
|
|
276
|
+
console.log(" strategy", client.connectionStrategy);
|
|
277
|
+
|
|
278
|
+
try {
|
|
279
|
+
await client.connect(endpointUrl);
|
|
280
|
+
console.log(" Connected ! exact endpoint url is ", client.endpointUrl);
|
|
281
|
+
} catch (err) {
|
|
282
|
+
console.log(chalk.red(" Cannot connect to ") + endpointUrl);
|
|
283
|
+
console.log(" Error = ", err.message);
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
const endpoints = await client.getEndpoints();
|
|
288
|
+
|
|
289
|
+
if (argv.debug) {
|
|
290
|
+
fs.writeFileSync("tmp/endpoints.log", JSON.stringify(endpoints, null, " "));
|
|
291
|
+
endpoints.forEach((a) => (a.serverCertificate = a.serverCertificate.toString("base64")));
|
|
292
|
+
console.log(treeify.asTree(endpoints, true));
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const table = new Table();
|
|
296
|
+
|
|
297
|
+
let serverCertificate;
|
|
298
|
+
|
|
299
|
+
let i = 0;
|
|
300
|
+
for (const endpoint of endpoints) {
|
|
301
|
+
table.cell("endpoint", endpoint.endpointUrl + "");
|
|
302
|
+
table.cell("Application URI", endpoint.server.applicationUri);
|
|
303
|
+
table.cell("Product URI", endpoint.server.productUri);
|
|
304
|
+
table.cell("Application Name", endpoint.server.applicationName.text);
|
|
305
|
+
table.cell("Security Mode", MessageSecurityMode[endpoint.securityMode].toString());
|
|
306
|
+
table.cell("securityPolicyUri", endpoint.securityPolicyUri);
|
|
307
|
+
table.cell("Type", ApplicationType[endpoint.server.applicationType]);
|
|
308
|
+
table.cell("certificate", "..." /*endpoint.serverCertificate*/);
|
|
309
|
+
endpoint.server.discoveryUrls = endpoint.server.discoveryUrls || [];
|
|
310
|
+
table.cell("discoveryUrls", endpoint.server.discoveryUrls.join(" - "));
|
|
311
|
+
|
|
312
|
+
serverCertificate = endpoint.serverCertificate;
|
|
313
|
+
|
|
314
|
+
const certificate_filename = path.join(__dirname, "../certificates/PKI/server_certificate" + i + ".pem");
|
|
315
|
+
|
|
316
|
+
if (serverCertificate) {
|
|
317
|
+
fs.writeFile(certificate_filename, toPem(serverCertificate, "CERTIFICATE"), () => {
|
|
318
|
+
/**/
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
table.newRow();
|
|
322
|
+
i++;
|
|
323
|
+
}
|
|
324
|
+
console.log(table.toString());
|
|
325
|
+
|
|
326
|
+
for (const endpoint of endpoints) {
|
|
327
|
+
console.log(
|
|
328
|
+
"Identify Token for : Security Mode=",
|
|
329
|
+
endpoint.securityMode.toString(),
|
|
330
|
+
" Policy=",
|
|
331
|
+
endpoint.securityPolicyUri
|
|
332
|
+
);
|
|
333
|
+
const table2 = new Table();
|
|
334
|
+
for (const token of endpoint.userIdentityTokens) {
|
|
335
|
+
table2.cell("policyId", token.policyId);
|
|
336
|
+
table2.cell("tokenType", token.tokenType.toString());
|
|
337
|
+
table2.cell("issuedTokenType", token.issuedTokenType);
|
|
338
|
+
table2.cell("issuerEndpointUrl", token.issuerEndpointUrl);
|
|
339
|
+
table2.cell("securityPolicyUri", token.securityPolicyUri);
|
|
340
|
+
table2.newRow();
|
|
341
|
+
}
|
|
342
|
+
console.log(table2.toString());
|
|
343
|
+
}
|
|
344
|
+
await client.disconnect();
|
|
345
|
+
console.log(chalk.cyan("Server Certificate :"));
|
|
346
|
+
console.log(chalk.yellow(hexDump(serverCertificate)));
|
|
347
|
+
return client.endpointUrl;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
async function main() {
|
|
351
|
+
// reconnect using the correct end point URL now
|
|
352
|
+
const adjustedEndpointUrl = await exploreEndpoint(endpointUrl);
|
|
353
|
+
|
|
354
|
+
const options = {
|
|
355
|
+
securityMode,
|
|
356
|
+
securityPolicy,
|
|
357
|
+
|
|
358
|
+
// we provide here server certificate , so it is important to connect with proper endpoint Url
|
|
359
|
+
// serverCertificate,
|
|
360
|
+
|
|
361
|
+
defaultSecureTokenLifetime: 40000,
|
|
362
|
+
|
|
363
|
+
endpointMustExist: false,
|
|
364
|
+
|
|
365
|
+
connectionStrategy: {
|
|
366
|
+
initialDelay: 2000,
|
|
367
|
+
maxDelay: 10 * 1000,
|
|
368
|
+
maxRetry: 10
|
|
369
|
+
}
|
|
370
|
+
};
|
|
371
|
+
console.log("Options = ", options.securityMode.toString(), options.securityPolicy.toString());
|
|
372
|
+
|
|
373
|
+
const client = OPCUAClient.create(options);
|
|
374
|
+
|
|
375
|
+
console.log(" reconnecting to ", chalk.cyan.bold(adjustedEndpointUrl));
|
|
376
|
+
await client.connect(adjustedEndpointUrl);
|
|
377
|
+
|
|
378
|
+
console.log(" Connected ! exact endpoint url is ", client.endpointUrl);
|
|
379
|
+
|
|
380
|
+
let userIdentity; // anonymous
|
|
381
|
+
if (argv.userName && argv.password) {
|
|
382
|
+
userIdentity = {
|
|
383
|
+
password: argv.password,
|
|
384
|
+
userName: argv.userName,
|
|
385
|
+
type: UserTokenType.UserName
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
const session = await client.createSession(userIdentity);
|
|
390
|
+
client.on("connection_reestablished", () => {
|
|
391
|
+
console.log(chalk.bgWhite.red(" !!!!!!!!!!!!!!!!!!!!!!!! CONNECTION RE-ESTABLISHED !!!!!!!!!!!!!!!!!!!"));
|
|
392
|
+
});
|
|
393
|
+
console.log(chalk.yellow(" session created"));
|
|
394
|
+
console.log(" sessionId : ", session.sessionId.toString());
|
|
395
|
+
|
|
396
|
+
client.on("backoff", (retry, delay) => {
|
|
397
|
+
console.log(chalk.bgWhite.yellow("backoff attempt #"), retry, " retrying in ", delay / 1000.0, " seconds");
|
|
398
|
+
});
|
|
399
|
+
client.on("start_reconnection", () => {
|
|
400
|
+
console.log(chalk.bgWhite.red(" !!!!!!!!!!!!!!!!!!!!!!!! Starting Reconnection !!!!!!!!!!!!!!!!!!!"));
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
// -----------------------------------------------------------------------------------------------------------
|
|
404
|
+
// NAMESPACE
|
|
405
|
+
// display namespace array
|
|
406
|
+
// -----------------------------------------------------------------------------------------------------------
|
|
407
|
+
const server_NamespaceArray_Id = makeNodeId(VariableIds.Server_NamespaceArray); // ns=0;i=2006
|
|
408
|
+
|
|
409
|
+
const dataValue = await session.readVariableValue(server_NamespaceArray_Id);
|
|
410
|
+
|
|
411
|
+
console.log(" --- NAMESPACE ARRAY ---");
|
|
412
|
+
const namespaceArray = dataValue.value.value; /*as string[] */
|
|
413
|
+
namespaceArray.forEach((namespace, index) => {
|
|
414
|
+
console.log(" Namespace ", index, " : ", namespace);
|
|
415
|
+
});
|
|
416
|
+
console.log(" -----------------------");
|
|
417
|
+
|
|
418
|
+
// -----------------------------------------------------------------------------------------------------------
|
|
419
|
+
// enumerate all EVENT TYPES
|
|
420
|
+
// -----------------------------------------------------------------------------------------------------------
|
|
421
|
+
const result = getAllEventTypes(session);
|
|
422
|
+
console.log(chalk.cyan("---------------------------------------------------- All Event Types "));
|
|
423
|
+
console.log(treeify.asTree(result, true));
|
|
424
|
+
console.log(" -----------------------");
|
|
425
|
+
|
|
426
|
+
// -----------------------------------------------------------------------------------------------------------------
|
|
427
|
+
// enumerate all Condition Types exposed by the server
|
|
428
|
+
// -----------------------------------------------------------------------------------------------------------------
|
|
429
|
+
|
|
430
|
+
console.log(
|
|
431
|
+
"--------------------------------------------------------------- Enumerate all Condition Types exposed by the server"
|
|
432
|
+
);
|
|
433
|
+
const conditionTree = await enumerateAllConditionTypes(session);
|
|
434
|
+
console.log(treeify.asTree(conditionTree));
|
|
435
|
+
console.log(
|
|
436
|
+
" -----------------------------------------------------------------------------------------------------------------"
|
|
437
|
+
);
|
|
438
|
+
|
|
439
|
+
// -----------------------------------------------------------------------------------------------------------------
|
|
440
|
+
// enumerate all objects that have an Alarm & Condition instances
|
|
441
|
+
// -----------------------------------------------------------------------------------------------------------------
|
|
442
|
+
|
|
443
|
+
const alarms = await enumerateAllAlarmAndConditionInstances(session);
|
|
444
|
+
|
|
445
|
+
console.log(" -------------------------------------------------------------- Alarms & Conditions ------------------------");
|
|
446
|
+
for (const alarm of alarms) {
|
|
447
|
+
console.log(
|
|
448
|
+
"parent = ",
|
|
449
|
+
chalk.cyan(w(alarm.parent.toString(), 30)),
|
|
450
|
+
chalk.green.bold(w(alarm.typeDefinitionName, 30)),
|
|
451
|
+
"alarmName = ",
|
|
452
|
+
chalk.cyan(w(alarm.browseName.toString(), 30)),
|
|
453
|
+
chalk.yellow(w(alarm.alarmNodeId.toString(), 40))
|
|
454
|
+
);
|
|
455
|
+
}
|
|
456
|
+
console.log(
|
|
457
|
+
" -----------------------------------------------------------------------------------------------------------------"
|
|
458
|
+
);
|
|
459
|
+
|
|
460
|
+
// -----------------------------------------------------------------------------------------------------------------
|
|
461
|
+
// Testing if server implements QueryFirst
|
|
462
|
+
// -----------------------------------------------------------------------------------------------------------------
|
|
463
|
+
try {
|
|
464
|
+
console.log(" ---------------------------------------------------------- Testing QueryFirst");
|
|
465
|
+
const queryFirstRequest = {
|
|
466
|
+
view: {
|
|
467
|
+
viewId: NodeId.nullNodeId
|
|
468
|
+
},
|
|
469
|
+
|
|
470
|
+
nodeTypes: [
|
|
471
|
+
{
|
|
472
|
+
typeDefinitionNode: makeExpandedNodeId("i=58"),
|
|
473
|
+
|
|
474
|
+
includeSubTypes: true,
|
|
475
|
+
|
|
476
|
+
dataToReturn: [
|
|
477
|
+
{
|
|
478
|
+
attributeId: AttributeIds.AccessLevel,
|
|
479
|
+
relativePath: undefined
|
|
480
|
+
}
|
|
481
|
+
]
|
|
482
|
+
}
|
|
483
|
+
]
|
|
484
|
+
};
|
|
485
|
+
|
|
486
|
+
console.log(
|
|
487
|
+
" -----------------------------------------------------------------------------------------------------------------"
|
|
488
|
+
);
|
|
489
|
+
} catch (err) {
|
|
490
|
+
console.log(" Server is not supporting queryFirst err=", err.message);
|
|
491
|
+
}
|
|
492
|
+
// create Read
|
|
493
|
+
if (doHistory) {
|
|
494
|
+
console.log(" ---------------------------------------------------------- History Read------------------------");
|
|
495
|
+
const now = Date.now();
|
|
496
|
+
const start = now - 1000; // read 1 seconds of history
|
|
497
|
+
console.log(
|
|
498
|
+
" -----------------------------------------------------------------------------------------------------------------"
|
|
499
|
+
);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// ----------------------------------------------------------------------------------
|
|
503
|
+
// create subscription
|
|
504
|
+
// ----------------------------------------------------------------------------------
|
|
505
|
+
console.log(" ---------------------------------------------------------- Create Subscription ");
|
|
506
|
+
const parameters = {
|
|
507
|
+
maxNotificationsPerPublish: 10,
|
|
508
|
+
priority: 10,
|
|
509
|
+
publishingEnabled: true,
|
|
510
|
+
requestedLifetimeCount: 1000,
|
|
511
|
+
requestedMaxKeepAliveCount: 12,
|
|
512
|
+
requestedPublishingInterval: 2000
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
const subscription = await session.createSubscription2(parameters);
|
|
516
|
+
|
|
517
|
+
let t = getTick();
|
|
518
|
+
|
|
519
|
+
console.log("started subscription :", subscription.subscriptionId);
|
|
520
|
+
console.log(" revised parameters ");
|
|
521
|
+
console.log(
|
|
522
|
+
" revised maxKeepAliveCount ",
|
|
523
|
+
subscription.maxKeepAliveCount,
|
|
524
|
+
" ( requested ",
|
|
525
|
+
parameters.requestedMaxKeepAliveCount + ")"
|
|
526
|
+
);
|
|
527
|
+
console.log(
|
|
528
|
+
" revised lifetimeCount ",
|
|
529
|
+
subscription.lifetimeCount,
|
|
530
|
+
" ( requested ",
|
|
531
|
+
parameters.requestedLifetimeCount + ")"
|
|
532
|
+
);
|
|
533
|
+
console.log(
|
|
534
|
+
" revised publishingInterval ",
|
|
535
|
+
subscription.publishingInterval,
|
|
536
|
+
" ( requested ",
|
|
537
|
+
parameters.requestedPublishingInterval + ")"
|
|
538
|
+
);
|
|
539
|
+
|
|
540
|
+
console.log(
|
|
541
|
+
"subscription duration ",
|
|
542
|
+
((subscription.lifetimeCount * subscription.publishingInterval) / 1000).toFixed(3),
|
|
543
|
+
"seconds"
|
|
544
|
+
);
|
|
545
|
+
subscription
|
|
546
|
+
.on("internal_error", (err) => {
|
|
547
|
+
console.log(" received internal error", err.message);
|
|
548
|
+
})
|
|
549
|
+
.on("keepalive", () => {
|
|
550
|
+
const t4 = getTick();
|
|
551
|
+
const span = t4 - t;
|
|
552
|
+
t = t4;
|
|
553
|
+
console.log(
|
|
554
|
+
"keepalive ",
|
|
555
|
+
span / 1000,
|
|
556
|
+
"sec",
|
|
557
|
+
" pending request on server = ",
|
|
558
|
+
subscription.getPublishEngine().nbPendingPublishRequests
|
|
559
|
+
);
|
|
560
|
+
})
|
|
561
|
+
.on("terminated", () => {
|
|
562
|
+
/* */
|
|
563
|
+
|
|
564
|
+
console.log("Subscription is terminated ....");
|
|
565
|
+
});
|
|
566
|
+
|
|
567
|
+
try {
|
|
568
|
+
const results1 = await subscription.getMonitoredItems();
|
|
569
|
+
console.log("MonitoredItems clientHandles", results1.clientHandles);
|
|
570
|
+
console.log("MonitoredItems serverHandles", results1.serverHandles);
|
|
571
|
+
} catch (err) {
|
|
572
|
+
console.log("Server doesn't seems to implement getMonitoredItems method ", err.message);
|
|
573
|
+
}
|
|
574
|
+
// get_monitored_item
|
|
575
|
+
|
|
576
|
+
// monitor_a_variable_node_value
|
|
577
|
+
console.log("Monitoring monitor_a_variable_node_value");
|
|
578
|
+
|
|
579
|
+
// ---------------------------------------------------------------
|
|
580
|
+
// monitor a variable node value
|
|
581
|
+
// ---------------------------------------------------------------
|
|
582
|
+
console.log(" Monitoring node ", monitored_node.toString());
|
|
583
|
+
const monitoredItem = ClientMonitoredItem.create(
|
|
584
|
+
subscription,
|
|
585
|
+
{
|
|
586
|
+
attributeId: AttributeIds.Value,
|
|
587
|
+
nodeId: monitored_node
|
|
588
|
+
},
|
|
589
|
+
{
|
|
590
|
+
discardOldest: true,
|
|
591
|
+
queueSize: 10000,
|
|
592
|
+
samplingInterval: 1000
|
|
593
|
+
// xx filter: { parameterTypeId: "ns=0;i=0", encodingMask: 0 },
|
|
594
|
+
}
|
|
595
|
+
);
|
|
596
|
+
monitoredItem.on("initialized", () => {
|
|
597
|
+
console.log("monitoredItem initialized");
|
|
598
|
+
});
|
|
599
|
+
monitoredItem.on("changed", (dataValue1) => {
|
|
600
|
+
console.log(monitoredItem.itemToMonitor.nodeId.toString(), " value has changed to " + dataValue1.value.toString());
|
|
601
|
+
});
|
|
602
|
+
monitoredItem.on("err", (err_message) => {
|
|
603
|
+
console.log(monitoredItem.itemToMonitor.nodeId.toString(), chalk.red(" ERROR"), err_message);
|
|
604
|
+
});
|
|
605
|
+
|
|
606
|
+
const results = await subscription.getMonitoredItems();
|
|
607
|
+
console.log("MonitoredItems clientHandles", results.clientHandles);
|
|
608
|
+
console.log("MonitoredItems serverHandles", results.serverHandles);
|
|
609
|
+
|
|
610
|
+
console.log("Monitoring monitor_the_object_events");
|
|
611
|
+
|
|
612
|
+
// ---------------------------------------------------------------
|
|
613
|
+
// monitor the object events
|
|
614
|
+
// ---------------------------------------------------------------
|
|
615
|
+
|
|
616
|
+
const baseEventTypeId = "i=2041"; // BaseEventType;
|
|
617
|
+
const serverObjectId = "i=2253";
|
|
618
|
+
|
|
619
|
+
const fields = [
|
|
620
|
+
"EventId",
|
|
621
|
+
"EventType",
|
|
622
|
+
"SourceNode",
|
|
623
|
+
"SourceName",
|
|
624
|
+
"Time",
|
|
625
|
+
"ReceiveTime",
|
|
626
|
+
"Message",
|
|
627
|
+
"Severity",
|
|
628
|
+
|
|
629
|
+
// ConditionType
|
|
630
|
+
"ConditionClassId",
|
|
631
|
+
"ConditionClassName",
|
|
632
|
+
"ConditionName",
|
|
633
|
+
"BranchId",
|
|
634
|
+
"Retain",
|
|
635
|
+
"EnabledState",
|
|
636
|
+
"Quality",
|
|
637
|
+
"LastSeverity",
|
|
638
|
+
"Comment",
|
|
639
|
+
"ClientUserId",
|
|
640
|
+
|
|
641
|
+
// AcknowledgeConditionType
|
|
642
|
+
"AckedState",
|
|
643
|
+
"ConfirmedState",
|
|
644
|
+
|
|
645
|
+
// AlarmConditionType
|
|
646
|
+
"ActiveState",
|
|
647
|
+
"InputNode",
|
|
648
|
+
"SuppressedState",
|
|
649
|
+
|
|
650
|
+
"HighLimit",
|
|
651
|
+
"LowLimit",
|
|
652
|
+
"HighHighLimit",
|
|
653
|
+
"LowLowLimit",
|
|
654
|
+
|
|
655
|
+
"Value"
|
|
656
|
+
];
|
|
657
|
+
|
|
658
|
+
const eventFilter = constructEventFilter(fields, [resolveNodeId("ConditionType")]);
|
|
659
|
+
|
|
660
|
+
const event_monitoringItem = ClientMonitoredItem.create(
|
|
661
|
+
subscription,
|
|
662
|
+
{
|
|
663
|
+
attributeId: AttributeIds.EventNotifier,
|
|
664
|
+
nodeId: serverObjectId
|
|
665
|
+
},
|
|
666
|
+
{
|
|
667
|
+
discardOldest: true,
|
|
668
|
+
filter: eventFilter,
|
|
669
|
+
queueSize: 100000
|
|
670
|
+
}
|
|
671
|
+
);
|
|
672
|
+
|
|
673
|
+
event_monitoringItem.on("initialized", () => {
|
|
674
|
+
console.log("event_monitoringItem initialized");
|
|
675
|
+
});
|
|
676
|
+
|
|
677
|
+
event_monitoringItem.on("changed", (eventFields) => {
|
|
678
|
+
dumpEvent(session, fields, eventFields);
|
|
679
|
+
});
|
|
680
|
+
event_monitoringItem.on("err", (err_message) => {
|
|
681
|
+
console.log(chalk.red("event_monitoringItem ", baseEventTypeId, " ERROR"), err_message);
|
|
682
|
+
});
|
|
683
|
+
|
|
684
|
+
console.log("--------------------------------------------- Monitoring alarms");
|
|
685
|
+
const alarmNodeId = coerceNodeId("ns=2;s=1:Colours/EastTank?Green");
|
|
686
|
+
await monitorAlarm(session, subscription);
|
|
687
|
+
|
|
688
|
+
console.log("Starting timer ", timeout);
|
|
689
|
+
if (timeout > 0) {
|
|
690
|
+
// simulate a connection break at t =timeout/2
|
|
691
|
+
// new Promise((resolve) => {
|
|
692
|
+
setTimeout(() => {
|
|
693
|
+
console.log(chalk.red(" -------------------------------------------------------------------- "));
|
|
694
|
+
console.log(chalk.red(" -- SIMULATE CONNECTION BREAK -- "));
|
|
695
|
+
console.log(chalk.red(" -------------------------------------------------------------------- "));
|
|
696
|
+
const socket = client._secureChannel.getTransport()._socket;
|
|
697
|
+
socket.end();
|
|
698
|
+
socket.emit("error", new Error("ECONNRESET"));
|
|
699
|
+
}, timeout / 2.0);
|
|
700
|
+
// });
|
|
701
|
+
await new Promise((resolve) => process.once("SIGINT", resolve));
|
|
702
|
+
console.log(" user interruption ...");
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
await subscription.terminate();
|
|
706
|
+
await session.close();
|
|
707
|
+
await client.disconnect();
|
|
708
|
+
process.exit(0);
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
main();
|