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