@project-chip/matter-node.js-examples 0.11.0-alpha.0-20240911-db8f7c80 → 0.11.0-alpha.0-20241007-547af42a8
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +3 -271
- package/dist/esm/examples/BridgedDevicesNodeLegacy.js +5 -4
- package/dist/esm/examples/BridgedDevicesNodeLegacy.js.map +1 -2
- package/dist/esm/examples/ComposedDeviceNodeLegacy.js +4 -3
- package/dist/esm/examples/ComposedDeviceNodeLegacy.js.map +1 -2
- package/dist/esm/examples/ControllerNodeLegacy.js +7 -6
- package/dist/esm/examples/ControllerNodeLegacy.js.map +1 -2
- package/dist/esm/examples/DeviceNodeFullLegacy.js +8 -6
- package/dist/esm/examples/DeviceNodeFullLegacy.js.map +1 -2
- package/dist/esm/examples/LegacyStorageConverter.js +2 -2
- package/dist/esm/examples/LegacyStorageConverter.js.map +0 -1
- package/dist/esm/examples/MultiDeviceNodeLegacy.js +5 -4
- package/dist/esm/examples/MultiDeviceNodeLegacy.js.map +1 -2
- package/dist/esm/examples/cluster/DummyWifiNetworkCommissioningServerLegacy.js +7 -6
- package/dist/esm/examples/cluster/DummyWifiNetworkCommissioningServerLegacy.js.map +1 -2
- package/dist/esm/package.json +3 -0
- package/package.json +16 -37
- package/src/examples/BridgedDevicesNodeLegacy.ts +4 -3
- package/src/examples/ComposedDeviceNodeLegacy.ts +4 -3
- package/src/examples/ControllerNodeLegacy.ts +6 -5
- package/src/examples/DeviceNodeFullLegacy.ts +7 -5
- package/src/examples/LegacyStorageConverter.ts +2 -2
- package/src/examples/MultiDeviceNodeLegacy.ts +4 -4
- package/src/examples/cluster/DummyWifiNetworkCommissioningServerLegacy.ts +7 -6
- package/src/tsconfig.json +13 -13
- package/dist/esm/examples/BridgedDevicesNode.js +0 -144
- package/dist/esm/examples/BridgedDevicesNode.js.map +0 -7
- package/dist/esm/examples/ComposedDeviceNode.js +0 -123
- package/dist/esm/examples/ComposedDeviceNode.js.map +0 -7
- package/dist/esm/examples/ControllerNode.js +0 -194
- package/dist/esm/examples/ControllerNode.js.map +0 -7
- package/dist/esm/examples/DeviceNode.js +0 -127
- package/dist/esm/examples/DeviceNode.js.map +0 -7
- package/dist/esm/examples/DeviceNodeFull.js +0 -253
- package/dist/esm/examples/DeviceNodeFull.js.map +0 -7
- package/dist/esm/examples/IlluminatedRollerShade.js +0 -60
- package/dist/esm/examples/IlluminatedRollerShade.js.map +0 -7
- package/dist/esm/examples/LightDevice.js +0 -35
- package/dist/esm/examples/LightDevice.js.map +0 -7
- package/dist/esm/examples/MultiDeviceNode.js +0 -140
- package/dist/esm/examples/MultiDeviceNode.js.map +0 -7
- package/dist/esm/examples/SensorDeviceNode.js +0 -175
- package/dist/esm/examples/SensorDeviceNode.js.map +0 -7
- package/dist/esm/examples/cluster/DummyThreadNetworkCommissioningServer.js +0 -121
- package/dist/esm/examples/cluster/DummyThreadNetworkCommissioningServer.js.map +0 -7
- package/dist/esm/examples/cluster/DummyWifiNetworkCommissioningServer.js +0 -121
- package/dist/esm/examples/cluster/DummyWifiNetworkCommissioningServer.js.map +0 -7
- package/dist/esm/examples/cluster/MyFancyOwnFunctionality.js +0 -110
- package/dist/esm/examples/cluster/MyFancyOwnFunctionality.js.map +0 -7
- package/dist/esm/tutorial/example01.js +0 -5
- package/dist/esm/tutorial/example01.js.map +0 -7
- package/dist/esm/tutorial/example02.js +0 -7
- package/dist/esm/tutorial/example02.js.map +0 -7
- package/dist/esm/tutorial/example03.js +0 -15
- package/dist/esm/tutorial/example03.js.map +0 -7
- package/dist/esm/tutorial/example04.js +0 -10
- package/dist/esm/tutorial/example04.js.map +0 -7
- package/dist/esm/tutorial/example05.js +0 -14
- package/dist/esm/tutorial/example05.js.map +0 -7
- package/src/examples/BridgedDevicesNode.ts +0 -259
- package/src/examples/ComposedDeviceNode.ts +0 -196
- package/src/examples/ControllerNode.ts +0 -314
- package/src/examples/DeviceNode.ts +0 -201
- package/src/examples/DeviceNodeFull.ts +0 -440
- package/src/examples/IlluminatedRollerShade.ts +0 -96
- package/src/examples/LightDevice.ts +0 -61
- package/src/examples/MultiDeviceNode.ts +0 -208
- package/src/examples/SensorDeviceNode.ts +0 -247
- package/src/examples/cluster/DummyThreadNetworkCommissioningServer.ts +0 -156
- package/src/examples/cluster/DummyWifiNetworkCommissioningServer.ts +0 -154
- package/src/examples/cluster/MyFancyOwnFunctionality.ts +0 -185
- package/src/tutorial/example01.ts +0 -6
- package/src/tutorial/example02.ts +0 -9
- package/src/tutorial/example03.ts +0 -19
- package/src/tutorial/example04.ts +0 -13
- package/src/tutorial/example05.ts +0 -19
@@ -1,196 +0,0 @@
|
|
1
|
-
#!/usr/bin/env node
|
2
|
-
/**
|
3
|
-
* @license
|
4
|
-
* Copyright 2022-2024 Matter.js Authors
|
5
|
-
* SPDX-License-Identifier: Apache-2.0
|
6
|
-
*/
|
7
|
-
|
8
|
-
/**
|
9
|
-
* This example shows how to create a new device node that is composed of multiple devices.
|
10
|
-
* It creates multiple endpoints on the server. For information on how to add a composed device to a bridge please
|
11
|
-
* refer to the bridge example!
|
12
|
-
* It can be used as CLI script and starting point for your own device node implementation.
|
13
|
-
*/
|
14
|
-
|
15
|
-
/**
|
16
|
-
* Import needed modules from @project-chip/matter-node.js
|
17
|
-
*/
|
18
|
-
// Include this first to auto-register Crypto, Network and Time Node.js implementations
|
19
|
-
import { requireMinNodeVersion } from "@project-chip/matter.js-nodejs";
|
20
|
-
|
21
|
-
import { Time } from "@project-chip/matter.js-general";
|
22
|
-
import { DeviceTypeId, VendorId } from "@project-chip/matter.js/datatype";
|
23
|
-
import { logEndpoint } from "@project-chip/matter.js/device";
|
24
|
-
import { OnOffLightDevice } from "@project-chip/matter.js/devices/OnOffLightDevice";
|
25
|
-
import { OnOffPlugInUnitDevice } from "@project-chip/matter.js/devices/OnOffPlugInUnitDevice";
|
26
|
-
import { Endpoint, EndpointServer } from "@project-chip/matter.js/endpoint";
|
27
|
-
import { Environment, StorageService } from "@project-chip/matter.js/environment";
|
28
|
-
import { ServerNode } from "@project-chip/matter.js/node";
|
29
|
-
import { execSync } from "child_process";
|
30
|
-
|
31
|
-
requireMinNodeVersion(16);
|
32
|
-
|
33
|
-
/** Initialize configuration values */
|
34
|
-
const { isSocket, deviceName, vendorName, passcode, discriminator, vendorId, productName, productId, port, uniqueId } =
|
35
|
-
await getConfiguration();
|
36
|
-
|
37
|
-
/**
|
38
|
-
* Create a Matter ServerNode, which contains the Root Endpoint and all relevant data and configuration
|
39
|
-
*/
|
40
|
-
const server = await ServerNode.create({
|
41
|
-
// Required: Give the Node a unique ID which is used to store the state of this node
|
42
|
-
id: uniqueId,
|
43
|
-
|
44
|
-
// Provide Network relevant configuration like the port
|
45
|
-
// Optional when operating only one device on a host, Default port is 5540
|
46
|
-
network: {
|
47
|
-
port,
|
48
|
-
},
|
49
|
-
|
50
|
-
// Provide Commissioning relevant settings
|
51
|
-
// Optional for development/testing purposes
|
52
|
-
commissioning: {
|
53
|
-
passcode,
|
54
|
-
discriminator,
|
55
|
-
},
|
56
|
-
|
57
|
-
// Provide Node announcement settings
|
58
|
-
// Optional: If Ommitted some development defaults are used
|
59
|
-
productDescription: {
|
60
|
-
name: deviceName,
|
61
|
-
deviceType: DeviceTypeId(isSocket[0] ? OnOffPlugInUnitDevice.deviceType : OnOffLightDevice.deviceType),
|
62
|
-
},
|
63
|
-
|
64
|
-
// Provide defaults for the BasicInformation cluster on the Root endpoint
|
65
|
-
// Optional: If Omitted some development defaults are used
|
66
|
-
basicInformation: {
|
67
|
-
vendorName,
|
68
|
-
vendorId: VendorId(vendorId),
|
69
|
-
nodeLabel: productName,
|
70
|
-
productName,
|
71
|
-
productLabel: productName,
|
72
|
-
productId,
|
73
|
-
serialNumber: `matterjs-${uniqueId}`,
|
74
|
-
uniqueId,
|
75
|
-
},
|
76
|
-
});
|
77
|
-
|
78
|
-
/**
|
79
|
-
* Matter Nodes are a composition of endpoints. Create and add a single multiple endpoint to the node to make it a
|
80
|
-
* composed device. This example uses the OnOffLightDevice or OnOffPlugInUnitDevice depending on the value of the type
|
81
|
-
* parameter. It also assigns each Endpoint a unique ID to store the endpoint number for it in the storage to restore
|
82
|
-
* the device on restart.
|
83
|
-
*
|
84
|
-
* In this case we directly use the default command implementation from matter.js. Check out the DeviceNodeFull example
|
85
|
-
* to see how to customize the command handlers.
|
86
|
-
*/
|
87
|
-
|
88
|
-
for (let idx = 0; idx < isSocket.length; idx++) {
|
89
|
-
const i = idx + 1;
|
90
|
-
const isASocket = isSocket[idx]; // Is the Device we add a Socket or a Light?
|
91
|
-
const endpoint = new Endpoint(isASocket ? OnOffPlugInUnitDevice : OnOffLightDevice, { id: `onoff-${i}` });
|
92
|
-
await server.add(endpoint);
|
93
|
-
|
94
|
-
/**
|
95
|
-
* Register state change handlers of the endpoint for identify and onoff states to react to the commands.
|
96
|
-
*
|
97
|
-
* If the code in these change handlers fail then the change is also rolled back and not executed and an error is
|
98
|
-
* reported back to the controller.
|
99
|
-
*/
|
100
|
-
endpoint.events.identify.startIdentifying.on(() => {
|
101
|
-
console.log(`Run identify logic for sub device ${i}, ideally blink a light every 0.5s ...`);
|
102
|
-
});
|
103
|
-
|
104
|
-
endpoint.events.identify.stopIdentifying.on(() => {
|
105
|
-
console.log(`Stop identify logic for sub device ${i}...`);
|
106
|
-
});
|
107
|
-
|
108
|
-
endpoint.events.onOff.onOff$Changed.on(value => {
|
109
|
-
executeCommand(value ? `on${i}` : `off${i}`);
|
110
|
-
console.log(`OnOff ${i} is now ${value ? "ON" : "OFF"}`);
|
111
|
-
});
|
112
|
-
}
|
113
|
-
|
114
|
-
/**
|
115
|
-
* Log the endpoint structure for debugging reasons and to allow to verify anything is correct
|
116
|
-
*/
|
117
|
-
logEndpoint(EndpointServer.forEndpoint(server));
|
118
|
-
|
119
|
-
/**
|
120
|
-
* In order to start the node and announce it into the network we use the run method which resolves when the node goes
|
121
|
-
* offline again because we do not need anything more here. See the Full example for other starting options.
|
122
|
-
* The QR Code is printed automatically.
|
123
|
-
*/
|
124
|
-
await server.run();
|
125
|
-
|
126
|
-
/*********************************************************************************************************
|
127
|
-
* Convenience Methods
|
128
|
-
*********************************************************************************************************/
|
129
|
-
|
130
|
-
/**
|
131
|
-
* Defines a shell command from an environment variable and execute it and log the response
|
132
|
-
*/
|
133
|
-
function executeCommand(scriptParamName: string) {
|
134
|
-
const script = Environment.default.vars.string(scriptParamName);
|
135
|
-
if (script === undefined) return undefined;
|
136
|
-
console.log(`${scriptParamName}: ${execSync(script).toString().slice(0, -1)}`);
|
137
|
-
}
|
138
|
-
|
139
|
-
async function getConfiguration() {
|
140
|
-
const environment = Environment.default;
|
141
|
-
|
142
|
-
const storageService = environment.get(StorageService);
|
143
|
-
console.log(`Storage location: ${storageService.location} (Directory)`);
|
144
|
-
console.log(
|
145
|
-
'Use the parameter "--storage-path=NAME-OR-PATH" to specify a different storage location in this directory, use --storage-clear to start with an empty storage.',
|
146
|
-
);
|
147
|
-
const deviceStorage = (await storageService.open("device")).createContext("data");
|
148
|
-
|
149
|
-
const isSocket = Array<boolean>();
|
150
|
-
const numDevices = environment.vars.number("num") || 2;
|
151
|
-
if (await deviceStorage.has("isSocket")) {
|
152
|
-
console.log(`Device types found in storage. --type parameter is ignored.`);
|
153
|
-
(await deviceStorage.get<Array<boolean>>("isSocket")).forEach(type => isSocket.push(type));
|
154
|
-
}
|
155
|
-
for (let i = 1; i < numDevices; i++) {
|
156
|
-
if (isSocket[i - 1] !== undefined) continue;
|
157
|
-
isSocket.push(environment.vars.string(`type${i}`) === "socket");
|
158
|
-
}
|
159
|
-
|
160
|
-
const deviceName = "Matter test device";
|
161
|
-
const vendorName = "matter-node.js";
|
162
|
-
const passcode = environment.vars.number("passcode") ?? (await deviceStorage.get("passcode", 20202021));
|
163
|
-
const discriminator = environment.vars.number("discriminator") ?? (await deviceStorage.get("discriminator", 3840));
|
164
|
-
// product name / id and vendor id should match what is in the device certificate
|
165
|
-
const vendorId = environment.vars.number("vendorid") ?? (await deviceStorage.get("vendorid", 0xfff1));
|
166
|
-
const productName = `node-matter OnOff ${isSocket ? "Socket" : "Light"}`;
|
167
|
-
const productId = environment.vars.number("productid") ?? (await deviceStorage.get("productid", 0x8000));
|
168
|
-
|
169
|
-
const port = environment.vars.number("port") ?? 5540;
|
170
|
-
|
171
|
-
const uniqueId =
|
172
|
-
environment.vars.string("uniqueid") ?? (await deviceStorage.get("uniqueid", Time.nowMs().toString()));
|
173
|
-
|
174
|
-
// Persist basic data to keep them also on restart
|
175
|
-
await deviceStorage.set({
|
176
|
-
passcode,
|
177
|
-
discriminator,
|
178
|
-
vendorid: vendorId,
|
179
|
-
productid: productId,
|
180
|
-
isSocket,
|
181
|
-
uniqueid: uniqueId,
|
182
|
-
});
|
183
|
-
|
184
|
-
return {
|
185
|
-
isSocket,
|
186
|
-
deviceName,
|
187
|
-
vendorName,
|
188
|
-
passcode,
|
189
|
-
discriminator,
|
190
|
-
vendorId,
|
191
|
-
productName,
|
192
|
-
productId,
|
193
|
-
port,
|
194
|
-
uniqueId,
|
195
|
-
};
|
196
|
-
}
|
@@ -1,314 +0,0 @@
|
|
1
|
-
#!/usr/bin/env node
|
2
|
-
/**
|
3
|
-
* @license
|
4
|
-
* Copyright 2022-2024 Matter.js Authors
|
5
|
-
* SPDX-License-Identifier: Apache-2.0
|
6
|
-
*/
|
7
|
-
|
8
|
-
/**
|
9
|
-
* This example shows how to create a Matter controller to pair with a device and interfact with it.
|
10
|
-
* It can be used as CLI script, but is more thought as a starting point for your own controller implementation
|
11
|
-
* because you need to adjust the code in any way depending on your use case.
|
12
|
-
*/
|
13
|
-
|
14
|
-
/**
|
15
|
-
* Import needed modules from @project-chip/matter-node.js
|
16
|
-
*/
|
17
|
-
// Include this first to auto-register Crypto, Network and Time Node.js implementations
|
18
|
-
import { requireMinNodeVersion } from "@project-chip/matter.js-nodejs";
|
19
|
-
|
20
|
-
import { BleNode } from "@project-chip/matter-node-ble.js/ble";
|
21
|
-
import { CommissioningController, NodeCommissioningOptions } from "@project-chip/matter.js";
|
22
|
-
import { Logger, singleton, Time } from "@project-chip/matter.js-general";
|
23
|
-
import { Ble } from "@project-chip/matter.js/ble";
|
24
|
-
import {
|
25
|
-
BasicInformationCluster,
|
26
|
-
ClusterClientObj,
|
27
|
-
DescriptorCluster,
|
28
|
-
GeneralCommissioning,
|
29
|
-
OnOff,
|
30
|
-
} from "@project-chip/matter.js/cluster";
|
31
|
-
import { NodeId } from "@project-chip/matter.js/datatype";
|
32
|
-
import { NodeStateInformation } from "@project-chip/matter.js/device";
|
33
|
-
import { Environment, StorageService } from "@project-chip/matter.js/environment";
|
34
|
-
import { CommissioningOptions } from "@project-chip/matter.js/protocol";
|
35
|
-
import { ManualPairingCodeCodec } from "@project-chip/matter.js/schema";
|
36
|
-
|
37
|
-
const logger = Logger.get("Controller");
|
38
|
-
|
39
|
-
requireMinNodeVersion(16);
|
40
|
-
|
41
|
-
const environment = Environment.default;
|
42
|
-
|
43
|
-
if (environment.vars.get("ble")) {
|
44
|
-
// Initialize Ble
|
45
|
-
Ble.get = singleton(
|
46
|
-
() =>
|
47
|
-
new BleNode({
|
48
|
-
hciId: environment.vars.number("ble-hci-id"),
|
49
|
-
}),
|
50
|
-
);
|
51
|
-
}
|
52
|
-
|
53
|
-
const storageService = environment.get(StorageService);
|
54
|
-
|
55
|
-
console.log(`Storage location: ${storageService.location} (Directory)`);
|
56
|
-
logger.info(
|
57
|
-
'Use the parameter "--storage-path=NAME-OR-PATH" to specify a different storage location in this directory, use --storage-clear to start with an empty storage.',
|
58
|
-
);
|
59
|
-
|
60
|
-
class ControllerNode {
|
61
|
-
async start() {
|
62
|
-
logger.info(`node-matter Controller started`);
|
63
|
-
|
64
|
-
/**
|
65
|
-
* Collect all needed data
|
66
|
-
*
|
67
|
-
* This block makes sure to collect all needed data from cli or storage. Replace this with where ever your data
|
68
|
-
* come from.
|
69
|
-
*
|
70
|
-
* Note: This example also uses the initialized storage system to store the device parameter data for convenience
|
71
|
-
* and easy reuse. When you also do that be careful to not overlap with Matter-Server own contexts
|
72
|
-
* (so maybe better not ;-)).
|
73
|
-
*/
|
74
|
-
|
75
|
-
const controllerStorage = (await storageService.open("controller")).createContext("data");
|
76
|
-
const ip = (await controllerStorage.has("ip"))
|
77
|
-
? await controllerStorage.get<string>("ip")
|
78
|
-
: environment.vars.string("ip");
|
79
|
-
const port = (await controllerStorage.has("port"))
|
80
|
-
? await controllerStorage.get<number>("port")
|
81
|
-
: environment.vars.number("port");
|
82
|
-
const uniqueId = (await controllerStorage.has("uniqueid"))
|
83
|
-
? await controllerStorage.get<string>("uniqueid")
|
84
|
-
: (environment.vars.string("uniqueid") ?? Time.nowMs().toString());
|
85
|
-
await controllerStorage.set("uniqueid", uniqueId);
|
86
|
-
|
87
|
-
const pairingCode = environment.vars.string("pairingcode");
|
88
|
-
let longDiscriminator, setupPin, shortDiscriminator;
|
89
|
-
if (pairingCode !== undefined) {
|
90
|
-
const pairingCodeCodec = ManualPairingCodeCodec.decode(pairingCode);
|
91
|
-
shortDiscriminator = pairingCodeCodec.shortDiscriminator;
|
92
|
-
longDiscriminator = undefined;
|
93
|
-
setupPin = pairingCodeCodec.passcode;
|
94
|
-
logger.debug(`Data extracted from pairing code: ${Logger.toJSON(pairingCodeCodec)}`);
|
95
|
-
} else {
|
96
|
-
longDiscriminator =
|
97
|
-
environment.vars.number("longDiscriminator") ??
|
98
|
-
(await controllerStorage.get("longDiscriminator", 3840));
|
99
|
-
if (longDiscriminator > 4095) throw new Error("Discriminator value must be less than 4096");
|
100
|
-
setupPin = environment.vars.number("pin") ?? (await controllerStorage.get("pin", 20202021));
|
101
|
-
}
|
102
|
-
if ((shortDiscriminator === undefined && longDiscriminator === undefined) || setupPin === undefined) {
|
103
|
-
throw new Error(
|
104
|
-
"Please specify the longDiscriminator of the device to commission with -longDiscriminator or provide a valid passcode with -passcode",
|
105
|
-
);
|
106
|
-
}
|
107
|
-
|
108
|
-
// Collect commissioning options from commandline parameters
|
109
|
-
const commissioningOptions: CommissioningOptions = {
|
110
|
-
regulatoryLocation: GeneralCommissioning.RegulatoryLocationType.IndoorOutdoor,
|
111
|
-
regulatoryCountryCode: "XX",
|
112
|
-
};
|
113
|
-
|
114
|
-
let ble = false;
|
115
|
-
if (environment.vars.get("ble")) {
|
116
|
-
ble = true;
|
117
|
-
const wifiSsid = environment.vars.string("ble-wifi-ssid");
|
118
|
-
const wifiCredentials = environment.vars.string("ble-wifi-credentials");
|
119
|
-
const threadNetworkName = environment.vars.string("ble-thread-networkname");
|
120
|
-
const threadOperationalDataset = environment.vars.string("ble-thread-operationaldataset");
|
121
|
-
if (wifiSsid !== undefined && wifiCredentials !== undefined) {
|
122
|
-
logger.info(`Registering Commissioning over BLE with WiFi: ${wifiSsid}`);
|
123
|
-
commissioningOptions.wifiNetwork = {
|
124
|
-
wifiSsid: wifiSsid,
|
125
|
-
wifiCredentials: wifiCredentials,
|
126
|
-
};
|
127
|
-
}
|
128
|
-
if (threadNetworkName !== undefined && threadOperationalDataset !== undefined) {
|
129
|
-
logger.info(`Registering Commissioning over BLE with Thread: ${threadNetworkName}`);
|
130
|
-
commissioningOptions.threadNetwork = {
|
131
|
-
networkName: threadNetworkName,
|
132
|
-
operationalDataset: threadOperationalDataset,
|
133
|
-
};
|
134
|
-
}
|
135
|
-
}
|
136
|
-
|
137
|
-
/**
|
138
|
-
* Create Matter Server and Controller Node
|
139
|
-
*
|
140
|
-
* To allow the device to be announced, found, paired and operated we need a MatterServer instance and add a
|
141
|
-
* CommissioningController to it and add the just created device instance to it.
|
142
|
-
* The Controller node defines the port where the server listens for the UDP packages of the Matter protocol
|
143
|
-
* and initializes deice specific certificates and such.
|
144
|
-
*
|
145
|
-
* The below logic also adds command handlers for commands of clusters that normally are handled internally
|
146
|
-
* like testEventTrigger (General Diagnostic Cluster) that can be implemented with the logic when these commands
|
147
|
-
* are called.
|
148
|
-
*/
|
149
|
-
|
150
|
-
const commissioningController = new CommissioningController({
|
151
|
-
environment: {
|
152
|
-
environment,
|
153
|
-
id: uniqueId,
|
154
|
-
},
|
155
|
-
autoConnect: false,
|
156
|
-
});
|
157
|
-
|
158
|
-
/**
|
159
|
-
* Start the Matter Server
|
160
|
-
*
|
161
|
-
* After everything was plugged together we can start the server. When not delayed announcement is set for the
|
162
|
-
* CommissioningServer node then this command also starts the announcement of the device into the network.
|
163
|
-
*/
|
164
|
-
await commissioningController.start();
|
165
|
-
|
166
|
-
if (!commissioningController.isCommissioned()) {
|
167
|
-
const options = {
|
168
|
-
commissioning: commissioningOptions,
|
169
|
-
discovery: {
|
170
|
-
knownAddress: ip !== undefined && port !== undefined ? { ip, port, type: "udp" } : undefined,
|
171
|
-
identifierData:
|
172
|
-
longDiscriminator !== undefined
|
173
|
-
? { longDiscriminator }
|
174
|
-
: shortDiscriminator !== undefined
|
175
|
-
? { shortDiscriminator }
|
176
|
-
: {},
|
177
|
-
discoveryCapabilities: {
|
178
|
-
ble,
|
179
|
-
},
|
180
|
-
},
|
181
|
-
passcode: setupPin,
|
182
|
-
} as NodeCommissioningOptions;
|
183
|
-
logger.info(`Commissioning ... ${Logger.toJSON(options)}`);
|
184
|
-
const nodeId = await commissioningController.commissionNode(options);
|
185
|
-
|
186
|
-
console.log(`Commissioning successfully done with nodeId ${nodeId}`);
|
187
|
-
}
|
188
|
-
|
189
|
-
/**
|
190
|
-
* TBD
|
191
|
-
*/
|
192
|
-
try {
|
193
|
-
const nodes = commissioningController.getCommissionedNodes();
|
194
|
-
console.log("Found commissioned nodes:", Logger.toJSON(nodes));
|
195
|
-
|
196
|
-
const nodeId = NodeId(environment.vars.number("nodeid") ?? nodes[0]);
|
197
|
-
if (!nodes.includes(nodeId)) {
|
198
|
-
throw new Error(`Node ${nodeId} not found in commissioned nodes`);
|
199
|
-
}
|
200
|
-
|
201
|
-
const node = await commissioningController.connectNode(nodeId, {
|
202
|
-
attributeChangedCallback: (
|
203
|
-
peerNodeId,
|
204
|
-
{ path: { nodeId, clusterId, endpointId, attributeName }, value },
|
205
|
-
) =>
|
206
|
-
console.log(
|
207
|
-
`attributeChangedCallback ${peerNodeId}: Attribute ${nodeId}/${endpointId}/${clusterId}/${attributeName} changed to ${Logger.toJSON(
|
208
|
-
value,
|
209
|
-
)}`,
|
210
|
-
),
|
211
|
-
eventTriggeredCallback: (peerNodeId, { path: { nodeId, clusterId, endpointId, eventName }, events }) =>
|
212
|
-
console.log(
|
213
|
-
`eventTriggeredCallback ${peerNodeId}: Event ${nodeId}/${endpointId}/${clusterId}/${eventName} triggered with ${Logger.toJSON(
|
214
|
-
events,
|
215
|
-
)}`,
|
216
|
-
),
|
217
|
-
stateInformationCallback: (peerNodeId, info) => {
|
218
|
-
switch (info) {
|
219
|
-
case NodeStateInformation.Connected:
|
220
|
-
console.log(`stateInformationCallback ${peerNodeId}: Node ${nodeId} connected`);
|
221
|
-
break;
|
222
|
-
case NodeStateInformation.Disconnected:
|
223
|
-
console.log(`stateInformationCallback ${peerNodeId}: Node ${nodeId} disconnected`);
|
224
|
-
break;
|
225
|
-
case NodeStateInformation.Reconnecting:
|
226
|
-
console.log(`stateInformationCallback ${peerNodeId}: Node ${nodeId} reconnecting`);
|
227
|
-
break;
|
228
|
-
case NodeStateInformation.WaitingForDeviceDiscovery:
|
229
|
-
console.log(
|
230
|
-
`stateInformationCallback ${peerNodeId}: Node ${nodeId} waiting for device discovery`,
|
231
|
-
);
|
232
|
-
break;
|
233
|
-
case NodeStateInformation.StructureChanged:
|
234
|
-
console.log(`stateInformationCallback ${peerNodeId}: Node ${nodeId} structure changed`);
|
235
|
-
break;
|
236
|
-
case NodeStateInformation.Decommissioned:
|
237
|
-
console.log(`stateInformationCallback ${peerNodeId}: Node ${nodeId} decommissioned`);
|
238
|
-
break;
|
239
|
-
}
|
240
|
-
},
|
241
|
-
});
|
242
|
-
|
243
|
-
// Important: This is a temporary API to proof the methods working and this will change soon and is NOT stable!
|
244
|
-
// It is provided to proof the concept
|
245
|
-
|
246
|
-
node.logStructure();
|
247
|
-
|
248
|
-
// Example to initialize a ClusterClient and access concrete fields as API methods
|
249
|
-
const descriptor = node.getRootClusterClient(DescriptorCluster);
|
250
|
-
if (descriptor !== undefined) {
|
251
|
-
console.log(await descriptor.attributes.deviceTypeList.get()); // you can call that way
|
252
|
-
console.log(await descriptor.getServerListAttribute()); // or more convenient that way
|
253
|
-
} else {
|
254
|
-
console.log("No Descriptor Cluster found. This should never happen!");
|
255
|
-
}
|
256
|
-
|
257
|
-
// Example to subscribe to a field and get the value
|
258
|
-
const info = node.getRootClusterClient(BasicInformationCluster);
|
259
|
-
if (info !== undefined) {
|
260
|
-
console.log(await info.getProductNameAttribute()); // This call is executed remotely
|
261
|
-
//console.log(await info.subscribeProductNameAttribute(value => console.log("productName", value), 5, 30));
|
262
|
-
//console.log(await info.getProductNameAttribute()); // This call is resolved locally because we have subscribed to the value!
|
263
|
-
} else {
|
264
|
-
console.log("No BasicInformation Cluster found. This should never happen!");
|
265
|
-
}
|
266
|
-
|
267
|
-
// Example to get all Attributes of the commissioned node: */*/*
|
268
|
-
//const attributesAll = await interactionClient.getAllAttributes();
|
269
|
-
//console.log("Attributes-All:", Logger.toJSON(attributesAll));
|
270
|
-
|
271
|
-
// Example to get all Attributes of all Descriptor Clusters of the commissioned node: */DescriptorCluster/*
|
272
|
-
//const attributesAllDescriptor = await interactionClient.getMultipleAttributes([{ clusterId: DescriptorCluster.id} ]);
|
273
|
-
//console.log("Attributes-Descriptor:", JSON.stringify(attributesAllDescriptor, null, 2));
|
274
|
-
|
275
|
-
// Example to get all Attributes of the Basic Information Cluster of endpoint 0 of the commissioned node: 0/BasicInformationCluster/*
|
276
|
-
//const attributesBasicInformation = await interactionClient.getMultipleAttributes([{ endpointId: 0, clusterId: BasicInformationCluster.id} ]);
|
277
|
-
//console.log("Attributes-BasicInformation:", JSON.stringify(attributesBasicInformation, null, 2));
|
278
|
-
|
279
|
-
const devices = node.getDevices();
|
280
|
-
if (devices[0] && devices[0].number === 1) {
|
281
|
-
// Example to subscribe to all Attributes of endpoint 1 of the commissioned node: */*/*
|
282
|
-
//await interactionClient.subscribeMultipleAttributes([{ endpointId: 1, /* subscribe anything from endpoint 1 */ }], 0, 180, data => {
|
283
|
-
// console.log("Subscribe-All Data:", Logger.toJSON(data));
|
284
|
-
//});
|
285
|
-
|
286
|
-
const onOff: ClusterClientObj<OnOff.Complete> | undefined = devices[0].getClusterClient(OnOff.Complete);
|
287
|
-
if (onOff !== undefined) {
|
288
|
-
let onOffStatus = await onOff.getOnOffAttribute();
|
289
|
-
console.log("initial onOffStatus", onOffStatus);
|
290
|
-
|
291
|
-
onOff.addOnOffAttributeListener(value => {
|
292
|
-
console.log("subscription onOffStatus", value);
|
293
|
-
onOffStatus = value;
|
294
|
-
});
|
295
|
-
// read data every minute to keep up the connection to show the subscription is working
|
296
|
-
setInterval(() => {
|
297
|
-
onOff
|
298
|
-
.toggle()
|
299
|
-
.then(() => {
|
300
|
-
onOffStatus = !onOffStatus;
|
301
|
-
console.log("onOffStatus", onOffStatus);
|
302
|
-
})
|
303
|
-
.catch(error => logger.error(error));
|
304
|
-
}, 60000);
|
305
|
-
}
|
306
|
-
}
|
307
|
-
} finally {
|
308
|
-
//await matterServer.close(); // Comment out when subscribes are used, else the connection will be closed
|
309
|
-
setTimeout(() => process.exit(0), 1000000);
|
310
|
-
}
|
311
|
-
}
|
312
|
-
}
|
313
|
-
|
314
|
-
new ControllerNode().start().catch(error => logger.error(error));
|