node-opcua-samples 2.64.1 → 2.65.0

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