@project-chip/matter-node.js-examples 0.8.0-alpha.0-20240328-f7f45db5 → 0.8.0-alpha.0-20240329-133f426f

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. package/dist/esm/examples/{BridgedDeviceNode.js → BridgedDevicesNode.js} +16 -14
  2. package/dist/esm/examples/{BridgedDeviceNode.js.map → BridgedDevicesNode.js.map} +3 -3
  3. package/dist/esm/examples/BridgedDevicesNodeLegacy.js +9 -6
  4. package/dist/esm/examples/BridgedDevicesNodeLegacy.js.map +2 -2
  5. package/dist/esm/examples/ComposedDeviceNode.js +15 -13
  6. package/dist/esm/examples/ComposedDeviceNode.js.map +2 -2
  7. package/dist/esm/examples/ComposedDeviceNodeLegacy.js +10 -7
  8. package/dist/esm/examples/ComposedDeviceNodeLegacy.js.map +2 -2
  9. package/dist/esm/examples/ControllerNode.js +6 -6
  10. package/dist/esm/examples/ControllerNode.js.map +2 -2
  11. package/dist/esm/examples/ControllerNodeLegacy.js +2 -1
  12. package/dist/esm/examples/ControllerNodeLegacy.js.map +2 -2
  13. package/dist/esm/examples/DeviceNode.js +15 -13
  14. package/dist/esm/examples/DeviceNode.js.map +2 -2
  15. package/dist/esm/examples/DeviceNodeFull.js +15 -13
  16. package/dist/esm/examples/DeviceNodeFull.js.map +2 -2
  17. package/dist/esm/examples/DeviceNodeFullLegacy.js +10 -7
  18. package/dist/esm/examples/DeviceNodeFullLegacy.js.map +2 -2
  19. package/dist/esm/examples/LegacyStorageConverter.js +35 -23
  20. package/dist/esm/examples/LegacyStorageConverter.js.map +2 -2
  21. package/dist/esm/examples/MultiDeviceNode.js +13 -13
  22. package/dist/esm/examples/MultiDeviceNode.js.map +2 -2
  23. package/dist/esm/examples/MultiDeviceNodeLegacy.js +2 -1
  24. package/dist/esm/examples/MultiDeviceNodeLegacy.js.map +2 -2
  25. package/dist/esm/examples/SensorDeviceNode.js +17 -15
  26. package/dist/esm/examples/SensorDeviceNode.js.map +2 -2
  27. package/package.json +6 -6
  28. package/src/examples/{BridgedDeviceNode.ts → BridgedDevicesNode.ts} +16 -13
  29. package/src/examples/BridgedDevicesNodeLegacy.ts +9 -9
  30. package/src/examples/ComposedDeviceNode.ts +16 -13
  31. package/src/examples/ComposedDeviceNodeLegacy.ts +10 -10
  32. package/src/examples/ControllerNode.ts +10 -7
  33. package/src/examples/ControllerNodeLegacy.ts +2 -4
  34. package/src/examples/DeviceNode.ts +16 -13
  35. package/src/examples/DeviceNodeFull.ts +15 -13
  36. package/src/examples/DeviceNodeFullLegacy.ts +10 -11
  37. package/src/examples/LegacyStorageConverter.ts +35 -23
  38. package/src/examples/MultiDeviceNode.ts +15 -13
  39. package/src/examples/MultiDeviceNodeLegacy.ts +2 -4
  40. package/src/examples/SensorDeviceNode.ts +18 -15
@@ -117,11 +117,13 @@ class BridgedDevice {
117
117
 
118
118
  const uniqueId = getIntParameter("uniqueid") ?? deviceStorage.get("uniqueid", Time.nowMs());
119
119
 
120
- deviceStorage.set("passcode", passcode);
121
- deviceStorage.set("discriminator", discriminator);
122
- deviceStorage.set("vendorid", vendorId);
123
- deviceStorage.set("productid", productId);
124
- deviceStorage.set("uniqueid", uniqueId);
120
+ deviceStorage.set({
121
+ passcode,
122
+ discriminator,
123
+ vendorid: vendorId,
124
+ productid: productId,
125
+ uniqueid: uniqueId,
126
+ });
125
127
 
126
128
  /**
127
129
  * Create Matter Server and CommissioningServer Node
@@ -245,10 +247,8 @@ process.on("SIGINT", () => {
245
247
  .stop()
246
248
  .then(() => {
247
249
  // Pragmatic way to make sure the storage is correctly closed before the process ends.
248
- storage
249
- .close()
250
- .then(() => process.exit(0))
251
- .catch(err => console.error(err));
250
+ storage.close();
251
+ process.exit(0);
252
252
  })
253
253
  .catch(err => console.error(err));
254
254
  });
@@ -152,9 +152,9 @@ async function getConfiguration() {
152
152
 
153
153
  const isSocket = Array<boolean>();
154
154
  const numDevices = environment.vars.number("num") || 2;
155
- if (deviceStorage.has("isSocket")) {
155
+ if (await deviceStorage.has("isSocket")) {
156
156
  console.log(`Device types found in storage. --type parameter is ignored.`);
157
- deviceStorage.get<Array<boolean>>("isSocket").forEach(type => isSocket.push(type));
157
+ (await deviceStorage.get<Array<boolean>>("isSocket")).forEach(type => isSocket.push(type));
158
158
  }
159
159
  for (let i = 1; i < numDevices; i++) {
160
160
  if (isSocket[i - 1] !== undefined) continue;
@@ -163,24 +163,27 @@ async function getConfiguration() {
163
163
 
164
164
  const deviceName = "Matter test device";
165
165
  const vendorName = "matter-node.js";
166
- const passcode = environment.vars.number("passcode") ?? deviceStorage.get("passcode", 20202021);
167
- const discriminator = environment.vars.number("discriminator") ?? deviceStorage.get("discriminator", 3840);
166
+ const passcode = environment.vars.number("passcode") ?? (await deviceStorage.get("passcode", 20202021));
167
+ const discriminator = environment.vars.number("discriminator") ?? (await deviceStorage.get("discriminator", 3840));
168
168
  // product name / id and vendor id should match what is in the device certificate
169
- const vendorId = environment.vars.number("vendorid") ?? deviceStorage.get("vendorid", 0xfff1);
169
+ const vendorId = environment.vars.number("vendorid") ?? (await deviceStorage.get("vendorid", 0xfff1));
170
170
  const productName = `node-matter OnOff ${isSocket ? "Socket" : "Light"}`;
171
- const productId = environment.vars.number("productid") ?? deviceStorage.get("productid", 0x8000);
171
+ const productId = environment.vars.number("productid") ?? (await deviceStorage.get("productid", 0x8000));
172
172
 
173
173
  const port = environment.vars.number("port") ?? 5540;
174
174
 
175
- const uniqueId = environment.vars.string("uniqueid") ?? deviceStorage.get("uniqueid", Time.nowMs().toString());
175
+ const uniqueId =
176
+ environment.vars.string("uniqueid") ?? (await deviceStorage.get("uniqueid", Time.nowMs().toString()));
176
177
 
177
178
  // Persist basic data to keep them also on restart
178
- deviceStorage.set("passcode", passcode);
179
- deviceStorage.set("discriminator", discriminator);
180
- deviceStorage.set("vendorid", vendorId);
181
- deviceStorage.set("productid", productId);
182
- deviceStorage.set("isSocket", isSocket);
183
- deviceStorage.set("uniqueid", uniqueId);
179
+ await deviceStorage.set({
180
+ passcode,
181
+ discriminator,
182
+ vendorid: vendorId,
183
+ productid: productId,
184
+ isSocket,
185
+ uniqueid: uniqueId,
186
+ });
184
187
 
185
188
  return {
186
189
  isSocket,
@@ -124,12 +124,14 @@ class ComposedDevice {
124
124
 
125
125
  const uniqueId = getIntParameter("uniqueid") ?? deviceStorage.get("uniqueid", Time.nowMs());
126
126
 
127
- deviceStorage.set("passcode", passcode);
128
- deviceStorage.set("discriminator", discriminator);
129
- deviceStorage.set("vendorid", vendorId);
130
- deviceStorage.set("productid", productId);
131
- deviceStorage.set("isSocket", isSocket);
132
- deviceStorage.set("uniqueid", uniqueId);
127
+ deviceStorage.set({
128
+ passcode,
129
+ discriminator,
130
+ vendorid: vendorId,
131
+ productid: productId,
132
+ isSocket,
133
+ uniqueid: uniqueId,
134
+ });
133
135
 
134
136
  /**
135
137
  * Create Matter Server and CommissioningServer Node
@@ -243,10 +245,8 @@ process.on("SIGINT", () => {
243
245
  .stop()
244
246
  .then(() => {
245
247
  // Pragmatic way to make sure the storage is correctly closed before the process ends.
246
- storage
247
- .close()
248
- .then(() => process.exit(0))
249
- .catch(err => console.error(err));
248
+ storage.close();
249
+ process.exit(0);
250
250
  })
251
251
  .catch(err => console.error(err));
252
252
  });
@@ -77,14 +77,16 @@ class ControllerNode {
77
77
  */
78
78
 
79
79
  const controllerStorage = (await storageService.open("controller")).createContext("data");
80
- const ip = controllerStorage.has("ip") ? controllerStorage.get<string>("ip") : environment.vars.string("ip");
81
- const port = controllerStorage.has("port")
80
+ const ip = (await controllerStorage.has("ip"))
81
+ ? controllerStorage.get<string>("ip")
82
+ : environment.vars.string("ip");
83
+ const port = (await controllerStorage.has("port"))
82
84
  ? controllerStorage.get<number>("port")
83
85
  : environment.vars.number("port");
84
- const uniqueId = controllerStorage.has("uniqueid")
85
- ? controllerStorage.get<string>("uniqueid")
86
+ const uniqueId = (await controllerStorage.has("uniqueid"))
87
+ ? await controllerStorage.get<string>("uniqueid")
86
88
  : environment.vars.string("uniqueid") ?? Time.nowMs().toString();
87
- controllerStorage.set("uniqueid", uniqueId);
89
+ await controllerStorage.set("uniqueid", uniqueId);
88
90
 
89
91
  const pairingCode = environment.vars.string("pairingcode");
90
92
  let longDiscriminator, setupPin, shortDiscriminator;
@@ -96,9 +98,10 @@ class ControllerNode {
96
98
  logger.debug(`Data extracted from pairing code: ${Logger.toJSON(pairingCodeCodec)}`);
97
99
  } else {
98
100
  longDiscriminator =
99
- environment.vars.number("longDiscriminator") ?? controllerStorage.get("longDiscriminator", 3840);
101
+ environment.vars.number("longDiscriminator") ??
102
+ (await controllerStorage.get("longDiscriminator", 3840));
100
103
  if (longDiscriminator > 4095) throw new Error("Discriminator value must be less than 4096");
101
- setupPin = environment.vars.number("pin") ?? controllerStorage.get("pin", 20202021);
104
+ setupPin = environment.vars.number("pin") ?? (await controllerStorage.get("pin", 20202021));
102
105
  }
103
106
  if ((shortDiscriminator === undefined && longDiscriminator === undefined) || setupPin === undefined) {
104
107
  throw new Error(
@@ -347,8 +347,6 @@ new ControllerNode().start().catch(error => logger.error(error));
347
347
  process.on("SIGINT", () => {
348
348
  // Clean up on CTRL-C
349
349
  // Pragmatic way to make sure the storage is correctly closed before the process ends.
350
- storage
351
- .close()
352
- .then(() => process.exit(0))
353
- .catch(() => process.exit(1));
350
+ storage.close();
351
+ process.exit(0);
354
352
  });
@@ -162,30 +162,33 @@ async function getConfiguration() {
162
162
  );
163
163
  const deviceStorage = (await storageService.open("device")).createContext("data");
164
164
 
165
- const isSocket = deviceStorage.get("isSocket", environment.vars.get("type") === "socket");
166
- if (deviceStorage.has("isSocket")) {
165
+ const isSocket = await deviceStorage.get("isSocket", environment.vars.get("type") === "socket");
166
+ if (await deviceStorage.has("isSocket")) {
167
167
  console.log(`Device type ${isSocket ? "socket" : "light"} found in storage. --type parameter is ignored.`);
168
168
  }
169
169
  const deviceName = "Matter test device";
170
170
  const vendorName = "matter-node.js";
171
- const passcode = environment.vars.number("passcode") ?? deviceStorage.get("passcode", 20202021);
172
- const discriminator = environment.vars.number("discriminator") ?? deviceStorage.get("discriminator", 3840);
171
+ const passcode = environment.vars.number("passcode") ?? (await deviceStorage.get("passcode", 20202021));
172
+ const discriminator = environment.vars.number("discriminator") ?? (await deviceStorage.get("discriminator", 3840));
173
173
  // product name / id and vendor id should match what is in the device certificate
174
- const vendorId = environment.vars.number("vendorid") ?? deviceStorage.get("vendorid", 0xfff1);
174
+ const vendorId = environment.vars.number("vendorid") ?? (await deviceStorage.get("vendorid", 0xfff1));
175
175
  const productName = `node-matter OnOff ${isSocket ? "Socket" : "Light"}`;
176
- const productId = environment.vars.number("productid") ?? deviceStorage.get("productid", 0x8000);
176
+ const productId = environment.vars.number("productid") ?? (await deviceStorage.get("productid", 0x8000));
177
177
 
178
178
  const port = environment.vars.number("port") ?? 5540;
179
179
 
180
- const uniqueId = environment.vars.string("uniqueid") ?? deviceStorage.get("uniqueid", Time.nowMs()).toString();
180
+ const uniqueId =
181
+ environment.vars.string("uniqueid") ?? (await deviceStorage.get("uniqueid", Time.nowMs())).toString();
181
182
 
182
183
  // Persist basic data to keep them also on restart
183
- deviceStorage.set("passcode", passcode);
184
- deviceStorage.set("discriminator", discriminator);
185
- deviceStorage.set("vendorid", vendorId);
186
- deviceStorage.set("productid", productId);
187
- deviceStorage.set("isSocket", isSocket);
188
- deviceStorage.set("uniqueid", uniqueId);
184
+ await deviceStorage.set({
185
+ passcode,
186
+ discriminator,
187
+ vendorid: vendorId,
188
+ productid: productId,
189
+ isSocket,
190
+ uniqueid: uniqueId,
191
+ });
189
192
 
190
193
  return {
191
194
  isSocket,
@@ -115,29 +115,31 @@ console.log(
115
115
 
116
116
  const deviceStorage = (await storageService.open("device")).createContext("data");
117
117
 
118
- if (deviceStorage.has("isSocket")) {
118
+ if (await deviceStorage.has("isSocket")) {
119
119
  console.log("Device type found in storage. --type parameter is ignored.");
120
120
  }
121
- const isSocket = deviceStorage.get("isSocket", environment.vars.string("type") === "socket");
121
+ const isSocket = await deviceStorage.get("isSocket", environment.vars.string("type") === "socket");
122
122
  const deviceName = "Matter test device";
123
123
  const vendorName = "matter-node.js";
124
- const passcode = environment.vars.number("passcode") ?? deviceStorage.get("passcode", 20202021);
125
- const discriminator = environment.vars.number("discriminator") ?? deviceStorage.get("discriminator", 3840);
124
+ const passcode = environment.vars.number("passcode") ?? (await deviceStorage.get("passcode", 20202021));
125
+ const discriminator = environment.vars.number("discriminator") ?? (await deviceStorage.get("discriminator", 3840));
126
126
  // product name / id and vendor id should match what is in the device certificate
127
- const vendorId = environment.vars.number("vendorid") ?? deviceStorage.get("vendorid", 0xfff1);
127
+ const vendorId = environment.vars.number("vendorid") ?? (await deviceStorage.get("vendorid", 0xfff1));
128
128
  const productName = `node-matter OnOff ${isSocket ? "Socket" : "Light"}`;
129
- const productId = environment.vars.number("productid") ?? deviceStorage.get("productid", 0x8000);
129
+ const productId = environment.vars.number("productid") ?? (await deviceStorage.get("productid", 0x8000));
130
130
 
131
131
  const port = environment.vars.number("port") ?? 5540;
132
132
 
133
- const uniqueId = environment.vars.string("uniqueid") ?? deviceStorage.get("uniqueid", Time.nowMs().toString());
133
+ const uniqueId = environment.vars.string("uniqueid") ?? (await deviceStorage.get("uniqueid", Time.nowMs().toString()));
134
134
 
135
- deviceStorage.set("passcode", passcode);
136
- deviceStorage.set("discriminator", discriminator);
137
- deviceStorage.set("vendorid", vendorId);
138
- deviceStorage.set("productid", productId);
139
- deviceStorage.set("isSocket", isSocket);
140
- deviceStorage.set("uniqueid", uniqueId);
135
+ await deviceStorage.set({
136
+ passcode,
137
+ discriminator,
138
+ vendorid: vendorId,
139
+ productid: productId,
140
+ isSocket,
141
+ uniqueid: uniqueId,
142
+ });
141
143
 
142
144
  // Matter exposes functionality in groups called "clusters". For this example device we override the matter.js "On/Off"
143
145
  // cluster implementation to print status to the console.
@@ -134,13 +134,14 @@ class Device {
134
134
 
135
135
  const uniqueId = getIntParameter("uniqueid") ?? deviceStorage.get("uniqueid", Time.nowMs());
136
136
 
137
- deviceStorage.set("passcode", passcode);
138
- deviceStorage.set("discriminator", discriminator);
139
- deviceStorage.set("vendorid", vendorId);
140
- deviceStorage.set("productid", productId);
141
- deviceStorage.set("isSocket", isSocket);
142
- deviceStorage.set("uniqueid", uniqueId);
143
-
137
+ deviceStorage.set({
138
+ passcode,
139
+ discriminator,
140
+ vendorid: vendorId,
141
+ productid: productId,
142
+ isSocket,
143
+ uniqueid: uniqueId,
144
+ });
144
145
  /**
145
146
  * Create Device instance and add needed Listener
146
147
  *
@@ -298,10 +299,8 @@ process.on("SIGINT", () => {
298
299
  .stop()
299
300
  .then(() => {
300
301
  // Pragmatic way to make sure the storage is correctly closed before the process ends.
301
- storage
302
- .close()
303
- .then(() => process.exit(0))
304
- .catch(err => console.error(err));
302
+ storage.close();
303
+ process.exit(0);
305
304
  })
306
305
  .catch(err => console.error(err));
307
306
  });
@@ -33,7 +33,7 @@ Object.keys(legacyLocalStorage).forEach(key => {
33
33
  const storageService = environment.get(StorageService);
34
34
 
35
35
  const legacyStorage = new StorageBackendDisk(legacyStoragePath);
36
- await legacyStorage.initialize();
36
+ legacyStorage.initialize();
37
37
 
38
38
  const uniqueIds: Record<string, string> = {};
39
39
  if (legacyNodes.includes("Device")) {
@@ -42,19 +42,19 @@ if (legacyNodes.includes("Device")) {
42
42
 
43
43
  const newDeviceStorage = (await storageService.open("device")).createContext("data");
44
44
 
45
- legacyStorage.keys(["Device"]).forEach(key => {
45
+ for (const key of legacyStorage.keys(["Device"])) {
46
46
  console.log("Migrate Device.", key);
47
47
  const value = legacyStorage.get(["Device"], key);
48
- newDeviceStorage.set(key, value);
48
+ await newDeviceStorage.set(key, value);
49
49
  if (key === "uniqueid") {
50
50
  uniqueIds["0"] = String(value);
51
- newDeviceStorage.set(key, String(value));
51
+ await newDeviceStorage.set(key, String(value));
52
52
  } else if (key.startsWith("uniqueid")) {
53
53
  const id = parseInt(key.substring(8));
54
54
  uniqueIds[id - 1] = String(value);
55
- newDeviceStorage.set(key, String(value));
55
+ await newDeviceStorage.set(key, String(value));
56
56
  }
57
- });
57
+ }
58
58
  }
59
59
 
60
60
  if (legacyNodes.includes("Controller")) {
@@ -63,15 +63,15 @@ if (legacyNodes.includes("Controller")) {
63
63
 
64
64
  const newControllerStorage = (await storageService.open("controller")).createContext("data");
65
65
 
66
- legacyStorage.keys(["Controller"]).forEach(key => {
66
+ for (const key of legacyStorage.keys(["Controller"])) {
67
67
  console.log("Migrate Controller.", key);
68
68
  const value = legacyStorage.get(["Controller"], key);
69
- newControllerStorage.set(key, value);
69
+ await newControllerStorage.set(key, value);
70
70
  if (key === "uniqueid") {
71
71
  uniqueIds["0"] = String(value);
72
- newControllerStorage.set(key, String(value));
72
+ await newControllerStorage.set(key, String(value));
73
73
  }
74
- });
74
+ }
75
75
  }
76
76
 
77
77
  console.log(uniqueIds);
@@ -93,29 +93,38 @@ for (const nodeId of legacyNodes) {
93
93
  // Migrate the controller storage
94
94
  const newControllerStorage = (await storageService.open("controller")).createContext("data");
95
95
  const uniqueId = Time.nowMs().toString();
96
- newControllerStorage.set("uniqueid", uniqueId);
96
+ await newControllerStorage.set("uniqueid", uniqueId);
97
97
 
98
98
  const newNodeStorage = await storageService.open(uniqueId);
99
99
 
100
100
  const credentialsStorage = newNodeStorage.createContext("credentials");
101
- credentialsStorage.set("rootCertId", rootCertId);
102
- credentialsStorage.set(
101
+ await credentialsStorage.set("rootCertId", rootCertId);
102
+ await credentialsStorage.set(
103
103
  "nextCertificateId",
104
104
  legacyStorage.get(["0", "RootCertificateManager"], "nextCertificateId"),
105
105
  );
106
- credentialsStorage.set("rootCertBytes", legacyStorage.get(["0", "RootCertificateManager"], "rootCertBytes"));
107
- credentialsStorage.set(
106
+ await credentialsStorage.set(
107
+ "rootCertBytes",
108
+ legacyStorage.get(["0", "RootCertificateManager"], "rootCertBytes"),
109
+ );
110
+ await credentialsStorage.set(
108
111
  "rootKeyIdentifier",
109
112
  legacyStorage.get(["0", "RootCertificateManager"], "rootKeyIdentifier"),
110
113
  );
111
- credentialsStorage.set("rootKeyPair", legacyStorage.get(["0", "RootCertificateManager"], "rootKeyPair"));
112
- credentialsStorage.set("fabric", legacyStorage.get(["0", "MatterController"], "fabric"));
114
+ await credentialsStorage.set("rootKeyPair", legacyStorage.get(["0", "RootCertificateManager"], "rootKeyPair"));
115
+ await credentialsStorage.set("fabric", legacyStorage.get(["0", "MatterController"], "fabric"));
113
116
 
114
117
  const sessionsStorage = newNodeStorage.createContext("sessions");
115
- sessionsStorage.set("resumptionRecords", legacyStorage.get([nodeId, "SessionManager"], "resumptionRecords"));
118
+ await sessionsStorage.set(
119
+ "resumptionRecords",
120
+ legacyStorage.get([nodeId, "SessionManager"], "resumptionRecords"),
121
+ );
116
122
 
117
123
  const nodesStorage = newNodeStorage.createContext("nodes");
118
- nodesStorage.set("resumptionRecords", legacyStorage.get([nodeId, "MatterController"], "commissionedNodes"));
124
+ await nodesStorage.set(
125
+ "resumptionRecords",
126
+ legacyStorage.get([nodeId, "MatterController"], "commissionedNodes"),
127
+ );
119
128
 
120
129
  console.log(`Controller Node ${nodeId} with new unique id ${uniqueId} migrated successfully.`);
121
130
  } else {
@@ -130,14 +139,17 @@ for (const nodeId of legacyNodes) {
130
139
  }
131
140
 
132
141
  const eventsStorage = newNodeStorage.createContext("events");
133
- eventsStorage.set("lastEventNumber", legacyStorage.get([nodeId, "EventHandler"], "lastEventNumber"));
142
+ await eventsStorage.set("lastEventNumber", legacyStorage.get([nodeId, "EventHandler"], "lastEventNumber"));
134
143
 
135
144
  const fabricsStorage = newNodeStorage.createContext("fabrics");
136
- fabricsStorage.set("fabrics", legacyStorage.get([nodeId, "FabricManager"], "fabrics"));
137
- fabricsStorage.set("nextFabricIndex", legacyStorage.get([nodeId, "FabricManager"], "nextFabricIndex"));
145
+ await fabricsStorage.set("fabrics", legacyStorage.get([nodeId, "FabricManager"], "fabrics"));
146
+ await fabricsStorage.set("nextFabricIndex", legacyStorage.get([nodeId, "FabricManager"], "nextFabricIndex"));
138
147
 
139
148
  const sessionsStorage = newNodeStorage.createContext("sessions");
140
- sessionsStorage.set("resumptionRecords", legacyStorage.get([nodeId, "SessionManager"], "resumptionRecords"));
149
+ await sessionsStorage.set(
150
+ "resumptionRecords",
151
+ legacyStorage.get([nodeId, "SessionManager"], "resumptionRecords"),
152
+ );
141
153
 
142
154
  console.log(`Device Node ${nodeId} with unique id ${uniqueIds[nodeId]} migrated successfully.`);
143
155
  }
@@ -163,34 +163,36 @@ async function getConfiguration() {
163
163
  const devices = [];
164
164
  const numDevices = environment.vars.number("num") ?? 2;
165
165
  for (let i = 1; i <= numDevices; i++) {
166
- const isSocket = deviceStorage.get(`isSocket${i}`, environment.vars.string(`type${i}`) === "socket");
167
- if (deviceStorage.has(`isSocket${i}`)) {
166
+ const isSocket = await deviceStorage.get(`isSocket${i}`, environment.vars.string(`type${i}`) === "socket");
167
+ if (await deviceStorage.has(`isSocket${i}`)) {
168
168
  console.log(`Device type ${isSocket ? "socket" : "light"} found in storage. --type parameter is ignored.`);
169
169
  }
170
170
  const deviceName = `Matter ${environment.vars.string(`type${i}`) ?? "light"} device ${i}`;
171
171
  const vendorName = "matter-node.js";
172
172
  const passcode =
173
- environment.vars.number(`passcode${i}`) ?? deviceStorage.get(`passcode${i}`, defaultPasscode++);
173
+ environment.vars.number(`passcode${i}`) ?? (await deviceStorage.get(`passcode${i}`, defaultPasscode++));
174
174
  const discriminator =
175
175
  environment.vars.number(`discriminator${i}`) ??
176
- deviceStorage.get(`discriminator${i}`, defaultDiscriminator++);
176
+ (await deviceStorage.get(`discriminator${i}`, defaultDiscriminator++));
177
177
  // product name / id and vendor id should match what is in the device certificate
178
- const vendorId = environment.vars.number(`vendorid${i}`) ?? deviceStorage.get(`vendorid${i}`, 0xfff1);
178
+ const vendorId = environment.vars.number(`vendorid${i}`) ?? (await deviceStorage.get(`vendorid${i}`, 0xfff1));
179
179
  const productName = `node-matter OnOff-Device ${i}`;
180
- const productId = environment.vars.number(`productid${i}`) ?? deviceStorage.get(`productid${i}`, 0x8000);
180
+ const productId =
181
+ environment.vars.number(`productid${i}`) ?? (await deviceStorage.get(`productid${i}`, 0x8000));
181
182
 
182
183
  const port = environment.vars.number(`port${i}`) ?? defaultPort++;
183
184
 
184
185
  const uniqueId =
185
- environment.vars.string(`uniqueid${i}`) ?? deviceStorage.get(`uniqueid${i}`, `${i}-${Time.nowMs()}`);
186
+ environment.vars.string(`uniqueid${i}`) ??
187
+ (await deviceStorage.get(`uniqueid${i}`, `${i}-${Time.nowMs()}`));
186
188
 
187
189
  // Persist basic data to keep them also on restart
188
- deviceStorage.set(`passcode${i}`, passcode);
189
- deviceStorage.set(`discriminator${i}`, discriminator);
190
- deviceStorage.set(`vendorid${i}`, vendorId);
191
- deviceStorage.set(`productid${i}`, productId);
192
- deviceStorage.set(`isSocket${i}`, isSocket);
193
- deviceStorage.set(`uniqueid${i}`, uniqueId);
190
+ await deviceStorage.set(`passcode${i}`, passcode);
191
+ await deviceStorage.set(`discriminator${i}`, discriminator);
192
+ await deviceStorage.set(`vendorid${i}`, vendorId);
193
+ await deviceStorage.set(`productid${i}`, productId);
194
+ await deviceStorage.set(`isSocket${i}`, isSocket);
195
+ await deviceStorage.set(`uniqueid${i}`, uniqueId);
194
196
 
195
197
  devices.push({
196
198
  isSocket,
@@ -258,10 +258,8 @@ process.on("SIGINT", () => {
258
258
  .stop()
259
259
  .then(() => {
260
260
  // Pragmatic way to make sure the storage is correctly closed before the process ends.
261
- storage
262
- .close()
263
- .then(() => process.exit(0))
264
- .catch(err => console.error(err));
261
+ storage.close();
262
+ process.exit(0);
265
263
  })
266
264
  .catch(err => console.error(err));
267
265
  });
@@ -186,13 +186,13 @@ async function getConfiguration() {
186
186
  );
187
187
  const deviceStorage = (await storageService.open("device")).createContext("data");
188
188
 
189
- const isTemperature = deviceStorage.get("isTemperature", environment.vars.get("type") !== "humidity");
190
- if (deviceStorage.has("isTemperature")) {
189
+ const isTemperature = await deviceStorage.get("isTemperature", environment.vars.get("type") !== "humidity");
190
+ if (await deviceStorage.has("isTemperature")) {
191
191
  console.log(
192
192
  `Device type ${isTemperature ? "temperature" : "humidity"} found in storage. --type parameter is ignored.`,
193
193
  );
194
194
  }
195
- let interval = environment.vars.number("interval") ?? deviceStorage.get("interval", 60);
195
+ let interval = environment.vars.number("interval") ?? (await deviceStorage.get("interval", 60));
196
196
  if (interval < 1) {
197
197
  console.log(`Invalid Interval ${interval}, set to 60s`);
198
198
  interval = 60;
@@ -200,25 +200,28 @@ async function getConfiguration() {
200
200
 
201
201
  const deviceName = "Matter test device";
202
202
  const vendorName = "matter-node.js";
203
- const passcode = environment.vars.number("passcode") ?? deviceStorage.get("passcode", 20202021);
204
- const discriminator = environment.vars.number("discriminator") ?? deviceStorage.get("discriminator", 3840);
203
+ const passcode = environment.vars.number("passcode") ?? (await deviceStorage.get("passcode", 20202021));
204
+ const discriminator = environment.vars.number("discriminator") ?? (await deviceStorage.get("discriminator", 3840));
205
205
  // product name / id and vendor id should match what is in the device certificate
206
- const vendorId = environment.vars.number("vendorid") ?? deviceStorage.get("vendorid", 0xfff1);
206
+ const vendorId = environment.vars.number("vendorid") ?? (await deviceStorage.get("vendorid", 0xfff1));
207
207
  const productName = `node-matter OnOff ${isTemperature ? "Temperature" : "Humidity"}`;
208
- const productId = environment.vars.number("productid") ?? deviceStorage.get("productid", 0x8000);
208
+ const productId = environment.vars.number("productid") ?? (await deviceStorage.get("productid", 0x8000));
209
209
 
210
210
  const port = environment.vars.number("port") ?? 5540;
211
211
 
212
- const uniqueId = environment.vars.string("uniqueid") ?? deviceStorage.get("uniqueid", Time.nowMs().toString());
212
+ const uniqueId =
213
+ environment.vars.string("uniqueid") ?? (await deviceStorage.get("uniqueid", Time.nowMs().toString()));
213
214
 
214
215
  // Persist basic data to keep them also on restart
215
- deviceStorage.set("passcode", passcode);
216
- deviceStorage.set("discriminator", discriminator);
217
- deviceStorage.set("vendorid", vendorId);
218
- deviceStorage.set("productid", productId);
219
- deviceStorage.set("interval", interval);
220
- deviceStorage.set("isTemperature", isTemperature);
221
- deviceStorage.set("uniqueid", uniqueId);
216
+ await deviceStorage.set({
217
+ passcode,
218
+ discriminator,
219
+ vendorid: vendorId,
220
+ productid: productId,
221
+ interval,
222
+ isTemperature,
223
+ uniqueid: uniqueId,
224
+ });
222
225
 
223
226
  return {
224
227
  isTemperature,