@matterbridge/core 3.11.1-dev-20261028-f791c14 → 3.11.1-dev-20261030-e12e668

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -131,9 +131,9 @@ export async function main() {
131
131
  instance = await Matterbridge.loadInstance(true);
132
132
  log.debug(`***Matterbridge.loadInstance(true) exited`);
133
133
  if (hasParameter('experimental-backend')) {
134
- log.debug(`***Starting experimental Backend thread...`);
134
+ log.notice(`***Starting experimental Backend thread...`);
135
135
  manager.runThread('Backend');
136
- log.debug(`***Started experimental Backend thread`);
136
+ log.notice(`***Started experimental Backend thread`);
137
137
  }
138
138
  if (!instance || instance.shutdown) {
139
139
  await shutdown();
@@ -0,0 +1,7 @@
1
+ import { type Diagnostic, LogLevel as MatterLogLevel } from '@matter/general';
2
+ import { type SharedMatterbridge } from '@matterbridge/types';
3
+ import { AnsiLogger, LogLevel } from 'node-ansi-logger';
4
+ export declare function matterToAnsiLogLevel(level: MatterLogLevel): LogLevel;
5
+ export declare function ansiToMatterLogLevel(level: LogLevel): MatterLogLevel;
6
+ export declare function setupLoggers(matterbridge: SharedMatterbridge, log: AnsiLogger, matterLog: AnsiLogger, verbose: boolean): void;
7
+ export declare function createDestinationMatterLogger(matterbridge: SharedMatterbridge, log: AnsiLogger, matterLog: AnsiLogger): (text: string, message: Diagnostic.Message) => void;
@@ -0,0 +1,67 @@
1
+ import path from 'node:path';
2
+ import { LogFormat as MatterLogFormat, Logger, LogLevel as MatterLogLevel } from '@matter/general';
3
+ import { MATTER_LOGGER_FILE, MATTERBRIDGE_LOGGER_FILE } from '@matterbridge/types';
4
+ import { AnsiLogger } from 'node-ansi-logger';
5
+ export function matterToAnsiLogLevel(level) {
6
+ switch (level) {
7
+ case 0:
8
+ return "debug";
9
+ case 1:
10
+ return "info";
11
+ case 2:
12
+ return "notice";
13
+ case 3:
14
+ return "warn";
15
+ case 4:
16
+ return "error";
17
+ default:
18
+ return "fatal";
19
+ }
20
+ }
21
+ export function ansiToMatterLogLevel(level) {
22
+ switch (level) {
23
+ case "debug":
24
+ return MatterLogLevel.DEBUG;
25
+ case "info":
26
+ return MatterLogLevel.INFO;
27
+ case "notice":
28
+ return MatterLogLevel.NOTICE;
29
+ case "warn":
30
+ return MatterLogLevel.WARN;
31
+ case "error":
32
+ return MatterLogLevel.ERROR;
33
+ default:
34
+ return MatterLogLevel.FATAL;
35
+ }
36
+ }
37
+ export function setupLoggers(matterbridge, log, matterLog, verbose) {
38
+ if (matterbridge.fileLogger) {
39
+ AnsiLogger.setGlobalLogfile(path.join(matterbridge.matterbridgeDirectory, MATTERBRIDGE_LOGGER_FILE), matterbridge.logLevel);
40
+ }
41
+ Logger.destinations.default.write = createDestinationMatterLogger(matterbridge, log, matterLog);
42
+ if (verbose) {
43
+ log.debug(`Matterbridge logLevel: ${log.logLevel} fileLogger: ${matterbridge.fileLogger}.`);
44
+ log.debug(`Matter logLevel: ${matterLog.logLevel} fileLogger: ${matterbridge.matterFileLogger}.`);
45
+ }
46
+ }
47
+ export function createDestinationMatterLogger(matterbridge, log, matterLog) {
48
+ if (matterbridge.matterFileLogger) {
49
+ matterLog.logFilePath = path.join(matterbridge.matterbridgeDirectory, MATTER_LOGGER_FILE);
50
+ }
51
+ return (text, message) => {
52
+ try {
53
+ let msg;
54
+ if (Logger.format === MatterLogFormat.ANSI) {
55
+ msg = text.slice(65);
56
+ }
57
+ else {
58
+ msg = text.split(message.facility)[1]?.trim();
59
+ }
60
+ matterLog.logName = message.facility;
61
+ matterLog.log(matterToAnsiLogLevel(message.level), msg);
62
+ }
63
+ catch (_error) {
64
+ log.debug(`Error parsing matter log message facility ${message.facility}`);
65
+ }
66
+ };
67
+ }
@@ -1,9 +1,9 @@
1
1
  import '@matter/nodejs';
2
2
  import EventEmitter from 'node:events';
3
- import { type Diagnostic, type StorageContext, type StorageManager, StorageService } from '@matter/general';
3
+ import { type StorageContext, type StorageManager } from '@matter/general';
4
4
  import { Endpoint, ServerNode } from '@matter/node';
5
5
  import { AggregatorEndpoint } from '@matter/node/endpoints/aggregator';
6
- import { type DeviceCertification, MdnsService } from '@matter/protocol';
6
+ import { type DeviceCertification } from '@matter/protocol';
7
7
  import { DeviceTypeId, VendorId } from '@matter/types/datatype';
8
8
  import type { BridgeStatus, PluginName, PluginStatusUpdate, SharedMatterbridge, WorkerMessage } from '@matterbridge/types';
9
9
  import type { MatterbridgeEndpoint } from './matterbridgeEndpoint.js';
@@ -21,9 +21,7 @@ export declare class MatterNode extends EventEmitter<MatterEvents> {
21
21
  private readonly log;
22
22
  private readonly matterLog;
23
23
  private readonly environment;
24
- storeId: string | undefined;
25
- matterMdnsService: MdnsService | undefined;
26
- matterStorageService: StorageService | undefined;
24
+ storeId: string;
27
25
  matterStorageManager: StorageManager | undefined;
28
26
  matterStorageContext: StorageContext | undefined;
29
27
  mdnsInterface: string | undefined;
@@ -49,13 +47,11 @@ export declare class MatterNode extends EventEmitter<MatterEvents> {
49
47
  private readonly verbose;
50
48
  constructor(matterbridge: SharedMatterbridge, pluginName?: PluginName | undefined, device?: MatterbridgeEndpoint | undefined);
51
49
  msgHandler(msg: WorkerMessage): Promise<void>;
52
- destroy(closeMdns?: boolean): Promise<void>;
50
+ destroy(): Promise<void>;
53
51
  create(): Promise<void>;
54
52
  start(): Promise<void>;
55
53
  stop(): Promise<void>;
56
- createDestinationMatterLogger(): (text: string, message: Diagnostic.Message) => void;
57
54
  startMatterStorage(): Promise<void>;
58
- backupMatterStorage(storageName: string, backupName: string): Promise<void>;
59
55
  stopMatterStorage(): Promise<void>;
60
56
  createServerNodeContext(storeId: string, deviceName: string, deviceType: DeviceTypeId, vendorId: VendorId, vendorName: string, productId: number, productName: string, serialNumber?: string, uniqueId?: string, softwareVersion?: number, softwareVersionString?: string, hardwareVersion?: number, hardwareVersionString?: string, productLabel?: string, productUrl?: string, configurationVersion?: number): Promise<StorageContext>;
61
57
  createServerNode(port?: number, passcode?: number, discriminator?: number): Promise<ServerNode>;
@@ -63,9 +59,9 @@ export declare class MatterNode extends EventEmitter<MatterEvents> {
63
59
  stopServerNode(timeout?: number): Promise<void>;
64
60
  createAggregatorNode(): Promise<Endpoint<AggregatorEndpoint>>;
65
61
  createMatterbridgeServerNode(): Promise<ServerNode>;
66
- createAccessoryPlugin(plugin: Plugin | PluginName, device: MatterbridgeEndpoint): Promise<ServerNode | undefined>;
67
- createDynamicPlugin(plugin: Plugin | PluginName): Promise<ServerNode | undefined>;
68
- createDeviceServerNode(plugin: Plugin | PluginName, device: MatterbridgeEndpoint): Promise<ServerNode | undefined>;
62
+ createAccessoryPlugin(pluginOrName: Plugin | PluginName, device: MatterbridgeEndpoint): Promise<ServerNode | undefined>;
63
+ createDynamicPlugin(pluginOrName: Plugin | PluginName): Promise<ServerNode | undefined>;
64
+ createDeviceServerNode(pluginOrName: Plugin | PluginName, device: MatterbridgeEndpoint): Promise<ServerNode | undefined>;
69
65
  addBridgedEndpoint(pluginName: string, device: MatterbridgeEndpoint): Promise<MatterbridgeEndpoint | undefined>;
70
66
  removeBridgedEndpoint(pluginName: string, device: MatterbridgeEndpoint): Promise<MatterbridgeEndpoint | undefined>;
71
67
  removeAllBridgedEndpoints(pluginName: string, delay?: number): Promise<void>;
@@ -3,40 +3,39 @@ import EventEmitter from 'node:events';
3
3
  import fs from 'node:fs';
4
4
  import path from 'node:path';
5
5
  import { fileURLToPath } from 'node:url';
6
- import { Crypto, Environment, LogFormat as MatterLogFormat, Logger, LogLevel as MatterLogLevel, StorageService, UINT16_MAX, UINT32_MAX, } from '@matter/general';
6
+ import { Crypto, Environment, LogFormat as MatterLogFormat, LogLevel as MatterLogLevel, UINT16_MAX, UINT32_MAX } from '@matter/general';
7
7
  import { Endpoint, ServerNode } from '@matter/node';
8
8
  import { BasicInformationServer } from '@matter/node/behaviors/basic-information';
9
9
  import { BridgedDeviceBasicInformationServer } from '@matter/node/behaviors/bridged-device-basic-information';
10
10
  import { PowerSourceServer } from '@matter/node/behaviors/power-source';
11
11
  import { AggregatorEndpoint } from '@matter/node/endpoints/aggregator';
12
- import { MdnsService, PaseClient } from '@matter/protocol';
12
+ import { PaseClient } from '@matter/protocol';
13
13
  import { PowerSource } from '@matter/types/clusters/power-source';
14
14
  import { DeviceTypeId, EndpointNumber, VendorId } from '@matter/types/datatype';
15
15
  import { BroadcastServer } from '@matterbridge/thread/server';
16
- import { dev, MATTER_LOGGER_FILE, MATTER_STORAGE_DIR, MATTERBRIDGE_LOGGER_FILE, NODE_STORAGE_DIR, plg } from '@matterbridge/types';
16
+ import { dev, MATTER_STORAGE_DIR, NODE_STORAGE_DIR, plg } from '@matterbridge/types';
17
17
  import { getIntParameter, getParameter, hasAnyParameter, hasParameter } from '@matterbridge/utils/cli';
18
- import { copyDirectory } from '@matterbridge/utils/copy-dir';
19
18
  import { getErrorMessage, inspectError } from '@matterbridge/utils/error';
20
19
  import { logModuleLoaded } from '@matterbridge/utils/loader';
21
- import { isValidInteger, isValidString, parseVersionString } from '@matterbridge/utils/validate';
20
+ import { isValidInteger, isValidObject, isValidString, parseVersionString } from '@matterbridge/utils/validate';
22
21
  import { fireAndForget, wait, withTimeout } from '@matterbridge/utils/wait';
23
22
  import { AnsiLogger, BLUE, CYAN, db, debugStringify, er, nf, or, zb } from 'node-ansi-logger';
24
23
  import { NodeStorageManager } from 'node-persist-manager';
25
24
  import { addVirtualDevice } from './helpers.js';
26
25
  import { bridge } from './matterbridgeDeviceTypes.js';
26
+ import { setupLoggers } from './matterLogger.js';
27
27
  import { deleteAdvertisingNode, getServerNodeData, setAdvertisingNode, toBaseDevice } from './matterNodeHelpers.js';
28
+ import { startMatterStorage, stopMatterStorage } from './matterStorage.js';
28
29
  import { PluginManager } from './pluginManager.js';
29
30
  logModuleLoaded('MatterNode');
30
31
  export class MatterNode extends EventEmitter {
31
32
  matterbridge;
32
33
  pluginName;
33
34
  device;
34
- log = new AnsiLogger({ logName: 'MatterNode', logTimestampFormat: 4, logLevel: "debug" });
35
- matterLog = new AnsiLogger({ logName: 'MatterNode', logTimestampFormat: 4, logLevel: "debug" });
35
+ log = new AnsiLogger({ logName: 'MatterNode', logNameColor: '\x1b[38;5;65m', logTimestampFormat: 4, logLevel: "debug" });
36
+ matterLog = new AnsiLogger({ logName: 'MatterNode', logNameColor: '\x1b[34m', logTimestampFormat: 4, logLevel: "debug" });
36
37
  environment = Environment.default;
37
38
  storeId;
38
- matterMdnsService;
39
- matterStorageService;
40
39
  matterStorageManager;
41
40
  matterStorageContext;
42
41
  mdnsInterface;
@@ -65,7 +64,6 @@ export class MatterNode extends EventEmitter {
65
64
  this.matterbridge = matterbridge;
66
65
  this.pluginName = pluginName;
67
66
  this.device = device;
68
- this.log.logNameColor = '\x1b[38;5;65m';
69
67
  this.log.logLevel = matterbridge.logLevel;
70
68
  this.matterLog.logLevel = matterbridge.matterLogLevel;
71
69
  if (this.debug)
@@ -89,7 +87,7 @@ export class MatterNode extends EventEmitter {
89
87
  };
90
88
  this.pluginManager = new PluginManager(injectMatterbridge);
91
89
  this.pluginManager.server.close();
92
- this.pluginManager.logLevel = this.debug ? "debug" : "info";
90
+ this.pluginManager.logLevel = matterbridge.logLevel;
93
91
  if (this.verbose)
94
92
  this.log.debug(`PluginManager is ready`);
95
93
  this.environment.vars.set('log.level', MatterLogLevel.DEBUG);
@@ -102,19 +100,7 @@ export class MatterNode extends EventEmitter {
102
100
  this.environment.vars.set('mdns.networkInterface', this.mdnsInterface);
103
101
  if (this.verbose)
104
102
  this.log.debug(`Matter Environment is ready`);
105
- this.matterMdnsService = new MdnsService(this.environment);
106
- setImmediate(async () => {
107
- await this.matterMdnsService?.construction.ready;
108
- if (this.verbose)
109
- this.log.debug(`Matter MdnsService is ready`);
110
- });
111
- if (this.matterbridge.fileLogger) {
112
- AnsiLogger.setGlobalLogfile(path.join(this.matterbridge.matterbridgeDirectory, MATTERBRIDGE_LOGGER_FILE), this.matterbridge.logLevel);
113
- }
114
- Logger.destinations.default.write = this.createDestinationMatterLogger();
115
- const levels = ['debug', 'info', 'notice', 'warn', 'error', 'fatal'];
116
- if (this.verbose)
117
- this.log.debug(`Matter logLevel: ${levels[Logger.level]} fileLogger: ${matterbridge.matterFileLogger}.`);
103
+ setupLoggers(matterbridge, this.log, this.matterLog, this.verbose);
118
104
  if (this.debug)
119
105
  this.log.debug(`MatterNode for ${this.storeId} loaded`);
120
106
  }
@@ -148,7 +134,7 @@ export class MatterNode extends EventEmitter {
148
134
  try {
149
135
  await this.stop();
150
136
  this.server.respond({ ...msg, result: { storeId: this.storeId, success: true } });
151
- await this.destroy(!this.pluginName);
137
+ await this.destroy();
152
138
  }
153
139
  catch (error) {
154
140
  inspectError(this.log, `Error stopping MatterNode ${this.storeId}`, error);
@@ -178,16 +164,9 @@ export class MatterNode extends EventEmitter {
178
164
  }
179
165
  }
180
166
  }
181
- async destroy(closeMdns = true) {
167
+ async destroy() {
182
168
  if (this.verbose)
183
169
  this.log.debug(`Destroying MatterNode instance for ${this.storeId}...`);
184
- if (closeMdns) {
185
- if (this.verbose)
186
- this.log.debug(`Closing Matter MdnsService for ${this.storeId}...`);
187
- await this.matterMdnsService?.close();
188
- if (this.verbose)
189
- this.log.debug(`Closed Matter MdnsService for ${this.storeId}`);
190
- }
191
170
  this.pluginManager.destroy();
192
171
  this.server.close();
193
172
  await this.yieldToNode();
@@ -200,7 +179,7 @@ export class MatterNode extends EventEmitter {
200
179
  this.port = result.port;
201
180
  this.passcode = result.passcode;
202
181
  this.discriminator = result.discriminator;
203
- this.log.debug(`Loaded Matter node parameters for ${this.storeId}: port ${CYAN}${this.port}${db} passcode ${CYAN}${this.passcode}${db} discriminator ${CYAN}${this.discriminator}${db}`);
182
+ this.log.debug(`Loaded Matter node parameters for ${this.storeId}: port ${CYAN}${this.port}${db} passcode ${CYAN}${this.passcode}${db} discriminator ${CYAN}${this.discriminator}${db}`);
204
183
  await this.startMatterStorage();
205
184
  this.pluginManager.matterbridge.nodeStorage = new NodeStorageManager({
206
185
  dir: path.join(this.matterbridge.matterbridgeDirectory, NODE_STORAGE_DIR),
@@ -312,7 +291,7 @@ export class MatterNode extends EventEmitter {
312
291
  this.serverNode = undefined;
313
292
  this.aggregatorNode = undefined;
314
293
  await this.stopMatterStorage();
315
- await this.destroy(false);
294
+ await this.destroy();
316
295
  this.log.debug(`Stopped MatterNode for server device ${this.pluginName}:${this.device.deviceName}`);
317
296
  this.log.info(`Stopped MatterNode for ${this.storeId}`);
318
297
  await this.yieldToNode();
@@ -344,67 +323,23 @@ export class MatterNode extends EventEmitter {
344
323
  this.log.info(`Stopped MatterNode for ${this.storeId}`);
345
324
  await this.yieldToNode();
346
325
  }
347
- createDestinationMatterLogger() {
348
- this.matterLog.logNameColor = '\x1b[34m';
349
- if (this.matterbridge.matterFileLogger) {
350
- this.matterLog.logFilePath = path.join(this.matterbridge.matterbridgeDirectory, MATTER_LOGGER_FILE);
351
- }
352
- return (text, message) => {
353
- try {
354
- let msg;
355
- if (Logger.format === MatterLogFormat.ANSI) {
356
- msg = text.slice(65);
357
- }
358
- else {
359
- msg = text.split(message.facility)[1]?.trim();
360
- }
361
- this.matterLog.logName = message.facility;
362
- this.matterLog.log(MatterLogLevel.names[message.level], msg);
363
- }
364
- catch (_error) {
365
- this.log.debug(`Error parsing matter log message facility ${message.facility}`);
366
- }
367
- };
368
- }
369
326
  async startMatterStorage() {
370
- this.log.info(`Starting matter node storage...`);
371
- this.matterStorageService = this.environment.get(StorageService);
372
- this.log.info(`Started matter node storage in ${CYAN}${this.matterStorageService.location}${nf}`);
373
- await this.backupMatterStorage(path.join(this.matterbridge.matterbridgeDirectory, MATTER_STORAGE_DIR), path.join(this.matterbridge.matterbridgeDirectory, MATTER_STORAGE_DIR + '.backup'));
374
- }
375
- async backupMatterStorage(storageName, backupName) {
376
- this.log.info(`Creating matter node storage backup from ${CYAN}${storageName}${nf} to ${CYAN}${backupName}${nf}...`);
377
- if (storageName && !fs.existsSync(storageName)) {
378
- this.log.info(`No matter node storage found to backup from ${CYAN}${storageName}${nf} to ${CYAN}${backupName}${nf}`);
379
- return;
380
- }
381
- try {
382
- if (await copyDirectory(storageName, backupName, this.log))
383
- this.log.info('Created matter node storage backup');
384
- else
385
- this.log.error(`Error creating matter node storage backup from ${storageName} to ${backupName}`);
386
- }
387
- catch (error) {
388
- this.log.error(`Error creating matter node storage backup from ${storageName} to ${backupName}:`, error);
389
- }
327
+ this.matterStorageManager = await startMatterStorage(this.matterbridge, this.storeId, this.log);
390
328
  }
391
329
  async stopMatterStorage() {
392
- this.log.info('Closing matter node storage...');
393
- await this.matterStorageManager?.close();
394
- this.matterStorageService = undefined;
330
+ if (this.matterStorageManager)
331
+ await stopMatterStorage(this.matterStorageManager, this.log);
395
332
  this.matterStorageManager = undefined;
396
333
  this.matterStorageContext = undefined;
397
- this.log.info('Closed matter node storage');
398
334
  this.emit('closed');
399
335
  }
400
336
  async createServerNodeContext(storeId, deviceName, deviceType, vendorId, vendorName, productId, productName, serialNumber, uniqueId, softwareVersion = 1, softwareVersionString = '1.0.0', hardwareVersion = 1, hardwareVersionString = '1.0.0', productLabel = 'Matter Endpoint', productUrl = 'https://matterbridge.io', configurationVersion = 1) {
401
- if (!this.matterStorageService) {
402
- throw new Error('No storage service initialized');
337
+ if (!this.matterStorageManager) {
338
+ throw new Error('No storage manager initialized');
403
339
  }
404
340
  const { randomBytes } = await import('node:crypto');
405
- this.log.info(`Creating server node storage context "${storeId}.persist" for ${storeId}...`);
406
- const storageManager = await this.matterStorageService.open(storeId);
407
- const storageContext = storageManager.createContext('persist');
341
+ this.log.info(`Creating server node storage context "${storeId}.matterbridge" for ${storeId}...`);
342
+ const storageContext = this.matterStorageManager.createContext('matterbridge');
408
343
  const random = randomBytes(8).toString('hex');
409
344
  async function setIfDifferent(key, value) {
410
345
  if (!(await storageContext.has(key)) || (await storageContext.get(key)) !== value) {
@@ -428,7 +363,7 @@ export class MatterNode extends EventEmitter {
428
363
  await setIfDifferent('hardwareVersion', isValidInteger(hardwareVersion, 0, UINT16_MAX) ? hardwareVersion : 1);
429
364
  await setIfDifferent('hardwareVersionString', isValidString(hardwareVersionString, 1, 64) ? hardwareVersionString : '1.0.0');
430
365
  await setIfDifferent('configurationVersion', isValidInteger(configurationVersion, 1, UINT32_MAX) ? configurationVersion : 1);
431
- this.log.debug(`Created server node storage context "${storeId}.persist" for ${storeId}:`);
366
+ this.log.debug(`Created server node storage context "${storeId}.matterbridge" for ${storeId}:`);
432
367
  this.log.debug(`- storeId: ${await storageContext.get('storeId')}`);
433
368
  this.log.debug(`- deviceName: ${await storageContext.get('deviceName')}`);
434
369
  this.log.debug(`- deviceType: ${await storageContext.get('deviceType')}(0x${(await storageContext.get('deviceType'))?.toString(16).padStart(4, '0')})`);
@@ -471,13 +406,15 @@ export class MatterNode extends EventEmitter {
471
406
  this.log.debug(`- hardwareVersion: ${await this.matterStorageContext.get('hardwareVersion')}`);
472
407
  this.log.debug(`- hardwareVersionString: ${await this.matterStorageContext.get('hardwareVersionString')}`);
473
408
  this.log.debug(`- configurationVersion: ${await this.matterStorageContext.get('configurationVersion')}`);
409
+ let validPasscode = passcode;
474
410
  if (passcode < 0 || passcode > 99999999) {
475
411
  this.log.warn(`Invalid passcode ${passcode} for server node ${storeId}. Passcode must be between 0 and 99999999. Generating a random passcode...`);
476
- passcode = PaseClient.generateRandomPasscode(this.environment.get(Crypto));
412
+ validPasscode = PaseClient.generateRandomPasscode(this.environment.get(Crypto));
477
413
  }
414
+ let validDiscriminator = discriminator;
478
415
  if (discriminator < 0 || discriminator > 0xfff) {
479
416
  this.log.warn(`Invalid discriminator ${discriminator} for server node ${storeId}. Discriminator must be between 0 and 4095 (0xFFF). Generating a random discriminator...`);
480
- discriminator = PaseClient.generateRandomDiscriminator(this.environment.get(Crypto));
417
+ validDiscriminator = PaseClient.generateRandomDiscriminator(this.environment.get(Crypto));
481
418
  }
482
419
  let rootEndpoint;
483
420
  if (hasParameter('root-power-source') || process.env.MATTERBRIDGE_CHIP_TEST) {
@@ -510,8 +447,8 @@ export class MatterNode extends EventEmitter {
510
447
  certification: this.certification,
511
448
  },
512
449
  commissioning: {
513
- passcode,
514
- discriminator,
450
+ passcode: validPasscode,
451
+ discriminator: validDiscriminator,
515
452
  },
516
453
  productDescription: {
517
454
  name: await this.matterStorageContext.get('deviceName'),
@@ -680,13 +617,10 @@ export class MatterNode extends EventEmitter {
680
617
  this.log.debug(`Created ${plg}Matterbridge${db} server node`);
681
618
  return this.serverNode;
682
619
  }
683
- async createAccessoryPlugin(plugin, device) {
684
- if (typeof plugin === 'string') {
685
- const pluginInstance = this.pluginManager.get(plugin);
686
- if (!pluginInstance)
687
- throw new Error(`Plugin ${BLUE}${this.pluginName}${er} not found`);
688
- plugin = pluginInstance;
689
- }
620
+ async createAccessoryPlugin(pluginOrName, device) {
621
+ const plugin = typeof pluginOrName === 'string' ? this.pluginManager.get(pluginOrName) : pluginOrName;
622
+ if (!plugin)
623
+ throw new Error(`Plugin ${BLUE}${this.pluginName}${er} not found`);
690
624
  if (!plugin.locked && device.deviceType && device.deviceName && device.vendorId && device.vendorName && device.productId && device.productName) {
691
625
  plugin.locked = true;
692
626
  this.log.debug(`Creating accessory plugin ${plg}${plugin.name}${db} server node...`);
@@ -701,13 +635,10 @@ export class MatterNode extends EventEmitter {
701
635
  }
702
636
  return this.serverNode;
703
637
  }
704
- async createDynamicPlugin(plugin) {
705
- if (typeof plugin === 'string') {
706
- const pluginInstance = this.pluginManager.get(plugin);
707
- if (!pluginInstance)
708
- throw new Error(`Plugin ${BLUE}${this.pluginName}${er} not found`);
709
- plugin = pluginInstance;
710
- }
638
+ async createDynamicPlugin(pluginOrName) {
639
+ const plugin = typeof pluginOrName === 'string' ? this.pluginManager.get(pluginOrName) : pluginOrName;
640
+ if (!plugin)
641
+ throw new Error(`Plugin ${BLUE}${this.pluginName}${er} not found`);
711
642
  if (!plugin.locked) {
712
643
  plugin.locked = true;
713
644
  this.log.debug(`Creating dynamic plugin ${plg}${plugin.name}${db} server node...`);
@@ -724,13 +655,10 @@ export class MatterNode extends EventEmitter {
724
655
  }
725
656
  return this.serverNode;
726
657
  }
727
- async createDeviceServerNode(plugin, device) {
728
- if (typeof plugin === 'string') {
729
- const pluginInstance = this.pluginManager.get(plugin);
730
- if (!pluginInstance)
731
- throw new Error(`Plugin ${BLUE}${this.pluginName}${er} not found`);
732
- plugin = pluginInstance;
733
- }
658
+ async createDeviceServerNode(pluginOrName, device) {
659
+ const plugin = typeof pluginOrName === 'string' ? this.pluginManager.get(pluginOrName) : pluginOrName;
660
+ if (!plugin)
661
+ throw new Error(`Plugin ${BLUE}${this.pluginName}${er} not found`);
734
662
  if (device.mode === 'server' && device.deviceType && device.deviceName && device.vendorId && device.vendorName && device.productId && device.productName) {
735
663
  this.log.debug(`Creating device ${plg}${plugin.name}${db}:${dev}${device.deviceName}${db} server node...`);
736
664
  this.matterStorageContext = await this.createServerNodeContext(device.deviceName.replace(/[ .]/g, ''), device.deviceName, DeviceTypeId(device.deviceType), VendorId(device.vendorId), device.vendorName, device.productId, device.productName, device.serialNumber, device.uniqueId, device.softwareVersion, device.softwareVersionString, device.hardwareVersion, device.hardwareVersionString, device.productLabel, device.productUrl, device.configurationVersion);
@@ -839,7 +767,7 @@ export class MatterNode extends EventEmitter {
839
767
  if (matterNode) {
840
768
  this.log.debug(`Closing MatterNode for device ${plg}${pluginName}${db}:${dev}${device.deviceName}${db} (${zb}${device.name}${db})...`);
841
769
  await matterNode.stop();
842
- await matterNode.destroy(false);
770
+ await matterNode.destroy();
843
771
  this.dependantMatterNodes.delete(device.id);
844
772
  this.log.debug(`Closed MatterNode for device ${plg}${pluginName}${db}:${dev}${device.deviceName}${db} (${zb}${device.name}${db})`);
845
773
  }
@@ -955,6 +883,8 @@ export class MatterNode extends EventEmitter {
955
883
  { cluster: 'Thermostat', attribute: 'occupiedCoolingSetpoint' },
956
884
  { cluster: 'Thermostat', attribute: 'occupiedHeatingSetpoint' },
957
885
  { cluster: 'Thermostat', attribute: 'systemMode' },
886
+ { cluster: 'Thermostat', attribute: 'thermostatRunningMode' },
887
+ { cluster: 'Thermostat', attribute: 'thermostatRunningState' },
958
888
  { cluster: 'WindowCovering', attribute: 'operationalStatus' },
959
889
  { cluster: 'WindowCovering', attribute: 'currentPositionLiftPercent100ths' },
960
890
  { cluster: 'DoorLock', attribute: 'lockState' },
@@ -987,7 +917,7 @@ export class MatterNode extends EventEmitter {
987
917
  device.subscribeAttribute(sub.cluster, sub.attribute, (value) => {
988
918
  if (!device.plugin || !device.serialNumber || !device.uniqueId)
989
919
  return;
990
- this.log.debug(`Bridged endpoint ${plg}${plugin.name}${db}:${dev}${device.deviceName}${db}:${or}${device.id}${db}:${or}${device.number}${db} attribute ${dev}${sub.cluster}${db}.${dev}${sub.attribute}${db} changed to ${CYAN}${value}${db}`);
920
+ this.log.debug(`Bridged endpoint ${plg}${plugin.name}${db}:${dev}${device.deviceName}${db}:${or}${device.id}${db}:${or}${device.number}${db} attribute ${dev}${sub.cluster}${db}.${dev}${sub.attribute}${db} changed to ${CYAN}${isValidObject(value) ? debugStringify(value) : value}${db}`);
991
921
  this.server.request({
992
922
  type: 'frontend_attributechanged',
993
923
  src: 'matter',
@@ -1011,7 +941,7 @@ export class MatterNode extends EventEmitter {
1011
941
  child.subscribeAttribute(sub.cluster, sub.attribute, (value) => {
1012
942
  if (!device.plugin || !device.serialNumber || !device.uniqueId)
1013
943
  return;
1014
- this.log.debug(`Bridged child endpoint ${plg}${plugin.name}${db}:${dev}${device.deviceName}${db}:${or}${child.id}${db}:${or}${child.number}${db} attribute ${dev}${sub.cluster}${db}.${dev}${sub.attribute}${db} changed to ${CYAN}${value}${db}`);
944
+ this.log.debug(`Bridged child endpoint ${plg}${plugin.name}${db}:${dev}${device.deviceName}${db}:${or}${child.id}${db}:${or}${child.number}${db} attribute ${dev}${sub.cluster}${db}.${dev}${sub.attribute}${db} changed to ${CYAN}${isValidObject(value) ? debugStringify(value) : value}${db}`);
1015
945
  this.server.request({
1016
946
  type: 'frontend_attributechanged',
1017
947
  src: 'matter',
@@ -0,0 +1,6 @@
1
+ import { type StorageManager } from '@matter/general';
2
+ import { type SharedMatterbridge } from '@matterbridge/types';
3
+ import { type AnsiLogger } from 'node-ansi-logger';
4
+ export declare function startMatterStorage(matterbridge: SharedMatterbridge, storeId: string, log: AnsiLogger): Promise<StorageManager>;
5
+ export declare function backupMatterStorage(storageName: string, backupName: string, log: AnsiLogger): Promise<void>;
6
+ export declare function stopMatterStorage(storageManager: StorageManager, log: AnsiLogger): Promise<void>;
@@ -0,0 +1,66 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { Environment, StorageService } from '@matter/general';
4
+ import { MATTER_STORAGE_DIR } from '@matterbridge/types';
5
+ import { copyDirectory } from '@matterbridge/utils/copy-dir';
6
+ import { CYAN, er, nf } from 'node-ansi-logger';
7
+ export async function startMatterStorage(matterbridge, storeId, log) {
8
+ log.info(`Starting matter node storage...`);
9
+ const environment = Environment.default;
10
+ const storageDir = path.join(matterbridge.matterbridgeDirectory, MATTER_STORAGE_DIR);
11
+ environment.vars.set('path.root', storageDir);
12
+ const matterStorageService = environment.get(StorageService);
13
+ log.info(`Started matter node storage service in ${CYAN}${matterStorageService.location}${nf}`);
14
+ const matterStorageManager = await matterStorageService.open(storeId);
15
+ log.info(`Opened matter node storage manager with store ID ${CYAN}${storeId}${nf}`);
16
+ try {
17
+ await backupMatterStorage(path.join(storageDir, storeId), path.join(storageDir + '.backup', storeId), log);
18
+ const storageContextPersist = matterStorageManager.createContext('persist');
19
+ log.info(`Created matter node storage context with legacy name ${CYAN}persist${nf}`);
20
+ const storageContext = matterStorageManager.createContext('matterbridge');
21
+ log.info(`Created matter node storage context with name ${CYAN}matterbridge${nf}`);
22
+ const values = await storageContextPersist.values();
23
+ let migrated = 0;
24
+ for (const [key, value] of Object.entries(values)) {
25
+ if (value === undefined || (await storageContext.has(key)))
26
+ continue;
27
+ await storageContext.set(key, value);
28
+ migrated++;
29
+ }
30
+ const total = Object.keys(values).length;
31
+ if (total) {
32
+ await storageContextPersist.clearAll();
33
+ if (migrated)
34
+ log.info(`Migrated ${migrated} keys of matter node storage context from ${CYAN}persist${nf} to ${CYAN}matterbridge${nf}`);
35
+ if (total > migrated)
36
+ log.info(`Cleared ${total - migrated} keys of matter node storage context ${CYAN}persist${nf} already migrated to ${CYAN}matterbridge${nf}`);
37
+ }
38
+ }
39
+ catch (error) {
40
+ log.error(`Error starting matter node storage with store ID ${CYAN}${storeId}${er}:`, error);
41
+ await matterStorageManager.close();
42
+ throw error;
43
+ }
44
+ return matterStorageManager;
45
+ }
46
+ export async function backupMatterStorage(storageName, backupName, log) {
47
+ log.info(`Creating matter node storage backup from ${CYAN}${storageName}${nf} to ${CYAN}${backupName}${nf}...`);
48
+ if (!storageName || !fs.existsSync(storageName)) {
49
+ log.info(`No matter node storage found to backup from ${CYAN}${storageName}${nf} to ${CYAN}${backupName}${nf}`);
50
+ return;
51
+ }
52
+ try {
53
+ if (await copyDirectory(storageName, backupName, log))
54
+ log.info('Created matter node storage backup');
55
+ else
56
+ log.error(`Error creating matter node storage backup from ${CYAN}${storageName}${nf} to ${CYAN}${backupName}${nf}`);
57
+ }
58
+ catch (error) {
59
+ log.error(`Error creating matter node storage backup from ${CYAN}${storageName}${nf} to ${CYAN}${backupName}${nf}:`, error);
60
+ }
61
+ }
62
+ export async function stopMatterStorage(storageManager, log) {
63
+ log.info('Closing matter node storage manager...');
64
+ await storageManager.close();
65
+ log.info('Closed matter node storage');
66
+ }
@@ -635,9 +635,9 @@ export class Matterbridge extends EventEmitter {
635
635
  this.uuid = randomUUID();
636
636
  await this.nodeContext.set('matterbridgeUuid', this.uuid);
637
637
  }
638
- this.log.debug(`Matterbridge UUID: ${CYAN}${this.uuid}${db}`);
638
+ this.log.debug(`Matterbridge UUID: ${CYAN}${this.uuid}${db} pid: ${CYAN}${process.pid}${db}`);
639
639
  this.log.notice('Matterbridge is starting...');
640
- this.log.debug(`Matterbridge logLevel: ${this.log.logLevel} fileLoger: ${this.fileLogger}.`);
640
+ this.log.debug(`Matterbridge logLevel: ${this.log.logLevel} fileLogger: ${this.fileLogger}.`);
641
641
  if (this.profile !== undefined)
642
642
  this.log.debug(`Matterbridge profile: ${this.profile}.`);
643
643
  if (hasParameter('matterlogger')) {
@@ -674,7 +674,7 @@ export class Matterbridge extends EventEmitter {
674
674
  this.matterFileLogger = true;
675
675
  }
676
676
  Logger.destinations.default.write = this.createDestinationMatterLogger(this.matterFileLogger);
677
- this.log.debug(`Matter logLevel: ${this.matterLogLevel} fileLoger: ${this.matterFileLogger}.`);
677
+ this.log.debug(`Matter logLevel: ${this.matterLogLevel} fileLogger: ${this.matterFileLogger}.`);
678
678
  const networkInterfaces = os.networkInterfaces();
679
679
  const availableAddresses = Object.entries(networkInterfaces);
680
680
  const availableInterfaceNames = Object.keys(networkInterfaces);
@@ -952,19 +952,21 @@ export class Matterbridge extends EventEmitter {
952
952
  }
953
953
  if ((getIntParameter('frontend') !== 0 || getIntParameter('frontend') === undefined) && !hasParameter('experimental-backend'))
954
954
  await this.frontend.start(getIntParameter('frontend'));
955
- try {
956
- await this.startMatterStorage();
957
- if (this.aggregatorSerialNumber && this.aggregatorUniqueId && this.matterStorageService) {
958
- const storageManager = await this.matterStorageService.open('Matterbridge');
959
- const storageContext = storageManager?.createContext('persist');
960
- await storageContext?.set('serialNumber', this.aggregatorSerialNumber);
961
- await storageContext?.set('uniqueId', this.aggregatorUniqueId);
962
- await storageManager.close();
955
+ if (!hasParameter('experimental-node')) {
956
+ try {
957
+ await this.startMatterStorage();
958
+ if (this.aggregatorSerialNumber && this.aggregatorUniqueId && this.matterStorageService) {
959
+ const storageManager = await this.matterStorageService.open('Matterbridge');
960
+ const storageContext = storageManager?.createContext('persist');
961
+ await storageContext?.set('serialNumber', this.aggregatorSerialNumber);
962
+ await storageContext?.set('uniqueId', this.aggregatorUniqueId);
963
+ await storageManager.close();
964
+ }
965
+ }
966
+ catch (error) {
967
+ this.log.fatal(`Fatal error creating matter storage: ${getErrorMessage(error)}`);
968
+ throw new Error(`Fatal error creating matter storage: ${getErrorMessage(error)}`, { cause: error });
963
969
  }
964
- }
965
- catch (error) {
966
- this.log.fatal(`Fatal error creating matter storage: ${getErrorMessage(error)}`);
967
- throw new Error(`Fatal error creating matter storage: ${getErrorMessage(error)}`, { cause: error });
968
970
  }
969
971
  if (hasParameter('reset') && getParameter('reset') === undefined) {
970
972
  this.initialized = true;
@@ -1053,14 +1055,14 @@ export class Matterbridge extends EventEmitter {
1053
1055
  this.bridgeMode = 'bridge';
1054
1056
  this.log.debug(`Starting matterbridge in mode ${this.bridgeMode}`);
1055
1057
  if (hasParameter('experimental-node')) {
1056
- this.log.notice(`***Starting experimental MatterNode in ${this.bridgeMode} mode...`);
1058
+ this.log.notice(`***Starting experimental MatterNode thread in ${this.bridgeMode} mode...`);
1057
1059
  this.server.request({
1058
1060
  type: 'manager_run',
1059
1061
  src: 'matterbridge',
1060
1062
  dst: 'manager',
1061
1063
  params: { name: 'RootNode', workerData: { threadName: 'RootNode', sharedMatterbridge: this.getSharedMatterbridge() } },
1062
1064
  });
1063
- this.log.notice(`***Started experimental MatterNode in ${this.bridgeMode} mode`);
1065
+ this.log.notice(`***Started experimental MatterNode thread in ${this.bridgeMode} mode`);
1064
1066
  return;
1065
1067
  }
1066
1068
  await this.startBridge();
@@ -1070,7 +1072,7 @@ export class Matterbridge extends EventEmitter {
1070
1072
  this.bridgeMode = 'childbridge';
1071
1073
  this.log.debug(`Starting matterbridge in mode ${this.bridgeMode}`);
1072
1074
  if (hasParameter('experimental-node')) {
1073
- this.log.notice(`***Starting experimental MatterNode in ${this.bridgeMode} mode...`);
1075
+ this.log.notice(`***Starting experimental MatterNode threads in ${this.bridgeMode} mode...`);
1074
1076
  for (const plugin of this.plugins.array().filter((p) => p.enabled)) {
1075
1077
  this.server.request({
1076
1078
  type: 'manager_run',
@@ -1079,7 +1081,7 @@ export class Matterbridge extends EventEmitter {
1079
1081
  params: { name: 'RootNode', workerData: { threadName: 'RootNode', sharedMatterbridge: this.getSharedMatterbridge(), pluginName: plugin.name } },
1080
1082
  });
1081
1083
  }
1082
- this.log.notice(`***Started experimental MatterNode in ${this.bridgeMode} mode`);
1084
+ this.log.notice(`***Started experimental MatterNode threads in ${this.bridgeMode} mode`);
1083
1085
  return;
1084
1086
  }
1085
1087
  await this.startChildbridge();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@matterbridge/core",
3
- "version": "3.11.1-dev-20261028-f791c14",
3
+ "version": "3.11.1-dev-20261030-e12e668",
4
4
  "description": "Matterbridge core library",
5
5
  "author": "https://github.com/Luligu",
6
6
  "homepage": "https://matterbridge.io/",
@@ -136,10 +136,10 @@
136
136
  },
137
137
  "dependencies": {
138
138
  "@matter/main": "0.18.0",
139
- "@matterbridge/dgram": "3.11.1-dev-20261028-f791c14",
140
- "@matterbridge/thread": "3.11.1-dev-20261028-f791c14",
141
- "@matterbridge/types": "3.11.1-dev-20261028-f791c14",
142
- "@matterbridge/utils": "3.11.1-dev-20261028-f791c14",
139
+ "@matterbridge/dgram": "3.11.1-dev-20261030-e12e668",
140
+ "@matterbridge/thread": "3.11.1-dev-20261030-e12e668",
141
+ "@matterbridge/types": "3.11.1-dev-20261030-e12e668",
142
+ "@matterbridge/utils": "3.11.1-dev-20261030-e12e668",
143
143
  "escape-html": "1.0.3",
144
144
  "express": "5.3.0",
145
145
  "express-rate-limit": "8.7.1",