matterbridge 3.3.6 → 3.3.7-dev-20251102-c85d574
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/CHANGELOG.md +17 -0
- package/dist/broadcastServer.js +1 -92
- package/dist/broadcastServerTypes.js +0 -24
- package/dist/cli.js +1 -97
- package/dist/cliEmitter.js +0 -37
- package/dist/cliHistory.js +0 -38
- package/dist/clusters/export.js +0 -2
- package/dist/defaultConfigSchema.js +0 -24
- package/dist/deviceManager.js +1 -124
- package/dist/devices/airConditioner.js +0 -57
- package/dist/devices/batteryStorage.js +1 -48
- package/dist/devices/cooktop.js +0 -55
- package/dist/devices/dishwasher.js +0 -57
- package/dist/devices/evse.js +10 -74
- package/dist/devices/export.js +0 -5
- package/dist/devices/extractorHood.js +0 -42
- package/dist/devices/heatPump.js +2 -50
- package/dist/devices/laundryDryer.js +3 -62
- package/dist/devices/laundryWasher.js +4 -70
- package/dist/devices/microwaveOven.js +5 -88
- package/dist/devices/oven.js +0 -85
- package/dist/devices/refrigerator.js +0 -102
- package/dist/devices/roboticVacuumCleaner.js +9 -100
- package/dist/devices/solarPower.js +0 -38
- package/dist/devices/speaker.js +0 -84
- package/dist/devices/temperatureControl.js +3 -24
- package/dist/devices/waterHeater.js +2 -82
- package/dist/dgram/coap.js +13 -126
- package/dist/dgram/dgram.js +2 -114
- package/dist/dgram/mb_coap.js +3 -41
- package/dist/dgram/mb_mdns.js +15 -80
- package/dist/dgram/mdns.js +137 -299
- package/dist/dgram/multicast.js +1 -62
- package/dist/dgram/unicast.js +0 -54
- package/dist/frontend.js +34 -431
- package/dist/frontendTypes.js +0 -45
- package/dist/helpers.js +0 -53
- package/dist/index.js +0 -25
- package/dist/logger/export.js +0 -1
- package/dist/matter/behaviors.js +0 -2
- package/dist/matter/clusters.js +0 -2
- package/dist/matter/devices.js +0 -2
- package/dist/matter/endpoints.js +0 -2
- package/dist/matter/export.js +0 -3
- package/dist/matter/types.js +0 -3
- package/dist/matterbridge.js +46 -828
- package/dist/matterbridgeAccessoryPlatform.js +0 -37
- package/dist/matterbridgeBehaviors.js +5 -68
- package/dist/matterbridgeDeviceTypes.js +17 -638
- package/dist/matterbridgeDynamicPlatform.js +0 -37
- package/dist/matterbridgeEndpoint.js +52 -1408
- package/dist/matterbridgeEndpointHelpers.js +19 -464
- package/dist/matterbridgePlatform.js +1 -341
- package/dist/matterbridgeTypes.js +0 -26
- package/dist/pluginManager.js +3 -319
- package/dist/shelly.js +7 -168
- package/dist/storage/export.js +0 -1
- package/dist/update.js +0 -69
- package/dist/utils/colorUtils.js +2 -97
- package/dist/utils/commandLine.js +0 -60
- package/dist/utils/copyDirectory.js +1 -38
- package/dist/utils/createDirectory.js +0 -33
- package/dist/utils/createZip.js +2 -47
- package/dist/utils/deepCopy.js +0 -39
- package/dist/utils/deepEqual.js +1 -72
- package/dist/utils/error.js +0 -41
- package/dist/utils/export.js +0 -1
- package/dist/utils/format.js +0 -49
- package/dist/utils/hex.js +0 -124
- package/dist/utils/inspector.js +1 -69
- package/dist/utils/isvalid.js +0 -101
- package/dist/utils/jestHelpers.js +3 -153
- package/dist/utils/network.js +5 -96
- package/dist/utils/spawn.js +0 -71
- package/dist/utils/tracker.js +1 -64
- package/dist/utils/wait.js +8 -60
- package/frontend/build/assets/index.js +4 -4
- package/frontend/package.json +1 -1
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -2
- package/dist/broadcastServer.d.ts +0 -112
- package/dist/broadcastServer.d.ts.map +0 -1
- package/dist/broadcastServer.js.map +0 -1
- package/dist/broadcastServerTypes.d.ts +0 -803
- package/dist/broadcastServerTypes.d.ts.map +0 -1
- package/dist/broadcastServerTypes.js.map +0 -1
- package/dist/cli.d.ts +0 -30
- package/dist/cli.d.ts.map +0 -1
- package/dist/cli.js.map +0 -1
- package/dist/cliEmitter.d.ts +0 -50
- package/dist/cliEmitter.d.ts.map +0 -1
- package/dist/cliEmitter.js.map +0 -1
- package/dist/cliHistory.d.ts +0 -48
- package/dist/cliHistory.d.ts.map +0 -1
- package/dist/cliHistory.js.map +0 -1
- package/dist/clusters/export.d.ts +0 -2
- package/dist/clusters/export.d.ts.map +0 -1
- package/dist/clusters/export.js.map +0 -1
- package/dist/defaultConfigSchema.d.ts +0 -28
- package/dist/defaultConfigSchema.d.ts.map +0 -1
- package/dist/defaultConfigSchema.js.map +0 -1
- package/dist/deviceManager.d.ts +0 -117
- package/dist/deviceManager.d.ts.map +0 -1
- package/dist/deviceManager.js.map +0 -1
- package/dist/devices/airConditioner.d.ts +0 -98
- package/dist/devices/airConditioner.d.ts.map +0 -1
- package/dist/devices/airConditioner.js.map +0 -1
- package/dist/devices/batteryStorage.d.ts +0 -48
- package/dist/devices/batteryStorage.d.ts.map +0 -1
- package/dist/devices/batteryStorage.js.map +0 -1
- package/dist/devices/cooktop.d.ts +0 -60
- package/dist/devices/cooktop.d.ts.map +0 -1
- package/dist/devices/cooktop.js.map +0 -1
- package/dist/devices/dishwasher.d.ts +0 -71
- package/dist/devices/dishwasher.d.ts.map +0 -1
- package/dist/devices/dishwasher.js.map +0 -1
- package/dist/devices/evse.d.ts +0 -76
- package/dist/devices/evse.d.ts.map +0 -1
- package/dist/devices/evse.js.map +0 -1
- package/dist/devices/export.d.ts +0 -17
- package/dist/devices/export.d.ts.map +0 -1
- package/dist/devices/export.js.map +0 -1
- package/dist/devices/extractorHood.d.ts +0 -46
- package/dist/devices/extractorHood.d.ts.map +0 -1
- package/dist/devices/extractorHood.js.map +0 -1
- package/dist/devices/heatPump.d.ts +0 -47
- package/dist/devices/heatPump.d.ts.map +0 -1
- package/dist/devices/heatPump.js.map +0 -1
- package/dist/devices/laundryDryer.d.ts +0 -67
- package/dist/devices/laundryDryer.d.ts.map +0 -1
- package/dist/devices/laundryDryer.js.map +0 -1
- package/dist/devices/laundryWasher.d.ts +0 -81
- package/dist/devices/laundryWasher.d.ts.map +0 -1
- package/dist/devices/laundryWasher.js.map +0 -1
- package/dist/devices/microwaveOven.d.ts +0 -168
- package/dist/devices/microwaveOven.d.ts.map +0 -1
- package/dist/devices/microwaveOven.js.map +0 -1
- package/dist/devices/oven.d.ts +0 -105
- package/dist/devices/oven.d.ts.map +0 -1
- package/dist/devices/oven.js.map +0 -1
- package/dist/devices/refrigerator.d.ts +0 -118
- package/dist/devices/refrigerator.d.ts.map +0 -1
- package/dist/devices/refrigerator.js.map +0 -1
- package/dist/devices/roboticVacuumCleaner.d.ts +0 -112
- package/dist/devices/roboticVacuumCleaner.d.ts.map +0 -1
- package/dist/devices/roboticVacuumCleaner.js.map +0 -1
- package/dist/devices/solarPower.d.ts +0 -40
- package/dist/devices/solarPower.d.ts.map +0 -1
- package/dist/devices/solarPower.js.map +0 -1
- package/dist/devices/speaker.d.ts +0 -87
- package/dist/devices/speaker.d.ts.map +0 -1
- package/dist/devices/speaker.js.map +0 -1
- package/dist/devices/temperatureControl.d.ts +0 -166
- package/dist/devices/temperatureControl.d.ts.map +0 -1
- package/dist/devices/temperatureControl.js.map +0 -1
- package/dist/devices/waterHeater.d.ts +0 -111
- package/dist/devices/waterHeater.d.ts.map +0 -1
- package/dist/devices/waterHeater.js.map +0 -1
- package/dist/dgram/coap.d.ts +0 -205
- package/dist/dgram/coap.d.ts.map +0 -1
- package/dist/dgram/coap.js.map +0 -1
- package/dist/dgram/dgram.d.ts +0 -141
- package/dist/dgram/dgram.d.ts.map +0 -1
- package/dist/dgram/dgram.js.map +0 -1
- package/dist/dgram/mb_coap.d.ts +0 -24
- package/dist/dgram/mb_coap.d.ts.map +0 -1
- package/dist/dgram/mb_coap.js.map +0 -1
- package/dist/dgram/mb_mdns.d.ts +0 -24
- package/dist/dgram/mb_mdns.d.ts.map +0 -1
- package/dist/dgram/mb_mdns.js.map +0 -1
- package/dist/dgram/mdns.d.ts +0 -290
- package/dist/dgram/mdns.d.ts.map +0 -1
- package/dist/dgram/mdns.js.map +0 -1
- package/dist/dgram/multicast.d.ts +0 -67
- package/dist/dgram/multicast.d.ts.map +0 -1
- package/dist/dgram/multicast.js.map +0 -1
- package/dist/dgram/unicast.d.ts +0 -56
- package/dist/dgram/unicast.d.ts.map +0 -1
- package/dist/dgram/unicast.js.map +0 -1
- package/dist/frontend.d.ts +0 -236
- package/dist/frontend.d.ts.map +0 -1
- package/dist/frontend.js.map +0 -1
- package/dist/frontendTypes.d.ts +0 -529
- package/dist/frontendTypes.d.ts.map +0 -1
- package/dist/frontendTypes.js.map +0 -1
- package/dist/helpers.d.ts +0 -48
- package/dist/helpers.d.ts.map +0 -1
- package/dist/helpers.js.map +0 -1
- package/dist/index.d.ts +0 -33
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/logger/export.d.ts +0 -2
- package/dist/logger/export.d.ts.map +0 -1
- package/dist/logger/export.js.map +0 -1
- package/dist/matter/behaviors.d.ts +0 -2
- package/dist/matter/behaviors.d.ts.map +0 -1
- package/dist/matter/behaviors.js.map +0 -1
- package/dist/matter/clusters.d.ts +0 -2
- package/dist/matter/clusters.d.ts.map +0 -1
- package/dist/matter/clusters.js.map +0 -1
- package/dist/matter/devices.d.ts +0 -2
- package/dist/matter/devices.d.ts.map +0 -1
- package/dist/matter/devices.js.map +0 -1
- package/dist/matter/endpoints.d.ts +0 -2
- package/dist/matter/endpoints.d.ts.map +0 -1
- package/dist/matter/endpoints.js.map +0 -1
- package/dist/matter/export.d.ts +0 -5
- package/dist/matter/export.d.ts.map +0 -1
- package/dist/matter/export.js.map +0 -1
- package/dist/matter/types.d.ts +0 -3
- package/dist/matter/types.d.ts.map +0 -1
- package/dist/matter/types.js.map +0 -1
- package/dist/matterbridge.d.ts +0 -476
- package/dist/matterbridge.d.ts.map +0 -1
- package/dist/matterbridge.js.map +0 -1
- package/dist/matterbridgeAccessoryPlatform.d.ts +0 -42
- package/dist/matterbridgeAccessoryPlatform.d.ts.map +0 -1
- package/dist/matterbridgeAccessoryPlatform.js.map +0 -1
- package/dist/matterbridgeBehaviors.d.ts +0 -2404
- package/dist/matterbridgeBehaviors.d.ts.map +0 -1
- package/dist/matterbridgeBehaviors.js.map +0 -1
- package/dist/matterbridgeDeviceTypes.d.ts +0 -770
- package/dist/matterbridgeDeviceTypes.d.ts.map +0 -1
- package/dist/matterbridgeDeviceTypes.js.map +0 -1
- package/dist/matterbridgeDynamicPlatform.d.ts +0 -42
- package/dist/matterbridgeDynamicPlatform.d.ts.map +0 -1
- package/dist/matterbridgeDynamicPlatform.js.map +0 -1
- package/dist/matterbridgeEndpoint.d.ts +0 -1556
- package/dist/matterbridgeEndpoint.d.ts.map +0 -1
- package/dist/matterbridgeEndpoint.js.map +0 -1
- package/dist/matterbridgeEndpointHelpers.d.ts +0 -758
- package/dist/matterbridgeEndpointHelpers.d.ts.map +0 -1
- package/dist/matterbridgeEndpointHelpers.js.map +0 -1
- package/dist/matterbridgePlatform.d.ts +0 -402
- package/dist/matterbridgePlatform.d.ts.map +0 -1
- package/dist/matterbridgePlatform.js.map +0 -1
- package/dist/matterbridgeTypes.d.ts +0 -226
- package/dist/matterbridgeTypes.d.ts.map +0 -1
- package/dist/matterbridgeTypes.js.map +0 -1
- package/dist/pluginManager.d.ts +0 -347
- package/dist/pluginManager.d.ts.map +0 -1
- package/dist/pluginManager.js.map +0 -1
- package/dist/shelly.d.ts +0 -174
- package/dist/shelly.d.ts.map +0 -1
- package/dist/shelly.js.map +0 -1
- package/dist/storage/export.d.ts +0 -2
- package/dist/storage/export.d.ts.map +0 -1
- package/dist/storage/export.js.map +0 -1
- package/dist/update.d.ts +0 -75
- package/dist/update.d.ts.map +0 -1
- package/dist/update.js.map +0 -1
- package/dist/utils/colorUtils.d.ts +0 -101
- package/dist/utils/colorUtils.d.ts.map +0 -1
- package/dist/utils/colorUtils.js.map +0 -1
- package/dist/utils/commandLine.d.ts +0 -66
- package/dist/utils/commandLine.d.ts.map +0 -1
- package/dist/utils/commandLine.js.map +0 -1
- package/dist/utils/copyDirectory.d.ts +0 -33
- package/dist/utils/copyDirectory.d.ts.map +0 -1
- package/dist/utils/copyDirectory.js.map +0 -1
- package/dist/utils/createDirectory.d.ts +0 -34
- package/dist/utils/createDirectory.d.ts.map +0 -1
- package/dist/utils/createDirectory.js.map +0 -1
- package/dist/utils/createZip.d.ts +0 -39
- package/dist/utils/createZip.d.ts.map +0 -1
- package/dist/utils/createZip.js.map +0 -1
- package/dist/utils/deepCopy.d.ts +0 -32
- package/dist/utils/deepCopy.d.ts.map +0 -1
- package/dist/utils/deepCopy.js.map +0 -1
- package/dist/utils/deepEqual.d.ts +0 -54
- package/dist/utils/deepEqual.d.ts.map +0 -1
- package/dist/utils/deepEqual.js.map +0 -1
- package/dist/utils/error.d.ts +0 -44
- package/dist/utils/error.d.ts.map +0 -1
- package/dist/utils/error.js.map +0 -1
- package/dist/utils/export.d.ts +0 -13
- package/dist/utils/export.d.ts.map +0 -1
- package/dist/utils/export.js.map +0 -1
- package/dist/utils/format.d.ts +0 -53
- package/dist/utils/format.d.ts.map +0 -1
- package/dist/utils/format.js.map +0 -1
- package/dist/utils/hex.d.ts +0 -89
- package/dist/utils/hex.d.ts.map +0 -1
- package/dist/utils/hex.js.map +0 -1
- package/dist/utils/inspector.d.ts +0 -87
- package/dist/utils/inspector.d.ts.map +0 -1
- package/dist/utils/inspector.js.map +0 -1
- package/dist/utils/isvalid.d.ts +0 -103
- package/dist/utils/isvalid.d.ts.map +0 -1
- package/dist/utils/isvalid.js.map +0 -1
- package/dist/utils/jestHelpers.d.ts +0 -139
- package/dist/utils/jestHelpers.d.ts.map +0 -1
- package/dist/utils/jestHelpers.js.map +0 -1
- package/dist/utils/network.d.ts +0 -101
- package/dist/utils/network.d.ts.map +0 -1
- package/dist/utils/network.js.map +0 -1
- package/dist/utils/spawn.d.ts +0 -35
- package/dist/utils/spawn.d.ts.map +0 -1
- package/dist/utils/spawn.js.map +0 -1
- package/dist/utils/tracker.d.ts +0 -108
- package/dist/utils/tracker.d.ts.map +0 -1
- package/dist/utils/tracker.js.map +0 -1
- package/dist/utils/wait.d.ts +0 -54
- package/dist/utils/wait.d.ts.map +0 -1
- package/dist/utils/wait.js.map +0 -1
package/dist/matterbridge.js
CHANGED
|
@@ -1,48 +1,19 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file contains the class Matterbridge.
|
|
3
|
-
*
|
|
4
|
-
* @file matterbridge.ts
|
|
5
|
-
* @author Luca Liguori
|
|
6
|
-
* @created 2023-12-29
|
|
7
|
-
* @version 1.6.0
|
|
8
|
-
* @license Apache-2.0
|
|
9
|
-
*
|
|
10
|
-
* Copyright 2023, 2024, 2025 Luca Liguori.
|
|
11
|
-
*
|
|
12
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
13
|
-
* you may not use this file except in compliance with the License.
|
|
14
|
-
* You may obtain a copy of the License at
|
|
15
|
-
*
|
|
16
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
17
|
-
*
|
|
18
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
19
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
20
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
21
|
-
* See the License for the specific language governing permissions and
|
|
22
|
-
* limitations under the License.
|
|
23
|
-
*/
|
|
24
|
-
// eslint-disable-next-line no-console
|
|
25
1
|
if (process.argv.includes('--loader') || process.argv.includes('-loader'))
|
|
26
2
|
console.log('\u001B[32mMatterbridge loaded.\u001B[40;0m');
|
|
27
|
-
// Node.js modules
|
|
28
3
|
import os from 'node:os';
|
|
29
4
|
import path from 'node:path';
|
|
30
5
|
import { promises as fs } from 'node:fs';
|
|
31
6
|
import EventEmitter from 'node:events';
|
|
32
7
|
import { inspect } from 'node:util';
|
|
33
|
-
// AnsiLogger module
|
|
34
8
|
import { AnsiLogger, UNDERLINE, UNDERLINEOFF, db, debugStringify, BRIGHT, RESET, er, nf, rs, wr, RED, GREEN, zb, CYAN, nt, BLUE, or } from 'node-ansi-logger';
|
|
35
|
-
// NodeStorage module
|
|
36
9
|
import { NodeStorageManager } from 'node-persist-manager';
|
|
37
|
-
|
|
38
|
-
import '@matter/nodejs'; // Set up Node.js environment for matter.js
|
|
10
|
+
import '@matter/nodejs';
|
|
39
11
|
import { Logger, LogLevel as MatterLogLevel, LogFormat as MatterLogFormat, UINT32_MAX, UINT16_MAX, Crypto, Environment, StorageService } from '@matter/general';
|
|
40
12
|
import { FabricAction, PaseClient } from '@matter/protocol';
|
|
41
13
|
import { Endpoint, ServerNode } from '@matter/node';
|
|
42
14
|
import { DeviceTypeId, VendorId } from '@matter/types/datatype';
|
|
43
15
|
import { AggregatorEndpoint } from '@matter/node/endpoints';
|
|
44
16
|
import { BasicInformationServer } from '@matter/node/behaviors/basic-information';
|
|
45
|
-
// Matterbridge
|
|
46
17
|
import { getParameter, getIntParameter, hasParameter } from './utils/commandLine.js';
|
|
47
18
|
import { copyDirectory } from './utils/copyDirectory.js';
|
|
48
19
|
import { createDirectory } from './utils/createDirectory.js';
|
|
@@ -57,27 +28,19 @@ import { bridge } from './matterbridgeDeviceTypes.js';
|
|
|
57
28
|
import { Frontend } from './frontend.js';
|
|
58
29
|
import { addVirtualDevices } from './helpers.js';
|
|
59
30
|
import { BroadcastServer } from './broadcastServer.js';
|
|
60
|
-
/**
|
|
61
|
-
* Represents the Matterbridge application.
|
|
62
|
-
*/
|
|
63
31
|
export class Matterbridge extends EventEmitter {
|
|
64
|
-
/** Matterbridge system information */
|
|
65
32
|
systemInformation = {
|
|
66
|
-
// Network properties
|
|
67
33
|
interfaceName: '',
|
|
68
34
|
macAddress: '',
|
|
69
35
|
ipv4Address: '',
|
|
70
36
|
ipv6Address: '',
|
|
71
|
-
// Node.js properties
|
|
72
37
|
nodeVersion: '',
|
|
73
|
-
// Fixed system properties
|
|
74
38
|
hostname: '',
|
|
75
39
|
user: '',
|
|
76
40
|
osType: '',
|
|
77
41
|
osRelease: '',
|
|
78
42
|
osPlatform: '',
|
|
79
43
|
osArch: '',
|
|
80
|
-
// Cpu and memory properties
|
|
81
44
|
totalMemory: '',
|
|
82
45
|
freeMemory: '',
|
|
83
46
|
systemUptime: '',
|
|
@@ -88,7 +51,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
88
51
|
heapTotal: '',
|
|
89
52
|
heapUsed: '',
|
|
90
53
|
};
|
|
91
|
-
// Matterbridge settings
|
|
92
54
|
homeDirectory = '';
|
|
93
55
|
rootDirectory = '';
|
|
94
56
|
matterbridgeDirectory = '';
|
|
@@ -103,15 +65,10 @@ export class Matterbridge extends EventEmitter {
|
|
|
103
65
|
restartMode = '';
|
|
104
66
|
virtualMode = 'outlet';
|
|
105
67
|
profile = getParameter('profile');
|
|
106
|
-
|
|
107
|
-
log = new AnsiLogger({ logName: 'Matterbridge', logTimestampFormat: 4 /* TimestampFormat.TIME_MILLIS */, logLevel: hasParameter('debug') ? "debug" /* LogLevel.DEBUG */ : "info" /* LogLevel.INFO */ });
|
|
108
|
-
/** Whether to log to a file */
|
|
68
|
+
log = new AnsiLogger({ logName: 'Matterbridge', logTimestampFormat: 4, logLevel: hasParameter('debug') ? "debug" : "info" });
|
|
109
69
|
fileLogger = false;
|
|
110
|
-
|
|
111
|
-
matterLog = new AnsiLogger({ logName: 'Matter', logTimestampFormat: 4 /* TimestampFormat.TIME_MILLIS */, logLevel: "debug" /* LogLevel.DEBUG */ });
|
|
112
|
-
/** Whether to log Matter to a file */
|
|
70
|
+
matterLog = new AnsiLogger({ logName: 'Matter', logTimestampFormat: 4, logLevel: "debug" });
|
|
113
71
|
matterFileLogger = false;
|
|
114
|
-
// Frontend settings
|
|
115
72
|
readOnly = hasParameter('readonly') || hasParameter('shelly');
|
|
116
73
|
shellyBoard = hasParameter('shelly');
|
|
117
74
|
shellySysUpdate = false;
|
|
@@ -119,18 +76,12 @@ export class Matterbridge extends EventEmitter {
|
|
|
119
76
|
restartRequired = false;
|
|
120
77
|
fixedRestartRequired = false;
|
|
121
78
|
updateRequired = false;
|
|
122
|
-
// Managers
|
|
123
79
|
plugins = new PluginManager(this);
|
|
124
80
|
devices = new DeviceManager();
|
|
125
|
-
// Frontend
|
|
126
81
|
frontend = new Frontend(this);
|
|
127
|
-
/** Matterbridge node storage manager created in the directory 'storage' in matterbridgeDirectory */
|
|
128
82
|
nodeStorage;
|
|
129
|
-
/** Matterbridge node context created with name 'matterbridge' */
|
|
130
83
|
nodeContext;
|
|
131
|
-
/** The main instance of the Matterbridge class (singleton) */
|
|
132
84
|
static instance;
|
|
133
|
-
// Instance properties
|
|
134
85
|
shutdown = false;
|
|
135
86
|
failCountLimit = hasParameter('shelly') ? 600 : 120;
|
|
136
87
|
hasCleanupStarted = false;
|
|
@@ -145,32 +96,19 @@ export class Matterbridge extends EventEmitter {
|
|
|
145
96
|
sigtermHandler;
|
|
146
97
|
exceptionHandler;
|
|
147
98
|
rejectionHandler;
|
|
148
|
-
/** Matter environment default */
|
|
149
99
|
environment = Environment.default;
|
|
150
|
-
/** Matter storage service from environment default */
|
|
151
100
|
matterStorageService;
|
|
152
|
-
/** Matter storage manager created with name 'Matterbridge' */
|
|
153
101
|
matterStorageManager;
|
|
154
|
-
/** Matter matterbridge storage context created in the storage manager with name 'persist' */
|
|
155
102
|
matterbridgeContext;
|
|
156
103
|
controllerContext;
|
|
157
|
-
/** Matter mdns interface e.g. 'eth0' or 'wlan0' or 'Wi-Fi' */
|
|
158
104
|
mdnsInterface;
|
|
159
|
-
/** Matter listeningAddressIpv4 address */
|
|
160
105
|
ipv4Address;
|
|
161
|
-
/** Matter listeningAddressIpv6 address */
|
|
162
106
|
ipv6Address;
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
/** Matter commissioning discriminator */
|
|
168
|
-
discriminator; // first server node discriminator
|
|
169
|
-
/** Matter device certification */
|
|
170
|
-
certification; // device certification
|
|
171
|
-
/** Matter server node in bridge mode */
|
|
107
|
+
port;
|
|
108
|
+
passcode;
|
|
109
|
+
discriminator;
|
|
110
|
+
certification;
|
|
172
111
|
serverNode;
|
|
173
|
-
/** Matter aggregator node in bridge mode */
|
|
174
112
|
aggregatorNode;
|
|
175
113
|
aggregatorVendorId = VendorId(getIntParameter('vendorId') ?? 0xfff1);
|
|
176
114
|
aggregatorVendorName = getParameter('vendorName') ?? 'Matterbridge';
|
|
@@ -179,11 +117,8 @@ export class Matterbridge extends EventEmitter {
|
|
|
179
117
|
aggregatorDeviceType = DeviceTypeId(getIntParameter('deviceType') ?? bridge.code);
|
|
180
118
|
aggregatorSerialNumber = getParameter('serialNumber');
|
|
181
119
|
aggregatorUniqueId = getParameter('uniqueId');
|
|
182
|
-
/** Advertising nodes map: time advertising started keyed by storeId */
|
|
183
120
|
advertisingNodes = new Map();
|
|
184
|
-
/** Broadcast server */
|
|
185
121
|
server;
|
|
186
|
-
/** We load asyncronously so is private */
|
|
187
122
|
constructor() {
|
|
188
123
|
super();
|
|
189
124
|
this.log.logNameColor = '\x1b[38;5;115m';
|
|
@@ -219,19 +154,8 @@ export class Matterbridge extends EventEmitter {
|
|
|
219
154
|
}
|
|
220
155
|
}
|
|
221
156
|
}
|
|
222
|
-
//* ************************************************************************************************************************************ */
|
|
223
|
-
// loadInstance() and cleanup() methods */
|
|
224
|
-
//* ************************************************************************************************************************************ */
|
|
225
|
-
/**
|
|
226
|
-
* Loads an instance of the Matterbridge class.
|
|
227
|
-
* If an instance already exists, return that instance.
|
|
228
|
-
*
|
|
229
|
-
* @param {boolean} initialize - Whether to initialize the Matterbridge instance after loading. Defaults to false.
|
|
230
|
-
* @returns {Matterbridge} A promise that resolves to the Matterbridge instance.
|
|
231
|
-
*/
|
|
232
157
|
static async loadInstance(initialize = false) {
|
|
233
158
|
if (!Matterbridge.instance) {
|
|
234
|
-
// eslint-disable-next-line no-console
|
|
235
159
|
if (hasParameter('debug'))
|
|
236
160
|
console.log(GREEN + 'Creating a new instance of Matterbridge.', initialize ? 'Initializing...' : 'Not initializing...', rs);
|
|
237
161
|
Matterbridge.instance = new Matterbridge();
|
|
@@ -240,131 +164,62 @@ export class Matterbridge extends EventEmitter {
|
|
|
240
164
|
}
|
|
241
165
|
return Matterbridge.instance;
|
|
242
166
|
}
|
|
243
|
-
/**
|
|
244
|
-
* Call cleanup() and dispose MdnsService. Will be removed since matter.js 0.15.6 dispose MdnsService.
|
|
245
|
-
*
|
|
246
|
-
* @param {number} [timeout] - The timeout duration to wait for the cleanup to complete in milliseconds. Default is 1000.
|
|
247
|
-
* @param {number} [pause] - The pause duration after the cleanup in milliseconds. Default is 250.
|
|
248
|
-
*
|
|
249
|
-
* @deprecated This method is deprecated and is ONLY used for jest tests.
|
|
250
|
-
*/
|
|
251
167
|
async destroyInstance(timeout = 1000, pause = 250) {
|
|
252
168
|
this.log.info(`Destroy instance...`);
|
|
253
|
-
// Save server nodes to close
|
|
254
|
-
/*
|
|
255
|
-
const servers: ServerNode<ServerNode.RootEndpoint>[] = [];
|
|
256
|
-
if (this.bridgeMode === 'bridge') {
|
|
257
|
-
if (this.serverNode) servers.push(this.serverNode);
|
|
258
|
-
}
|
|
259
|
-
if (this.bridgeMode === 'childbridge' && this.plugins !== undefined) {
|
|
260
|
-
for (const plugin of this.plugins.array()) {
|
|
261
|
-
if (plugin.serverNode) servers.push(plugin.serverNode);
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
if (this.devices !== undefined) {
|
|
265
|
-
for (const device of this.devices.array()) {
|
|
266
|
-
if (device.mode === 'server' && device.serverNode) servers.push(device.serverNode);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
*/
|
|
270
|
-
// Let any already‐queued microtasks run first
|
|
271
|
-
// await Promise.resolve();
|
|
272
|
-
// Wait for the cleanup to finish
|
|
273
|
-
// await wait(pause, 'destroyInstance start', true);
|
|
274
|
-
// Cleanup
|
|
275
169
|
await this.cleanup('destroying instance...', false, timeout);
|
|
276
|
-
// Close servers mdns service
|
|
277
|
-
/*
|
|
278
|
-
this.log.info(`Dispose ${servers.length} MdnsService...`);
|
|
279
|
-
for (const server of servers) {
|
|
280
|
-
// await server.env.get(MdnsService)[Symbol.asyncDispose]();
|
|
281
|
-
this.log.info(`Closed ${server.id} MdnsService`);
|
|
282
|
-
}
|
|
283
|
-
*/
|
|
284
|
-
// Let any already‐queued microtasks run first
|
|
285
|
-
// await Promise.resolve();
|
|
286
|
-
// Wait for the cleanup to finish
|
|
287
170
|
if (pause)
|
|
288
171
|
await wait(pause, 'destroyInstance stop', true);
|
|
289
172
|
}
|
|
290
|
-
/**
|
|
291
|
-
* Initializes the Matterbridge application.
|
|
292
|
-
*
|
|
293
|
-
* @remarks
|
|
294
|
-
* This method performs the necessary setup and initialization steps for the Matterbridge application.
|
|
295
|
-
* It displays the help information if the 'help' parameter is provided, sets up the logger, checks the
|
|
296
|
-
* node version, registers signal handlers, initializes storage, and parses the command line.
|
|
297
|
-
*
|
|
298
|
-
* @returns {Promise<void>} A Promise that resolves when the initialization is complete.
|
|
299
|
-
*/
|
|
300
173
|
async initialize() {
|
|
301
|
-
// for (let i = 1; i <= 255; i++) console.log(`\x1b[38;5;${i}mColor: ${i}`);
|
|
302
|
-
// Emit the initialize_started event
|
|
303
174
|
this.emit('initialize_started');
|
|
304
|
-
// Set the restart mode
|
|
305
175
|
if (hasParameter('service'))
|
|
306
176
|
this.restartMode = 'service';
|
|
307
177
|
if (hasParameter('docker'))
|
|
308
178
|
this.restartMode = 'docker';
|
|
309
|
-
// Set the matterbridge home directory
|
|
310
179
|
this.homeDirectory = getParameter('homedir') ?? os.homedir();
|
|
311
180
|
await createDirectory(this.homeDirectory, 'Matterbridge Home Directory', this.log);
|
|
312
|
-
// Set the matterbridge directory
|
|
313
181
|
this.matterbridgeDirectory = this.profile ? path.join(this.homeDirectory, '.matterbridge', 'profiles', this.profile) : path.join(this.homeDirectory, '.matterbridge');
|
|
314
182
|
await createDirectory(this.matterbridgeDirectory, 'Matterbridge Directory', this.log);
|
|
315
183
|
await createDirectory(path.join(this.matterbridgeDirectory, 'certs'), 'Matterbridge Frontend Certificate Directory', this.log);
|
|
316
184
|
await createDirectory(path.join(this.matterbridgeDirectory, 'uploads'), 'Matterbridge Frontend Uploads Directory', this.log);
|
|
317
|
-
// Set the matterbridge plugin directory
|
|
318
185
|
this.matterbridgePluginDirectory = this.profile ? path.join(this.homeDirectory, 'Matterbridge', 'profiles', this.profile) : path.join(this.homeDirectory, 'Matterbridge');
|
|
319
186
|
await createDirectory(this.matterbridgePluginDirectory, 'Matterbridge Plugin Directory', this.log);
|
|
320
|
-
// Set the matterbridge cert directory
|
|
321
187
|
this.matterbridgeCertDirectory = this.profile ? path.join(this.homeDirectory, '.mattercert', 'profiles', this.profile) : path.join(this.homeDirectory, '.mattercert');
|
|
322
188
|
await createDirectory(this.matterbridgeCertDirectory, 'Matterbridge Matter Certificate Directory', this.log);
|
|
323
|
-
// Set the matterbridge root directory
|
|
324
189
|
const { fileURLToPath } = await import('node:url');
|
|
325
190
|
const currentFileDirectory = path.dirname(fileURLToPath(import.meta.url));
|
|
326
|
-
this.rootDirectory = path.resolve(currentFileDirectory, '../');
|
|
327
|
-
// Setup the matter environment with default values
|
|
191
|
+
this.rootDirectory = path.resolve(currentFileDirectory, '../');
|
|
328
192
|
this.environment.vars.set('log.level', MatterLogLevel.INFO);
|
|
329
193
|
this.environment.vars.set('log.format', MatterLogFormat.ANSI);
|
|
330
194
|
this.environment.vars.set('path.root', path.join(this.matterbridgeDirectory, MATTER_STORAGE_NAME));
|
|
331
195
|
this.environment.vars.set('runtime.signals', false);
|
|
332
196
|
this.environment.vars.set('runtime.exitcode', false);
|
|
333
|
-
// Register process handlers
|
|
334
197
|
this.registerProcessHandlers();
|
|
335
|
-
// Initialize nodeStorage and nodeContext
|
|
336
198
|
try {
|
|
337
199
|
this.log.debug(`Creating node storage manager: ${CYAN}${NODE_STORAGE_DIR}${db}`);
|
|
338
200
|
this.nodeStorage = new NodeStorageManager({ dir: path.join(this.matterbridgeDirectory, NODE_STORAGE_DIR), writeQueue: false, expiredInterval: undefined, logging: false });
|
|
339
201
|
this.log.debug('Creating node storage context for matterbridge');
|
|
340
202
|
this.nodeContext = await this.nodeStorage.createStorage('matterbridge');
|
|
341
|
-
// TODO: Remove this code when node-persist-manager is updated
|
|
342
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
343
203
|
const keys = (await this.nodeStorage?.storage.keys());
|
|
344
204
|
for (const key of keys) {
|
|
345
205
|
this.log.debug(`Checking node storage manager key: ${CYAN}${key}${db}`);
|
|
346
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
347
206
|
await this.nodeStorage?.storage.get(key);
|
|
348
207
|
}
|
|
349
208
|
const storages = await this.nodeStorage.getStorageNames();
|
|
350
209
|
for (const storage of storages) {
|
|
351
210
|
this.log.debug(`Checking storage: ${CYAN}${storage}${db}`);
|
|
352
211
|
const nodeContext = await this.nodeStorage?.createStorage(storage);
|
|
353
|
-
// TODO: Remove this code when node-persist-manager is updated
|
|
354
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
355
212
|
const keys = (await nodeContext?.storage.keys());
|
|
356
213
|
keys.forEach(async (key) => {
|
|
357
214
|
this.log.debug(`Checking key: ${CYAN}${storage}:${key}${db}`);
|
|
358
215
|
await nodeContext?.get(key);
|
|
359
216
|
});
|
|
360
217
|
}
|
|
361
|
-
// Creating a backup of the node storage since it is not corrupted
|
|
362
218
|
this.log.debug('Creating node storage backup...');
|
|
363
219
|
await copyDirectory(path.join(this.matterbridgeDirectory, NODE_STORAGE_DIR), path.join(this.matterbridgeDirectory, NODE_STORAGE_DIR + '.backup'));
|
|
364
220
|
this.log.debug('Created node storage backup');
|
|
365
221
|
}
|
|
366
222
|
catch (error) {
|
|
367
|
-
// Restoring the backup of the node storage since it is corrupted
|
|
368
223
|
this.log.error(`Error creating node storage manager and context: ${error instanceof Error ? error.message : error}`);
|
|
369
224
|
if (hasParameter('norestore')) {
|
|
370
225
|
this.log.fatal(`The matterbridge storage is corrupted. Found -norestore parameter: exiting...`);
|
|
@@ -378,19 +233,14 @@ export class Matterbridge extends EventEmitter {
|
|
|
378
233
|
if (!this.nodeStorage || !this.nodeContext) {
|
|
379
234
|
throw new Error('Fatal error creating node storage manager and context for matterbridge');
|
|
380
235
|
}
|
|
381
|
-
// Set the first port to use for the commissioning server (will be incremented in childbridge mode)
|
|
382
236
|
this.port = getIntParameter('port') ?? (await this.nodeContext.get('matterport', 5540)) ?? 5540;
|
|
383
|
-
// Set the first passcode to use for the commissioning server (will be incremented in childbridge mode)
|
|
384
237
|
this.passcode = getIntParameter('passcode') ?? (await this.nodeContext.get('matterpasscode')) ?? PaseClient.generateRandomPasscode(this.environment.get(Crypto));
|
|
385
|
-
// Set the first discriminator to use for the commissioning server (will be incremented in childbridge mode)
|
|
386
238
|
this.discriminator = getIntParameter('discriminator') ?? (await this.nodeContext.get('matterdiscriminator')) ?? PaseClient.generateRandomDiscriminator(this.environment.get(Crypto));
|
|
387
|
-
// Certificate management
|
|
388
239
|
const pairingFilePath = path.join(this.matterbridgeCertDirectory, 'pairing.json');
|
|
389
240
|
try {
|
|
390
241
|
await fs.access(pairingFilePath, fs.constants.R_OK);
|
|
391
242
|
const pairingFileContent = await fs.readFile(pairingFilePath, 'utf8');
|
|
392
243
|
const pairingFileJson = JSON.parse(pairingFileContent);
|
|
393
|
-
// Set the vendorId, vendorName, productId, productName, deviceType, serialNumber, uniqueId if they are present in the pairing file
|
|
394
244
|
if (isValidNumber(pairingFileJson.vendorId)) {
|
|
395
245
|
this.aggregatorVendorId = VendorId(pairingFileJson.vendorId);
|
|
396
246
|
this.log.info(`Pairing file ${CYAN}${pairingFilePath}${nf} found. Using vendorId ${CYAN}${this.aggregatorVendorId}${nf} from pairing file.`);
|
|
@@ -419,13 +269,11 @@ export class Matterbridge extends EventEmitter {
|
|
|
419
269
|
this.aggregatorUniqueId = pairingFileJson.uniqueId;
|
|
420
270
|
this.log.info(`Pairing file ${CYAN}${pairingFilePath}${nf} found. Using uniqueId ${CYAN}${this.aggregatorUniqueId}${nf} from pairing file.`);
|
|
421
271
|
}
|
|
422
|
-
// Override the passcode and discriminator if they are present in the pairing file
|
|
423
272
|
if (isValidNumber(pairingFileJson.passcode) && isValidNumber(pairingFileJson.discriminator)) {
|
|
424
273
|
this.passcode = pairingFileJson.passcode;
|
|
425
274
|
this.discriminator = pairingFileJson.discriminator;
|
|
426
275
|
this.log.info(`Pairing file ${CYAN}${pairingFilePath}${nf} found. Using passcode ${CYAN}${this.passcode}${nf} and discriminator ${CYAN}${this.discriminator}${nf} from pairing file.`);
|
|
427
276
|
}
|
|
428
|
-
// Set the certification for matter.js if it is present in the pairing file
|
|
429
277
|
if (pairingFileJson.privateKey && pairingFileJson.certificate && pairingFileJson.intermediateCertificate && pairingFileJson.declaration) {
|
|
430
278
|
const { hexToBuffer } = await import('./utils/hex.js');
|
|
431
279
|
this.certification = {
|
|
@@ -440,43 +288,40 @@ export class Matterbridge extends EventEmitter {
|
|
|
440
288
|
catch (error) {
|
|
441
289
|
this.log.debug(`Pairing file ${CYAN}${pairingFilePath}${db} not found: ${error instanceof Error ? error.message : error}`);
|
|
442
290
|
}
|
|
443
|
-
// Store the passcode, discriminator and port in the node context
|
|
444
291
|
await this.nodeContext.set('matterport', this.port);
|
|
445
292
|
await this.nodeContext.set('matterpasscode', this.passcode);
|
|
446
293
|
await this.nodeContext.set('matterdiscriminator', this.discriminator);
|
|
447
294
|
this.log.debug(`Initializing server node for Matterbridge on port ${this.port} with passcode ${this.passcode} and discriminator ${this.discriminator}`);
|
|
448
|
-
// Set matterbridge logger level (context: matterbridgeLogLevel)
|
|
449
295
|
if (hasParameter('logger')) {
|
|
450
296
|
const level = getParameter('logger');
|
|
451
297
|
if (level === 'debug') {
|
|
452
|
-
this.log.logLevel = "debug"
|
|
298
|
+
this.log.logLevel = "debug";
|
|
453
299
|
}
|
|
454
300
|
else if (level === 'info') {
|
|
455
|
-
this.log.logLevel = "info"
|
|
301
|
+
this.log.logLevel = "info";
|
|
456
302
|
}
|
|
457
303
|
else if (level === 'notice') {
|
|
458
|
-
this.log.logLevel = "notice"
|
|
304
|
+
this.log.logLevel = "notice";
|
|
459
305
|
}
|
|
460
306
|
else if (level === 'warn') {
|
|
461
|
-
this.log.logLevel = "warn"
|
|
307
|
+
this.log.logLevel = "warn";
|
|
462
308
|
}
|
|
463
309
|
else if (level === 'error') {
|
|
464
|
-
this.log.logLevel = "error"
|
|
310
|
+
this.log.logLevel = "error";
|
|
465
311
|
}
|
|
466
312
|
else if (level === 'fatal') {
|
|
467
|
-
this.log.logLevel = "fatal"
|
|
313
|
+
this.log.logLevel = "fatal";
|
|
468
314
|
}
|
|
469
315
|
else {
|
|
470
316
|
this.log.warn(`Invalid matterbridge logger level: ${level}. Using default level "info".`);
|
|
471
|
-
this.log.logLevel = "info"
|
|
317
|
+
this.log.logLevel = "info";
|
|
472
318
|
}
|
|
473
319
|
}
|
|
474
320
|
else {
|
|
475
|
-
this.log.logLevel = await this.nodeContext.get('matterbridgeLogLevel', this.shellyBoard ? "notice"
|
|
321
|
+
this.log.logLevel = await this.nodeContext.get('matterbridgeLogLevel', this.shellyBoard ? "notice" : "info");
|
|
476
322
|
}
|
|
477
323
|
this.frontend.logLevel = this.log.logLevel;
|
|
478
324
|
MatterbridgeEndpoint.logLevel = this.log.logLevel;
|
|
479
|
-
// Create the file logger for matterbridge (context: matterbridgeFileLog)
|
|
480
325
|
if (hasParameter('filelogger') || (await this.nodeContext.get('matterbridgeFileLog', false))) {
|
|
481
326
|
AnsiLogger.setGlobalLogfile(path.join(this.matterbridgeDirectory, MATTERBRIDGE_LOGGER_FILE), this.log.logLevel, true);
|
|
482
327
|
this.fileLogger = true;
|
|
@@ -485,7 +330,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
485
330
|
this.log.debug(`Matterbridge logLevel: ${this.log.logLevel} fileLoger: ${this.fileLogger}.`);
|
|
486
331
|
if (this.profile !== undefined)
|
|
487
332
|
this.log.debug(`Matterbridge profile: ${this.profile}.`);
|
|
488
|
-
// Set matter.js logger level, format and logger (context: matterLogLevel)
|
|
489
333
|
if (hasParameter('matterlogger')) {
|
|
490
334
|
const level = getParameter('matterlogger');
|
|
491
335
|
if (level === 'debug') {
|
|
@@ -515,13 +359,11 @@ export class Matterbridge extends EventEmitter {
|
|
|
515
359
|
Logger.level = (await this.nodeContext.get('matterLogLevel', this.shellyBoard ? MatterLogLevel.NOTICE : MatterLogLevel.INFO));
|
|
516
360
|
}
|
|
517
361
|
Logger.format = MatterLogFormat.ANSI;
|
|
518
|
-
// Create the logger for matter.js with file logging (context: matterFileLog)
|
|
519
362
|
if (hasParameter('matterfilelogger') || (await this.nodeContext.get('matterFileLog', false))) {
|
|
520
363
|
this.matterFileLogger = true;
|
|
521
364
|
}
|
|
522
365
|
Logger.destinations.default.write = this.createDestinationMatterLogger(this.matterFileLogger);
|
|
523
366
|
this.log.debug(`Matter logLevel: ${Logger.level} fileLoger: ${this.matterFileLogger}.`);
|
|
524
|
-
// Log network interfaces
|
|
525
367
|
const networkInterfaces = os.networkInterfaces();
|
|
526
368
|
const availableAddresses = Object.entries(networkInterfaces);
|
|
527
369
|
const availableInterfaceNames = Object.keys(networkInterfaces);
|
|
@@ -534,7 +376,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
534
376
|
});
|
|
535
377
|
}
|
|
536
378
|
}
|
|
537
|
-
// Set the interface to use for matter server node mdnsInterface
|
|
538
379
|
if (hasParameter('mdnsinterface')) {
|
|
539
380
|
this.mdnsInterface = getParameter('mdnsinterface');
|
|
540
381
|
}
|
|
@@ -543,7 +384,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
543
384
|
if (this.mdnsInterface === '')
|
|
544
385
|
this.mdnsInterface = undefined;
|
|
545
386
|
}
|
|
546
|
-
// Validate mdnsInterface
|
|
547
387
|
if (this.mdnsInterface) {
|
|
548
388
|
if (!availableInterfaceNames.includes(this.mdnsInterface)) {
|
|
549
389
|
this.log.error(`Invalid mdnsinterface: ${this.mdnsInterface}. Available interfaces are: ${availableInterfaceNames.join(', ')}. Using all available interfaces.`);
|
|
@@ -556,7 +396,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
556
396
|
}
|
|
557
397
|
if (this.mdnsInterface)
|
|
558
398
|
this.environment.vars.set('mdns.networkInterface', this.mdnsInterface);
|
|
559
|
-
// Set the listeningAddressIpv4 for the matter commissioning server
|
|
560
399
|
if (hasParameter('ipv4address')) {
|
|
561
400
|
this.ipv4Address = getParameter('ipv4address');
|
|
562
401
|
}
|
|
@@ -565,7 +404,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
565
404
|
if (this.ipv4Address === '')
|
|
566
405
|
this.ipv4Address = undefined;
|
|
567
406
|
}
|
|
568
|
-
// Validate ipv4address
|
|
569
407
|
if (this.ipv4Address) {
|
|
570
408
|
let isValid = false;
|
|
571
409
|
for (const [ifaceName, ifaces] of availableAddresses) {
|
|
@@ -581,7 +419,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
581
419
|
await this.nodeContext.remove('matteripv4address');
|
|
582
420
|
}
|
|
583
421
|
}
|
|
584
|
-
// Set the listeningAddressIpv6 for the matter commissioning server
|
|
585
422
|
if (hasParameter('ipv6address')) {
|
|
586
423
|
this.ipv6Address = getParameter('ipv6address');
|
|
587
424
|
}
|
|
@@ -590,7 +427,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
590
427
|
if (this.ipv6Address === '')
|
|
591
428
|
this.ipv6Address = undefined;
|
|
592
429
|
}
|
|
593
|
-
// Validate ipv6address
|
|
594
430
|
if (this.ipv6Address) {
|
|
595
431
|
let isValid = false;
|
|
596
432
|
for (const [ifaceName, ifaces] of availableAddresses) {
|
|
@@ -599,7 +435,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
599
435
|
isValid = true;
|
|
600
436
|
break;
|
|
601
437
|
}
|
|
602
|
-
/* istanbul ignore next */
|
|
603
438
|
if (ifaces && ifaces.find((iface) => iface.scopeid && iface.scopeid > 0 && iface.address + '%' + (process.platform === 'win32' ? iface.scopeid : ifaceName) === this.ipv6Address)) {
|
|
604
439
|
this.log.info(`Using ipv6address ${CYAN}${this.ipv6Address}${nf} on interface ${CYAN}${ifaceName}${nf} for the Matter server node.`);
|
|
605
440
|
isValid = true;
|
|
@@ -612,7 +447,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
612
447
|
await this.nodeContext.remove('matteripv6address');
|
|
613
448
|
}
|
|
614
449
|
}
|
|
615
|
-
// Initialize the virtual mode
|
|
616
450
|
if (hasParameter('novirtual')) {
|
|
617
451
|
this.virtualMode = 'disabled';
|
|
618
452
|
await this.nodeContext.set('virtualmode', 'disabled');
|
|
@@ -621,17 +455,12 @@ export class Matterbridge extends EventEmitter {
|
|
|
621
455
|
this.virtualMode = (await this.nodeContext.get('virtualmode', 'outlet'));
|
|
622
456
|
}
|
|
623
457
|
this.log.debug(`Virtual mode ${this.virtualMode}.`);
|
|
624
|
-
// Initialize PluginManager
|
|
625
458
|
this.plugins.logLevel = this.log.logLevel;
|
|
626
459
|
await this.plugins.loadFromStorage();
|
|
627
|
-
// Initialize DeviceManager
|
|
628
460
|
this.devices.logLevel = this.log.logLevel;
|
|
629
|
-
// Get the plugins from node storage and create the plugins node storage contexts
|
|
630
461
|
for (const plugin of this.plugins) {
|
|
631
462
|
const packageJson = await this.plugins.parse(plugin);
|
|
632
463
|
if (packageJson === null && !hasParameter('add') && !hasParameter('remove') && !hasParameter('enable') && !hasParameter('disable') && !hasParameter('reset') && !hasParameter('factoryreset')) {
|
|
633
|
-
// Try to reinstall the plugin from npm (for Docker pull and external plugins)
|
|
634
|
-
// We don't do this when the add and other parameters are set because we shut down the process after adding the plugin
|
|
635
464
|
this.log.info(`Error parsing plugin ${plg}${plugin.name}${nf}. Trying to reinstall it from npm.`);
|
|
636
465
|
try {
|
|
637
466
|
const { spawnCommand } = await import('./utils/spawn.js');
|
|
@@ -654,7 +483,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
654
483
|
await plugin.nodeContext.set('description', plugin.description);
|
|
655
484
|
await plugin.nodeContext.set('author', plugin.author);
|
|
656
485
|
}
|
|
657
|
-
// Log system info and create .matterbridge directory
|
|
658
486
|
await this.logNodeAndSystemInfo();
|
|
659
487
|
this.log.notice(`Matterbridge version ${this.matterbridgeVersion} ` +
|
|
660
488
|
`${hasParameter('bridge') || (!hasParameter('childbridge') && (await this.nodeContext?.get('bridgeMode', '')) === 'bridge') ? 'mode bridge ' : ''}` +
|
|
@@ -662,7 +490,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
662
490
|
`${hasParameter('controller') ? 'mode controller ' : ''}` +
|
|
663
491
|
`${this.restartMode !== '' ? 'restart mode ' + this.restartMode + ' ' : ''}` +
|
|
664
492
|
`running on ${this.systemInformation.osType} (v.${this.systemInformation.osRelease}) platform ${this.systemInformation.osPlatform} arch ${this.systemInformation.osArch}`);
|
|
665
|
-
// Check node version and throw error
|
|
666
493
|
const minNodeVersion = 20;
|
|
667
494
|
const nodeVersion = process.versions.node;
|
|
668
495
|
const versionMajor = parseInt(nodeVersion.split('.')[0]);
|
|
@@ -670,18 +497,10 @@ export class Matterbridge extends EventEmitter {
|
|
|
670
497
|
this.log.error(`Node version ${versionMajor} is not supported. Please upgrade to ${minNodeVersion} or above.`);
|
|
671
498
|
throw new Error(`Node version ${versionMajor} is not supported. Please upgrade to ${minNodeVersion} or above.`);
|
|
672
499
|
}
|
|
673
|
-
// Parse command line
|
|
674
500
|
await this.parseCommandLine();
|
|
675
|
-
// Emit the initialize_completed event
|
|
676
501
|
this.emit('initialize_completed');
|
|
677
502
|
this.initialized = true;
|
|
678
503
|
}
|
|
679
|
-
/**
|
|
680
|
-
* Parses the command line arguments and performs the corresponding actions.
|
|
681
|
-
*
|
|
682
|
-
* @private
|
|
683
|
-
* @returns {Promise<void>} A promise that resolves when the command line arguments have been processed, or the process exits.
|
|
684
|
-
*/
|
|
685
504
|
async parseCommandLine() {
|
|
686
505
|
if (hasParameter('list')) {
|
|
687
506
|
this.log.info(`│ Registered plugins (${this.plugins.length})`);
|
|
@@ -697,19 +516,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
697
516
|
}
|
|
698
517
|
index++;
|
|
699
518
|
}
|
|
700
|
-
/*
|
|
701
|
-
const serializedRegisteredDevices = await this.nodeContext?.get<SerializedMatterbridgeEndpoint[]>('devices', []);
|
|
702
|
-
this.log.info(`│ Registered devices (${serializedRegisteredDevices?.length})`);
|
|
703
|
-
serializedRegisteredDevices?.forEach((device, index) => {
|
|
704
|
-
if (index !== serializedRegisteredDevices.length - 1) {
|
|
705
|
-
this.log.info(`├─┬─ plugin ${plg}${device.pluginName}${nf} device: ${dev}${device.deviceName}${nf} uniqueId: ${YELLOW}${device.uniqueId}${nf}`);
|
|
706
|
-
this.log.info(`│ └─ endpoint ${RED}${device.endpoint}${nf} ${typ}${device.endpointName}${nf} ${debugStringify(device.clusterServersId)}`);
|
|
707
|
-
} else {
|
|
708
|
-
this.log.info(`└─┬─ plugin ${plg}${device.pluginName}${nf} device: ${dev}${device.deviceName}${nf} uniqueId: ${YELLOW}${device.uniqueId}${nf}`);
|
|
709
|
-
this.log.info(` └─ endpoint ${RED}${device.endpoint}${nf} ${typ}${device.endpointName}${nf} ${debugStringify(device.clusterServersId)}`);
|
|
710
|
-
}
|
|
711
|
-
});
|
|
712
|
-
*/
|
|
713
519
|
this.shutdown = true;
|
|
714
520
|
return;
|
|
715
521
|
}
|
|
@@ -759,10 +565,8 @@ export class Matterbridge extends EventEmitter {
|
|
|
759
565
|
this.shutdown = true;
|
|
760
566
|
return;
|
|
761
567
|
}
|
|
762
|
-
// Initialize frontend
|
|
763
568
|
if (getIntParameter('frontend') !== 0 || getIntParameter('frontend') === undefined)
|
|
764
569
|
await this.frontend.start(getIntParameter('frontend'));
|
|
765
|
-
// Start the matter storage and create the matterbridge context
|
|
766
570
|
try {
|
|
767
571
|
await this.startMatterStorage();
|
|
768
572
|
if (this.aggregatorSerialNumber && this.aggregatorUniqueId && this.matterStorageService) {
|
|
@@ -776,21 +580,18 @@ export class Matterbridge extends EventEmitter {
|
|
|
776
580
|
this.log.fatal(`Fatal error creating matter storage: ${error instanceof Error ? error.message : error}`);
|
|
777
581
|
throw new Error(`Fatal error creating matter storage: ${error instanceof Error ? error.message : error}`);
|
|
778
582
|
}
|
|
779
|
-
// Clear the matterbridge context if the reset parameter is set (bridge mode)
|
|
780
583
|
if (hasParameter('reset') && getParameter('reset') === undefined) {
|
|
781
584
|
this.initialized = true;
|
|
782
585
|
await this.shutdownProcessAndReset();
|
|
783
586
|
this.shutdown = true;
|
|
784
587
|
return;
|
|
785
588
|
}
|
|
786
|
-
// Clear matterbridge plugin context if the reset parameter is set (childbridge mode)
|
|
787
589
|
if (hasParameter('reset') && getParameter('reset') !== undefined) {
|
|
788
590
|
this.log.debug(`Reset plugin ${getParameter('reset')}`);
|
|
789
591
|
const plugin = this.plugins.get(getParameter('reset'));
|
|
790
592
|
if (plugin) {
|
|
791
593
|
const matterStorageManager = await this.matterStorageService?.open(plugin.name);
|
|
792
594
|
if (!matterStorageManager) {
|
|
793
|
-
/* istanbul ignore next */
|
|
794
595
|
this.log.error(`Plugin ${plg}${plugin.name}${er} storageManager not found`);
|
|
795
596
|
}
|
|
796
597
|
else {
|
|
@@ -809,42 +610,35 @@ export class Matterbridge extends EventEmitter {
|
|
|
809
610
|
this.shutdown = true;
|
|
810
611
|
return;
|
|
811
612
|
}
|
|
812
|
-
// Check in 30 seconds the latest and dev versions of matterbridge and the plugins
|
|
813
613
|
clearTimeout(this.checkUpdateTimeout);
|
|
814
614
|
this.checkUpdateTimeout = setTimeout(async () => {
|
|
815
615
|
const { checkUpdates } = await import('./update.js');
|
|
816
616
|
checkUpdates(this);
|
|
817
617
|
}, 30 * 1000).unref();
|
|
818
|
-
// Check each 12 hours the latest and dev versions of matterbridge and the plugins
|
|
819
618
|
clearInterval(this.checkUpdateInterval);
|
|
820
619
|
this.checkUpdateInterval = setInterval(async () => {
|
|
821
620
|
const { checkUpdates } = await import('./update.js');
|
|
822
621
|
checkUpdates(this);
|
|
823
622
|
}, 12 * 60 * 60 * 1000).unref();
|
|
824
|
-
// Start the matterbridge in mode test
|
|
825
623
|
if (hasParameter('test')) {
|
|
826
624
|
this.bridgeMode = 'bridge';
|
|
827
625
|
return;
|
|
828
626
|
}
|
|
829
|
-
// Start the matterbridge in mode controller
|
|
830
627
|
if (hasParameter('controller')) {
|
|
831
628
|
this.bridgeMode = 'controller';
|
|
832
629
|
await this.startController();
|
|
833
630
|
return;
|
|
834
631
|
}
|
|
835
|
-
// Check if the bridge mode is set and start matterbridge in bridge mode if not set
|
|
836
632
|
if (!hasParameter('bridge') && !hasParameter('childbridge') && (await this.nodeContext?.get('bridgeMode', '')) === '') {
|
|
837
633
|
this.log.info('Setting default matterbridge start mode to bridge');
|
|
838
634
|
await this.nodeContext?.set('bridgeMode', 'bridge');
|
|
839
635
|
}
|
|
840
|
-
// Start matterbridge in bridge mode
|
|
841
636
|
if (hasParameter('bridge') || (!hasParameter('childbridge') && (await this.nodeContext?.get('bridgeMode', '')) === 'bridge')) {
|
|
842
637
|
this.bridgeMode = 'bridge';
|
|
843
638
|
this.log.debug(`Starting matterbridge in mode ${this.bridgeMode}`);
|
|
844
639
|
await this.startBridge();
|
|
845
640
|
return;
|
|
846
641
|
}
|
|
847
|
-
// Start matterbridge in childbridge mode
|
|
848
642
|
if (hasParameter('childbridge') || (!hasParameter('bridge') && (await this.nodeContext?.get('bridgeMode', '')) === 'childbridge')) {
|
|
849
643
|
this.bridgeMode = 'childbridge';
|
|
850
644
|
this.log.debug(`Starting matterbridge in mode ${this.bridgeMode}`);
|
|
@@ -852,22 +646,10 @@ export class Matterbridge extends EventEmitter {
|
|
|
852
646
|
return;
|
|
853
647
|
}
|
|
854
648
|
}
|
|
855
|
-
/**
|
|
856
|
-
* Asynchronously loads and starts the registered plugins.
|
|
857
|
-
*
|
|
858
|
-
* This method is responsible for initializing and starting all enabled plugins.
|
|
859
|
-
* It ensures that each plugin is properly loaded and started before the bridge starts.
|
|
860
|
-
*
|
|
861
|
-
* @param {boolean} [wait] - If true, the method will wait for all plugins to be fully loaded and started before resolving.
|
|
862
|
-
* @param {boolean} [start] - If true, the method will start the plugins after loading them.
|
|
863
|
-
* @returns {Promise<void>} A promise that resolves when all plugins have been loaded and started.
|
|
864
|
-
*/
|
|
865
649
|
async startPlugins(wait = false, start = true) {
|
|
866
|
-
// Check, load and start the plugins
|
|
867
650
|
for (const plugin of this.plugins) {
|
|
868
651
|
plugin.configJson = await this.plugins.loadConfig(plugin);
|
|
869
652
|
plugin.schemaJson = await this.plugins.loadSchema(plugin);
|
|
870
|
-
// Check if the plugin is available
|
|
871
653
|
if (!(await this.plugins.resolve(plugin.path))) {
|
|
872
654
|
this.log.error(`Plugin ${plg}${plugin.name}${er} not found or not validated. Disabling it.`);
|
|
873
655
|
plugin.enabled = false;
|
|
@@ -887,16 +669,10 @@ export class Matterbridge extends EventEmitter {
|
|
|
887
669
|
if (wait)
|
|
888
670
|
await this.plugins.load(plugin, start, 'Matterbridge is starting');
|
|
889
671
|
else
|
|
890
|
-
this.plugins.load(plugin, start, 'Matterbridge is starting');
|
|
672
|
+
this.plugins.load(plugin, start, 'Matterbridge is starting');
|
|
891
673
|
}
|
|
892
674
|
this.frontend.wssSendRefreshRequired('plugins');
|
|
893
675
|
}
|
|
894
|
-
/**
|
|
895
|
-
* Registers the process handlers for uncaughtException, unhandledRejection, SIGINT and SIGTERM.
|
|
896
|
-
* - When an uncaught exception occurs, the exceptionHandler logs the error message and stack trace.
|
|
897
|
-
* - When an unhandled promise rejection occurs, the rejectionHandler logs the reason and stack trace.
|
|
898
|
-
* - When either of SIGINT and SIGTERM signals are received, the cleanup method is called with an appropriate message.
|
|
899
|
-
*/
|
|
900
676
|
registerProcessHandlers() {
|
|
901
677
|
this.log.debug(`Registering uncaughtException and unhandledRejection handlers...`);
|
|
902
678
|
process.removeAllListeners('uncaughtException');
|
|
@@ -923,9 +699,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
923
699
|
};
|
|
924
700
|
process.on('SIGTERM', this.sigtermHandler);
|
|
925
701
|
}
|
|
926
|
-
/**
|
|
927
|
-
* Deregisters the process uncaughtException, unhandledRejection, SIGINT and SIGTERM signal handlers.
|
|
928
|
-
*/
|
|
929
702
|
deregisterProcessHandlers() {
|
|
930
703
|
this.log.debug(`Deregistering uncaughtException and unhandledRejection handlers...`);
|
|
931
704
|
if (this.exceptionHandler)
|
|
@@ -942,18 +715,13 @@ export class Matterbridge extends EventEmitter {
|
|
|
942
715
|
process.off('SIGTERM', this.sigtermHandler);
|
|
943
716
|
this.sigtermHandler = undefined;
|
|
944
717
|
}
|
|
945
|
-
/**
|
|
946
|
-
* Logs the node and system information.
|
|
947
|
-
*/
|
|
948
718
|
async logNodeAndSystemInfo() {
|
|
949
|
-
// IP address information
|
|
950
719
|
const networkInterfaces = os.networkInterfaces();
|
|
951
720
|
this.systemInformation.interfaceName = '';
|
|
952
721
|
this.systemInformation.ipv4Address = '';
|
|
953
722
|
this.systemInformation.ipv6Address = '';
|
|
954
723
|
this.systemInformation.macAddress = '';
|
|
955
724
|
for (const [interfaceName, interfaceDetails] of Object.entries(networkInterfaces)) {
|
|
956
|
-
// this.log.debug(`Checking interface: '${interfaceName}' for '${this.mdnsInterface}'`);
|
|
957
725
|
if (this.mdnsInterface && interfaceName !== this.mdnsInterface)
|
|
958
726
|
continue;
|
|
959
727
|
if (!interfaceDetails) {
|
|
@@ -979,18 +747,16 @@ export class Matterbridge extends EventEmitter {
|
|
|
979
747
|
break;
|
|
980
748
|
}
|
|
981
749
|
}
|
|
982
|
-
// Node information
|
|
983
750
|
this.systemInformation.nodeVersion = process.versions.node;
|
|
984
751
|
const versionMajor = parseInt(this.systemInformation.nodeVersion.split('.')[0]);
|
|
985
752
|
const versionMinor = parseInt(this.systemInformation.nodeVersion.split('.')[1]);
|
|
986
753
|
const versionPatch = parseInt(this.systemInformation.nodeVersion.split('.')[2]);
|
|
987
|
-
// Host system information
|
|
988
754
|
this.systemInformation.hostname = os.hostname();
|
|
989
755
|
this.systemInformation.user = os.userInfo().username;
|
|
990
|
-
this.systemInformation.osType = os.type();
|
|
991
|
-
this.systemInformation.osRelease = os.release();
|
|
992
|
-
this.systemInformation.osPlatform = os.platform();
|
|
993
|
-
this.systemInformation.osArch = os.arch();
|
|
756
|
+
this.systemInformation.osType = os.type();
|
|
757
|
+
this.systemInformation.osRelease = os.release();
|
|
758
|
+
this.systemInformation.osPlatform = os.platform();
|
|
759
|
+
this.systemInformation.osArch = os.arch();
|
|
994
760
|
this.systemInformation.totalMemory = formatBytes(os.totalmem());
|
|
995
761
|
this.systemInformation.freeMemory = formatBytes(os.freemem());
|
|
996
762
|
this.systemInformation.systemUptime = formatUptime(os.uptime());
|
|
@@ -1000,7 +766,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
1000
766
|
this.systemInformation.rss = formatBytes(process.memoryUsage().rss);
|
|
1001
767
|
this.systemInformation.heapTotal = formatBytes(process.memoryUsage().heapTotal);
|
|
1002
768
|
this.systemInformation.heapUsed = formatBytes(process.memoryUsage().heapUsed);
|
|
1003
|
-
// Log the system information
|
|
1004
769
|
this.log.debug('Host System Information:');
|
|
1005
770
|
this.log.debug(`- Hostname: ${this.systemInformation.hostname}`);
|
|
1006
771
|
this.log.debug(`- User: ${this.systemInformation.user}`);
|
|
@@ -1020,17 +785,14 @@ export class Matterbridge extends EventEmitter {
|
|
|
1020
785
|
this.log.debug(`- RSS: ${this.systemInformation.rss}`);
|
|
1021
786
|
this.log.debug(`- Heap Total: ${this.systemInformation.heapTotal}`);
|
|
1022
787
|
this.log.debug(`- Heap Used: ${this.systemInformation.heapUsed}`);
|
|
1023
|
-
// Log directories
|
|
1024
788
|
this.log.debug(`Root Directory: ${this.rootDirectory}`);
|
|
1025
789
|
this.log.debug(`Home Directory: ${this.homeDirectory}`);
|
|
1026
790
|
this.log.debug(`Matterbridge Directory: ${this.matterbridgeDirectory}`);
|
|
1027
791
|
this.log.debug(`Matterbridge Plugin Directory: ${this.matterbridgePluginDirectory}`);
|
|
1028
792
|
this.log.debug(`Matterbridge Matter Certificate Directory: ${this.matterbridgeCertDirectory}`);
|
|
1029
|
-
// Global node_modules directory
|
|
1030
793
|
if (this.nodeContext)
|
|
1031
794
|
this.globalModulesDirectory = await this.nodeContext.get('globalModulesDirectory', '');
|
|
1032
795
|
if (this.globalModulesDirectory === '') {
|
|
1033
|
-
// First run of Matterbridge so the node storage is empty
|
|
1034
796
|
this.log.debug(`Getting global node_modules directory...`);
|
|
1035
797
|
try {
|
|
1036
798
|
const { getGlobalNodeModules } = await import('./utils/network.js');
|
|
@@ -1043,7 +805,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
1043
805
|
}
|
|
1044
806
|
}
|
|
1045
807
|
else {
|
|
1046
|
-
// The global node_modules directory is already set in the node storage and we check if it is still valid
|
|
1047
808
|
this.log.debug(`Checking global node_modules directory: ${this.globalModulesDirectory}`);
|
|
1048
809
|
try {
|
|
1049
810
|
const { getGlobalNodeModules } = await import('./utils/network.js');
|
|
@@ -1055,37 +816,25 @@ export class Matterbridge extends EventEmitter {
|
|
|
1055
816
|
this.log.error(`Error checking global node_modules directory: ${error}`);
|
|
1056
817
|
}
|
|
1057
818
|
}
|
|
1058
|
-
// Matterbridge version
|
|
1059
819
|
this.log.debug(`Reading matterbridge package.json...`);
|
|
1060
820
|
const packageJson = JSON.parse(await fs.readFile(path.join(this.rootDirectory, 'package.json'), 'utf-8'));
|
|
1061
821
|
this.matterbridgeVersion = this.matterbridgeLatestVersion = this.matterbridgeDevVersion = packageJson.version;
|
|
1062
822
|
this.log.debug(`Matterbridge Version: ${this.matterbridgeVersion}`);
|
|
1063
|
-
// Matterbridge latest version (will be set in the checkUpdate function)
|
|
1064
823
|
if (this.nodeContext)
|
|
1065
824
|
this.matterbridgeLatestVersion = await this.nodeContext.get('matterbridgeLatestVersion', this.matterbridgeVersion);
|
|
1066
825
|
this.log.debug(`Matterbridge Latest Version: ${this.matterbridgeLatestVersion}`);
|
|
1067
|
-
// Matterbridge dev version (will be set in the checkUpdate function)
|
|
1068
826
|
if (this.nodeContext)
|
|
1069
827
|
this.matterbridgeDevVersion = await this.nodeContext.get('matterbridgeDevVersion', this.matterbridgeVersion);
|
|
1070
828
|
this.log.debug(`Matterbridge Dev Version: ${this.matterbridgeDevVersion}`);
|
|
1071
|
-
// Frontend version
|
|
1072
829
|
this.log.debug(`Reading frontend package.json...`);
|
|
1073
830
|
const frontendPackageJson = JSON.parse(await fs.readFile(path.join(this.rootDirectory, 'frontend/package.json'), 'utf8'));
|
|
1074
831
|
this.frontendVersion = frontendPackageJson.version;
|
|
1075
832
|
this.log.debug(`Frontend version ${CYAN}${this.frontendVersion}${db}`);
|
|
1076
|
-
// Current working directory
|
|
1077
833
|
const currentDir = process.cwd();
|
|
1078
834
|
this.log.debug(`Current Working Directory: ${currentDir}`);
|
|
1079
|
-
// Command line arguments (excluding 'node' and the script name)
|
|
1080
835
|
const cmdArgs = process.argv.slice(2).join(' ');
|
|
1081
836
|
this.log.debug(`Command Line Arguments: ${cmdArgs}`);
|
|
1082
837
|
}
|
|
1083
|
-
/**
|
|
1084
|
-
* Set the logger logLevel for the Matterbridge classes and call onChangeLoggerLevel() for each plugin.
|
|
1085
|
-
*
|
|
1086
|
-
* @param {LogLevel} logLevel The logger logLevel to set.
|
|
1087
|
-
* @returns {Promise<LogLevel>} A promise that resolves when the logLevel has been set.
|
|
1088
|
-
*/
|
|
1089
838
|
async setLogLevel(logLevel) {
|
|
1090
839
|
this.log.logLevel = logLevel;
|
|
1091
840
|
this.frontend.logLevel = logLevel;
|
|
@@ -1095,87 +844,58 @@ export class Matterbridge extends EventEmitter {
|
|
|
1095
844
|
for (const plugin of this.plugins) {
|
|
1096
845
|
if (!plugin.platform || !plugin.platform.log || !plugin.platform.config)
|
|
1097
846
|
continue;
|
|
1098
|
-
plugin.platform.log.logLevel = plugin.platform.config.debug === true ? "debug"
|
|
1099
|
-
await plugin.platform.onChangeLoggerLevel(plugin.platform.config.debug === true ? "debug"
|
|
1100
|
-
}
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
callbackLogLevel = "debug" /* LogLevel.DEBUG */;
|
|
847
|
+
plugin.platform.log.logLevel = plugin.platform.config.debug === true ? "debug" : logLevel;
|
|
848
|
+
await plugin.platform.onChangeLoggerLevel(plugin.platform.config.debug === true ? "debug" : logLevel);
|
|
849
|
+
}
|
|
850
|
+
let callbackLogLevel = "notice";
|
|
851
|
+
if (logLevel === "info" || Logger.level === MatterLogLevel.INFO)
|
|
852
|
+
callbackLogLevel = "info";
|
|
853
|
+
if (logLevel === "debug" || Logger.level === MatterLogLevel.DEBUG)
|
|
854
|
+
callbackLogLevel = "debug";
|
|
1107
855
|
AnsiLogger.setGlobalCallbackLevel(callbackLogLevel);
|
|
1108
856
|
this.log.debug(`WebSocketServer logger global callback set to ${callbackLogLevel}`);
|
|
1109
857
|
return logLevel;
|
|
1110
858
|
}
|
|
1111
|
-
/**
|
|
1112
|
-
* Get the current logger logLevel.
|
|
1113
|
-
*
|
|
1114
|
-
* @returns {LogLevel} The current logger logLevel.
|
|
1115
|
-
*/
|
|
1116
859
|
getLogLevel() {
|
|
1117
860
|
return this.log.logLevel;
|
|
1118
861
|
}
|
|
1119
|
-
/**
|
|
1120
|
-
* Creates a MatterLogger function to show the matter.js log messages in AnsiLogger (for the frontend).
|
|
1121
|
-
* It also logs to file (matter.log) if fileLogger is true.
|
|
1122
|
-
*
|
|
1123
|
-
* @param {boolean} fileLogger - Whether to log to file or not.
|
|
1124
|
-
* @returns {Function} The MatterLogger function. \x1b[35m for violet \x1b[34m is blue
|
|
1125
|
-
*/
|
|
1126
862
|
createDestinationMatterLogger(fileLogger) {
|
|
1127
|
-
this.matterLog.logNameColor = '\x1b[34m';
|
|
863
|
+
this.matterLog.logNameColor = '\x1b[34m';
|
|
1128
864
|
if (fileLogger) {
|
|
1129
865
|
this.matterLog.logFilePath = path.join(this.matterbridgeDirectory, MATTER_LOGGER_FILE);
|
|
1130
866
|
}
|
|
1131
867
|
return (text, message) => {
|
|
1132
|
-
// 2024-08-21 08:55:19.488 DEBUG InteractionMessenger Sending DataReport chunk with 28 attributes and 0 events: 1004 bytes
|
|
1133
868
|
const logger = text.slice(44, 44 + 20).trim();
|
|
1134
869
|
const msg = text.slice(65);
|
|
1135
870
|
this.matterLog.logName = logger;
|
|
1136
871
|
switch (message.level) {
|
|
1137
872
|
case MatterLogLevel.DEBUG:
|
|
1138
|
-
this.matterLog.log("debug"
|
|
873
|
+
this.matterLog.log("debug", msg);
|
|
1139
874
|
break;
|
|
1140
875
|
case MatterLogLevel.INFO:
|
|
1141
|
-
this.matterLog.log("info"
|
|
876
|
+
this.matterLog.log("info", msg);
|
|
1142
877
|
break;
|
|
1143
878
|
case MatterLogLevel.NOTICE:
|
|
1144
|
-
this.matterLog.log("notice"
|
|
879
|
+
this.matterLog.log("notice", msg);
|
|
1145
880
|
break;
|
|
1146
881
|
case MatterLogLevel.WARN:
|
|
1147
|
-
this.matterLog.log("warn"
|
|
882
|
+
this.matterLog.log("warn", msg);
|
|
1148
883
|
break;
|
|
1149
884
|
case MatterLogLevel.ERROR:
|
|
1150
|
-
this.matterLog.log("error"
|
|
885
|
+
this.matterLog.log("error", msg);
|
|
1151
886
|
break;
|
|
1152
887
|
case MatterLogLevel.FATAL:
|
|
1153
|
-
this.matterLog.log("fatal"
|
|
888
|
+
this.matterLog.log("fatal", msg);
|
|
1154
889
|
break;
|
|
1155
890
|
}
|
|
1156
891
|
};
|
|
1157
892
|
}
|
|
1158
|
-
/**
|
|
1159
|
-
* Restarts the process by exiting the current instance and loading a new instance (/api/restart).
|
|
1160
|
-
*
|
|
1161
|
-
* @returns {Promise<void>} A promise that resolves when the restart is completed.
|
|
1162
|
-
*/
|
|
1163
893
|
async restartProcess() {
|
|
1164
894
|
await this.cleanup('restarting...', true);
|
|
1165
895
|
}
|
|
1166
|
-
/**
|
|
1167
|
-
* Shut down the process (/api/shutdown).
|
|
1168
|
-
*
|
|
1169
|
-
* @returns {Promise<void>} A promise that resolves when the shutdown is completed.
|
|
1170
|
-
*/
|
|
1171
896
|
async shutdownProcess() {
|
|
1172
897
|
await this.cleanup('shutting down...', false);
|
|
1173
898
|
}
|
|
1174
|
-
/**
|
|
1175
|
-
* Update matterbridge and shut down the process (virtual device 'Update Matterbridge').
|
|
1176
|
-
*
|
|
1177
|
-
* @returns {Promise<void>} A promise that resolves when the update is completed.
|
|
1178
|
-
*/
|
|
1179
899
|
async updateProcess() {
|
|
1180
900
|
this.log.info('Updating matterbridge...');
|
|
1181
901
|
try {
|
|
@@ -1189,13 +909,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
1189
909
|
this.frontend.wssSendRestartRequired();
|
|
1190
910
|
await this.cleanup('updating...', false);
|
|
1191
911
|
}
|
|
1192
|
-
/**
|
|
1193
|
-
* Unregister all devices and shut down the process (/api/unregister).
|
|
1194
|
-
*
|
|
1195
|
-
* @param {number} [timeout] - The timeout duration to wait for the message exchange to complete in milliseconds. Default is 1000.
|
|
1196
|
-
*
|
|
1197
|
-
* @returns {Promise<void>} A promise that resolves when the cleanup is completed.
|
|
1198
|
-
*/
|
|
1199
912
|
async unregisterAndShutdownProcess(timeout = 1000) {
|
|
1200
913
|
this.log.info('Unregistering all devices and shutting down...');
|
|
1201
914
|
for (const plugin of this.plugins.array()) {
|
|
@@ -1207,71 +920,46 @@ export class Matterbridge extends EventEmitter {
|
|
|
1207
920
|
await this.removeAllBridgedEndpoints(plugin.name, 100);
|
|
1208
921
|
}
|
|
1209
922
|
this.log.debug('Waiting for the MessageExchange to finish...');
|
|
1210
|
-
await wait(timeout);
|
|
923
|
+
await wait(timeout);
|
|
1211
924
|
this.log.debug('Cleaning up and shutting down...');
|
|
1212
925
|
await this.cleanup('unregistered all devices and shutting down...', false, timeout);
|
|
1213
926
|
}
|
|
1214
|
-
/**
|
|
1215
|
-
* Reset commissioning and shut down the process (/api/reset).
|
|
1216
|
-
*
|
|
1217
|
-
* @returns {Promise<void>} A promise that resolves when the cleanup is completed.
|
|
1218
|
-
*/
|
|
1219
927
|
async shutdownProcessAndReset() {
|
|
1220
928
|
await this.cleanup('shutting down with reset...', false);
|
|
1221
929
|
}
|
|
1222
|
-
/**
|
|
1223
|
-
* Factory reset and shut down the process (/api/factory-reset).
|
|
1224
|
-
*
|
|
1225
|
-
* @returns {Promise<void>} A promise that resolves when the cleanup is completed.
|
|
1226
|
-
*/
|
|
1227
930
|
async shutdownProcessAndFactoryReset() {
|
|
1228
931
|
await this.cleanup('shutting down with factory reset...', false);
|
|
1229
932
|
}
|
|
1230
|
-
/**
|
|
1231
|
-
* Cleans up the Matterbridge instance.
|
|
1232
|
-
*
|
|
1233
|
-
* @param {string} message - The cleanup message.
|
|
1234
|
-
* @param {boolean} [restart] - Indicates whether to restart the instance after cleanup. Default is `false`.
|
|
1235
|
-
* @param {number} [timeout] - The timeout duration to wait for the message exchange to complete in milliseconds. Default is 1000.
|
|
1236
|
-
*
|
|
1237
|
-
* @returns {Promise<void>} A promise that resolves when the cleanup is completed.
|
|
1238
|
-
*/
|
|
1239
933
|
async cleanup(message, restart = false, timeout = 1000) {
|
|
1240
934
|
if (this.initialized && !this.hasCleanupStarted) {
|
|
1241
935
|
this.emit('cleanup_started');
|
|
1242
936
|
this.hasCleanupStarted = true;
|
|
1243
937
|
this.log.info(message);
|
|
1244
|
-
// Clear the start matter interval
|
|
1245
938
|
if (this.startMatterInterval) {
|
|
1246
939
|
clearInterval(this.startMatterInterval);
|
|
1247
940
|
this.startMatterInterval = undefined;
|
|
1248
941
|
this.log.debug('Start matter interval cleared');
|
|
1249
942
|
}
|
|
1250
|
-
// Clear the check update timeout
|
|
1251
943
|
if (this.checkUpdateTimeout) {
|
|
1252
944
|
clearTimeout(this.checkUpdateTimeout);
|
|
1253
945
|
this.checkUpdateTimeout = undefined;
|
|
1254
946
|
this.log.debug('Check update timeout cleared');
|
|
1255
947
|
}
|
|
1256
|
-
// Clear the check update interval
|
|
1257
948
|
if (this.checkUpdateInterval) {
|
|
1258
949
|
clearInterval(this.checkUpdateInterval);
|
|
1259
950
|
this.checkUpdateInterval = undefined;
|
|
1260
951
|
this.log.debug('Check update interval cleared');
|
|
1261
952
|
}
|
|
1262
|
-
// Clear the configure timeout
|
|
1263
953
|
if (this.configureTimeout) {
|
|
1264
954
|
clearTimeout(this.configureTimeout);
|
|
1265
955
|
this.configureTimeout = undefined;
|
|
1266
956
|
this.log.debug('Matterbridge configure timeout cleared');
|
|
1267
957
|
}
|
|
1268
|
-
// Clear the reachability timeout
|
|
1269
958
|
if (this.reachabilityTimeout) {
|
|
1270
959
|
clearTimeout(this.reachabilityTimeout);
|
|
1271
960
|
this.reachabilityTimeout = undefined;
|
|
1272
961
|
this.log.debug('Matterbridge reachability timeout cleared');
|
|
1273
962
|
}
|
|
1274
|
-
// Call the shutdown method of each plugin and clear the plugins reachability timeout
|
|
1275
963
|
for (const plugin of this.plugins) {
|
|
1276
964
|
if (!plugin.enabled || plugin.error)
|
|
1277
965
|
continue;
|
|
@@ -1282,7 +970,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
1282
970
|
this.log.debug(`Plugin ${plg}${plugin.name}${db} reachability timeout cleared`);
|
|
1283
971
|
}
|
|
1284
972
|
}
|
|
1285
|
-
// Stop matter server nodes
|
|
1286
973
|
this.log.notice(`Stopping matter server nodes in ${this.bridgeMode} mode...`);
|
|
1287
974
|
if (timeout > 0) {
|
|
1288
975
|
this.log.debug(`Waiting ${timeout}ms for the MessageExchange to finish...`);
|
|
@@ -1309,7 +996,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
1309
996
|
}
|
|
1310
997
|
}
|
|
1311
998
|
this.log.notice('Stopped matter server nodes');
|
|
1312
|
-
// Matter commisioning reset
|
|
1313
999
|
if (message === 'shutting down with reset...') {
|
|
1314
1000
|
this.log.info('Resetting Matterbridge commissioning information...');
|
|
1315
1001
|
await this.matterStorageManager?.createContext('events')?.clearAll();
|
|
@@ -1319,7 +1005,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
1319
1005
|
await this.matterbridgeContext?.clearAll();
|
|
1320
1006
|
this.log.info('Matter storage reset done! Remove the bridge from the controller.');
|
|
1321
1007
|
}
|
|
1322
|
-
// Unregister all devices
|
|
1323
1008
|
if (message === 'unregistered all devices and shutting down...') {
|
|
1324
1009
|
if (this.bridgeMode === 'bridge') {
|
|
1325
1010
|
await this.matterStorageManager?.createContext('root')?.createContext('parts')?.createContext('Matterbridge')?.createContext('parts')?.clearAll();
|
|
@@ -1337,35 +1022,16 @@ export class Matterbridge extends EventEmitter {
|
|
|
1337
1022
|
}
|
|
1338
1023
|
this.log.info('Matter storage reset done!');
|
|
1339
1024
|
}
|
|
1340
|
-
// Stop matter storage
|
|
1341
1025
|
await this.stopMatterStorage();
|
|
1342
|
-
// Stop the frontend
|
|
1343
1026
|
await this.frontend.stop();
|
|
1344
1027
|
this.frontend.destroy();
|
|
1345
|
-
// Close PluginManager and DeviceManager
|
|
1346
1028
|
this.plugins.destroy();
|
|
1347
1029
|
this.devices.destroy();
|
|
1348
|
-
// Stop thread messaging server
|
|
1349
1030
|
this.server.close();
|
|
1350
|
-
// Close the matterbridge node storage and context
|
|
1351
1031
|
if (this.nodeStorage && this.nodeContext) {
|
|
1352
|
-
/*
|
|
1353
|
-
TODO: Implement serialization of registered devices
|
|
1354
|
-
this.log.info('Saving registered devices...');
|
|
1355
|
-
const serializedRegisteredDevices: SerializedMatterbridgeEndpoint[] = [];
|
|
1356
|
-
this.devices.forEach(async (device) => {
|
|
1357
|
-
const serializedMatterbridgeDevice = MatterbridgeEndpoint.serialize(device);
|
|
1358
|
-
this.log.info(`- ${serializedMatterbridgeDevice.deviceName}${rs}\n`, serializedMatterbridgeDevice);
|
|
1359
|
-
if (serializedMatterbridgeDevice) serializedRegisteredDevices.push(serializedMatterbridgeDevice);
|
|
1360
|
-
});
|
|
1361
|
-
await this.nodeContext.set<SerializedMatterbridgeEndpoint[]>('devices', serializedRegisteredDevices);
|
|
1362
|
-
this.log.info(`Saved registered devices (${serializedRegisteredDevices?.length})`);
|
|
1363
|
-
*/
|
|
1364
|
-
// Clear nodeContext and nodeStorage (they just need 1000ms to write the data to disk)
|
|
1365
1032
|
this.log.debug(`Closing node storage context for ${plg}Matterbridge${db}...`);
|
|
1366
1033
|
await this.nodeContext.close();
|
|
1367
1034
|
this.nodeContext = undefined;
|
|
1368
|
-
// Clear nodeContext for each plugin (they just need 1000ms to write the data to disk)
|
|
1369
1035
|
for (const plugin of this.plugins) {
|
|
1370
1036
|
if (plugin.nodeContext) {
|
|
1371
1037
|
this.log.debug(`Closing node storage context for plugin ${plg}${plugin.name}${db}...`);
|
|
@@ -1382,10 +1048,8 @@ export class Matterbridge extends EventEmitter {
|
|
|
1382
1048
|
}
|
|
1383
1049
|
this.plugins.clear();
|
|
1384
1050
|
this.devices.clear();
|
|
1385
|
-
// Factory reset
|
|
1386
1051
|
if (message === 'shutting down with factory reset...') {
|
|
1387
1052
|
try {
|
|
1388
|
-
// Delete matter storage directory with its subdirectories and backup
|
|
1389
1053
|
const dir = path.join(this.matterbridgeDirectory, MATTER_STORAGE_NAME);
|
|
1390
1054
|
this.log.info(`Removing matter storage directory: ${dir}`);
|
|
1391
1055
|
await fs.rm(dir, { recursive: true });
|
|
@@ -1394,13 +1058,11 @@ export class Matterbridge extends EventEmitter {
|
|
|
1394
1058
|
await fs.rm(backup, { recursive: true });
|
|
1395
1059
|
}
|
|
1396
1060
|
catch (error) {
|
|
1397
|
-
// istanbul ignore next if
|
|
1398
1061
|
if (error instanceof Error && error.code !== 'ENOENT') {
|
|
1399
1062
|
this.log.error(`Error removing matter storage directory: ${error}`);
|
|
1400
1063
|
}
|
|
1401
1064
|
}
|
|
1402
1065
|
try {
|
|
1403
|
-
// Delete matterbridge storage directory with its subdirectories and backup
|
|
1404
1066
|
const dir = path.join(this.matterbridgeDirectory, NODE_STORAGE_DIR);
|
|
1405
1067
|
this.log.info(`Removing matterbridge storage directory: ${dir}`);
|
|
1406
1068
|
await fs.rm(dir, { recursive: true });
|
|
@@ -1409,20 +1071,18 @@ export class Matterbridge extends EventEmitter {
|
|
|
1409
1071
|
await fs.rm(backup, { recursive: true });
|
|
1410
1072
|
}
|
|
1411
1073
|
catch (error) {
|
|
1412
|
-
// istanbul ignore next if
|
|
1413
1074
|
if (error instanceof Error && error.code !== 'ENOENT') {
|
|
1414
1075
|
this.log.error(`Error removing matterbridge storage directory: ${error}`);
|
|
1415
1076
|
}
|
|
1416
1077
|
}
|
|
1417
1078
|
this.log.info('Factory reset done! Remove all paired fabrics from the controllers.');
|
|
1418
1079
|
}
|
|
1419
|
-
// Deregisters the process handlers
|
|
1420
1080
|
this.deregisterProcessHandlers();
|
|
1421
1081
|
if (restart) {
|
|
1422
1082
|
if (message === 'updating...') {
|
|
1423
1083
|
this.log.info('Cleanup completed. Updating...');
|
|
1424
1084
|
Matterbridge.instance = undefined;
|
|
1425
|
-
this.emit('update');
|
|
1085
|
+
this.emit('update');
|
|
1426
1086
|
}
|
|
1427
1087
|
else if (message === 'restarting...') {
|
|
1428
1088
|
this.log.info('Cleanup completed. Restarting...');
|
|
@@ -1451,14 +1111,7 @@ export class Matterbridge extends EventEmitter {
|
|
|
1451
1111
|
this.log.debug('Cleanup already started...');
|
|
1452
1112
|
}
|
|
1453
1113
|
}
|
|
1454
|
-
/**
|
|
1455
|
-
* Starts the Matterbridge in bridge mode.
|
|
1456
|
-
*
|
|
1457
|
-
* @private
|
|
1458
|
-
* @returns {Promise<void>} A promise that resolves when the Matterbridge is started.
|
|
1459
|
-
*/
|
|
1460
1114
|
async startBridge() {
|
|
1461
|
-
// Plugins are configured by a timer when matter server is started and plugin.configured is set to true
|
|
1462
1115
|
if (!this.matterStorageManager)
|
|
1463
1116
|
throw new Error('No storage manager initialized');
|
|
1464
1117
|
if (!this.matterbridgeContext)
|
|
@@ -1497,16 +1150,13 @@ export class Matterbridge extends EventEmitter {
|
|
|
1497
1150
|
clearInterval(this.startMatterInterval);
|
|
1498
1151
|
this.startMatterInterval = undefined;
|
|
1499
1152
|
this.log.debug('Cleared startMatterInterval interval for Matterbridge');
|
|
1500
|
-
|
|
1501
|
-
this.startServerNode(this.serverNode); // We don't await this, because the server node is started in the background
|
|
1502
|
-
// Start the Matter server node of single devices in mode 'server'
|
|
1153
|
+
this.startServerNode(this.serverNode);
|
|
1503
1154
|
for (const device of this.devices.array()) {
|
|
1504
1155
|
if (device.mode === 'server' && device.serverNode) {
|
|
1505
1156
|
this.log.debug(`Starting server node for device ${dev}${device.deviceName}${db} in server mode...`);
|
|
1506
|
-
this.startServerNode(device.serverNode);
|
|
1157
|
+
this.startServerNode(device.serverNode);
|
|
1507
1158
|
}
|
|
1508
1159
|
}
|
|
1509
|
-
// Configure the plugins
|
|
1510
1160
|
this.configureTimeout = setTimeout(async () => {
|
|
1511
1161
|
for (const plugin of this.plugins.array()) {
|
|
1512
1162
|
if (!plugin.enabled || !plugin.loaded || !plugin.started || plugin.error)
|
|
@@ -1524,40 +1174,28 @@ export class Matterbridge extends EventEmitter {
|
|
|
1524
1174
|
}
|
|
1525
1175
|
this.frontend.wssSendRefreshRequired('plugins');
|
|
1526
1176
|
}, 30 * 1000).unref();
|
|
1527
|
-
// Setting reachability to true
|
|
1528
1177
|
this.reachabilityTimeout = setTimeout(() => {
|
|
1529
1178
|
this.log.info(`Setting reachability to true for ${plg}Matterbridge${db}`);
|
|
1530
1179
|
if (this.aggregatorNode)
|
|
1531
1180
|
this.setAggregatorReachability(this.aggregatorNode, true);
|
|
1532
1181
|
}, 60 * 1000).unref();
|
|
1533
|
-
// Logger.get('LogServerNode').info(this.serverNode);
|
|
1534
1182
|
this.emit('bridge_started');
|
|
1535
1183
|
this.log.notice('Matterbridge bridge started successfully');
|
|
1536
1184
|
this.frontend.wssSendRefreshRequired('settings');
|
|
1537
1185
|
this.frontend.wssSendRefreshRequired('plugins');
|
|
1538
1186
|
}, this.startMatterIntervalMs);
|
|
1539
1187
|
}
|
|
1540
|
-
/**
|
|
1541
|
-
* Starts the Matterbridge in childbridge mode.
|
|
1542
|
-
*
|
|
1543
|
-
* @param {number} [delay] - The delay before starting the childbridge. Default is 1000 milliseconds.
|
|
1544
|
-
*
|
|
1545
|
-
* @returns {Promise<void>} A promise that resolves when the Matterbridge is started.
|
|
1546
|
-
*/
|
|
1547
1188
|
async startChildbridge(delay = 1000) {
|
|
1548
1189
|
if (!this.matterStorageManager)
|
|
1549
1190
|
throw new Error('No storage manager initialized');
|
|
1550
|
-
// Load with await all plugins but don't start them. We get the platform.type to pre-create server nodes for DynamicPlatform plugins
|
|
1551
1191
|
this.log.debug('Loading all plugins in childbridge mode...');
|
|
1552
1192
|
await this.startPlugins(true, false);
|
|
1553
|
-
// Create server nodes for DynamicPlatform plugins and start all plugins in the background
|
|
1554
1193
|
this.log.debug('Creating server nodes for DynamicPlatform plugins and starting all plugins in childbridge mode...');
|
|
1555
1194
|
for (const plugin of this.plugins.array().filter((p) => p.enabled && !p.error)) {
|
|
1556
1195
|
if (plugin.type === 'DynamicPlatform')
|
|
1557
1196
|
await this.createDynamicPlugin(plugin);
|
|
1558
|
-
this.plugins.start(plugin, 'Matterbridge is starting');
|
|
1197
|
+
this.plugins.start(plugin, 'Matterbridge is starting');
|
|
1559
1198
|
}
|
|
1560
|
-
// Start the Matterbridge in childbridge mode when all plugins are loaded and started
|
|
1561
1199
|
this.log.debug('Starting start matter interval in childbridge mode...');
|
|
1562
1200
|
let failCount = 0;
|
|
1563
1201
|
this.startMatterInterval = setInterval(async () => {
|
|
@@ -1591,9 +1229,8 @@ export class Matterbridge extends EventEmitter {
|
|
|
1591
1229
|
clearInterval(this.startMatterInterval);
|
|
1592
1230
|
this.startMatterInterval = undefined;
|
|
1593
1231
|
if (delay > 0)
|
|
1594
|
-
await wait(delay);
|
|
1232
|
+
await wait(delay);
|
|
1595
1233
|
this.log.debug('Cleared startMatterInterval interval in childbridge mode');
|
|
1596
|
-
// Configure the plugins
|
|
1597
1234
|
this.configureTimeout = setTimeout(async () => {
|
|
1598
1235
|
for (const plugin of this.plugins.array()) {
|
|
1599
1236
|
if (!plugin.enabled || !plugin.loaded || !plugin.started || plugin.error)
|
|
@@ -1618,7 +1255,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
1618
1255
|
this.log.error(`Plugin ${plg}${plugin.name}${er} didn't register any devices to Matterbridge. Verify the plugin configuration.`);
|
|
1619
1256
|
continue;
|
|
1620
1257
|
}
|
|
1621
|
-
// istanbul ignore next if cause is just a safety check
|
|
1622
1258
|
if (!plugin.serverNode) {
|
|
1623
1259
|
this.log.error(`Server node not found for plugin ${plg}${plugin.name}${er}`);
|
|
1624
1260
|
continue;
|
|
@@ -1631,252 +1267,28 @@ export class Matterbridge extends EventEmitter {
|
|
|
1631
1267
|
this.log.error(`Node storage context not found for plugin ${plg}${plugin.name}${er}`);
|
|
1632
1268
|
continue;
|
|
1633
1269
|
}
|
|
1634
|
-
|
|
1635
|
-
this.startServerNode(plugin.serverNode); // We don't await this, because the server node is started in the background
|
|
1636
|
-
// Setting reachability to true
|
|
1270
|
+
this.startServerNode(plugin.serverNode);
|
|
1637
1271
|
plugin.reachabilityTimeout = setTimeout(() => {
|
|
1638
1272
|
this.log.info(`Setting reachability to true for ${plg}${plugin.name}${nf}`);
|
|
1639
1273
|
if (plugin.type === 'DynamicPlatform' && plugin.aggregatorNode)
|
|
1640
1274
|
this.setAggregatorReachability(plugin.aggregatorNode, true);
|
|
1641
1275
|
}, 60 * 1000).unref();
|
|
1642
1276
|
}
|
|
1643
|
-
// Start the Matter server node of single devices in mode 'server'
|
|
1644
1277
|
for (const device of this.devices.array()) {
|
|
1645
1278
|
if (device.mode === 'server' && device.serverNode) {
|
|
1646
1279
|
this.log.debug(`Starting server node for device ${dev}${device.deviceName}${db} in server mode...`);
|
|
1647
|
-
this.startServerNode(device.serverNode);
|
|
1280
|
+
this.startServerNode(device.serverNode);
|
|
1648
1281
|
}
|
|
1649
1282
|
}
|
|
1650
|
-
// Logger.get('LogServerNode').info(this.serverNode);
|
|
1651
1283
|
this.emit('childbridge_started');
|
|
1652
1284
|
this.log.notice('Matterbridge childbridge started successfully');
|
|
1653
1285
|
this.frontend.wssSendRefreshRequired('settings');
|
|
1654
1286
|
this.frontend.wssSendRefreshRequired('plugins');
|
|
1655
1287
|
}, this.startMatterIntervalMs);
|
|
1656
1288
|
}
|
|
1657
|
-
/**
|
|
1658
|
-
* Starts the Matterbridge controller.
|
|
1659
|
-
*
|
|
1660
|
-
* @private
|
|
1661
|
-
* @returns {Promise<void>} A promise that resolves when the Matterbridge is started.
|
|
1662
|
-
*/
|
|
1663
1289
|
async startController() {
|
|
1664
|
-
/*
|
|
1665
|
-
if (!this.matterStorageManager) {
|
|
1666
|
-
this.log.error('No storage manager initialized');
|
|
1667
|
-
await this.cleanup('No storage manager initialized');
|
|
1668
|
-
return;
|
|
1669
|
-
}
|
|
1670
|
-
this.log.info('Creating context: mattercontrollerContext');
|
|
1671
|
-
this.controllerContext = this.matterStorageManager.createContext('mattercontrollerContext');
|
|
1672
|
-
if (!this.controllerContext) {
|
|
1673
|
-
this.log.error('No storage context mattercontrollerContext initialized');
|
|
1674
|
-
await this.cleanup('No storage context mattercontrollerContext initialized');
|
|
1675
|
-
return;
|
|
1676
|
-
}
|
|
1677
|
-
|
|
1678
|
-
this.log.debug('Starting matterbridge in mode', this.bridgeMode);
|
|
1679
|
-
this.matterServer = await this.createMatterServer(this.storageManager);
|
|
1680
|
-
this.log.info('Creating matter commissioning controller');
|
|
1681
|
-
this.commissioningController = new CommissioningController({
|
|
1682
|
-
autoConnect: false,
|
|
1683
|
-
});
|
|
1684
|
-
this.log.info('Adding matter commissioning controller to matter server');
|
|
1685
|
-
await this.matterServer.addCommissioningController(this.commissioningController);
|
|
1686
|
-
|
|
1687
|
-
this.log.info('Starting matter server');
|
|
1688
|
-
await this.matterServer.start();
|
|
1689
|
-
this.log.info('Matter server started');
|
|
1690
|
-
const commissioningOptions: ControllerCommissioningFlowOptions = {
|
|
1691
|
-
regulatoryLocation: GeneralCommissioning.RegulatoryLocationType.IndoorOutdoor,
|
|
1692
|
-
regulatoryCountryCode: 'XX',
|
|
1693
|
-
};
|
|
1694
|
-
const commissioningController = new CommissioningController({
|
|
1695
|
-
environment: {
|
|
1696
|
-
environment,
|
|
1697
|
-
id: uniqueId,
|
|
1698
|
-
},
|
|
1699
|
-
autoConnect: false, // Do not auto connect to the commissioned nodes
|
|
1700
|
-
adminFabricLabel,
|
|
1701
|
-
});
|
|
1702
|
-
|
|
1703
|
-
if (hasParameter('pairingcode')) {
|
|
1704
|
-
this.log.info('Pairing device with pairingcode:', getParameter('pairingcode'));
|
|
1705
|
-
const pairingCode = getParameter('pairingcode');
|
|
1706
|
-
const ip = this.controllerContext.has('ip') ? this.controllerContext.get<string>('ip') : undefined;
|
|
1707
|
-
const port = this.controllerContext.has('port') ? this.controllerContext.get<number>('port') : undefined;
|
|
1708
|
-
|
|
1709
|
-
let longDiscriminator, setupPin, shortDiscriminator;
|
|
1710
|
-
if (pairingCode !== undefined) {
|
|
1711
|
-
const pairingCodeCodec = ManualPairingCodeCodec.decode(pairingCode);
|
|
1712
|
-
shortDiscriminator = pairingCodeCodec.shortDiscriminator;
|
|
1713
|
-
longDiscriminator = undefined;
|
|
1714
|
-
setupPin = pairingCodeCodec.passcode;
|
|
1715
|
-
this.log.info(`Data extracted from pairing code: ${Logger.toJSON(pairingCodeCodec)}`);
|
|
1716
|
-
} else {
|
|
1717
|
-
longDiscriminator = await this.controllerContext.get('longDiscriminator', 3840);
|
|
1718
|
-
if (longDiscriminator > 4095) throw new Error('Discriminator value must be less than 4096');
|
|
1719
|
-
setupPin = this.controllerContext.get('pin', 20202021);
|
|
1720
|
-
}
|
|
1721
|
-
if ((shortDiscriminator === undefined && longDiscriminator === undefined) || setupPin === undefined) {
|
|
1722
|
-
throw new Error('Please specify the longDiscriminator of the device to commission with -longDiscriminator or provide a valid passcode with -passcode');
|
|
1723
|
-
}
|
|
1724
|
-
|
|
1725
|
-
const options = {
|
|
1726
|
-
commissioning: commissioningOptions,
|
|
1727
|
-
discovery: {
|
|
1728
|
-
knownAddress: ip !== undefined && port !== undefined ? { ip, port, type: 'udp' } : undefined,
|
|
1729
|
-
identifierData: longDiscriminator !== undefined ? { longDiscriminator } : shortDiscriminator !== undefined ? { shortDiscriminator } : {},
|
|
1730
|
-
},
|
|
1731
|
-
passcode: setupPin,
|
|
1732
|
-
} as NodeCommissioningOptions;
|
|
1733
|
-
this.log.info('Commissioning with options:', options);
|
|
1734
|
-
const nodeId = await this.commissioningController.commissionNode(options);
|
|
1735
|
-
this.log.info(`Commissioning successfully done with nodeId: ${nodeId}`);
|
|
1736
|
-
this.log.info('ActiveSessionInformation:', this.commissioningController.getActiveSessionInformation());
|
|
1737
|
-
} // (hasParameter('pairingcode'))
|
|
1738
|
-
|
|
1739
|
-
if (hasParameter('unpairall')) {
|
|
1740
|
-
this.log.info('***Commissioning controller unpairing all nodes...');
|
|
1741
|
-
const nodeIds = this.commissioningController.getCommissionedNodes();
|
|
1742
|
-
for (const nodeId of nodeIds) {
|
|
1743
|
-
this.log.info('***Commissioning controller unpairing node:', nodeId);
|
|
1744
|
-
await this.commissioningController.removeNode(nodeId);
|
|
1745
|
-
}
|
|
1746
|
-
return;
|
|
1747
|
-
}
|
|
1748
|
-
|
|
1749
|
-
if (hasParameter('discover')) {
|
|
1750
|
-
// const discover = await this.commissioningController.discoverCommissionableDevices({ productId: 0x8000, deviceType: 0xfff1 });
|
|
1751
|
-
// console.log(discover);
|
|
1752
|
-
}
|
|
1753
|
-
|
|
1754
|
-
if (!this.commissioningController.isCommissioned()) {
|
|
1755
|
-
this.log.info('***Commissioning controller is not commissioned: use matterbridge -controller -pairingcode [pairingcode] to commission a device');
|
|
1756
|
-
return;
|
|
1757
|
-
}
|
|
1758
|
-
|
|
1759
|
-
const nodeIds = this.commissioningController.getCommissionedNodes();
|
|
1760
|
-
this.log.info(`***Commissioning controller is commissioned ${this.commissioningController.isCommissioned()} and has ${nodeIds.length} nodes commisioned: `);
|
|
1761
|
-
for (const nodeId of nodeIds) {
|
|
1762
|
-
this.log.info(`***Connecting to commissioned node: ${nodeId}`);
|
|
1763
|
-
|
|
1764
|
-
const node = await this.commissioningController.connectNode(nodeId, {
|
|
1765
|
-
autoSubscribe: false,
|
|
1766
|
-
attributeChangedCallback: (peerNodeId, { path: { nodeId, clusterId, endpointId, attributeName }, value }) =>
|
|
1767
|
-
this.log.info(`***Commissioning controller attributeChangedCallback ${peerNodeId}: attribute ${nodeId}/${endpointId}/${clusterId}/${attributeName} changed to ${Logger.toJSON(value)}`),
|
|
1768
|
-
eventTriggeredCallback: (peerNodeId, { path: { nodeId, clusterId, endpointId, eventName }, events }) =>
|
|
1769
|
-
this.log.info(`***Commissioning controller eventTriggeredCallback ${peerNodeId}: Event ${nodeId}/${endpointId}/${clusterId}/${eventName} triggered with ${Logger.toJSON(events)}`),
|
|
1770
|
-
stateInformationCallback: (peerNodeId, info) => {
|
|
1771
|
-
switch (info) {
|
|
1772
|
-
case NodeStateInformation.Connected:
|
|
1773
|
-
this.log.info(`***Commissioning controller stateInformationCallback ${peerNodeId}: Node ${nodeId} connected`);
|
|
1774
|
-
break;
|
|
1775
|
-
case NodeStateInformation.Disconnected:
|
|
1776
|
-
this.log.info(`***Commissioning controller stateInformationCallback ${peerNodeId}: Node ${nodeId} disconnected`);
|
|
1777
|
-
break;
|
|
1778
|
-
case NodeStateInformation.Reconnecting:
|
|
1779
|
-
this.log.info(`***Commissioning controller stateInformationCallback ${peerNodeId}: Node ${nodeId} reconnecting`);
|
|
1780
|
-
break;
|
|
1781
|
-
case NodeStateInformation.WaitingForDeviceDiscovery:
|
|
1782
|
-
this.log.info(`***Commissioning controller stateInformationCallback ${peerNodeId}: Node ${nodeId} waiting for device discovery`);
|
|
1783
|
-
break;
|
|
1784
|
-
case NodeStateInformation.StructureChanged:
|
|
1785
|
-
this.log.info(`***Commissioning controller stateInformationCallback ${peerNodeId}: Node ${nodeId} structure changed`);
|
|
1786
|
-
break;
|
|
1787
|
-
case NodeStateInformation.Decommissioned:
|
|
1788
|
-
this.log.info(`***Commissioning controller stateInformationCallback ${peerNodeId}: Node ${nodeId} decommissioned`);
|
|
1789
|
-
break;
|
|
1790
|
-
default:
|
|
1791
|
-
this.log.info(`***Commissioning controller stateInformationCallback ${peerNodeId}: Node ${nodeId} NodeStateInformation.${info}`);
|
|
1792
|
-
break;
|
|
1793
|
-
}
|
|
1794
|
-
},
|
|
1795
|
-
});
|
|
1796
|
-
|
|
1797
|
-
node.logStructure();
|
|
1798
|
-
|
|
1799
|
-
// Get the interaction client
|
|
1800
|
-
this.log.info('Getting the interaction client');
|
|
1801
|
-
const interactionClient = await node.getInteractionClient();
|
|
1802
|
-
let cluster;
|
|
1803
|
-
let attributes;
|
|
1804
|
-
|
|
1805
|
-
// Log BasicInformationCluster
|
|
1806
|
-
cluster = BasicInformationCluster;
|
|
1807
|
-
attributes = await interactionClient.getMultipleAttributes({
|
|
1808
|
-
attributes: [{ clusterId: cluster.id }],
|
|
1809
|
-
});
|
|
1810
|
-
if (attributes.length > 0) this.log.info(`Cluster: ${idn}${cluster.name}${rs}${nf} attributes:`);
|
|
1811
|
-
attributes.forEach((attribute) => {
|
|
1812
|
-
this.log.info(
|
|
1813
|
-
`- endpoint ${or}${attribute.path.endpointId}${nf} cluster ${hk}${getClusterNameById(attribute.path.clusterId)}${nf} (${hk}0x${attribute.path.clusterId.toString(16)}${nf}) attribute ${zb}${attribute.path.attributeName}${nf} (${zb}0x${attribute.path.attributeId.toString(16)}${nf}): ${typeof attribute.value === 'object' ? stringify(attribute.value) : attribute.value}`,
|
|
1814
|
-
);
|
|
1815
|
-
});
|
|
1816
|
-
|
|
1817
|
-
// Log PowerSourceCluster
|
|
1818
|
-
cluster = PowerSourceCluster;
|
|
1819
|
-
attributes = await interactionClient.getMultipleAttributes({
|
|
1820
|
-
attributes: [{ clusterId: cluster.id }],
|
|
1821
|
-
});
|
|
1822
|
-
if (attributes.length > 0) this.log.info(`Cluster: ${idn}${cluster.name}${rs}${nf} attributes:`);
|
|
1823
|
-
attributes.forEach((attribute) => {
|
|
1824
|
-
this.log.info(
|
|
1825
|
-
`- endpoint ${or}${attribute.path.endpointId}${nf} cluster ${hk}${getClusterNameById(attribute.path.clusterId)}${nf} (${hk}0x${attribute.path.clusterId.toString(16)}${nf}) attribute ${zb}${attribute.path.attributeName}${nf} (${zb}0x${attribute.path.attributeId.toString(16)}${nf}): ${typeof attribute.value === 'object' ? stringify(attribute.value) : attribute.value}`,
|
|
1826
|
-
);
|
|
1827
|
-
});
|
|
1828
|
-
|
|
1829
|
-
// Log ThreadNetworkDiagnostics
|
|
1830
|
-
cluster = ThreadNetworkDiagnosticsCluster;
|
|
1831
|
-
attributes = await interactionClient.getMultipleAttributes({
|
|
1832
|
-
attributes: [{ clusterId: cluster.id }],
|
|
1833
|
-
});
|
|
1834
|
-
if (attributes.length > 0) this.log.info(`Cluster: ${idn}${cluster.name}${rs}${nf} attributes:`);
|
|
1835
|
-
attributes.forEach((attribute) => {
|
|
1836
|
-
this.log.info(
|
|
1837
|
-
`- endpoint ${or}${attribute.path.endpointId}${nf} cluster ${hk}${getClusterNameById(attribute.path.clusterId)}${nf} (${hk}0x${attribute.path.clusterId.toString(16)}${nf}) attribute ${zb}${attribute.path.attributeName}${nf} (${zb}0x${attribute.path.attributeId.toString(16)}${nf}): ${typeof attribute.value === 'object' ? stringify(attribute.value) : attribute.value}`,
|
|
1838
|
-
);
|
|
1839
|
-
});
|
|
1840
|
-
|
|
1841
|
-
// Log SwitchCluster
|
|
1842
|
-
cluster = SwitchCluster;
|
|
1843
|
-
attributes = await interactionClient.getMultipleAttributes({
|
|
1844
|
-
attributes: [{ clusterId: cluster.id }],
|
|
1845
|
-
});
|
|
1846
|
-
if (attributes.length > 0) this.log.info(`Cluster: ${idn}${cluster.name}${rs}${nf} attributes:`);
|
|
1847
|
-
attributes.forEach((attribute) => {
|
|
1848
|
-
this.log.info(
|
|
1849
|
-
`- endpoint ${or}${attribute.path.endpointId}${nf} cluster ${hk}${getClusterNameById(attribute.path.clusterId)}${nf} (${hk}0x${attribute.path.clusterId.toString(16)}${nf}) attribute ${zb}${attribute.path.attributeName}${nf} (${zb}0x${attribute.path.attributeId.toString(16)}${nf}): ${typeof attribute.value === 'object' ? stringify(attribute.value) : attribute.value}`,
|
|
1850
|
-
);
|
|
1851
|
-
});
|
|
1852
|
-
|
|
1853
|
-
this.log.info('Subscribing to all attributes and events');
|
|
1854
|
-
await node.subscribeAllAttributesAndEvents({
|
|
1855
|
-
ignoreInitialTriggers: false,
|
|
1856
|
-
attributeChangedCallback: ({ path: { nodeId, clusterId, endpointId, attributeName }, version, value }) =>
|
|
1857
|
-
this.log.info(
|
|
1858
|
-
`***${db}Commissioning controller attributeChangedCallback version ${version}: attribute ${BLUE}${nodeId}${db}/${or}${endpointId}${db}/${hk}${getClusterNameById(clusterId)}${db}/${zb}${attributeName}${db} changed to ${typeof value === 'object' ? debugStringify(value ?? { none: true }) : value}`,
|
|
1859
|
-
),
|
|
1860
|
-
eventTriggeredCallback: ({ path: { nodeId, clusterId, endpointId, eventName }, events }) => {
|
|
1861
|
-
this.log.info(
|
|
1862
|
-
`***${db}Commissioning controller eventTriggeredCallback: event ${BLUE}${nodeId}${db}/${or}${endpointId}${db}/${hk}${getClusterNameById(clusterId)}${db}/${zb}${eventName}${db} triggered with ${debugStringify(events ?? { none: true })}`,
|
|
1863
|
-
);
|
|
1864
|
-
},
|
|
1865
|
-
});
|
|
1866
|
-
this.log.info('Subscribed to all attributes and events');
|
|
1867
|
-
}
|
|
1868
|
-
*/
|
|
1869
1290
|
}
|
|
1870
|
-
/** */
|
|
1871
|
-
/** Matter.js methods */
|
|
1872
|
-
/** */
|
|
1873
|
-
/**
|
|
1874
|
-
* Starts the matter storage with name Matterbridge, create the matterbridge context and performs a backup.
|
|
1875
|
-
*
|
|
1876
|
-
* @returns {Promise<void>} - A promise that resolves when the storage is started.
|
|
1877
|
-
*/
|
|
1878
1291
|
async startMatterStorage() {
|
|
1879
|
-
// Setup Matter storage
|
|
1880
1292
|
this.log.info(`Starting matter node storage...`);
|
|
1881
1293
|
this.matterStorageService = this.environment.get(StorageService);
|
|
1882
1294
|
this.log.info(`Matter node storage service created: ${this.matterStorageService.location}`);
|
|
@@ -1884,17 +1296,8 @@ export class Matterbridge extends EventEmitter {
|
|
|
1884
1296
|
this.log.info('Matter node storage manager "Matterbridge" created');
|
|
1885
1297
|
this.matterbridgeContext = await this.createServerNodeContext('Matterbridge', 'Matterbridge', this.aggregatorDeviceType, this.aggregatorVendorId, this.aggregatorVendorName, this.aggregatorProductId, this.aggregatorProductName, this.aggregatorSerialNumber, this.aggregatorUniqueId);
|
|
1886
1298
|
this.log.info('Matter node storage started');
|
|
1887
|
-
// Backup matter storage since it is created/opened correctly
|
|
1888
1299
|
await this.backupMatterStorage(path.join(this.matterbridgeDirectory, MATTER_STORAGE_NAME), path.join(this.matterbridgeDirectory, MATTER_STORAGE_NAME + '.backup'));
|
|
1889
1300
|
}
|
|
1890
|
-
/**
|
|
1891
|
-
* Makes a backup copy of the specified matter storage directory.
|
|
1892
|
-
*
|
|
1893
|
-
* @param {string} storageName - The name of the storage directory to be backed up.
|
|
1894
|
-
* @param {string} backupName - The name of the backup directory to be created.
|
|
1895
|
-
* @private
|
|
1896
|
-
* @returns {Promise<void>} A promise that resolves when the has been done.
|
|
1897
|
-
*/
|
|
1898
1301
|
async backupMatterStorage(storageName, backupName) {
|
|
1899
1302
|
this.log.info('Creating matter node storage backup...');
|
|
1900
1303
|
try {
|
|
@@ -1905,11 +1308,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
1905
1308
|
this.log.error(`Error creating matter node storage backup from ${storageName} to ${backupName}:`, error);
|
|
1906
1309
|
}
|
|
1907
1310
|
}
|
|
1908
|
-
/**
|
|
1909
|
-
* Stops the matter storage.
|
|
1910
|
-
*
|
|
1911
|
-
* @returns {Promise<void>} A promise that resolves when the storage is stopped.
|
|
1912
|
-
*/
|
|
1913
1311
|
async stopMatterStorage() {
|
|
1914
1312
|
this.log.info('Closing matter node storage...');
|
|
1915
1313
|
await this.matterStorageManager?.close();
|
|
@@ -1918,20 +1316,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
1918
1316
|
this.matterbridgeContext = undefined;
|
|
1919
1317
|
this.log.info('Matter node storage closed');
|
|
1920
1318
|
}
|
|
1921
|
-
/**
|
|
1922
|
-
* Creates a server node storage context.
|
|
1923
|
-
*
|
|
1924
|
-
* @param {string} storeId - The storeId.
|
|
1925
|
-
* @param {string} deviceName - The name of the device.
|
|
1926
|
-
* @param {DeviceTypeId} deviceType - The device type of the device.
|
|
1927
|
-
* @param {number} vendorId - The vendor ID.
|
|
1928
|
-
* @param {string} vendorName - The vendor name.
|
|
1929
|
-
* @param {number} productId - The product ID.
|
|
1930
|
-
* @param {string} productName - The product name.
|
|
1931
|
-
* @param {string} [serialNumber] - The serial number of the device (optional).
|
|
1932
|
-
* @param {string} [uniqueId] - The unique ID of the device (optional).
|
|
1933
|
-
* @returns {Promise<StorageContext>} The storage context for the commissioning server.
|
|
1934
|
-
*/
|
|
1935
1319
|
async createServerNodeContext(storeId, deviceName, deviceType, vendorId, vendorName, productId, productName, serialNumber, uniqueId) {
|
|
1936
1320
|
const { randomBytes } = await import('node:crypto');
|
|
1937
1321
|
if (!this.matterStorageService)
|
|
@@ -1971,15 +1355,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
1971
1355
|
this.log.debug(`- hardwareVersion: ${await storageContext.get('hardwareVersion')} hardwareVersionString: ${await storageContext.get('hardwareVersionString')}`);
|
|
1972
1356
|
return storageContext;
|
|
1973
1357
|
}
|
|
1974
|
-
/**
|
|
1975
|
-
* Creates a server node.
|
|
1976
|
-
*
|
|
1977
|
-
* @param {StorageContext} storageContext - The storage context for the server node.
|
|
1978
|
-
* @param {number} [port] - The port number for the server node. Defaults to 5540.
|
|
1979
|
-
* @param {number} [passcode] - The passcode for the server node. Defaults to 20242025.
|
|
1980
|
-
* @param {number} [discriminator] - The discriminator for the server node. Defaults to 3850.
|
|
1981
|
-
* @returns {Promise<ServerNode<ServerNode.RootEndpoint>>} A promise that resolves to the created server node.
|
|
1982
|
-
*/
|
|
1983
1358
|
async createServerNode(storageContext, port = 5540, passcode = 20242025, discriminator = 3850) {
|
|
1984
1359
|
const storeId = await storageContext.get('storeId');
|
|
1985
1360
|
this.log.notice(`Creating server node for ${storeId} on port ${port} with passcode ${passcode} and discriminator ${discriminator}...`);
|
|
@@ -1989,37 +1364,24 @@ export class Matterbridge extends EventEmitter {
|
|
|
1989
1364
|
this.log.debug(`- uniqueId: ${await storageContext.get('uniqueId')}`);
|
|
1990
1365
|
this.log.debug(`- softwareVersion: ${await storageContext.get('softwareVersion')} softwareVersionString: ${await storageContext.get('softwareVersionString')}`);
|
|
1991
1366
|
this.log.debug(`- hardwareVersion: ${await storageContext.get('hardwareVersion')} hardwareVersionString: ${await storageContext.get('hardwareVersionString')}`);
|
|
1992
|
-
/**
|
|
1993
|
-
* Create a Matter ServerNode, which contains the Root Endpoint and all relevant data and configuration
|
|
1994
|
-
*/
|
|
1995
1367
|
const serverNode = await ServerNode.create({
|
|
1996
|
-
// Required: Give the Node a unique ID which is used to store the state of this node
|
|
1997
1368
|
id: storeId,
|
|
1998
|
-
// Provide Network relevant configuration like the port
|
|
1999
|
-
// Optional when operating only one device on a host, Default port is 5540
|
|
2000
1369
|
network: {
|
|
2001
1370
|
listeningAddressIpv4: this.ipv4Address,
|
|
2002
1371
|
listeningAddressIpv6: this.ipv6Address,
|
|
2003
1372
|
port,
|
|
2004
1373
|
},
|
|
2005
|
-
// Provide the certificate for the device
|
|
2006
1374
|
operationalCredentials: {
|
|
2007
1375
|
certification: this.certification,
|
|
2008
1376
|
},
|
|
2009
|
-
// Provide Commissioning relevant settings
|
|
2010
|
-
// Optional for development/testing purposes
|
|
2011
1377
|
commissioning: {
|
|
2012
1378
|
passcode,
|
|
2013
1379
|
discriminator,
|
|
2014
1380
|
},
|
|
2015
|
-
// Provide Node announcement settings
|
|
2016
|
-
// Optional: If Ommitted some development defaults are used
|
|
2017
1381
|
productDescription: {
|
|
2018
1382
|
name: await storageContext.get('deviceName'),
|
|
2019
1383
|
deviceType: DeviceTypeId(await storageContext.get('deviceType')),
|
|
2020
1384
|
},
|
|
2021
|
-
// Provide defaults for the BasicInformation cluster on the Root endpoint
|
|
2022
|
-
// Optional: If Omitted some development defaults are used
|
|
2023
1385
|
basicInformation: {
|
|
2024
1386
|
vendorId: VendorId(await storageContext.get('vendorId')),
|
|
2025
1387
|
vendorName: await storageContext.get('vendorName'),
|
|
@@ -2036,23 +1398,17 @@ export class Matterbridge extends EventEmitter {
|
|
|
2036
1398
|
reachable: true,
|
|
2037
1399
|
},
|
|
2038
1400
|
});
|
|
2039
|
-
/**
|
|
2040
|
-
* This event is triggered when the device is initially commissioned successfully.
|
|
2041
|
-
* This means: It is added to the first fabric.
|
|
2042
|
-
*/
|
|
2043
1401
|
serverNode.lifecycle.commissioned.on(() => {
|
|
2044
1402
|
this.log.notice(`Server node for ${storeId} was initially commissioned successfully!`);
|
|
2045
1403
|
this.advertisingNodes.delete(storeId);
|
|
2046
1404
|
this.frontend.wssSendRefreshRequired('matter', { matter: { ...this.getServerNodeData(serverNode) } });
|
|
2047
1405
|
});
|
|
2048
|
-
/** This event is triggered when all fabrics are removed from the device, usually it also does a factory reset then. */
|
|
2049
1406
|
serverNode.lifecycle.decommissioned.on(() => {
|
|
2050
1407
|
this.log.notice(`Server node for ${storeId} was fully decommissioned successfully!`);
|
|
2051
1408
|
this.advertisingNodes.delete(storeId);
|
|
2052
1409
|
this.frontend.wssSendRefreshRequired('matter', { matter: { ...this.getServerNodeData(serverNode) } });
|
|
2053
1410
|
this.frontend.wssSendSnackbarMessage(`${storeId} is offline`, 5, 'warning');
|
|
2054
1411
|
});
|
|
2055
|
-
/** This event is triggered when the device went online. This means that it is discoverable in the network. */
|
|
2056
1412
|
serverNode.lifecycle.online.on(async () => {
|
|
2057
1413
|
this.log.notice(`Server node for ${storeId} is online`);
|
|
2058
1414
|
if (!serverNode.lifecycle.isCommissioned) {
|
|
@@ -2063,16 +1419,13 @@ export class Matterbridge extends EventEmitter {
|
|
|
2063
1419
|
this.log.notice(`Manual pairing code: ${manualPairingCode}`);
|
|
2064
1420
|
}
|
|
2065
1421
|
else {
|
|
2066
|
-
// istanbul ignore next
|
|
2067
1422
|
this.log.notice(`Server node for ${storeId} is already commissioned. Waiting for controllers to connect ...`);
|
|
2068
|
-
// istanbul ignore next
|
|
2069
1423
|
this.advertisingNodes.delete(storeId);
|
|
2070
1424
|
}
|
|
2071
1425
|
this.frontend.wssSendRefreshRequired('matter', { matter: { ...this.getServerNodeData(serverNode) } });
|
|
2072
1426
|
this.frontend.wssSendSnackbarMessage(`${storeId} is online`, 5, 'success');
|
|
2073
1427
|
this.emit('online', storeId);
|
|
2074
1428
|
});
|
|
2075
|
-
/** This event is triggered when the device went offline. it is not longer discoverable or connectable in the network. */
|
|
2076
1429
|
serverNode.lifecycle.offline.on(() => {
|
|
2077
1430
|
this.log.notice(`Server node for ${storeId} is offline`);
|
|
2078
1431
|
this.advertisingNodes.delete(storeId);
|
|
@@ -2080,15 +1433,11 @@ export class Matterbridge extends EventEmitter {
|
|
|
2080
1433
|
this.frontend.wssSendSnackbarMessage(`${storeId} is offline`, 5, 'warning');
|
|
2081
1434
|
this.emit('offline', storeId);
|
|
2082
1435
|
});
|
|
2083
|
-
/**
|
|
2084
|
-
* This event is triggered when a fabric is added, removed or updated on the device. Use this if more granular
|
|
2085
|
-
* information is needed.
|
|
2086
|
-
*/
|
|
2087
1436
|
serverNode.events.commissioning.fabricsChanged.on((fabricIndex, fabricAction) => {
|
|
2088
1437
|
let action = '';
|
|
2089
1438
|
switch (fabricAction) {
|
|
2090
1439
|
case FabricAction.Added:
|
|
2091
|
-
this.advertisingNodes.delete(storeId);
|
|
1440
|
+
this.advertisingNodes.delete(storeId);
|
|
2092
1441
|
action = 'added';
|
|
2093
1442
|
break;
|
|
2094
1443
|
case FabricAction.Removed:
|
|
@@ -2101,22 +1450,14 @@ export class Matterbridge extends EventEmitter {
|
|
|
2101
1450
|
this.log.notice(`Commissioned fabric index ${fabricIndex} ${action} on server node for ${storeId}: ${debugStringify(serverNode.state.commissioning.fabrics[fabricIndex])}`);
|
|
2102
1451
|
this.frontend.wssSendRefreshRequired('matter', { matter: { ...this.getServerNodeData(serverNode) } });
|
|
2103
1452
|
});
|
|
2104
|
-
/**
|
|
2105
|
-
* This event is triggered when an operative new session was opened by a Controller.
|
|
2106
|
-
* It is not triggered for the initial commissioning process, just afterwards for real connections.
|
|
2107
|
-
*/
|
|
2108
1453
|
serverNode.events.sessions.opened.on((session) => {
|
|
2109
1454
|
this.log.notice(`Session opened on server node for ${storeId}: ${debugStringify(session)}`);
|
|
2110
1455
|
this.frontend.wssSendRefreshRequired('matter', { matter: { ...this.getServerNodeData(serverNode) } });
|
|
2111
1456
|
});
|
|
2112
|
-
/**
|
|
2113
|
-
* This event is triggered when an operative session is closed by a Controller or because the Device goes offline.
|
|
2114
|
-
*/
|
|
2115
1457
|
serverNode.events.sessions.closed.on((session) => {
|
|
2116
1458
|
this.log.notice(`Session closed on server node for ${storeId}: ${debugStringify(session)}`);
|
|
2117
1459
|
this.frontend.wssSendRefreshRequired('matter', { matter: { ...this.getServerNodeData(serverNode) } });
|
|
2118
1460
|
});
|
|
2119
|
-
/** This event is triggered when a subscription gets added or removed on an operative session. */
|
|
2120
1461
|
serverNode.events.sessions.subscriptionsChanged.on((session) => {
|
|
2121
1462
|
this.log.notice(`Session subscriptions changed on server node for ${storeId}: ${debugStringify(session)}`);
|
|
2122
1463
|
this.frontend.wssSendRefreshRequired('matter', { matter: { ...this.getServerNodeData(serverNode) } });
|
|
@@ -2124,12 +1465,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
2124
1465
|
this.log.info(`Created server node for ${storeId}`);
|
|
2125
1466
|
return serverNode;
|
|
2126
1467
|
}
|
|
2127
|
-
/**
|
|
2128
|
-
* Gets the matter sanitized data of the specified server node.
|
|
2129
|
-
*
|
|
2130
|
-
* @param {ServerNode} [serverNode] - The server node to start.
|
|
2131
|
-
* @returns {ApiMatter} The sanitized data of the server node.
|
|
2132
|
-
*/
|
|
2133
1468
|
getServerNodeData(serverNode) {
|
|
2134
1469
|
const advertiseTime = this.advertisingNodes.get(serverNode.id) || 0;
|
|
2135
1470
|
return {
|
|
@@ -2146,25 +1481,12 @@ export class Matterbridge extends EventEmitter {
|
|
|
2146
1481
|
serialNumber: serverNode.state.basicInformation.serialNumber,
|
|
2147
1482
|
};
|
|
2148
1483
|
}
|
|
2149
|
-
/**
|
|
2150
|
-
* Starts the specified server node.
|
|
2151
|
-
*
|
|
2152
|
-
* @param {ServerNode} [matterServerNode] - The server node to start.
|
|
2153
|
-
* @returns {Promise<void>} A promise that resolves when the server node has started.
|
|
2154
|
-
*/
|
|
2155
1484
|
async startServerNode(matterServerNode) {
|
|
2156
1485
|
if (!matterServerNode)
|
|
2157
1486
|
return;
|
|
2158
1487
|
this.log.notice(`Starting ${matterServerNode.id} server node`);
|
|
2159
1488
|
await matterServerNode.start();
|
|
2160
1489
|
}
|
|
2161
|
-
/**
|
|
2162
|
-
* Stops the specified server node.
|
|
2163
|
-
*
|
|
2164
|
-
* @param {ServerNode} matterServerNode - The server node to stop.
|
|
2165
|
-
* @param {number} [timeout] - The timeout in milliseconds for stopping the server node. Defaults to 30 seconds.
|
|
2166
|
-
* @returns {Promise<void>} A promise that resolves when the server node has stopped.
|
|
2167
|
-
*/
|
|
2168
1490
|
async stopServerNode(matterServerNode, timeout = 30000) {
|
|
2169
1491
|
if (!matterServerNode)
|
|
2170
1492
|
return;
|
|
@@ -2177,25 +1499,12 @@ export class Matterbridge extends EventEmitter {
|
|
|
2177
1499
|
this.log.error(`Failed to close ${matterServerNode.id} server node: ${error instanceof Error ? error.message : error}`);
|
|
2178
1500
|
}
|
|
2179
1501
|
}
|
|
2180
|
-
/**
|
|
2181
|
-
* Creates an aggregator node with the specified storage context.
|
|
2182
|
-
*
|
|
2183
|
-
* @param {StorageContext} storageContext - The storage context for the aggregator node.
|
|
2184
|
-
* @returns {Promise<Endpoint<AggregatorEndpoint>>} A promise that resolves to the created aggregator node.
|
|
2185
|
-
*/
|
|
2186
1502
|
async createAggregatorNode(storageContext) {
|
|
2187
1503
|
this.log.notice(`Creating ${await storageContext.get('storeId')} aggregator...`);
|
|
2188
1504
|
const aggregatorNode = new Endpoint(AggregatorEndpoint, { id: `${await storageContext.get('storeId')}` });
|
|
2189
1505
|
this.log.info(`Created ${await storageContext.get('storeId')} aggregator`);
|
|
2190
1506
|
return aggregatorNode;
|
|
2191
1507
|
}
|
|
2192
|
-
/**
|
|
2193
|
-
* Creates and configures the server node for an accessory plugin for a given device.
|
|
2194
|
-
*
|
|
2195
|
-
* @param {Plugin} plugin - The plugin to configure.
|
|
2196
|
-
* @param {MatterbridgeEndpoint} device - The device to associate with the plugin.
|
|
2197
|
-
* @returns {Promise<void>} A promise that resolves when the server node for the accessory plugin is created and configured.
|
|
2198
|
-
*/
|
|
2199
1508
|
async createAccessoryPlugin(plugin, device) {
|
|
2200
1509
|
if (!plugin.locked && device.deviceType && device.deviceName && device.vendorId && device.productId && device.vendorName && device.productName) {
|
|
2201
1510
|
plugin.locked = true;
|
|
@@ -2207,12 +1516,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
2207
1516
|
await plugin.serverNode.add(device);
|
|
2208
1517
|
}
|
|
2209
1518
|
}
|
|
2210
|
-
/**
|
|
2211
|
-
* Creates and configures the server node and the aggregator node for a dynamic plugin.
|
|
2212
|
-
*
|
|
2213
|
-
* @param {Plugin} plugin - The plugin to configure.
|
|
2214
|
-
* @returns {Promise<void>} A promise that resolves when the server node and the aggregator node for the dynamic plugin is created and configured.
|
|
2215
|
-
*/
|
|
2216
1519
|
async createDynamicPlugin(plugin) {
|
|
2217
1520
|
if (!plugin.locked) {
|
|
2218
1521
|
plugin.locked = true;
|
|
@@ -2225,13 +1528,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
2225
1528
|
await plugin.serverNode.add(plugin.aggregatorNode);
|
|
2226
1529
|
}
|
|
2227
1530
|
}
|
|
2228
|
-
/**
|
|
2229
|
-
* Creates and configures the server node for a single not bridged device.
|
|
2230
|
-
*
|
|
2231
|
-
* @param {Plugin} plugin - The plugin to configure.
|
|
2232
|
-
* @param {MatterbridgeEndpoint} device - The device to associate with the plugin.
|
|
2233
|
-
* @returns {Promise<void>} A promise that resolves when the server node for the accessory plugin is created and configured.
|
|
2234
|
-
*/
|
|
2235
1531
|
async createDeviceServerNode(plugin, device) {
|
|
2236
1532
|
if (device.mode === 'server' && !device.serverNode && device.deviceType && device.deviceName && device.vendorId && device.vendorName && device.productId && device.productName) {
|
|
2237
1533
|
this.log.debug(`Creating device ${plg}${plugin.name}${db}:${dev}${device.deviceName}${db} server node...`);
|
|
@@ -2242,15 +1538,7 @@ export class Matterbridge extends EventEmitter {
|
|
|
2242
1538
|
this.log.debug(`Added ${plg}${plugin.name}${db}:${dev}${device.deviceName}${db} to server node`);
|
|
2243
1539
|
}
|
|
2244
1540
|
}
|
|
2245
|
-
/**
|
|
2246
|
-
* Adds a MatterbridgeEndpoint to the specified plugin.
|
|
2247
|
-
*
|
|
2248
|
-
* @param {string} pluginName - The name of the plugin.
|
|
2249
|
-
* @param {MatterbridgeEndpoint} device - The device to add as a bridged endpoint.
|
|
2250
|
-
* @returns {Promise<void>} A promise that resolves when the bridged endpoint has been added.
|
|
2251
|
-
*/
|
|
2252
1541
|
async addBridgedEndpoint(pluginName, device) {
|
|
2253
|
-
// Check if the plugin is registered
|
|
2254
1542
|
const plugin = this.plugins.get(pluginName);
|
|
2255
1543
|
if (!plugin) {
|
|
2256
1544
|
this.log.error(`Error adding bridged endpoint ${dev}${device.deviceName}${er} (${zb}${device.id}${er}) plugin ${plg}${pluginName}${er} not found`);
|
|
@@ -2270,7 +1558,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
2270
1558
|
}
|
|
2271
1559
|
else if (this.bridgeMode === 'bridge') {
|
|
2272
1560
|
if (device.mode === 'matter') {
|
|
2273
|
-
// Register and add the device to the matterbridge server node
|
|
2274
1561
|
this.log.debug(`Adding matter endpoint ${plg}${pluginName}${db}:${dev}${device.deviceName}${db} to Matterbridge server node...`);
|
|
2275
1562
|
if (!this.serverNode) {
|
|
2276
1563
|
this.log.error('Server node not found for Matterbridge');
|
|
@@ -2287,7 +1574,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
2287
1574
|
}
|
|
2288
1575
|
}
|
|
2289
1576
|
else {
|
|
2290
|
-
// Register and add the device to the matterbridge aggregator node
|
|
2291
1577
|
this.log.debug(`Adding bridged endpoint ${plg}${pluginName}${db}:${dev}${device.deviceName}${db} to Matterbridge aggregator node`);
|
|
2292
1578
|
if (!this.aggregatorNode) {
|
|
2293
1579
|
this.log.error('Aggregator node not found for Matterbridge');
|
|
@@ -2305,7 +1591,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
2305
1591
|
}
|
|
2306
1592
|
}
|
|
2307
1593
|
else if (this.bridgeMode === 'childbridge') {
|
|
2308
|
-
// Register and add the device to the plugin server node
|
|
2309
1594
|
if (plugin.type === 'AccessoryPlatform') {
|
|
2310
1595
|
try {
|
|
2311
1596
|
this.log.debug(`Creating endpoint ${dev}${device.deviceName}${db} for AccessoryPlatform plugin ${plg}${plugin.name}${db} server node`);
|
|
@@ -2329,12 +1614,10 @@ export class Matterbridge extends EventEmitter {
|
|
|
2329
1614
|
return;
|
|
2330
1615
|
}
|
|
2331
1616
|
}
|
|
2332
|
-
// Register and add the device to the plugin aggregator node
|
|
2333
1617
|
if (plugin.type === 'DynamicPlatform') {
|
|
2334
1618
|
try {
|
|
2335
1619
|
this.log.debug(`Adding bridged endpoint ${dev}${device.deviceName}${db} for DynamicPlatform plugin ${plg}${plugin.name}${db} aggregator node`);
|
|
2336
1620
|
await this.createDynamicPlugin(plugin);
|
|
2337
|
-
// Fast plugins can add another device before the server node is ready, so we wait for the server node to be ready
|
|
2338
1621
|
await waiter(`createDynamicPlugin(${plugin.name})`, () => plugin.serverNode?.hasParts === true);
|
|
2339
1622
|
if (!plugin.aggregatorNode) {
|
|
2340
1623
|
this.log.error(`Aggregator node not found for plugin ${plg}${plugin.name}${er}`);
|
|
@@ -2355,28 +1638,17 @@ export class Matterbridge extends EventEmitter {
|
|
|
2355
1638
|
}
|
|
2356
1639
|
if (plugin.registeredDevices !== undefined)
|
|
2357
1640
|
plugin.registeredDevices++;
|
|
2358
|
-
// Add the device to the DeviceManager
|
|
2359
1641
|
this.devices.set(device);
|
|
2360
|
-
// Subscribe to the attributes changed event
|
|
2361
1642
|
await this.subscribeAttributeChanged(plugin, device);
|
|
2362
1643
|
this.log.info(`Added and registered bridged endpoint (${plugin.registeredDevices}) ${dev}${device.deviceName}${nf} (${dev}${device.id}${nf}) for plugin ${plg}${pluginName}${nf}`);
|
|
2363
1644
|
}
|
|
2364
|
-
/**
|
|
2365
|
-
* Removes a MatterbridgeEndpoint from the specified plugin.
|
|
2366
|
-
*
|
|
2367
|
-
* @param {string} pluginName - The name of the plugin.
|
|
2368
|
-
* @param {MatterbridgeEndpoint} device - The device to remove as a bridged endpoint.
|
|
2369
|
-
* @returns {Promise<void>} A promise that resolves when the bridged endpoint has been removed.
|
|
2370
|
-
*/
|
|
2371
1645
|
async removeBridgedEndpoint(pluginName, device) {
|
|
2372
1646
|
this.log.debug(`Removing bridged endpoint ${plg}${pluginName}${db}:${dev}${device.deviceName}${db} (${zb}${device.name}${db}) for plugin ${plg}${pluginName}${db}`);
|
|
2373
|
-
// Check if the plugin is registered
|
|
2374
1647
|
const plugin = this.plugins.get(pluginName);
|
|
2375
1648
|
if (!plugin) {
|
|
2376
1649
|
this.log.error(`Error removing bridged endpoint ${dev}${device.deviceName}${er} (${zb}${device.name}${er}) for plugin ${plg}${pluginName}${er}: plugin not found`);
|
|
2377
1650
|
return;
|
|
2378
1651
|
}
|
|
2379
|
-
// Register and add the device to the matterbridge aggregator node
|
|
2380
1652
|
if (this.bridgeMode === 'bridge') {
|
|
2381
1653
|
if (!this.aggregatorNode) {
|
|
2382
1654
|
this.log.error(`Error removing bridged endpoint ${dev}${device.deviceName}${er} (${zb}${device.name}${er}) for plugin ${plg}${pluginName}${er}: aggregator node not found`);
|
|
@@ -2389,7 +1661,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
2389
1661
|
}
|
|
2390
1662
|
else if (this.bridgeMode === 'childbridge') {
|
|
2391
1663
|
if (plugin.type === 'AccessoryPlatform') {
|
|
2392
|
-
// Nothing to do here since the server node has no aggregator node but only the device itself
|
|
2393
1664
|
}
|
|
2394
1665
|
else if (plugin.type === 'DynamicPlatform') {
|
|
2395
1666
|
if (!plugin.aggregatorNode) {
|
|
@@ -2402,21 +1673,8 @@ export class Matterbridge extends EventEmitter {
|
|
|
2402
1673
|
if (plugin.registeredDevices !== undefined)
|
|
2403
1674
|
plugin.registeredDevices--;
|
|
2404
1675
|
}
|
|
2405
|
-
// Remove the device from the DeviceManager
|
|
2406
1676
|
this.devices.remove(device);
|
|
2407
1677
|
}
|
|
2408
|
-
/**
|
|
2409
|
-
* Removes all bridged endpoints from the specified plugin.
|
|
2410
|
-
*
|
|
2411
|
-
* @param {string} pluginName - The name of the plugin.
|
|
2412
|
-
* @param {number} [delay] - The delay in milliseconds between removing each bridged endpoint (default: 0).
|
|
2413
|
-
* @returns {Promise<void>} A promise that resolves when all bridged endpoints have been removed.
|
|
2414
|
-
*
|
|
2415
|
-
* @remarks
|
|
2416
|
-
* This method iterates through all devices in the DeviceManager and removes each bridged endpoint associated with the specified plugin.
|
|
2417
|
-
* It also applies a delay between each removal if specified.
|
|
2418
|
-
* The delay is useful to allow the controllers to receive a single subscription for each device removed.
|
|
2419
|
-
*/
|
|
2420
1678
|
async removeAllBridgedEndpoints(pluginName, delay = 0) {
|
|
2421
1679
|
this.log.debug(`Removing all bridged endpoints for plugin ${plg}${pluginName}${db}${delay > 0 ? ` with delay ${delay} ms` : ''}`);
|
|
2422
1680
|
for (const device of this.devices.array().filter((device) => device.plugin === pluginName)) {
|
|
@@ -2427,24 +1685,13 @@ export class Matterbridge extends EventEmitter {
|
|
|
2427
1685
|
if (delay > 0)
|
|
2428
1686
|
await wait(2000);
|
|
2429
1687
|
}
|
|
2430
|
-
/**
|
|
2431
|
-
* Subscribes to the attribute change event for the given device and plugin.
|
|
2432
|
-
* Specifically, it listens for changes in the 'reachable' attribute of the
|
|
2433
|
-
* BridgedDeviceBasicInformationServer cluster server of the bridged device or BasicInformationServer cluster server of server node.
|
|
2434
|
-
*
|
|
2435
|
-
* @param {Plugin} plugin - The plugin associated with the device.
|
|
2436
|
-
* @param {MatterbridgeEndpoint} device - The device to subscribe to attribute changes for.
|
|
2437
|
-
* @returns {Promise<void>} A promise that resolves when the subscription is set up.
|
|
2438
|
-
*/
|
|
2439
1688
|
async subscribeAttributeChanged(plugin, device) {
|
|
2440
1689
|
if (!plugin || !device || !device.plugin || !device.serialNumber || !device.uniqueId || !device.maybeNumber)
|
|
2441
1690
|
return;
|
|
2442
1691
|
this.log.info(`Subscribing attributes for endpoint ${dev}${device.deviceName}${nf} (${dev}${device.id}${nf}) plugin ${plg}${plugin.name}${nf}`);
|
|
2443
|
-
// Subscribe to the reachable$Changed event of the BasicInformationServer cluster server of the server node in childbridge mode
|
|
2444
1692
|
if (this.bridgeMode === 'childbridge' && plugin.type === 'AccessoryPlatform' && plugin.serverNode) {
|
|
2445
1693
|
plugin.serverNode.eventsOf(BasicInformationServer).reachable$Changed?.on((reachable) => {
|
|
2446
1694
|
this.log.info(`Accessory endpoint ${dev}${device.deviceName}${nf} (${dev}${device.id}${nf}) is ${reachable ? 'reachable' : 'unreachable'}`);
|
|
2447
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
2448
1695
|
this.frontend.wssSendAttributeChangedMessage(device.plugin, device.serialNumber, device.uniqueId, device.number, device.id, 'BasicInformation', 'reachable', reachable);
|
|
2449
1696
|
});
|
|
2450
1697
|
}
|
|
@@ -2494,7 +1741,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
2494
1741
|
this.log.debug(`Subscribing to endpoint ${or}${device.id}${db}:${or}${device.number}${db} attribute ${dev}${sub.cluster}${db}.${dev}${sub.attribute}${db} changes...`);
|
|
2495
1742
|
await device.subscribeAttribute(sub.cluster, sub.attribute, (value) => {
|
|
2496
1743
|
this.log.debug(`Bridged endpoint ${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}`);
|
|
2497
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
2498
1744
|
this.frontend.wssSendAttributeChangedMessage(device.plugin, device.serialNumber, device.uniqueId, device.number, device.id, sub.cluster, sub.attribute, value);
|
|
2499
1745
|
});
|
|
2500
1746
|
}
|
|
@@ -2503,19 +1749,12 @@ export class Matterbridge extends EventEmitter {
|
|
|
2503
1749
|
this.log.debug(`Subscribing to child endpoint ${or}${child.id}${db}:${or}${child.number}${db} attribute ${dev}${sub.cluster}${db}.${dev}${sub.attribute}${db} changes...`);
|
|
2504
1750
|
await child.subscribeAttribute(sub.cluster, sub.attribute, (value) => {
|
|
2505
1751
|
this.log.debug(`Bridged child endpoint ${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}`);
|
|
2506
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
2507
1752
|
this.frontend.wssSendAttributeChangedMessage(device.plugin, device.serialNumber, device.uniqueId, child.number, child.id, sub.cluster, sub.attribute, value);
|
|
2508
1753
|
});
|
|
2509
1754
|
}
|
|
2510
1755
|
}
|
|
2511
1756
|
}
|
|
2512
1757
|
}
|
|
2513
|
-
/**
|
|
2514
|
-
* Sanitizes the fabric information by converting bigint properties to strings because `res.json` doesn't support bigint.
|
|
2515
|
-
*
|
|
2516
|
-
* @param {ExposedFabricInformation[]} fabricInfo - The array of exposed fabric information objects.
|
|
2517
|
-
* @returns {SanitizedExposedFabricInformation[]} An array of sanitized exposed fabric information objects.
|
|
2518
|
-
*/
|
|
2519
1758
|
sanitizeFabricInformations(fabricInfo) {
|
|
2520
1759
|
return fabricInfo.map((info) => {
|
|
2521
1760
|
return {
|
|
@@ -2529,12 +1768,6 @@ export class Matterbridge extends EventEmitter {
|
|
|
2529
1768
|
};
|
|
2530
1769
|
});
|
|
2531
1770
|
}
|
|
2532
|
-
/**
|
|
2533
|
-
* Sanitizes the session information by converting bigint properties to strings because `res.json` doesn't support bigint.
|
|
2534
|
-
*
|
|
2535
|
-
* @param {SessionsBehavior.Session[]} sessions - The array of session information objects.
|
|
2536
|
-
* @returns {SanitizedSession[]} An array of sanitized session information objects.
|
|
2537
|
-
*/
|
|
2538
1771
|
sanitizeSessionInformation(sessions) {
|
|
2539
1772
|
return sessions
|
|
2540
1773
|
.filter((session) => session.isPeerActive)
|
|
@@ -2561,21 +1794,7 @@ export class Matterbridge extends EventEmitter {
|
|
|
2561
1794
|
};
|
|
2562
1795
|
});
|
|
2563
1796
|
}
|
|
2564
|
-
/**
|
|
2565
|
-
* Sets the reachability of the specified aggregator node bridged devices and trigger.
|
|
2566
|
-
*
|
|
2567
|
-
* @param {Endpoint<AggregatorEndpoint>} aggregatorNode - The aggregator node to set the reachability for.
|
|
2568
|
-
* @param {boolean} reachable - A boolean indicating the reachability status to set.
|
|
2569
|
-
*/
|
|
2570
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2571
1797
|
async setAggregatorReachability(aggregatorNode, reachable) {
|
|
2572
|
-
/*
|
|
2573
|
-
for (const child of aggregatorNode.parts) {
|
|
2574
|
-
this.log.debug(`Setting reachability of ${(child as unknown as MatterbridgeEndpoint)?.deviceName} to ${reachable}`);
|
|
2575
|
-
await child.setStateOf(BridgedDeviceBasicInformationServer, { reachable });
|
|
2576
|
-
child.act((agent) => child.eventsOf(BridgedDeviceBasicInformationServer).reachableChanged.emit({ reachableNewValue: true }, agent.context));
|
|
2577
|
-
}
|
|
2578
|
-
*/
|
|
2579
1798
|
}
|
|
2580
1799
|
getVendorIdName = (vendorId) => {
|
|
2581
1800
|
if (!vendorId)
|
|
@@ -2615,11 +1834,10 @@ export class Matterbridge extends EventEmitter {
|
|
|
2615
1834
|
case 0x1488:
|
|
2616
1835
|
vendorName = '(ShortcutLabsFlic)';
|
|
2617
1836
|
break;
|
|
2618
|
-
case 65521:
|
|
1837
|
+
case 65521:
|
|
2619
1838
|
vendorName = '(MatterTest)';
|
|
2620
1839
|
break;
|
|
2621
1840
|
}
|
|
2622
1841
|
return vendorName;
|
|
2623
1842
|
};
|
|
2624
1843
|
}
|
|
2625
|
-
//# sourceMappingURL=matterbridge.js.map
|