node-opcua-samples 2.97.0 → 2.98.1

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