-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.js
269 lines (227 loc) · 12.7 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env node
/**
* @license
* Copyright 2022-2024 Matter.js Authors
* SPDX-License-Identifier: Apache-2.0
*/
import { Environment, Logger, singleton, StorageService, Time } from "@matter/main";
import { BasicInformationCluster, DescriptorCluster, GeneralCommissioning, OnOff } from "@matter/main/clusters";
import { ClusterClientObj, ControllerCommissioningFlowOptions } from "@matter/main/protocol";
import { ManualPairingCodeCodec, NodeId } from "@matter/main/types";
import { CommissioningController, NodeCommissioningOptions } from "@project-chip/matter.js";
import { NodeStates } from "@project-chip/matter.js/device";
const logger = Logger.get("Controller");
const environment = Environment.default;
const storageService = environment.get(StorageService);
console.log(`Storage location: ${storageService.location} (Directory)`);
logger.info(
'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.',
);
class ControllerNode {
async start() {
logger.info(`node-matter Controller started`);
/**
* Collect all needed data
*
* This block makes sure to collect all needed data from cli or storage. Replace this with where ever your data
* come from.
*
* Note: This example also uses the initialized storage system to store the device parameter data for convenience
* and easy reuse. When you also do that be careful to not overlap with Matter-Server own contexts
* (so maybe better not ;-)).
*/
const controllerStorage = (await storageService.open("controller")).createContext("data");
const ip = (await controllerStorage.has("ip"))
? await controllerStorage.get<string>("ip")
: environment.vars.string("ip");
const port = (await controllerStorage.has("port"))
? await controllerStorage.get<number>("port")
: environment.vars.number("port");
const uniqueId = (await controllerStorage.has("uniqueid"))
? await controllerStorage.get<string>("uniqueid")
: (environment.vars.string("uniqueid") ?? Time.nowMs().toString());
await controllerStorage.set("uniqueid", uniqueId);
const pairingCode = environment.vars.string("pairingcode");
let longDiscriminator, setupPin, shortDiscriminator;
if (pairingCode !== undefined) {
const pairingCodeCodec = ManualPairingCodeCodec.decode(pairingCode);
shortDiscriminator = pairingCodeCodec.shortDiscriminator;
longDiscriminator = undefined;
setupPin = pairingCodeCodec.passcode;
logger.debug(`Data extracted from pairing code: ${Logger.toJSON(pairingCodeCodec)}`);
} else {
longDiscriminator =
environment.vars.number("longDiscriminator") ??
(await controllerStorage.get("longDiscriminator", 3840));
if (longDiscriminator > 4095) throw new Error("Discriminator value must be less than 4096");
setupPin = environment.vars.number("pin") ?? (await controllerStorage.get("pin", 20202021));
}
if ((shortDiscriminator === undefined && longDiscriminator === undefined) || setupPin === undefined) {
throw new Error(
"Please specify the longDiscriminator of the device to commission with -longDiscriminator or provide a valid passcode with -passcode",
);
}
// Collect commissioning options from commandline parameters
const commissioningOptions: ControllerCommissioningFlowOptions = {
regulatoryLocation: GeneralCommissioning.RegulatoryLocationType.IndoorOutdoor,
regulatoryCountryCode: "XX",
};
/**
* Create Matter Server and Controller Node
*
* To allow the device to be announced, found, paired and operated we need a MatterServer instance and add a
* CommissioningController to it and add the just created device instance to it.
* The Controller node defines the port where the server listens for the UDP packages of the Matter protocol
* and initializes deice specific certificates and such.
*
* The below logic also adds command handlers for commands of clusters that normally are handled internally
* like testEventTrigger (General Diagnostic Cluster) that can be implemented with the logic when these commands
* are called.
*/
const commissioningController = new CommissioningController({
environment: {
environment,
id: uniqueId,
},
autoConnect: false,
});
/**
* Start the Matter Server
*
* After everything was plugged together we can start the server. When not delayed announcement is set for the
* CommissioningServer node then this command also starts the announcement of the device into the network.
*/
await commissioningController.start();
if (!commissioningController.isCommissioned()) {
const options = {
commissioning: commissioningOptions,
discovery: {
knownAddress: ip !== undefined && port !== undefined ? { ip, port, type: "udp" } : undefined,
identifierData:
longDiscriminator !== undefined
? { longDiscriminator }
: shortDiscriminator !== undefined
? { shortDiscriminator }
: {},
discoveryCapabilities: {
ble,
},
},
passcode: setupPin,
} as NodeCommissioningOptions;
logger.info(`Commissioning ... ${Logger.toJSON(options)}`);
const nodeId = await commissioningController.commissionNode(options);
console.log(`Commissioning successfully done with nodeId ${nodeId}`);
}
/**
* TBD
*/
try {
const nodes = commissioningController.getCommissionedNodes();
console.log("Found commissioned nodes:", Logger.toJSON(nodes));
const nodeId = NodeId(environment.vars.number("nodeid") ?? nodes[0]);
if (!nodes.includes(nodeId)) {
throw new Error(`Node ${nodeId} not found in commissioned nodes`);
}
// Trigger node connection. Returns once process started, events are there to wait for completion
// By default will subscript to all attributes and events
const node = await commissioningController.connectNode(nodeId);
// React on generic events
node.events.attributeChanged.on(({ path: { nodeId, clusterId, endpointId, attributeName }, value }) =>
console.log(
`attributeChangedCallback ${nodeId}: Attribute ${endpointId}/${clusterId}/${attributeName} changed to ${Logger.toJSON(
value,
)}`,
),
);
node.events.eventTriggered.on(({ path: { nodeId, clusterId, endpointId, eventName }, events }) =>
console.log(
`eventTriggeredCallback ${nodeId}: Event ${endpointId}/${clusterId}/${eventName} triggered with ${Logger.toJSON(
events,
)}`,
),
);
node.events.stateChanged.on(info => {
switch (info) {
case NodeStates.Connected:
console.log(`state changed: Node ${nodeId} connected`);
break;
case NodeStates.Disconnected:
console.log(`state changed: Node ${nodeId} disconnected`);
break;
case NodeStates.Reconnecting:
console.log(`state changed: Node ${nodeId} reconnecting`);
break;
case NodeStates.WaitingForDeviceDiscovery:
console.log(`state changed: Node ${nodeId} waiting for device discovery`);
break;
}
});
node.events.structureChanged.on(() => {
console.log(`Node ${nodeId} structure changed`);
});
// Now wait till the structure of the node gor initialized (potentially with persisted data)
await node.events.initialized;
// Or use this to wait for full remote initialization and reconnection.
// Will only return when node is connected!
// await node.events.initializedFromRemote;
node.logStructure();
// Example to initialize a ClusterClient and access concrete fields as API methods
const descriptor = node.getRootClusterClient(DescriptorCluster);
if (descriptor !== undefined) {
console.log(await descriptor.attributes.deviceTypeList.get()); // you can call that way
console.log(await descriptor.getServerListAttribute()); // or more convenient that way
} else {
console.log("No Descriptor Cluster found. This should never happen!");
}
// Example to subscribe to a field and get the value
const info = node.getRootClusterClient(BasicInformationCluster);
if (info !== undefined) {
console.log(await info.getProductNameAttribute()); // This call is executed remotely
//console.log(await info.subscribeProductNameAttribute(value => console.log("productName", value), 5, 30));
//console.log(await info.getProductNameAttribute()); // This call is resolved locally because we have subscribed to the value!
} else {
console.log("No BasicInformation Cluster found. This should never happen!");
}
// Example to get all Attributes of the commissioned node: */*/*
//const attributesAll = await interactionClient.getAllAttributes();
//console.log("Attributes-All:", Logger.toJSON(attributesAll));
// Example to get all Attributes of all Descriptor Clusters of the commissioned node: */DescriptorCluster/*
//const attributesAllDescriptor = await interactionClient.getMultipleAttributes([{ clusterId: DescriptorCluster.id} ]);
//console.log("Attributes-Descriptor:", JSON.stringify(attributesAllDescriptor, null, 2));
// Example to get all Attributes of the Basic Information Cluster of endpoint 0 of the commissioned node: 0/BasicInformationCluster/*
//const attributesBasicInformation = await interactionClient.getMultipleAttributes([{ endpointId: 0, clusterId: BasicInformationCluster.id} ]);
//console.log("Attributes-BasicInformation:", JSON.stringify(attributesBasicInformation, null, 2));
const devices = node.getDevices();
if (devices[0] && devices[0].number === 1) {
// Example to subscribe to all Attributes of endpoint 1 of the commissioned node: */*/*
//await interactionClient.subscribeMultipleAttributes([{ endpointId: 1, /* subscribe anything from endpoint 1 */ }], 0, 180, data => {
// console.log("Subscribe-All Data:", Logger.toJSON(data));
//});
const onOff: ClusterClientObj<OnOff.Complete> | undefined = devices[0].getClusterClient(OnOff.Complete);
if (onOff !== undefined) {
let onOffStatus = await onOff.getOnOffAttribute();
console.log("initial onOffStatus", onOffStatus);
onOff.addOnOffAttributeListener(value => {
console.log("subscription onOffStatus", value);
onOffStatus = value;
});
// read data every minute to keep up the connection to show the subscription is working
setInterval(() => {
onOff
.toggle()
.then(() => {
onOffStatus = !onOffStatus;
console.log("onOffStatus", onOffStatus);
})
.catch(error => logger.error(error));
}, 60000);
}
}
} finally {
//await matterServer.close(); // Comment out when subscribes are used, else the connection will be closed
setTimeout(() => process.exit(0), 1000000);
}
}
}
new ControllerNode().start().catch(error => logger.error(error));