@project-chip/matter-node.js-examples 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,358 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ /**
4
+ * @license
5
+ * Copyright 2022 The node-matter Authors
6
+ * SPDX-License-Identifier: Apache-2.0
7
+ */
8
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
9
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
10
+ return new (P || (P = Promise))(function (resolve, reject) {
11
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
12
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
13
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
14
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
15
+ });
16
+ };
17
+ var _a, _b;
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ /**
20
+ * This example shows how to create a simple on-off Matter device.
21
+ * It can be used as CLI script and starting point for your own device node implementation.
22
+ */
23
+ /**
24
+ * Import needed modules from @project-chip/matter-node.js
25
+ */
26
+ // Include this first to auto-register Crypto, Network and Time Node.js implementations
27
+ const matter_node_js_1 = require("@project-chip/matter-node.js");
28
+ const ble_1 = require("@project-chip/matter-node-ble.js/ble");
29
+ const ble_2 = require("@project-chip/matter-node.js/ble");
30
+ const cluster_1 = require("@project-chip/matter-node.js/cluster");
31
+ const device_1 = require("@project-chip/matter-node.js/device");
32
+ const log_1 = require("@project-chip/matter-node.js/log");
33
+ const storage_1 = require("@project-chip/matter-node.js/storage");
34
+ const time_1 = require("@project-chip/matter-node.js/time");
35
+ const util_1 = require("@project-chip/matter-node.js/util");
36
+ const datatype_1 = require("@project-chip/matter.js/datatype");
37
+ const logger = log_1.Logger.get("Device");
38
+ (0, util_1.requireMinNodeVersion)(16);
39
+ /** Configure logging */
40
+ switch ((0, util_1.getParameter)("loglevel")) {
41
+ case "fatal":
42
+ log_1.Logger.defaultLogLevel = log_1.Level.FATAL;
43
+ break;
44
+ case "error":
45
+ log_1.Logger.defaultLogLevel = log_1.Level.ERROR;
46
+ break;
47
+ case "warn":
48
+ log_1.Logger.defaultLogLevel = log_1.Level.WARN;
49
+ break;
50
+ case "info":
51
+ log_1.Logger.defaultLogLevel = log_1.Level.INFO;
52
+ break;
53
+ }
54
+ switch ((0, util_1.getParameter)("logformat")) {
55
+ case "plain":
56
+ log_1.Logger.format = log_1.Format.PLAIN;
57
+ break;
58
+ case "html":
59
+ log_1.Logger.format = log_1.Format.HTML;
60
+ break;
61
+ default:
62
+ if ((_a = process.stdin) === null || _a === void 0 ? void 0 : _a.isTTY)
63
+ log_1.Logger.format = log_1.Format.ANSI;
64
+ }
65
+ if ((0, util_1.hasParameter)("ble")) {
66
+ // Initialize Ble
67
+ ble_2.Ble.get = (0, util_1.singleton)(() => new ble_1.BleNode({
68
+ hciId: (0, util_1.getIntParameter)("ble-hci-id"),
69
+ }));
70
+ }
71
+ const storageLocation = (_b = (0, util_1.getParameter)("store")) !== null && _b !== void 0 ? _b : ".device-node";
72
+ const storage = new storage_1.StorageBackendDisk(storageLocation, (0, util_1.hasParameter)("clearstorage"));
73
+ logger.info(`Storage location: ${storageLocation} (Directory)`);
74
+ logger.info('Use the parameter "-store NAME" to specify a different storage location, use -clearstorage to start with an empty storage.');
75
+ class Device {
76
+ start() {
77
+ var _a, _b, _c, _d, _e, _f;
78
+ return __awaiter(this, void 0, void 0, function* () {
79
+ logger.info(`node-matter`);
80
+ /**
81
+ * Initialize the storage system.
82
+ *
83
+ * The storage manager is then also used by the Matter server, so this code block in general is required,
84
+ * but you can choose a different storage backend as long as it implements the required API.
85
+ */
86
+ const storageManager = new storage_1.StorageManager(storage);
87
+ yield storageManager.initialize();
88
+ /**
89
+ * Collect all needed data
90
+ *
91
+ * This block makes sure to collect all needed data from cli or storage. Replace this with where ever your data
92
+ * come from.
93
+ *
94
+ * Note: This example also uses the initialized storage system to store the device parameter data for convenience
95
+ * and easy reuse. When you also do that be careful to not overlap with Matter-Server own contexts
96
+ * (so maybe better not ;-)).
97
+ */
98
+ const deviceStorage = storageManager.createContext("Device");
99
+ if (deviceStorage.has("isSocket")) {
100
+ logger.info("Device type found in storage. -type parameter is ignored.");
101
+ }
102
+ const isSocket = deviceStorage.get("isSocket", (0, util_1.getParameter)("type") === "socket");
103
+ const deviceName = "Matter test device";
104
+ const vendorName = "matter-node.js";
105
+ const passcode = (_a = (0, util_1.getIntParameter)("passcode")) !== null && _a !== void 0 ? _a : deviceStorage.get("passcode", 20202021);
106
+ const discriminator = (_b = (0, util_1.getIntParameter)("discriminator")) !== null && _b !== void 0 ? _b : deviceStorage.get("discriminator", 3840);
107
+ // product name / id and vendor id should match what is in the device certificate
108
+ const vendorId = (_c = (0, util_1.getIntParameter)("vendorid")) !== null && _c !== void 0 ? _c : deviceStorage.get("vendorid", 0xfff1);
109
+ const productName = `node-matter OnOff ${isSocket ? "Socket" : "Light"}`;
110
+ const productId = (_d = (0, util_1.getIntParameter)("productid")) !== null && _d !== void 0 ? _d : deviceStorage.get("productid", 0x8000);
111
+ const netAnnounceInterface = (0, util_1.getParameter)("announceinterface");
112
+ const port = (_e = (0, util_1.getIntParameter)("port")) !== null && _e !== void 0 ? _e : 5540;
113
+ const uniqueId = (_f = (0, util_1.getIntParameter)("uniqueid")) !== null && _f !== void 0 ? _f : deviceStorage.get("uniqueid", time_1.Time.nowMs());
114
+ deviceStorage.set("passcode", passcode);
115
+ deviceStorage.set("discriminator", discriminator);
116
+ deviceStorage.set("vendorid", vendorId);
117
+ deviceStorage.set("productid", productId);
118
+ deviceStorage.set("isSocket", isSocket);
119
+ deviceStorage.set("uniqueid", uniqueId);
120
+ /**
121
+ * Create Device instance and add needed Listener
122
+ *
123
+ * Create an instance of the matter device class you want to use.
124
+ * This example uses the OnOffLightDevice or OnOffPluginUnitDevice depending on the value of the type parameter.
125
+ * To execute the on/off scripts defined as parameters a listener for the onOff attribute is registered via the
126
+ * device specific API.
127
+ *
128
+ * The below logic also adds command handlers for commands of clusters that normally are handled device internally
129
+ * like identify that can be implemented with the logic when these commands are called.
130
+ */
131
+ const onOffDevice = isSocket ? new device_1.OnOffPluginUnitDevice() : new device_1.OnOffLightDevice();
132
+ onOffDevice.addOnOffListener(on => { var _a; return (_a = (0, util_1.commandExecutor)(on ? "on" : "off")) === null || _a === void 0 ? void 0 : _a(); });
133
+ onOffDevice.addCommandHandler("identify", ({ request: { identifyTime } }) => __awaiter(this, void 0, void 0, function* () { return logger.info(`Identify called for OnOffDevice: ${identifyTime}`); }));
134
+ /**
135
+ * Create Matter Server and CommissioningServer Node
136
+ *
137
+ * To allow the device to be announced, found, paired and operated we need a MatterServer instance and add a
138
+ * commissioningServer to it and add the just created device instance to it.
139
+ * The CommissioningServer node defines the port where the server listens for the UDP packages of the Matter protocol
140
+ * and initializes deice specific certificates and such.
141
+ *
142
+ * The below logic also adds command handlers for commands of clusters that normally are handled internally
143
+ * like testEventTrigger (General Diagnostic Cluster) that can be implemented with the logic when these commands
144
+ * are called.
145
+ */
146
+ this.matterServer = new matter_node_js_1.MatterServer(storageManager, netAnnounceInterface);
147
+ const commissioningServer = new matter_node_js_1.CommissioningServer({
148
+ port,
149
+ deviceName,
150
+ deviceType: (0, datatype_1.DeviceTypeId)(onOffDevice.deviceType),
151
+ passcode,
152
+ discriminator,
153
+ basicInformation: {
154
+ vendorName,
155
+ vendorId: (0, datatype_1.VendorId)(vendorId),
156
+ nodeLabel: productName,
157
+ productName,
158
+ productLabel: productName,
159
+ productId,
160
+ serialNumber: `node-matter-${uniqueId}`,
161
+ },
162
+ delayedAnnouncement: (0, util_1.hasParameter)("ble"), // Delay announcement when BLE is used to show how limited advertisement works
163
+ });
164
+ // optionally add a listener for the testEventTrigger command from the GeneralDiagnostics cluster
165
+ commissioningServer.addCommandHandler("testEventTrigger", ({ request: { enableKey, eventTrigger } }) => __awaiter(this, void 0, void 0, function* () { return logger.info(`testEventTrigger called on GeneralDiagnostic cluster: ${enableKey} ${eventTrigger}`); }));
166
+ /**
167
+ * Modify automatically added clusters of the Root endpoint if needed
168
+ * In this example we change the networkCommissioning cluster into one for "Wifi only" devices when BLE is used
169
+ * for commissioning, to demonstrate how to do this.
170
+ * If you want to implement Ethernet only devices that get connected to the network via LAN/Ethernet cable,
171
+ * then all this is not needed.
172
+ * The same as shown here for Wi-Fi is also possible theoretical for Thread only or combined devices.
173
+ */
174
+ if ((0, util_1.hasParameter)("ble")) {
175
+ // matter.js will create a Ethernet-only device by default when ut comes to Network Commissioning Features.
176
+ // To offer e.g. a "Wi-Fi only device" (or any other combination) we need to override the Network Commissioning
177
+ // cluster and implement all the need handling here. This is a "static implementation" for pure demonstration
178
+ // purposes and just "simulates" the actions to be done. In a real world implementation this would be done by
179
+ // the device implementor based on the relevant networking stack.
180
+ // The NetworkCommissioningCluster and all logics are described in Matter Core Specifications section 11.8
181
+ const firstNetworkId = new util_1.ByteArray(32);
182
+ commissioningServer.addRootClusterServer((0, cluster_1.ClusterServer)(cluster_1.NetworkCommissioning.Cluster.with(cluster_1.NetworkCommissioning.Feature.WiFiNetworkInterface), {
183
+ maxNetworks: 1,
184
+ interfaceEnabled: true,
185
+ lastConnectErrorValue: 0,
186
+ lastNetworkId: null,
187
+ lastNetworkingStatus: null,
188
+ networks: [{ networkId: firstNetworkId, connected: false }],
189
+ scanMaxTimeSeconds: 3,
190
+ connectMaxTimeSeconds: 3,
191
+ }, {
192
+ scanNetworks: ({ request: { ssid, breadcrumb }, attributes: { lastNetworkingStatus }, endpoint, }) => __awaiter(this, void 0, void 0, function* () {
193
+ var _g, _h;
194
+ console.log(`---> scanNetworks called on NetworkCommissioning cluster: ${ssid === null || ssid === void 0 ? void 0 : ssid.toHex()} ${breadcrumb}`);
195
+ // Simulate successful scan
196
+ if (breadcrumb !== undefined) {
197
+ const generalCommissioningCluster = endpoint.getClusterServer(cluster_1.GeneralCommissioningCluster);
198
+ generalCommissioningCluster === null || generalCommissioningCluster === void 0 ? void 0 : generalCommissioningCluster.setBreadcrumbAttribute(breadcrumb);
199
+ }
200
+ const networkingStatus = 0 /* NetworkCommissioning.NetworkCommissioningStatus.Success */;
201
+ lastNetworkingStatus === null || lastNetworkingStatus === void 0 ? void 0 : lastNetworkingStatus.setLocal(networkingStatus);
202
+ return {
203
+ networkingStatus,
204
+ wiFiScanResults: [
205
+ {
206
+ security: {
207
+ Unencrypted: false,
208
+ Wep: false,
209
+ "WPA-PERSONAL": false,
210
+ "WPA2-PERSONAL": true,
211
+ "WPA3-PERSONAL": true,
212
+ },
213
+ ssid: ssid ||
214
+ util_1.ByteArray.fromString((_g = (0, util_1.getParameter)("ble-wifi-scan-ssid")) !== null && _g !== void 0 ? _g : "TestSSID"),
215
+ bssid: util_1.ByteArray.fromString((_h = (0, util_1.getParameter)("ble-wifi-scan-bssid")) !== null && _h !== void 0 ? _h : "00:00:00:00:00:00"),
216
+ channel: 1,
217
+ },
218
+ ],
219
+ };
220
+ }),
221
+ addOrUpdateWiFiNetwork: ({ request: { ssid, credentials, breadcrumb }, attributes: { lastNetworkingStatus, lastNetworkId }, endpoint, }) => __awaiter(this, void 0, void 0, function* () {
222
+ console.log(`---> addOrUpdateWiFiNetwork called on NetworkCommissioning cluster: ${ssid.toHex()} ${credentials.toHex()} ${breadcrumb}`);
223
+ // TODO Check if failsafe is armed
224
+ // Simulate successful add or update
225
+ if (breadcrumb !== undefined) {
226
+ const generalCommissioningCluster = endpoint.getClusterServer(cluster_1.GeneralCommissioningCluster);
227
+ generalCommissioningCluster === null || generalCommissioningCluster === void 0 ? void 0 : generalCommissioningCluster.setBreadcrumbAttribute(breadcrumb);
228
+ }
229
+ const networkingStatus = 0 /* NetworkCommissioning.NetworkCommissioningStatus.Success */;
230
+ lastNetworkingStatus === null || lastNetworkingStatus === void 0 ? void 0 : lastNetworkingStatus.setLocal(networkingStatus);
231
+ lastNetworkId === null || lastNetworkId === void 0 ? void 0 : lastNetworkId.setLocal(firstNetworkId);
232
+ return {
233
+ networkingStatus: 0 /* NetworkCommissioning.NetworkCommissioningStatus.Success */,
234
+ networkIndex: 0,
235
+ };
236
+ }),
237
+ removeNetwork: ({ request: { networkId, breadcrumb }, attributes: { lastNetworkingStatus, lastNetworkId }, endpoint, }) => __awaiter(this, void 0, void 0, function* () {
238
+ console.log(`---> removeNetwork called on NetworkCommissioning cluster: ${networkId.toHex()} ${breadcrumb}`);
239
+ // TODO Check if failsafe is armed
240
+ // Simulate successful add or update
241
+ if (breadcrumb !== undefined) {
242
+ const generalCommissioningCluster = endpoint.getClusterServer(cluster_1.GeneralCommissioningCluster);
243
+ generalCommissioningCluster === null || generalCommissioningCluster === void 0 ? void 0 : generalCommissioningCluster.setBreadcrumbAttribute(breadcrumb);
244
+ }
245
+ const networkingStatus = 0 /* NetworkCommissioning.NetworkCommissioningStatus.Success */;
246
+ lastNetworkingStatus === null || lastNetworkingStatus === void 0 ? void 0 : lastNetworkingStatus.setLocal(networkingStatus);
247
+ lastNetworkId === null || lastNetworkId === void 0 ? void 0 : lastNetworkId.setLocal(firstNetworkId);
248
+ return {
249
+ networkingStatus: 0 /* NetworkCommissioning.NetworkCommissioningStatus.Success */,
250
+ networkIndex: 0,
251
+ };
252
+ }),
253
+ connectNetwork: ({ request: { networkId, breadcrumb }, attributes: { lastNetworkingStatus, lastNetworkId, lastConnectErrorValue, networks }, endpoint, session, }) => __awaiter(this, void 0, void 0, function* () {
254
+ console.log(`---> connectNetwork called on NetworkCommissioning cluster: ${networkId.toHex()} ${breadcrumb}`);
255
+ // TODO Check if failsafe is armed
256
+ // Simulate successful connection
257
+ if (breadcrumb !== undefined) {
258
+ const generalCommissioningCluster = endpoint.getClusterServer(cluster_1.GeneralCommissioningCluster);
259
+ generalCommissioningCluster === null || generalCommissioningCluster === void 0 ? void 0 : generalCommissioningCluster.setBreadcrumbAttribute(breadcrumb);
260
+ }
261
+ const networkList = networks.getLocal();
262
+ networkList[0].connected = true;
263
+ networks.setLocal(networkList);
264
+ const networkingStatus = 0 /* NetworkCommissioning.NetworkCommissioningStatus.Success */;
265
+ lastNetworkingStatus === null || lastNetworkingStatus === void 0 ? void 0 : lastNetworkingStatus.setLocal(networkingStatus);
266
+ lastNetworkId === null || lastNetworkId === void 0 ? void 0 : lastNetworkId.setLocal(firstNetworkId);
267
+ lastConnectErrorValue === null || lastConnectErrorValue === void 0 ? void 0 : lastConnectErrorValue.setLocal(null);
268
+ // Announce operational in IP network
269
+ const device = session.getContext();
270
+ yield device.startAnnouncement();
271
+ return {
272
+ networkingStatus: 0 /* NetworkCommissioning.NetworkCommissioningStatus.Success */,
273
+ errorValue: null,
274
+ };
275
+ }),
276
+ reorderNetwork: ({ request: { networkId, networkIndex, breadcrumb }, attributes: { lastNetworkingStatus }, endpoint, }) => __awaiter(this, void 0, void 0, function* () {
277
+ console.log(`---> reorderNetwork called on NetworkCommissioning cluster: ${networkId.toHex()} ${networkIndex} ${breadcrumb}`);
278
+ // Simulate successful connection
279
+ if (breadcrumb !== undefined) {
280
+ const generalCommissioningCluster = endpoint.getClusterServer(cluster_1.GeneralCommissioningCluster);
281
+ generalCommissioningCluster === null || generalCommissioningCluster === void 0 ? void 0 : generalCommissioningCluster.setBreadcrumbAttribute(breadcrumb);
282
+ }
283
+ const networkingStatus = 0 /* NetworkCommissioning.NetworkCommissioningStatus.Success */;
284
+ lastNetworkingStatus === null || lastNetworkingStatus === void 0 ? void 0 : lastNetworkingStatus.setLocal(networkingStatus);
285
+ return {
286
+ networkingStatus: 0 /* NetworkCommissioning.NetworkCommissioningStatus.Success */,
287
+ networkIndex: 0,
288
+ };
289
+ }),
290
+ }));
291
+ }
292
+ commissioningServer.addDevice(onOffDevice);
293
+ this.matterServer.addCommissioningServer(commissioningServer);
294
+ /**
295
+ * Start the Matter Server
296
+ *
297
+ * After everything was plugged together we can start the server. When not delayed announcement is set for the
298
+ * CommissioningServer node then this command also starts the announcement of the device into the network.
299
+ */
300
+ yield this.matterServer.start();
301
+ (0, util_1.logEndpoint)(commissioningServer.getRootEndpoint());
302
+ // When we want to limit the initial announcement to one medium (e.g. BLE) then we need to delay the
303
+ // announcement and provide the limiting information.
304
+ // Without delaying the announcement is directly triggered with the above "start()" call.
305
+ if ((0, util_1.hasParameter)("ble")) {
306
+ // Announce operational in BLE network only if we have ble enabled, else everywhere
307
+ yield commissioningServer.advertise({ ble: true });
308
+ }
309
+ /**
310
+ * Print Pairing Information
311
+ *
312
+ * If the device is not already commissioned (this info is stored in the storage system) then get and print the
313
+ * pairing details. This includes the QR code that can be scanned by the Matter app to pair the device.
314
+ */
315
+ logger.info("Listening");
316
+ if (!commissioningServer.isCommissioned()) {
317
+ const pairingData = commissioningServer.getPairingCode({
318
+ ble: (0, util_1.hasParameter)("ble"),
319
+ softAccessPoint: false,
320
+ onIpNetwork: false,
321
+ });
322
+ const { qrCode, qrPairingCode, manualPairingCode } = pairingData;
323
+ console.log(qrCode);
324
+ logger.info(`QR Code URL: https://project-chip.github.io/connectedhomeip/qrcode.html?data=${qrPairingCode}`);
325
+ logger.info(`Manual pairing code: ${manualPairingCode}`);
326
+ }
327
+ else {
328
+ logger.info("Device is already commissioned. Waiting for controllers to connect ...");
329
+ }
330
+ });
331
+ }
332
+ stop() {
333
+ var _a;
334
+ return __awaiter(this, void 0, void 0, function* () {
335
+ yield ((_a = this.matterServer) === null || _a === void 0 ? void 0 : _a.close());
336
+ });
337
+ }
338
+ }
339
+ const device = new Device();
340
+ device
341
+ .start()
342
+ .then(() => {
343
+ /* done */
344
+ })
345
+ .catch(err => console.error(err));
346
+ process.on("SIGINT", () => {
347
+ device
348
+ .stop()
349
+ .then(() => {
350
+ // Pragmatic way to make sure the storage is correctly closed before the process ends.
351
+ storage
352
+ .close()
353
+ .then(() => process.exit(0))
354
+ .catch(err => console.error(err));
355
+ })
356
+ .catch(err => console.error(err));
357
+ });
358
+ //# sourceMappingURL=DeviceNode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DeviceNode.js","sourceRoot":"","sources":["../../src/examples/DeviceNode.ts"],"names":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,CAAC,CAAC;aACzF;;KACJ;IAEK,IAAI;;;YACN,MAAM,CAAA,MAAA,IAAI,CAAC,YAAY,0CAAE,KAAK,EAAE,CAAA,CAAC;;KACpC;CACJ;AAED,MAAM,MAAM,GAAG,IAAI,MAAM,EAAE,CAAC;AAC5B,MAAM;KACD,KAAK,EAAE;KACP,IAAI,CAAC,GAAG,EAAE;IACP,UAAU;AACd,CAAC,CAAC;KACD,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;AAEtC,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,GAAG,EAAE;IACtB,MAAM;SACD,IAAI,EAAE;SACN,IAAI,CAAC,GAAG,EAAE;QACP,sFAAsF;QACtF,OAAO;aACF,KAAK,EAAE;aACP,IAAI,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;aAC3B,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;IAC1C,CAAC,CAAC;SACD,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;AAC1C,CAAC,CAAC,CAAC"}
@@ -0,0 +1,228 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ /**
4
+ * @license
5
+ * Copyright 2022 The node-matter Authors
6
+ * SPDX-License-Identifier: Apache-2.0
7
+ */
8
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
9
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
10
+ return new (P || (P = Promise))(function (resolve, reject) {
11
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
12
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
13
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
14
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
15
+ });
16
+ };
17
+ var _a, _b;
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ /**
20
+ * This example shows how to create a new device node that is composed of multiple devices.
21
+ * It creates multiple endpoints on the server. When you want to add a composed devices to a Aggregator you need to
22
+ * add all endpoints of the composed device to an "ComposedDevice" instance! (not shown in this example).
23
+ * It can be used as CLI script and starting point for your own device node implementation.
24
+ */
25
+ /**
26
+ * Import needed modules from @project-chip/matter-node.js
27
+ */
28
+ // Include this first to auto-register Crypto, Network and Time Node.js implementations
29
+ const matter_node_js_1 = require("@project-chip/matter-node.js");
30
+ const device_1 = require("@project-chip/matter-node.js/device");
31
+ const log_1 = require("@project-chip/matter-node.js/log");
32
+ const storage_1 = require("@project-chip/matter-node.js/storage");
33
+ const time_1 = require("@project-chip/matter-node.js/time");
34
+ const util_1 = require("@project-chip/matter-node.js/util");
35
+ const datatype_1 = require("@project-chip/matter.js/datatype");
36
+ const logger = log_1.Logger.get("MultiDevice");
37
+ (0, util_1.requireMinNodeVersion)(16);
38
+ /** Configure logging */
39
+ switch ((0, util_1.getParameter)("loglevel")) {
40
+ case "fatal":
41
+ log_1.Logger.defaultLogLevel = log_1.Level.FATAL;
42
+ break;
43
+ case "error":
44
+ log_1.Logger.defaultLogLevel = log_1.Level.ERROR;
45
+ break;
46
+ case "warn":
47
+ log_1.Logger.defaultLogLevel = log_1.Level.WARN;
48
+ break;
49
+ case "info":
50
+ log_1.Logger.defaultLogLevel = log_1.Level.INFO;
51
+ break;
52
+ }
53
+ switch ((0, util_1.getParameter)("logformat")) {
54
+ case "plain":
55
+ log_1.Logger.format = log_1.Format.PLAIN;
56
+ break;
57
+ case "html":
58
+ log_1.Logger.format = log_1.Format.HTML;
59
+ break;
60
+ default:
61
+ if ((_a = process.stdin) === null || _a === void 0 ? void 0 : _a.isTTY)
62
+ log_1.Logger.format = log_1.Format.ANSI;
63
+ }
64
+ const storageLocation = (_b = (0, util_1.getParameter)("store")) !== null && _b !== void 0 ? _b : ".device-node";
65
+ const storage = new storage_1.StorageBackendDisk(storageLocation, (0, util_1.hasParameter)("clearstorage"));
66
+ logger.info(`Storage location: ${storageLocation} (Directory)`);
67
+ logger.info('Use the parameter "-store NAME" to specify a different storage location, use -clearstorage to start with an empty storage.');
68
+ class Device {
69
+ start() {
70
+ var _a, _b, _c, _d, _e, _f, _g, _h;
71
+ return __awaiter(this, void 0, void 0, function* () {
72
+ logger.info(`node-matter`);
73
+ /**
74
+ * Initialize the storage system.
75
+ *
76
+ * The storage manager is then also used by the Matter server, so this code block in general is required,
77
+ * but you can choose a different storage backend as long as it implements the required API.
78
+ */
79
+ const storageManager = new storage_1.StorageManager(storage);
80
+ yield storageManager.initialize();
81
+ /**
82
+ * Collect all needed data
83
+ *
84
+ * This block makes sure to collect all needed data from cli or storage. Replace this with where ever your data
85
+ * come from.
86
+ *
87
+ * Note: This example also uses the initialized storage system to store the device parameter data for convenience
88
+ * and easy reuse. When you also do that be careful to not overlap with Matter-Server own contexts
89
+ * (so maybe better not ;-)).
90
+ */
91
+ const netAnnounceInterface = (0, util_1.getParameter)("announceinterface");
92
+ const deviceStorage = storageManager.createContext("Device");
93
+ /**
94
+ * Create Matter Server and CommissioningServer Node
95
+ *
96
+ * To allow the device to be announced, found, paired and operated we need a MatterServer instance and add a
97
+ * commissioningServer to it and add the just created device instance to it.
98
+ * The CommissioningServer node defines the port where the server listens for the UDP packages of the Matter protocol
99
+ * and initializes deice specific certificates and such.
100
+ *
101
+ * The below logic also adds command handlers for commands of clusters that normally are handled internally
102
+ * like testEventTrigger (General Diagnostic Cluster) that can be implemented with the logic when these commands
103
+ * are called.
104
+ */
105
+ this.matterServer = new matter_node_js_1.MatterServer(storageManager, netAnnounceInterface);
106
+ /**
107
+ * Create Device instance and add needed Listener
108
+ *
109
+ * Create an instance of the matter device class you want to use.
110
+ * This example uses the OnOffLightDevice or OnOffPluginUnitDevice depending on the value of the type parameter.
111
+ * To execute the on/off scripts defined as parameters a listener for the onOff attribute is registered via the
112
+ * device specific API.
113
+ *
114
+ * The below logic also adds command handlers for commands of clusters that normally are handled device internally
115
+ * like identify that can be implemented with the logic when these commands are called.
116
+ */
117
+ const commissioningServers = new Array();
118
+ let defaultPasscode = 20202021;
119
+ let defaultDiscriminator = 3840;
120
+ let defaultPort = 5550;
121
+ const numDevices = (0, util_1.getIntParameter)("num") || 2;
122
+ for (let i = 1; i <= numDevices; i++) {
123
+ if (deviceStorage.has(`isSocket${i}`)) {
124
+ logger.info("Device type found in storage. -type parameter is ignored.");
125
+ }
126
+ const isSocket = deviceStorage.get(`isSocket${i}`, (0, util_1.getParameter)(`type${i}`) === "socket");
127
+ const deviceName = `Matter ${(_a = (0, util_1.getParameter)(`type${i}`)) !== null && _a !== void 0 ? _a : "light"} device ${i}`;
128
+ const deviceType = (0, util_1.getParameter)(`type${i}`) === "socket"
129
+ ? device_1.DeviceTypes.ON_OFF_PLUGIN_UNIT.code
130
+ : device_1.DeviceTypes.ON_OFF_LIGHT.code;
131
+ const vendorName = "matter-node.js";
132
+ const passcode = (_b = (0, util_1.getIntParameter)(`passcode${i}`)) !== null && _b !== void 0 ? _b : deviceStorage.get(`passcode${i}`, defaultPasscode++);
133
+ const discriminator = (_c = (0, util_1.getIntParameter)(`discriminator${i}`)) !== null && _c !== void 0 ? _c : deviceStorage.get(`discriminator${i}`, defaultDiscriminator++);
134
+ // product name / id and vendor id should match what is in the device certificate
135
+ const vendorId = (_d = (0, util_1.getIntParameter)(`vendorid${i}`)) !== null && _d !== void 0 ? _d : deviceStorage.get(`vendorid${i}`, 0xfff1);
136
+ const productName = `node-matter OnOff-Device ${i}`;
137
+ const productId = (_e = (0, util_1.getIntParameter)(`productid${i}`)) !== null && _e !== void 0 ? _e : deviceStorage.get(`productid${i}`, 0x8000);
138
+ const port = (_f = (0, util_1.getIntParameter)(`port${i}`)) !== null && _f !== void 0 ? _f : defaultPort++;
139
+ const uniqueId = (_g = (0, util_1.getIntParameter)(`uniqueid${i}`)) !== null && _g !== void 0 ? _g : deviceStorage.get(`uniqueid${i}`, `${i}-${time_1.Time.nowMs()}`);
140
+ deviceStorage.set(`passcode${i}`, passcode);
141
+ deviceStorage.set(`discriminator${i}`, discriminator);
142
+ deviceStorage.set(`vendorid${i}`, vendorId);
143
+ deviceStorage.set(`productid${i}`, productId);
144
+ deviceStorage.set(`isSocket${i}`, isSocket);
145
+ deviceStorage.set(`uniqueid${i}`, uniqueId);
146
+ const commissioningServer = new matter_node_js_1.CommissioningServer({
147
+ port,
148
+ deviceName,
149
+ deviceType,
150
+ passcode,
151
+ discriminator,
152
+ basicInformation: {
153
+ vendorName,
154
+ vendorId: (0, datatype_1.VendorId)(vendorId),
155
+ nodeLabel: productName,
156
+ productName,
157
+ productLabel: productName,
158
+ productId,
159
+ serialNumber: `node-matter-${uniqueId}`,
160
+ },
161
+ });
162
+ console.log(`Added device ${i} on port ${port} and unique id ${uniqueId}: Passcode: ${passcode}, Discriminator: ${discriminator}`);
163
+ const onOffDevice = (0, util_1.getParameter)(`type${i}`) === "socket" ? new device_1.OnOffPluginUnitDevice() : new device_1.OnOffLightDevice();
164
+ onOffDevice.addFixedLabel("orientation", (_h = (0, util_1.getParameter)(`orientation${i}`)) !== null && _h !== void 0 ? _h : `orientation ${i}`);
165
+ onOffDevice.addOnOffListener(on => { var _a; return (_a = (0, util_1.commandExecutor)(on ? `on${i}` : `off${i}`)) === null || _a === void 0 ? void 0 : _a(); });
166
+ commissioningServer.addDevice(onOffDevice);
167
+ this.matterServer.addCommissioningServer(commissioningServer);
168
+ commissioningServers.push(commissioningServer);
169
+ }
170
+ /**
171
+ * Start the Matter Server
172
+ *
173
+ * After everything was plugged together we can start the server. When not delayed announcement is set for the
174
+ * CommissioningServer node then this command also starts the announcement of the device into the network.
175
+ */
176
+ yield this.matterServer.start();
177
+ /**
178
+ * Print Pairing Information
179
+ *
180
+ * If the device is not already commissioned (this info is stored in the storage system) then get and print the
181
+ * pairing details. This includes the QR code that can be scanned by the Matter app to pair the device.
182
+ */
183
+ logger.info("Listening");
184
+ console.log();
185
+ commissioningServers.forEach((commissioningServer, index) => {
186
+ console.log("----------------------------");
187
+ console.log(`Device ${index + 1}:`);
188
+ if (!commissioningServer.isCommissioned()) {
189
+ const pairingData = commissioningServer.getPairingCode();
190
+ const { qrCode, qrPairingCode, manualPairingCode } = pairingData;
191
+ console.log(qrCode);
192
+ console.log(`QR Code URL: https://project-chip.github.io/connectedhomeip/qrcode.html?data=${qrPairingCode}`);
193
+ console.log(`Manual pairing code: ${manualPairingCode}`);
194
+ }
195
+ else {
196
+ console.log("Device is already commissioned. Waiting for controllers to connect ...");
197
+ }
198
+ console.log();
199
+ });
200
+ });
201
+ }
202
+ stop() {
203
+ var _a;
204
+ return __awaiter(this, void 0, void 0, function* () {
205
+ yield ((_a = this.matterServer) === null || _a === void 0 ? void 0 : _a.close());
206
+ });
207
+ }
208
+ }
209
+ const device = new Device();
210
+ device
211
+ .start()
212
+ .then(() => {
213
+ /* done */
214
+ })
215
+ .catch(err => console.error(err));
216
+ process.on("SIGINT", () => {
217
+ device
218
+ .stop()
219
+ .then(() => {
220
+ // Pragmatic way to make sure the storage is correctly closed before the process ends.
221
+ storage
222
+ .close()
223
+ .then(() => process.exit(0))
224
+ .catch(err => console.error(err));
225
+ })
226
+ .catch(err => console.error(err));
227
+ });
228
+ //# sourceMappingURL=MultiDeviceNode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MultiDeviceNode.js","sourceRoot":"","sources":["../../src/examples/MultiDeviceNode.ts"],"names":[],"mappings":";;AACA;;;;GAIG;;;;;;;;;;;;AAEH;;;;;GAKG;AAEH;;GAEG;AACH,uFAAuF;AACvF,iEAAiF;AAEjF,gEAA2G;AAC3G,0DAAyE;AACzE,kEAA0F;AAC1F,4DAAyD;AACzD,4DAM2C;AAC3C,+DAA4D;AAE5D,MAAM,MAAM,GAAG,YAAM,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;AAEzC,IA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