node-opcua-samples 2.100.0 → 2.101.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.
package/bin/di_server.js CHANGED
@@ -1,17 +1,22 @@
1
1
  #!/usr/bin/env node
2
+ /* eslint-disable max-statements */
2
3
  "use strict";
3
4
  const path = require("path");
4
5
  const os = require("os");
5
6
  const chalk = require("chalk");
6
- const { OPCUAServer, Variant, DataType, makeApplicationUrn, nodesets, RegisterServerMethod } = require("node-opcua");
7
+ const {
8
+ OPCUAServer,
9
+ Variant,
10
+ OPCUACertificateManager,
11
+ DataType,
12
+ makeApplicationUrn,
13
+ nodesets,
14
+ RegisterServerMethod
15
+ } = require("node-opcua");
7
16
  const { makeBoiler, createBoilerType } = require("node-opcua-address-space/testHelpers");
8
17
 
9
18
  Error.stackTraceLimit = Infinity;
10
19
 
11
- function constructFilename(filename) {
12
- return path.join(__dirname, "../", filename);
13
- }
14
-
15
20
  const { assert } = require("node-opcua-assert");
16
21
  const argv = require("yargs")
17
22
  .wrap(132)
@@ -30,12 +35,30 @@ const argv = require("yargs")
30
35
  .alias("k", "keySize")
31
36
 
32
37
  .string("discoveryServerEndpointUrl")
33
- .describe("discoveryServerEndpointUrl", " end point of the discovery server to regiser to")
38
+ .describe("discoveryServerEndpointUrl", " end point of the discovery server to register to")
34
39
  .default("discoveryServerEndpointUrl", "opc.tcp://localhost:4840")
35
40
  .alias("d", "discoveryServerEndpointUrl").argv;
36
41
 
37
42
  const port = parseInt(argv.port) || 26543;
38
43
 
44
+ function wn(s, w) {
45
+ return s.toString().padStart(w, "0");
46
+ }
47
+ function t(d) {
48
+ return wn(d.getHours(), 2) + ":" + wn(d.getMinutes(), 2) + ":" + wn(d.getSeconds(), 2) + ":" + wn(d.getMilliseconds(), 3);
49
+ }
50
+ function w(str, width) {
51
+ return (str || "").toString().padEnd(width).substring(0, width);
52
+ }
53
+
54
+ const envPaths = require("env-paths");
55
+ const config = envPaths("NodeOPCUA-DI-Server").config;
56
+ const pkiFolder = path.join(config, "PKI");
57
+
58
+ const serverCertificateManager = new OPCUACertificateManager({
59
+ rootFolder: pkiFolder
60
+ });
61
+
39
62
  const userManager = {
40
63
  isValidUser: function (userName, password) {
41
64
  if (userName === "user1" && password === "password1") {
@@ -48,53 +71,8 @@ const userManager = {
48
71
  }
49
72
  };
50
73
 
51
- const keySize = argv.keySize;
52
- const server_certificate_file = constructFilename("certificates/server_selfsigned_cert_" + keySize + ".pem");
53
- const server_certificate_privatekey_file = constructFilename("certificates/server_key_" + keySize + ".pem");
54
-
55
- const server = new OPCUAServer({
56
- alternateHostname: argv.alternateHostname,
57
-
58
- certificateFile: server_certificate_file,
59
- privateKeyFile: server_certificate_privatekey_file,
60
-
61
- port,
62
- resourcePath: "/UA/Server",
63
-
64
- nodeset_filename: [nodesets.standard, nodesets.di, nodesets.adi],
65
-
66
- serverInfo: {
67
- applicationUri: makeApplicationUrn(os.hostname(), "NodeOPCUA-SimpleADIDemoServer"),
68
- productUri: "urn:NodeOPCUA-SimpleADIDemoServer",
69
- applicationName: { text: "NodeOPCUA-SimpleADIDemoServer" },
70
- gatewayServerUri: null,
71
- discoveryProfileUri: null,
72
- discoveryUrls: []
73
- },
74
- buildInfo: {
75
- buildNumber: "1234"
76
- },
77
- serverCapabilities: {
78
- maxSessions: 1500,
79
- operationLimits: {
80
- maxNodesPerRead: 1000,
81
- maxNodesPerBrowse: 2000
82
- }
83
- },
84
- maxConnectionsPerEndpoint: 1500,
85
-
86
- userManager: userManager,
87
- registerServerMethod: RegisterServerMethod.LDS,
88
- discoveryServerEndpointUrl: argv.discoveryServerEndpointUrl || "opc.tcp://" + hostname() + ":4840"
89
- });
90
-
91
- process.title = "Node OPCUA Server on port : " + port;
92
-
93
- const hostname = os.hostname();
94
-
95
- server.on("post_initialize", function () {
74
+ function buildAddressSpace(server) {
96
75
  const addressSpace = server.engine.addressSpace;
97
-
98
76
  const namespace = addressSpace.getOwnNamespace();
99
77
 
100
78
  const rootFolder = addressSpace.findNode("RootFolder");
@@ -125,7 +103,6 @@ server.on("post_initialize", function () {
125
103
  console.log("discoveryServerEndpointUrl ", server.discoveryServerEndpointUrl);
126
104
 
127
105
  const mySpectrometer = spectrometerDeviceType.instantiate({ browseName: "MySpectrometer", organizedBy: deviceSet });
128
- // create a spectro meter
129
106
 
130
107
  function createChannel(options) {
131
108
  assert(typeof options.browseName === "string");
@@ -176,24 +153,115 @@ server.on("post_initialize", function () {
176
153
  makeBoiler(addressSpace, {
177
154
  browseName: "Boiler#1"
178
155
  });
179
- });
180
-
156
+ }
181
157
  function dumpNode(node) {
182
- function w(str, width) {
183
- return str.padEnd(width).substring(0, width);
184
- }
185
158
  return Object.entries(node)
186
159
  .map((key, value) => " " + w(key, 30) + " : " + (value === null ? null : value.toString()))
187
160
  .join("\n");
188
161
  }
189
162
 
190
- console.log(chalk.yellow(" server PID :"), process.pid);
163
+ (async () => {
164
+ const server = new OPCUAServer({
165
+ alternateHostname: argv.alternateHostname,
166
+ port,
167
+ resourcePath: "/UA/Server",
168
+
169
+ serverCertificateManager,
170
+
171
+ nodeset_filename: [nodesets.standard, nodesets.di, nodesets.adi],
172
+
173
+ serverInfo: {
174
+ applicationUri: makeApplicationUrn(os.hostname(), "NodeOPCUA-SimpleADIDemoServer"),
175
+ productUri: "urn:NodeOPCUA-SimpleADIDemoServer",
176
+ applicationName: { text: "NodeOPCUA-SimpleADIDemoServer" },
177
+ gatewayServerUri: null,
178
+ discoveryProfileUri: null,
179
+ discoveryUrls: []
180
+ },
181
+ buildInfo: {
182
+ buildNumber: "1234"
183
+ },
184
+ serverCapabilities: {
185
+ maxSessions: 1500,
186
+ operationLimits: {
187
+ maxNodesPerRead: 1000,
188
+ maxNodesPerBrowse: 2000
189
+ }
190
+ },
191
+ maxConnectionsPerEndpoint: 1500,
192
+
193
+ userManager,
194
+ registerServerMethod: RegisterServerMethod.LDS,
195
+ discoveryServerEndpointUrl: argv.discoveryServerEndpointUrl || "opc.tcp://" + hostname() + ":4840"
196
+ });
197
+ process.title = "Node OPCUA Server on port : " + port;
198
+
199
+ server.on("create_session", function (session) {
200
+ console.log(" SESSION CREATED");
201
+ console.log(chalk.cyan(" client application URI: "), session.clientDescription.applicationUri);
202
+ console.log(chalk.cyan(" client product URI: "), session.clientDescription.productUri);
203
+ console.log(chalk.cyan(" client application name: "), session.clientDescription.applicationName.toString());
204
+ console.log(chalk.cyan(" client application type: "), session.clientDescription.applicationType.toString());
205
+ console.log(chalk.cyan(" session name: "), session.sessionName ? session.sessionName.toString() : "<null>");
206
+ console.log(chalk.cyan(" session timeout: "), session.sessionTimeout);
207
+ console.log(chalk.cyan(" session id: "), session.sessionId);
208
+ });
191
209
 
192
- server.start(function (err) {
193
- if (err) {
194
- console.log(" Server failed to start ... exiting");
195
- process.exit(-3);
196
- }
210
+ server.on("session_closed", function (session, reason) {
211
+ console.log(" SESSION CLOSED :", reason);
212
+ console.log(chalk.cyan(" session name: "), session.sessionName ? session.sessionName.toString() : "<null>");
213
+ });
214
+
215
+ server.on("response", function (response) {
216
+ console.log(
217
+ t(response.responseHeader.timestamp),
218
+ response.responseHeader.requestHandle,
219
+ response.schema.name,
220
+ " status = ",
221
+ response.responseHeader.serviceResult.toString()
222
+ );
223
+ switch (response.schema.name) {
224
+ case "ModifySubscriptionResponse":
225
+ case "CreateMonitoredItemsResponse":
226
+ case "RepublishResponse":
227
+ case "WriteResponse":
228
+ //xx console.log(response.toString());
229
+ break;
230
+ }
231
+ });
232
+
233
+ server.on("request", function (request, channel) {
234
+ console.log(
235
+ t(request.requestHeader.timestamp),
236
+ request.requestHeader.requestHandle,
237
+ request.schema.name,
238
+ " ID =",
239
+ channel.channelId.toString()
240
+ );
241
+ switch (request.schema.name) {
242
+ case "ModifySubscriptionRequest":
243
+ case "CreateMonitoredItemsRequest":
244
+ case "RepublishRequest":
245
+ //xx console.log(request.toString());
246
+ break;
247
+ case "ReadRequest":
248
+ break;
249
+ case "WriteRequest":
250
+ break;
251
+ case "TranslateBrowsePathsToNodeIdsRequest":
252
+ break;
253
+ }
254
+ });
255
+
256
+ const hostname = os.hostname();
257
+
258
+ await server.initialize();
259
+
260
+ buildAddressSpace(server);
261
+
262
+ console.log(chalk.yellow(" server PID :"), process.pid);
263
+
264
+ await server.start();
197
265
 
198
266
  const endpointUrl = server.getEndpointUrl();
199
267
 
@@ -209,89 +277,14 @@ server.start(function (err) {
209
277
  console.log(chalk.yellow("\n server now waiting for connections. CTRL+C to stop"));
210
278
 
211
279
  console.log(server.buildInfo.toString());
212
- });
213
-
214
- server.on("create_session", function (session) {
215
- console.log(" SESSION CREATED");
216
- console.log(chalk.cyan(" client application URI: "), session.clientDescription.applicationUri);
217
- console.log(chalk.cyan(" client product URI: "), session.clientDescription.productUri);
218
- console.log(chalk.cyan(" client application name: "), session.clientDescription.applicationName.toString());
219
- console.log(chalk.cyan(" client application type: "), session.clientDescription.applicationType.toString());
220
- console.log(chalk.cyan(" session name: "), session.sessionName ? session.sessionName.toString() : "<null>");
221
- console.log(chalk.cyan(" session timeout: "), session.sessionTimeout);
222
- console.log(chalk.cyan(" session id: "), session.sessionId);
223
- });
224
-
225
- server.on("session_closed", function (session, reason) {
226
- console.log(" SESSION CLOSED :", reason);
227
- console.log(chalk.cyan(" session name: "), session.sessionName ? session.sessionName.toString() : "<null>");
228
- });
229
-
230
- function w(s, w) {
231
- return s.toString().padStart(w, "0");
232
- }
233
- function t(d) {
234
- return w(d.getHours(), 2) + ":" + w(d.getMinutes(), 2) + ":" + w(d.getSeconds(), 2) + ":" + w(d.getMilliseconds(), 3);
235
- }
236
-
237
- server.on("response", function (response) {
238
- console.log(
239
- t(response.responseHeader.timestamp),
240
- response.responseHeader.requestHandle,
241
- response.schema.name,
242
- " status = ",
243
- response.responseHeader.serviceResult.toString()
244
- );
245
- switch (response.schema.name) {
246
- case "ModifySubscriptionResponse":
247
- case "CreateMonitoredItemsResponse":
248
- case "RepublishResponse":
249
- case "WriteResponse":
250
- //xx console.log(response.toString());
251
- break;
252
- }
253
- });
254
280
 
255
- function indent(str, nb) {
256
- const spacer = " ".slice(0, nb);
257
- return str
258
- .split("\n")
259
- .map(function (s) {
260
- return spacer + s;
261
- })
262
- .join("\n");
263
- }
264
- server.on("request", function (request, channel) {
265
- console.log(
266
- t(request.requestHeader.timestamp),
267
- request.requestHeader.requestHandle,
268
- request.schema.name,
269
- " ID =",
270
- channel.channelId.toString()
271
- );
272
- switch (request.schema.name) {
273
- case "ModifySubscriptionRequest":
274
- case "CreateMonitoredItemsRequest":
275
- case "RepublishRequest":
276
- //xx console.log(request.toString());
277
- break;
278
- case "ReadRequest":
279
- break;
280
- case "WriteRequest":
281
- break;
282
- case "TranslateBrowsePathsToNodeIdsRequest":
283
- break;
284
- }
285
- });
286
-
287
- process.on("SIGINT", function () {
281
+ await new Promise((resolve) => process.once("SIGINT", resolve));
288
282
  // only work on linux apparently
289
283
  console.log(chalk.red.bold(" Received server interruption from user "));
290
284
  console.log(chalk.red.bold(" shutting down ..."));
291
- server.shutdown(1000, function () {
292
- console.log(chalk.red.bold(" shutting down completed "));
293
- console.log(chalk.red.bold(" done "));
294
- console.log("");
295
- process.exit(-1);
296
- });
297
- });
285
+ await server.shutdown(1000);
286
+ console.log(chalk.red.bold(" shutting down completed "));
287
+ console.log(chalk.red.bold(" done "));
288
+ console.log("");
289
+ process.exit(-1);
290
+ })();
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env ts-node
2
+ export {};
@@ -0,0 +1,34 @@
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
+ const node_opcua_1 = require("node-opcua");
14
+ const chalk = require("chalk");
15
+ function main() {
16
+ return __awaiter(this, void 0, void 0, function* () {
17
+ const serverOptions = {
18
+ port: 0,
19
+ nodeset_filename: [node_opcua_1.nodesets.standard],
20
+ };
21
+ const server = new node_opcua_1.OPCUAServer(serverOptions);
22
+ yield server.initialize();
23
+ yield server.start();
24
+ const endpointUrl = server.getEndpointUrl();
25
+ console.log(chalk.yellow(" server on port :"), chalk.cyan(server.endpoints[0].port.toString()));
26
+ console.log(chalk.yellow(" endpointUrl :"), chalk.cyan(endpointUrl));
27
+ console.log(chalk.yellow("\n server now waiting for connections. CTRL+C to stop"));
28
+ yield new Promise((resolve) => process.once("SIGINT", () => resolve()));
29
+ server.shutdown(1000);
30
+ console.log(chalk.red.bold(" shutting down completed "));
31
+ });
32
+ }
33
+ main();
34
+ //# sourceMappingURL=mini_server_on_port_0.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mini_server_on_port_0.js","sourceRoot":"","sources":["../bin/mini_server_on_port_0.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,2CAA2H;AAC3H,+BAA+B;AAC/B,SAAe,IAAI;;QAEf,MAAM,aAAa,GAAG;YAClB,IAAI,EAAE,CAAC;YACP,gBAAgB,EAAE,CAAC,qBAAQ,CAAC,QAAQ,CAAC;SACxC,CAAC;QAEF,MAAM,MAAM,GAAG,IAAI,wBAAW,CAAC,aAAa,CAAC,CAAC;QAC9C,MAAM,MAAM,CAAC,UAAU,EAAE,CAAC;QAC1B,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC;QAGrB,MAAM,WAAW,GAAG,MAAM,CAAC,cAAc,EAAE,CAAC;QAE5C,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,yBAAyB,CAAC,EAAE,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC;QACtG,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,yBAAyB,CAAC,EAAE,KAAK,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC;QAC9E,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,wDAAwD,CAAC,CAAC,CAAC;QAEpF,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAC,EAAE,CAAA,OAAO,CAAC,IAAI,CAAC,QAAQ,EAAE,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;QAC5E,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QACtB,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,2BAA2B,CAAC,CAAC,CAAC;IAC7D,CAAC;CAAA;AAED,IAAI,EAAE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-opcua-samples",
3
- "version": "2.100.0",
3
+ "version": "2.101.0",
4
4
  "description": "pure nodejs OPCUA SDK - module samples",
5
5
  "bin": {
6
6
  "simple_client": "./dist/simple_client_ts.js",
@@ -22,16 +22,16 @@
22
22
  "easy-table": "^1.2.0",
23
23
  "env-paths": "2.2.1",
24
24
  "exit": "^0.1.2",
25
- "node-opcua": "2.100.0",
26
- "node-opcua-address-space": "2.100.0",
27
- "node-opcua-address-space-for-conformance-testing": "2.100.0",
25
+ "node-opcua": "2.101.0",
26
+ "node-opcua-address-space": "2.101.0",
27
+ "node-opcua-address-space-for-conformance-testing": "2.101.0",
28
28
  "node-opcua-assert": "2.98.1",
29
- "node-opcua-client-crawler": "2.100.0",
29
+ "node-opcua-client-crawler": "2.101.0",
30
30
  "node-opcua-crypto": "^2.1.2",
31
31
  "node-opcua-pki": "^3.0.2",
32
- "node-opcua-server-configuration": "2.100.0",
32
+ "node-opcua-server-configuration": "2.101.0",
33
33
  "node-opcua-utils": "2.98.1",
34
- "node-opcua-vendor-diagnostic": "2.100.0",
34
+ "node-opcua-vendor-diagnostic": "2.101.0",
35
35
  "sprintf-js": "^1.1.2",
36
36
  "treeify": "^1.1.0",
37
37
  "underscore": "^1.13.6",
@@ -52,7 +52,7 @@
52
52
  "internet of things"
53
53
  ],
54
54
  "homepage": "http://node-opcua.github.io/",
55
- "gitHead": "e143ff72418bb3db8c0a2cb8d4b7e54a90521a73",
55
+ "gitHead": "77abad969406b6d7d76e6bc07a8aa94ae07f3db2",
56
56
  "files": [
57
57
  "dist"
58
58
  ]