@matterbridge/core 3.5.3
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/LICENSE +202 -0
- package/README.md +22 -0
- package/dist/cli.d.ts +29 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +268 -0
- package/dist/cli.js.map +1 -0
- package/dist/cliEmitter.d.ts +50 -0
- package/dist/cliEmitter.d.ts.map +1 -0
- package/dist/cliEmitter.js +49 -0
- package/dist/cliEmitter.js.map +1 -0
- package/dist/cliHistory.d.ts +48 -0
- package/dist/cliHistory.d.ts.map +1 -0
- package/dist/cliHistory.js +826 -0
- package/dist/cliHistory.js.map +1 -0
- package/dist/clusters/export.d.ts +2 -0
- package/dist/clusters/export.d.ts.map +1 -0
- package/dist/clusters/export.js +3 -0
- package/dist/clusters/export.js.map +1 -0
- package/dist/crypto/attestationDecoder.d.ts +180 -0
- package/dist/crypto/attestationDecoder.d.ts.map +1 -0
- package/dist/crypto/attestationDecoder.js +176 -0
- package/dist/crypto/attestationDecoder.js.map +1 -0
- package/dist/crypto/declarationDecoder.d.ts +72 -0
- package/dist/crypto/declarationDecoder.d.ts.map +1 -0
- package/dist/crypto/declarationDecoder.js +241 -0
- package/dist/crypto/declarationDecoder.js.map +1 -0
- package/dist/crypto/extract/342/200/220cert/342/200/220extensions.d.ts +9 -0
- package/dist/crypto/extract/342/200/220cert/342/200/220extensions.d.ts.map +1 -0
- package/dist/crypto/extract/342/200/220cert/342/200/220extensions.js +120 -0
- package/dist/crypto/extract/342/200/220cert/342/200/220extensions.js.map +1 -0
- package/dist/crypto/read-extensions.d.ts +2 -0
- package/dist/crypto/read-extensions.d.ts.map +1 -0
- package/dist/crypto/read-extensions.js +81 -0
- package/dist/crypto/read-extensions.js.map +1 -0
- package/dist/crypto/testData.d.ts +31 -0
- package/dist/crypto/testData.d.ts.map +1 -0
- package/dist/crypto/testData.js +131 -0
- package/dist/crypto/testData.js.map +1 -0
- package/dist/crypto/walk-der.d.ts +2 -0
- package/dist/crypto/walk-der.d.ts.map +1 -0
- package/dist/crypto/walk-der.js +165 -0
- package/dist/crypto/walk-der.js.map +1 -0
- package/dist/deviceManager.d.ts +135 -0
- package/dist/deviceManager.d.ts.map +1 -0
- package/dist/deviceManager.js +270 -0
- package/dist/deviceManager.js.map +1 -0
- package/dist/devices/airConditioner.d.ts +98 -0
- package/dist/devices/airConditioner.d.ts.map +1 -0
- package/dist/devices/airConditioner.js +74 -0
- package/dist/devices/airConditioner.js.map +1 -0
- package/dist/devices/basicVideoPlayer.d.ts +88 -0
- package/dist/devices/basicVideoPlayer.d.ts.map +1 -0
- package/dist/devices/basicVideoPlayer.js +155 -0
- package/dist/devices/basicVideoPlayer.js.map +1 -0
- package/dist/devices/batteryStorage.d.ts +48 -0
- package/dist/devices/batteryStorage.d.ts.map +1 -0
- package/dist/devices/batteryStorage.js +75 -0
- package/dist/devices/batteryStorage.js.map +1 -0
- package/dist/devices/castingVideoPlayer.d.ts +79 -0
- package/dist/devices/castingVideoPlayer.d.ts.map +1 -0
- package/dist/devices/castingVideoPlayer.js +101 -0
- package/dist/devices/castingVideoPlayer.js.map +1 -0
- package/dist/devices/cooktop.d.ts +61 -0
- package/dist/devices/cooktop.d.ts.map +1 -0
- package/dist/devices/cooktop.js +77 -0
- package/dist/devices/cooktop.js.map +1 -0
- package/dist/devices/dishwasher.d.ts +71 -0
- package/dist/devices/dishwasher.d.ts.map +1 -0
- package/dist/devices/dishwasher.js +130 -0
- package/dist/devices/dishwasher.js.map +1 -0
- package/dist/devices/evse.d.ts +76 -0
- package/dist/devices/evse.d.ts.map +1 -0
- package/dist/devices/evse.js +156 -0
- package/dist/devices/evse.js.map +1 -0
- package/dist/devices/export.d.ts +19 -0
- package/dist/devices/export.d.ts.map +1 -0
- package/dist/devices/export.js +23 -0
- package/dist/devices/export.js.map +1 -0
- package/dist/devices/extractorHood.d.ts +46 -0
- package/dist/devices/extractorHood.d.ts.map +1 -0
- package/dist/devices/extractorHood.js +78 -0
- package/dist/devices/extractorHood.js.map +1 -0
- package/dist/devices/heatPump.d.ts +47 -0
- package/dist/devices/heatPump.d.ts.map +1 -0
- package/dist/devices/heatPump.js +84 -0
- package/dist/devices/heatPump.js.map +1 -0
- package/dist/devices/laundryDryer.d.ts +67 -0
- package/dist/devices/laundryDryer.d.ts.map +1 -0
- package/dist/devices/laundryDryer.js +106 -0
- package/dist/devices/laundryDryer.js.map +1 -0
- package/dist/devices/laundryWasher.d.ts +81 -0
- package/dist/devices/laundryWasher.d.ts.map +1 -0
- package/dist/devices/laundryWasher.js +147 -0
- package/dist/devices/laundryWasher.js.map +1 -0
- package/dist/devices/microwaveOven.d.ts +168 -0
- package/dist/devices/microwaveOven.d.ts.map +1 -0
- package/dist/devices/microwaveOven.js +179 -0
- package/dist/devices/microwaveOven.js.map +1 -0
- package/dist/devices/oven.d.ts +105 -0
- package/dist/devices/oven.d.ts.map +1 -0
- package/dist/devices/oven.js +190 -0
- package/dist/devices/oven.js.map +1 -0
- package/dist/devices/refrigerator.d.ts +118 -0
- package/dist/devices/refrigerator.d.ts.map +1 -0
- package/dist/devices/refrigerator.js +186 -0
- package/dist/devices/refrigerator.js.map +1 -0
- package/dist/devices/roboticVacuumCleaner.d.ts +112 -0
- package/dist/devices/roboticVacuumCleaner.d.ts.map +1 -0
- package/dist/devices/roboticVacuumCleaner.js +268 -0
- package/dist/devices/roboticVacuumCleaner.js.map +1 -0
- package/dist/devices/solarPower.d.ts +40 -0
- package/dist/devices/solarPower.d.ts.map +1 -0
- package/dist/devices/solarPower.js +59 -0
- package/dist/devices/solarPower.js.map +1 -0
- package/dist/devices/speaker.d.ts +87 -0
- package/dist/devices/speaker.d.ts.map +1 -0
- package/dist/devices/speaker.js +120 -0
- package/dist/devices/speaker.js.map +1 -0
- package/dist/devices/temperatureControl.d.ts +166 -0
- package/dist/devices/temperatureControl.d.ts.map +1 -0
- package/dist/devices/temperatureControl.js +78 -0
- package/dist/devices/temperatureControl.js.map +1 -0
- package/dist/devices/waterHeater.d.ts +111 -0
- package/dist/devices/waterHeater.d.ts.map +1 -0
- package/dist/devices/waterHeater.js +166 -0
- package/dist/devices/waterHeater.js.map +1 -0
- package/dist/dgram/export.d.ts +2 -0
- package/dist/dgram/export.d.ts.map +1 -0
- package/dist/dgram/export.js +2 -0
- package/dist/dgram/export.js.map +1 -0
- package/dist/export.d.ts +32 -0
- package/dist/export.d.ts.map +1 -0
- package/dist/export.js +39 -0
- package/dist/export.js.map +1 -0
- package/dist/frontend.d.ts +248 -0
- package/dist/frontend.d.ts.map +1 -0
- package/dist/frontend.js +2605 -0
- package/dist/frontend.js.map +1 -0
- package/dist/helpers.d.ts +48 -0
- package/dist/helpers.d.ts.map +1 -0
- package/dist/helpers.js +161 -0
- package/dist/helpers.js.map +1 -0
- package/dist/jestutils/export.d.ts +2 -0
- package/dist/jestutils/export.d.ts.map +1 -0
- package/dist/jestutils/export.js +2 -0
- package/dist/jestutils/export.js.map +1 -0
- package/dist/jestutils/jestHelpers.d.ts +349 -0
- package/dist/jestutils/jestHelpers.d.ts.map +1 -0
- package/dist/jestutils/jestHelpers.js +980 -0
- package/dist/jestutils/jestHelpers.js.map +1 -0
- package/dist/matter/behaviors.d.ts +2 -0
- package/dist/matter/behaviors.d.ts.map +1 -0
- package/dist/matter/behaviors.js +3 -0
- package/dist/matter/behaviors.js.map +1 -0
- package/dist/matter/clusters.d.ts +2 -0
- package/dist/matter/clusters.d.ts.map +1 -0
- package/dist/matter/clusters.js +3 -0
- package/dist/matter/clusters.js.map +1 -0
- package/dist/matter/devices.d.ts +2 -0
- package/dist/matter/devices.d.ts.map +1 -0
- package/dist/matter/devices.js +3 -0
- package/dist/matter/devices.js.map +1 -0
- package/dist/matter/endpoints.d.ts +2 -0
- package/dist/matter/endpoints.d.ts.map +1 -0
- package/dist/matter/endpoints.js +3 -0
- package/dist/matter/endpoints.js.map +1 -0
- package/dist/matter/export.d.ts +4 -0
- package/dist/matter/export.d.ts.map +1 -0
- package/dist/matter/export.js +5 -0
- package/dist/matter/export.js.map +1 -0
- package/dist/matter/types.d.ts +2 -0
- package/dist/matter/types.d.ts.map +1 -0
- package/dist/matter/types.js +3 -0
- package/dist/matter/types.js.map +1 -0
- package/dist/matterNode.d.ts +341 -0
- package/dist/matterNode.d.ts.map +1 -0
- package/dist/matterNode.js +1329 -0
- package/dist/matterNode.js.map +1 -0
- package/dist/matterbridge.d.ts +544 -0
- package/dist/matterbridge.d.ts.map +1 -0
- package/dist/matterbridge.js +2880 -0
- package/dist/matterbridge.js.map +1 -0
- package/dist/matterbridgeAccessoryPlatform.d.ts +49 -0
- package/dist/matterbridgeAccessoryPlatform.d.ts.map +1 -0
- package/dist/matterbridgeAccessoryPlatform.js +80 -0
- package/dist/matterbridgeAccessoryPlatform.js.map +1 -0
- package/dist/matterbridgeBehaviors.d.ts +2428 -0
- package/dist/matterbridgeBehaviors.d.ts.map +1 -0
- package/dist/matterbridgeBehaviors.js +620 -0
- package/dist/matterbridgeBehaviors.js.map +1 -0
- package/dist/matterbridgeDeviceTypes.d.ts +744 -0
- package/dist/matterbridgeDeviceTypes.d.ts.map +1 -0
- package/dist/matterbridgeDeviceTypes.js +1312 -0
- package/dist/matterbridgeDeviceTypes.js.map +1 -0
- package/dist/matterbridgeDynamicPlatform.d.ts +49 -0
- package/dist/matterbridgeDynamicPlatform.d.ts.map +1 -0
- package/dist/matterbridgeDynamicPlatform.js +80 -0
- package/dist/matterbridgeDynamicPlatform.js.map +1 -0
- package/dist/matterbridgeEndpoint.d.ts +1548 -0
- package/dist/matterbridgeEndpoint.d.ts.map +1 -0
- package/dist/matterbridgeEndpoint.js +2883 -0
- package/dist/matterbridgeEndpoint.js.map +1 -0
- package/dist/matterbridgeEndpointHelpers.d.ts +1855 -0
- package/dist/matterbridgeEndpointHelpers.d.ts.map +1 -0
- package/dist/matterbridgeEndpointHelpers.js +1270 -0
- package/dist/matterbridgeEndpointHelpers.js.map +1 -0
- package/dist/matterbridgeEndpointTypes.d.ts +172 -0
- package/dist/matterbridgeEndpointTypes.d.ts.map +1 -0
- package/dist/matterbridgeEndpointTypes.js +28 -0
- package/dist/matterbridgeEndpointTypes.js.map +1 -0
- package/dist/matterbridgePlatform.d.ts +520 -0
- package/dist/matterbridgePlatform.d.ts.map +1 -0
- package/dist/matterbridgePlatform.js +921 -0
- package/dist/matterbridgePlatform.js.map +1 -0
- package/dist/mb_coap.d.ts +24 -0
- package/dist/mb_coap.d.ts.map +1 -0
- package/dist/mb_coap.js +89 -0
- package/dist/mb_coap.js.map +1 -0
- package/dist/mb_health.d.ts +77 -0
- package/dist/mb_health.d.ts.map +1 -0
- package/dist/mb_health.js +147 -0
- package/dist/mb_health.js.map +1 -0
- package/dist/mb_mdns.d.ts +24 -0
- package/dist/mb_mdns.d.ts.map +1 -0
- package/dist/mb_mdns.js +285 -0
- package/dist/mb_mdns.js.map +1 -0
- package/dist/pluginManager.d.ts +388 -0
- package/dist/pluginManager.d.ts.map +1 -0
- package/dist/pluginManager.js +1574 -0
- package/dist/pluginManager.js.map +1 -0
- package/dist/spawn.d.ts +33 -0
- package/dist/spawn.d.ts.map +1 -0
- package/dist/spawn.js +165 -0
- package/dist/spawn.js.map +1 -0
- package/dist/utils/export.d.ts +2 -0
- package/dist/utils/export.d.ts.map +1 -0
- package/dist/utils/export.js +2 -0
- package/dist/utils/export.js.map +1 -0
- package/dist/workers/brand.d.ts +25 -0
- package/dist/workers/brand.d.ts.map +1 -0
- package/dist/workers/brand.extend.d.ts +10 -0
- package/dist/workers/brand.extend.d.ts.map +1 -0
- package/dist/workers/brand.extend.js +15 -0
- package/dist/workers/brand.extend.js.map +1 -0
- package/dist/workers/brand.invalid.d.ts +9 -0
- package/dist/workers/brand.invalid.d.ts.map +1 -0
- package/dist/workers/brand.invalid.js +19 -0
- package/dist/workers/brand.invalid.js.map +1 -0
- package/dist/workers/brand.js +67 -0
- package/dist/workers/brand.js.map +1 -0
- package/dist/workers/clusterTypes.d.ts +47 -0
- package/dist/workers/clusterTypes.d.ts.map +1 -0
- package/dist/workers/clusterTypes.js +57 -0
- package/dist/workers/clusterTypes.js.map +1 -0
- package/dist/workers/frontendWorker.d.ts +2 -0
- package/dist/workers/frontendWorker.d.ts.map +1 -0
- package/dist/workers/frontendWorker.js +90 -0
- package/dist/workers/frontendWorker.js.map +1 -0
- package/dist/workers/helloWorld.d.ts +2 -0
- package/dist/workers/helloWorld.d.ts.map +1 -0
- package/dist/workers/helloWorld.js +135 -0
- package/dist/workers/helloWorld.js.map +1 -0
- package/dist/workers/matterWorker.d.ts +2 -0
- package/dist/workers/matterWorker.d.ts.map +1 -0
- package/dist/workers/matterWorker.js +104 -0
- package/dist/workers/matterWorker.js.map +1 -0
- package/dist/workers/matterbridgeWorker.d.ts +2 -0
- package/dist/workers/matterbridgeWorker.d.ts.map +1 -0
- package/dist/workers/matterbridgeWorker.js +75 -0
- package/dist/workers/matterbridgeWorker.js.map +1 -0
- package/dist/workers/messageLab.d.ts +134 -0
- package/dist/workers/messageLab.d.ts.map +1 -0
- package/dist/workers/messageLab.js +129 -0
- package/dist/workers/messageLab.js.map +1 -0
- package/dist/workers/testWorker.d.ts +2 -0
- package/dist/workers/testWorker.d.ts.map +1 -0
- package/dist/workers/testWorker.js +45 -0
- package/dist/workers/testWorker.js.map +1 -0
- package/dist/workers/usage.d.ts +19 -0
- package/dist/workers/usage.d.ts.map +1 -0
- package/dist/workers/usage.js +140 -0
- package/dist/workers/usage.js.map +1 -0
- package/dist/workers/workerManager.d.ts +115 -0
- package/dist/workers/workerManager.d.ts.map +1 -0
- package/dist/workers/workerManager.js +464 -0
- package/dist/workers/workerManager.js.map +1 -0
- package/dist/workers/workerServer.d.ts +126 -0
- package/dist/workers/workerServer.d.ts.map +1 -0
- package/dist/workers/workerServer.js +340 -0
- package/dist/workers/workerServer.js.map +1 -0
- package/dist/workers/workerTypes.d.ts +23 -0
- package/dist/workers/workerTypes.d.ts.map +1 -0
- package/dist/workers/workerTypes.js +3 -0
- package/dist/workers/workerTypes.js.map +1 -0
- package/package.json +120 -0
|
@@ -0,0 +1,1574 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file contains the Plugins class.
|
|
3
|
+
*
|
|
4
|
+
* @file plugins.ts
|
|
5
|
+
* @author Luca Liguori
|
|
6
|
+
* @created 2024-07-14
|
|
7
|
+
* @version 1.3.5
|
|
8
|
+
* @license Apache-2.0
|
|
9
|
+
*
|
|
10
|
+
* Copyright 2024, 2025, 2026 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 jsdoc/reject-function-type */
|
|
25
|
+
/* eslint-disable jsdoc/reject-any-type */
|
|
26
|
+
// Node.js import
|
|
27
|
+
import EventEmitter from 'node:events';
|
|
28
|
+
// AnsiLogger
|
|
29
|
+
import { AnsiLogger, UNDERLINE, UNDERLINEOFF, BLUE, db, er, nf, nt, rs, wr, debugStringify, CYAN } from 'node-ansi-logger';
|
|
30
|
+
// @matterbridge
|
|
31
|
+
import { BroadcastServer } from '@matterbridge/thread';
|
|
32
|
+
import { hasParameter, inspectError, logError } from '@matterbridge/utils';
|
|
33
|
+
import { plg, typ } from '@matterbridge/types';
|
|
34
|
+
import { assertMatterbridgePlatform } from './matterbridgePlatform.js';
|
|
35
|
+
import { isMatterbridgeAccessoryPlatform } from './matterbridgeAccessoryPlatform.js';
|
|
36
|
+
import { isMatterbridgeDynamicPlatform } from './matterbridgeDynamicPlatform.js';
|
|
37
|
+
/**
|
|
38
|
+
* Manages Matterbridge plugins.
|
|
39
|
+
*/
|
|
40
|
+
export class PluginManager extends EventEmitter {
|
|
41
|
+
matterbridge;
|
|
42
|
+
_plugins = new Map();
|
|
43
|
+
log;
|
|
44
|
+
server;
|
|
45
|
+
debug = hasParameter('debug') || hasParameter('verbose');
|
|
46
|
+
verbose = hasParameter('verbose');
|
|
47
|
+
/**
|
|
48
|
+
* Creates an instance of PluginManager.
|
|
49
|
+
*
|
|
50
|
+
* @param {Matterbridge} matterbridge - The Matterbridge instance.
|
|
51
|
+
*/
|
|
52
|
+
constructor(matterbridge) {
|
|
53
|
+
super();
|
|
54
|
+
this.matterbridge = matterbridge;
|
|
55
|
+
this.log = new AnsiLogger({ logName: 'PluginManager', logTimestampFormat: 4 /* TimestampFormat.TIME_MILLIS */, logLevel: hasParameter('debug') ? "debug" /* LogLevel.DEBUG */ : "info" /* LogLevel.INFO */ });
|
|
56
|
+
this.log.debug('Matterbridge plugin manager starting...');
|
|
57
|
+
this.server = new BroadcastServer('plugins', this.log);
|
|
58
|
+
this.server.on('broadcast_message', this.msgHandler.bind(this));
|
|
59
|
+
this.log.debug('Matterbridge plugin manager started');
|
|
60
|
+
}
|
|
61
|
+
destroy() {
|
|
62
|
+
this.server.off('broadcast_message', this.msgHandler.bind(this));
|
|
63
|
+
this.server.close();
|
|
64
|
+
}
|
|
65
|
+
async msgHandler(msg) {
|
|
66
|
+
if (this.server.isWorkerRequest(msg)) {
|
|
67
|
+
if (this.verbose)
|
|
68
|
+
this.log.debug(`Received request message ${CYAN}${msg.type}${db} from ${CYAN}${msg.src}${db}: ${debugStringify(msg)}${db}`);
|
|
69
|
+
switch (msg.type) {
|
|
70
|
+
case 'get_log_level':
|
|
71
|
+
this.server.respond({ ...msg, result: { logLevel: this.log.logLevel } });
|
|
72
|
+
break;
|
|
73
|
+
case 'set_log_level':
|
|
74
|
+
this.log.logLevel = msg.params.logLevel;
|
|
75
|
+
this.server.respond({ ...msg, result: { logLevel: this.log.logLevel } });
|
|
76
|
+
break;
|
|
77
|
+
case 'plugins_length':
|
|
78
|
+
this.server.respond({ ...msg, result: { length: this.length } });
|
|
79
|
+
break;
|
|
80
|
+
case 'plugins_size':
|
|
81
|
+
this.server.respond({ ...msg, result: { size: this.size } });
|
|
82
|
+
break;
|
|
83
|
+
case 'plugins_has':
|
|
84
|
+
this.server.respond({ ...msg, result: { has: this.has(msg.params.name) } });
|
|
85
|
+
break;
|
|
86
|
+
case 'plugins_get':
|
|
87
|
+
{
|
|
88
|
+
const plugin = this.get(msg.params.name);
|
|
89
|
+
if (plugin) {
|
|
90
|
+
this.server.respond({ ...msg, result: { plugin: this.toApiPlugin(plugin) } });
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
this.log.debug(`***Plugin ${plg}${msg.params.name}${db} not found in plugins_get`);
|
|
94
|
+
this.server.respond({ ...msg, result: { plugin: undefined } });
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
break;
|
|
98
|
+
case 'plugins_set':
|
|
99
|
+
this.server.respond({ ...msg, result: { plugin: this.set(msg.params.plugin) } });
|
|
100
|
+
break;
|
|
101
|
+
case 'plugins_storagepluginarray':
|
|
102
|
+
this.server.respond({ ...msg, result: { plugins: this.storagePluginArray() } });
|
|
103
|
+
break;
|
|
104
|
+
case 'plugins_apipluginarray':
|
|
105
|
+
this.server.respond({ ...msg, result: { plugins: this.apiPluginArray() } });
|
|
106
|
+
break;
|
|
107
|
+
case 'plugins_install':
|
|
108
|
+
this.server.respond({ ...msg, result: { packageName: msg.params.packageName, success: await this.install(msg.params.packageName) } });
|
|
109
|
+
break;
|
|
110
|
+
case 'plugins_uninstall':
|
|
111
|
+
this.server.respond({ ...msg, result: { packageName: msg.params.packageName, success: await this.uninstall(msg.params.packageName) } });
|
|
112
|
+
break;
|
|
113
|
+
case 'plugins_add':
|
|
114
|
+
{
|
|
115
|
+
const plugin = await this.add(msg.params.nameOrPath);
|
|
116
|
+
if (plugin) {
|
|
117
|
+
this.server.respond({ ...msg, result: { plugin: this.toApiPlugin(plugin) } });
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
this.server.respond({ ...msg, result: { plugin } });
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
break;
|
|
124
|
+
case 'plugins_remove':
|
|
125
|
+
{
|
|
126
|
+
const plugin = await this.remove(msg.params.nameOrPath);
|
|
127
|
+
if (plugin) {
|
|
128
|
+
this.server.respond({ ...msg, result: { plugin: this.toApiPlugin(plugin) } });
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
this.server.respond({ ...msg, result: { plugin } });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
break;
|
|
135
|
+
case 'plugins_enable':
|
|
136
|
+
{
|
|
137
|
+
const plugin = await this.enable(msg.params.nameOrPath);
|
|
138
|
+
if (plugin) {
|
|
139
|
+
this.server.respond({ ...msg, result: { plugin: this.toApiPlugin(plugin) } });
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
this.server.respond({ ...msg, result: { plugin } });
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
break;
|
|
146
|
+
case 'plugins_disable':
|
|
147
|
+
{
|
|
148
|
+
const plugin = await this.disable(msg.params.nameOrPath);
|
|
149
|
+
if (plugin) {
|
|
150
|
+
this.server.respond({ ...msg, result: { plugin: this.toApiPlugin(plugin) } });
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
this.server.respond({ ...msg, result: { plugin } });
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
break;
|
|
157
|
+
case 'plugins_load':
|
|
158
|
+
{
|
|
159
|
+
const platform = await this.load(msg.params.plugin);
|
|
160
|
+
if (platform) {
|
|
161
|
+
this.server.respond({ ...msg, result: { platform: {} } });
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
this.server.respond({ ...msg, result: { platform } });
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
break;
|
|
168
|
+
case 'plugins_start':
|
|
169
|
+
{
|
|
170
|
+
const plugin = await this.start(msg.params.plugin, msg.params.message, msg.params.configure);
|
|
171
|
+
if (plugin) {
|
|
172
|
+
this.server.respond({ ...msg, result: { plugin: this.toApiPlugin(plugin) } });
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
this.server.respond({ ...msg, result: { plugin } });
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
break;
|
|
179
|
+
case 'plugins_configure':
|
|
180
|
+
{
|
|
181
|
+
const plugin = await this.configure(msg.params.plugin);
|
|
182
|
+
if (plugin) {
|
|
183
|
+
this.server.respond({ ...msg, result: { plugin: this.toApiPlugin(plugin) } });
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
this.server.respond({ ...msg, result: { plugin } });
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
break;
|
|
190
|
+
case 'plugins_shutdown':
|
|
191
|
+
{
|
|
192
|
+
const plugin = await this.shutdown(msg.params.plugin, msg.params.reason, msg.params.removeAllDevices, msg.params.force);
|
|
193
|
+
if (plugin) {
|
|
194
|
+
this.server.respond({ ...msg, result: { plugin: this.toApiPlugin(plugin) } });
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
this.server.respond({ ...msg, result: { plugin } });
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
break;
|
|
201
|
+
case 'plugins_getschema':
|
|
202
|
+
{
|
|
203
|
+
const plugin = this.get(msg.params.name);
|
|
204
|
+
if (plugin) {
|
|
205
|
+
this.server.respond({ ...msg, result: { schema: plugin.schemaJson } });
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
this.server.respond({ ...msg, result: { schema: undefined } });
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
break;
|
|
212
|
+
case 'plugins_setschema':
|
|
213
|
+
{
|
|
214
|
+
const plugin = this.get(msg.params.name);
|
|
215
|
+
if (plugin) {
|
|
216
|
+
plugin.schemaJson = msg.params.schema;
|
|
217
|
+
this.server.respond({ ...msg, result: { success: true } });
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
this.server.respond({ ...msg, error: `Plugin ${msg.params.name} not found` });
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
break;
|
|
224
|
+
case 'plugins_saveconfigfromjson':
|
|
225
|
+
{
|
|
226
|
+
const plugin = this.get(msg.params.name);
|
|
227
|
+
if (plugin) {
|
|
228
|
+
this.saveConfigFromJson(plugin, msg.params.config, msg.params.restartRequired); // No await as it's not necessary to wait
|
|
229
|
+
this.server.respond({ ...msg, result: { success: true } });
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
this.server.respond({ ...msg, error: `Plugin ${msg.params.name} not found` });
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
break;
|
|
236
|
+
case 'plugins_set_latest_version':
|
|
237
|
+
{
|
|
238
|
+
const plugin = this.get(msg.params.plugin.name);
|
|
239
|
+
if (plugin) {
|
|
240
|
+
plugin.latestVersion = msg.params.version;
|
|
241
|
+
this.server.respond({ ...msg, result: { success: true } });
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
this.server.respond({ ...msg, error: `Plugin ${msg.params.plugin.name} not found` });
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
break;
|
|
248
|
+
case 'plugins_set_dev_version':
|
|
249
|
+
{
|
|
250
|
+
const plugin = this.get(msg.params.plugin.name);
|
|
251
|
+
if (plugin) {
|
|
252
|
+
plugin.devVersion = msg.params.version;
|
|
253
|
+
this.server.respond({ ...msg, result: { success: true } });
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
this.server.respond({ ...msg, error: `Plugin ${msg.params.plugin.name} not found` });
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
break;
|
|
260
|
+
default:
|
|
261
|
+
if (this.verbose)
|
|
262
|
+
this.log.debug(`Unknown broadcast message ${CYAN}${msg.type}${db} from ${CYAN}${msg.src}${db}`);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Gets the number of plugins.
|
|
268
|
+
*
|
|
269
|
+
* @returns {number} The number of plugins.
|
|
270
|
+
*/
|
|
271
|
+
get length() {
|
|
272
|
+
return this._plugins.size;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Gets the number of plugins.
|
|
276
|
+
*
|
|
277
|
+
* @returns {number} The number of plugins.
|
|
278
|
+
*/
|
|
279
|
+
get size() {
|
|
280
|
+
return this._plugins.size;
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Checks if a plugin with the specified name exists.
|
|
284
|
+
*
|
|
285
|
+
* @param {string} name - The name of the plugin.
|
|
286
|
+
* @returns {boolean} True if the plugin exists, false otherwise.
|
|
287
|
+
*/
|
|
288
|
+
has(name) {
|
|
289
|
+
return this._plugins.has(name);
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Gets a plugin by its name.
|
|
293
|
+
*
|
|
294
|
+
* @param {string} name - The name of the plugin.
|
|
295
|
+
* @returns {Plugin | undefined} The plugin, or undefined if not found.
|
|
296
|
+
*/
|
|
297
|
+
get(name) {
|
|
298
|
+
return this._plugins.get(name);
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Adds a plugin to the manager.
|
|
302
|
+
*
|
|
303
|
+
* @param {Plugin} plugin - The plugin to add.
|
|
304
|
+
* @returns {Plugin} The added plugin.
|
|
305
|
+
*/
|
|
306
|
+
set(plugin) {
|
|
307
|
+
this._plugins.set(plugin.name, plugin);
|
|
308
|
+
return plugin;
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Clears all plugins from the manager.
|
|
312
|
+
*/
|
|
313
|
+
clear() {
|
|
314
|
+
this._plugins.clear();
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Converts a plugin or API plugin to a storage plugin.
|
|
318
|
+
*
|
|
319
|
+
* @param {Plugin | ApiPlugin} plugin - The plugin or API plugin to convert.
|
|
320
|
+
* @returns {StoragePlugin} The converted storage plugin.
|
|
321
|
+
*/
|
|
322
|
+
toStoragePlugin(plugin) {
|
|
323
|
+
return {
|
|
324
|
+
name: plugin.name,
|
|
325
|
+
path: plugin.path,
|
|
326
|
+
type: plugin.type,
|
|
327
|
+
version: plugin.version,
|
|
328
|
+
description: plugin.description,
|
|
329
|
+
author: plugin.author,
|
|
330
|
+
enabled: plugin.enabled,
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Converts a plugin to an API plugin.
|
|
335
|
+
*
|
|
336
|
+
* @param {Plugin} plugin - The plugin to convert.
|
|
337
|
+
* @returns {ApiPlugin} The converted API plugin.
|
|
338
|
+
*/
|
|
339
|
+
toApiPlugin(plugin) {
|
|
340
|
+
return {
|
|
341
|
+
name: plugin.name,
|
|
342
|
+
version: plugin.version,
|
|
343
|
+
description: plugin.description,
|
|
344
|
+
author: plugin.author,
|
|
345
|
+
path: plugin.path,
|
|
346
|
+
type: plugin.type,
|
|
347
|
+
latestVersion: plugin.latestVersion,
|
|
348
|
+
devVersion: plugin.devVersion,
|
|
349
|
+
homepage: plugin.homepage,
|
|
350
|
+
help: plugin.help,
|
|
351
|
+
changelog: plugin.changelog,
|
|
352
|
+
funding: plugin.funding,
|
|
353
|
+
locked: plugin.locked,
|
|
354
|
+
error: plugin.error,
|
|
355
|
+
enabled: plugin.enabled,
|
|
356
|
+
loaded: plugin.loaded,
|
|
357
|
+
started: plugin.started,
|
|
358
|
+
configured: plugin.configured,
|
|
359
|
+
restartRequired: plugin.restartRequired,
|
|
360
|
+
registeredDevices: plugin.registeredDevices,
|
|
361
|
+
configJson: plugin.configJson,
|
|
362
|
+
schemaJson: plugin.schemaJson,
|
|
363
|
+
hasWhiteList: plugin.hasWhiteList,
|
|
364
|
+
hasBlackList: plugin.hasBlackList,
|
|
365
|
+
matter: plugin.serverNode ? this.matterbridge.getServerNodeData(plugin.serverNode) : undefined,
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Gets an array of all plugins.
|
|
370
|
+
*
|
|
371
|
+
* @returns {Plugin[]} An array of all plugins.
|
|
372
|
+
*/
|
|
373
|
+
array() {
|
|
374
|
+
return Array.from(this._plugins.values());
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Gets a StoragePlugin array of all plugins suitable for serialization.
|
|
378
|
+
*
|
|
379
|
+
* @returns {StoragePlugin[]} An array of all plugins.
|
|
380
|
+
*/
|
|
381
|
+
storagePluginArray() {
|
|
382
|
+
const storagePlugins = [];
|
|
383
|
+
for (const plugin of this._plugins.values()) {
|
|
384
|
+
storagePlugins.push(this.toStoragePlugin(plugin));
|
|
385
|
+
}
|
|
386
|
+
return storagePlugins;
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Gets an ApiPlugin array of all plugins suitable for serialization.
|
|
390
|
+
*
|
|
391
|
+
* @returns {ApiPlugin[]} An array of all plugins.
|
|
392
|
+
*/
|
|
393
|
+
apiPluginArray() {
|
|
394
|
+
const apiPlugins = [];
|
|
395
|
+
for (const plugin of this._plugins.values()) {
|
|
396
|
+
apiPlugins.push(this.toApiPlugin(plugin));
|
|
397
|
+
}
|
|
398
|
+
return apiPlugins;
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* Gets an iterator for the plugins.
|
|
402
|
+
*
|
|
403
|
+
* @returns {IterableIterator<Plugin>} An iterator for the plugins.
|
|
404
|
+
*/
|
|
405
|
+
[Symbol.iterator]() {
|
|
406
|
+
return this._plugins.values();
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Executes a provided function once for each plugin.
|
|
410
|
+
*
|
|
411
|
+
* @param {Function} callback - The function to execute for each plugin.
|
|
412
|
+
* @returns {Promise<void>}
|
|
413
|
+
*/
|
|
414
|
+
async forEach(callback) {
|
|
415
|
+
if (this.size === 0)
|
|
416
|
+
return;
|
|
417
|
+
const tasks = Array.from(this._plugins.values()).map(async (plugin) => {
|
|
418
|
+
try {
|
|
419
|
+
await callback(plugin);
|
|
420
|
+
}
|
|
421
|
+
catch (err) {
|
|
422
|
+
logError(this.log, `Error processing forEach plugin ${plg}${plugin.name}${er}`, err);
|
|
423
|
+
}
|
|
424
|
+
});
|
|
425
|
+
await Promise.all(tasks);
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Sets the log level for the plugin manager.
|
|
429
|
+
*
|
|
430
|
+
* @param {LogLevel} logLevel - The log level to set.
|
|
431
|
+
*/
|
|
432
|
+
set logLevel(logLevel) {
|
|
433
|
+
this.log.logLevel = logLevel;
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* Loads registered plugins from storage.
|
|
437
|
+
*
|
|
438
|
+
* @returns {Promise<StoragePlugin[]>} A promise that resolves to an array of registered plugins.
|
|
439
|
+
*/
|
|
440
|
+
async loadFromStorage() {
|
|
441
|
+
if (!this.matterbridge.nodeContext) {
|
|
442
|
+
throw new Error('loadFromStorage: node context is not available.');
|
|
443
|
+
}
|
|
444
|
+
// Load the array from storage and convert it to a map
|
|
445
|
+
const pluginsArray = await this.matterbridge.nodeContext.get('plugins', []);
|
|
446
|
+
for (const plugin of pluginsArray)
|
|
447
|
+
this._plugins.set(plugin.name, plugin);
|
|
448
|
+
this.log.debug(`Loaded ${BLUE}${pluginsArray.length}${db} plugins from storage`);
|
|
449
|
+
return pluginsArray;
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Saves registered plugins to storage.
|
|
453
|
+
*
|
|
454
|
+
* @returns {Promise<number>} A promise that resolves to the number of registered plugins.
|
|
455
|
+
*/
|
|
456
|
+
async saveToStorage() {
|
|
457
|
+
if (!this.matterbridge.nodeContext) {
|
|
458
|
+
throw new Error('loadFromStorage: node context is not available.');
|
|
459
|
+
}
|
|
460
|
+
// Convert the map to an array
|
|
461
|
+
const plugins = [];
|
|
462
|
+
for (const plugin of this.array()) {
|
|
463
|
+
plugins.push({
|
|
464
|
+
name: plugin.name,
|
|
465
|
+
path: plugin.path,
|
|
466
|
+
type: plugin.type,
|
|
467
|
+
version: plugin.version,
|
|
468
|
+
description: plugin.description,
|
|
469
|
+
author: plugin.author,
|
|
470
|
+
enabled: plugin.enabled,
|
|
471
|
+
});
|
|
472
|
+
}
|
|
473
|
+
await this.matterbridge.nodeContext.set('plugins', plugins);
|
|
474
|
+
this.log.debug(`Saved ${BLUE}${plugins.length}${db} plugins to storage`);
|
|
475
|
+
return plugins.length;
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* Resolves the name of a plugin by loading and parsing its package.json file.
|
|
479
|
+
* It will first try to resolve the path as is, then in the global modules directory, and finally in the matterbridge plugin directory.
|
|
480
|
+
*
|
|
481
|
+
* @param {string} nameOrPath - The name of the plugin or the path to the plugin's package.json file.
|
|
482
|
+
* @returns {Promise<string | null>} A promise that resolves to the path of the plugin's package.json file or null if it could not be resolved.
|
|
483
|
+
*/
|
|
484
|
+
async resolve(nameOrPath) {
|
|
485
|
+
const { default: path } = await import('node:path');
|
|
486
|
+
const { promises } = await import('node:fs');
|
|
487
|
+
if (!nameOrPath.endsWith('package.json'))
|
|
488
|
+
nameOrPath = path.join(nameOrPath, 'package.json');
|
|
489
|
+
// Resolve the package.json of the plugin
|
|
490
|
+
let packageJsonPath = path.resolve(nameOrPath);
|
|
491
|
+
this.log.debug(`Resolving plugin path ${plg}${packageJsonPath}${db}`);
|
|
492
|
+
// Check if the package.json file exists at the specified path or next try in the global modules directory
|
|
493
|
+
try {
|
|
494
|
+
await promises.access(packageJsonPath);
|
|
495
|
+
}
|
|
496
|
+
catch {
|
|
497
|
+
this.log.debug(`Package.json not found at ${plg}${packageJsonPath}${db}`);
|
|
498
|
+
packageJsonPath = path.join(this.matterbridge.globalModulesDirectory, nameOrPath);
|
|
499
|
+
this.log.debug(`Trying at ${plg}${packageJsonPath}${db}`);
|
|
500
|
+
}
|
|
501
|
+
// Check if the package.json file exists at the global modules directory or next try in the matterbridge plugin directory
|
|
502
|
+
try {
|
|
503
|
+
await promises.access(packageJsonPath);
|
|
504
|
+
}
|
|
505
|
+
catch {
|
|
506
|
+
this.log.debug(`Package.json not found at ${plg}${packageJsonPath}${db}`);
|
|
507
|
+
packageJsonPath = path.join(this.matterbridge.matterbridgePluginDirectory, nameOrPath);
|
|
508
|
+
this.log.debug(`Trying at ${plg}${packageJsonPath}${db}`);
|
|
509
|
+
}
|
|
510
|
+
try {
|
|
511
|
+
// Load the package.json of the plugin or fails if even not found in the matterbridge plugin directory
|
|
512
|
+
const packageJson = JSON.parse(await promises.readFile(packageJsonPath, 'utf8'));
|
|
513
|
+
// Check for main issues
|
|
514
|
+
if (!packageJson.name) {
|
|
515
|
+
this.log.error(`Package.json name not found at ${packageJsonPath}`);
|
|
516
|
+
return null;
|
|
517
|
+
}
|
|
518
|
+
if (!packageJson.type || packageJson.type !== 'module') {
|
|
519
|
+
this.log.error(`Plugin at ${packageJsonPath} is not a module`);
|
|
520
|
+
return null;
|
|
521
|
+
}
|
|
522
|
+
if (!packageJson.main) {
|
|
523
|
+
this.log.error(`Plugin at ${packageJsonPath} has no main entrypoint in package.json`);
|
|
524
|
+
return null;
|
|
525
|
+
}
|
|
526
|
+
// Check for @project-chip and @matter packages in dependencies and devDependencies
|
|
527
|
+
const checkForProjectChipPackages = (dependencies) => {
|
|
528
|
+
return Object.keys(dependencies).filter((pkg) => pkg.startsWith('@project-chip') || pkg.startsWith('@matter'));
|
|
529
|
+
};
|
|
530
|
+
const projectChipDependencies = checkForProjectChipPackages(packageJson.dependencies || {});
|
|
531
|
+
if (projectChipDependencies.length > 0) {
|
|
532
|
+
this.log.error(`Found @project-chip packages "${projectChipDependencies.join(', ')}" in plugin dependencies.`);
|
|
533
|
+
this.log.error(`Please open an issue on the plugin repository to remove them.`);
|
|
534
|
+
this.server.request({
|
|
535
|
+
type: 'frontend_snackbarmessage',
|
|
536
|
+
src: 'plugins',
|
|
537
|
+
dst: 'frontend',
|
|
538
|
+
params: { message: `Found not allowed package in plugin ${packageJson.name} package.json`, timeout: 30, severity: 'error' },
|
|
539
|
+
});
|
|
540
|
+
return null;
|
|
541
|
+
}
|
|
542
|
+
const projectChipDevDependencies = checkForProjectChipPackages(packageJson.devDependencies || {});
|
|
543
|
+
if (projectChipDevDependencies.length > 0) {
|
|
544
|
+
this.log.error(`Found @project-chip packages "${projectChipDevDependencies.join(', ')}" in plugin devDependencies.`);
|
|
545
|
+
this.log.error(`Please open an issue on the plugin repository to remove them.`);
|
|
546
|
+
this.server.request({
|
|
547
|
+
type: 'frontend_snackbarmessage',
|
|
548
|
+
src: 'plugins',
|
|
549
|
+
dst: 'frontend',
|
|
550
|
+
params: { message: `Found not allowed package in plugin ${packageJson.name} package.json`, timeout: 30, severity: 'error' },
|
|
551
|
+
});
|
|
552
|
+
return null;
|
|
553
|
+
}
|
|
554
|
+
const projectChipPeerDependencies = checkForProjectChipPackages(packageJson.peerDependencies || {});
|
|
555
|
+
if (projectChipPeerDependencies.length > 0) {
|
|
556
|
+
this.log.error(`Found @project-chip packages "${projectChipPeerDependencies.join(', ')}" in plugin peerDependencies.`);
|
|
557
|
+
this.log.error(`Please open an issue on the plugin repository to remove them.`);
|
|
558
|
+
this.server.request({
|
|
559
|
+
type: 'frontend_snackbarmessage',
|
|
560
|
+
src: 'plugins',
|
|
561
|
+
dst: 'frontend',
|
|
562
|
+
params: { message: `Found not allowed package in plugin ${packageJson.name} package.json`, timeout: 30, severity: 'error' },
|
|
563
|
+
});
|
|
564
|
+
return null;
|
|
565
|
+
}
|
|
566
|
+
// Check for matterbridge package in dependencies and devDependencies
|
|
567
|
+
const checkForMatterbridgePackage = (dependencies) => {
|
|
568
|
+
return Object.keys(dependencies).filter((pkg) => pkg === 'matterbridge');
|
|
569
|
+
};
|
|
570
|
+
const matterbridgeDependencies = checkForMatterbridgePackage(packageJson.dependencies || {});
|
|
571
|
+
if (matterbridgeDependencies.length > 0) {
|
|
572
|
+
this.log.error(`Found matterbridge package in the plugin dependencies.`);
|
|
573
|
+
this.log.error(`Please open an issue on the plugin repository to remove them.`);
|
|
574
|
+
this.server.request({
|
|
575
|
+
type: 'frontend_snackbarmessage',
|
|
576
|
+
src: 'plugins',
|
|
577
|
+
dst: 'frontend',
|
|
578
|
+
params: { message: `Found not allowed package in plugin ${packageJson.name} package.json`, timeout: 30, severity: 'error' },
|
|
579
|
+
});
|
|
580
|
+
return null;
|
|
581
|
+
}
|
|
582
|
+
const matterbridgeDevDependencies = checkForMatterbridgePackage(packageJson.devDependencies || {});
|
|
583
|
+
if (matterbridgeDevDependencies.length > 0) {
|
|
584
|
+
this.log.error(`Found matterbridge package in the plugin devDependencies.`);
|
|
585
|
+
this.log.error(`Please open an issue on the plugin repository to remove them.`);
|
|
586
|
+
this.server.request({
|
|
587
|
+
type: 'frontend_snackbarmessage',
|
|
588
|
+
src: 'plugins',
|
|
589
|
+
dst: 'frontend',
|
|
590
|
+
params: { message: `Found not allowed package in plugin ${packageJson.name} package.json`, timeout: 30, severity: 'error' },
|
|
591
|
+
});
|
|
592
|
+
return null;
|
|
593
|
+
}
|
|
594
|
+
const matterbridgePeerDependencies = checkForMatterbridgePackage(packageJson.peerDependencies || {});
|
|
595
|
+
if (matterbridgePeerDependencies.length > 0) {
|
|
596
|
+
this.log.error(`Found matterbridge package in the plugin peerDependencies.`);
|
|
597
|
+
this.log.error(`Please open an issue on the plugin repository to remove them.`);
|
|
598
|
+
this.server.request({
|
|
599
|
+
type: 'frontend_snackbarmessage',
|
|
600
|
+
src: 'plugins',
|
|
601
|
+
dst: 'frontend',
|
|
602
|
+
params: { message: `Found not allowed package in plugin ${packageJson.name} package.json`, timeout: 30, severity: 'error' },
|
|
603
|
+
});
|
|
604
|
+
return null;
|
|
605
|
+
}
|
|
606
|
+
this.log.debug(`Resolved plugin path ${plg}${nameOrPath}${db}: ${packageJsonPath}`);
|
|
607
|
+
return packageJsonPath;
|
|
608
|
+
}
|
|
609
|
+
catch (err) {
|
|
610
|
+
logError(this.log, `Failed to resolve plugin path ${plg}${nameOrPath}${er}`, err);
|
|
611
|
+
this.server.request({
|
|
612
|
+
type: 'frontend_snackbarmessage',
|
|
613
|
+
src: 'plugins',
|
|
614
|
+
dst: 'frontend',
|
|
615
|
+
params: { message: `Failed to resolve plugin path ${nameOrPath}`, timeout: 30, severity: 'error' },
|
|
616
|
+
});
|
|
617
|
+
return null;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
/**
|
|
621
|
+
* Installs a package globally using npm.
|
|
622
|
+
*
|
|
623
|
+
* @param {string} packageName - The name of the package to install.
|
|
624
|
+
* @returns {Promise<boolean>} A promise that resolves to true if the installation was successful, false otherwise.
|
|
625
|
+
*/
|
|
626
|
+
async install(packageName) {
|
|
627
|
+
this.log.debug(`Installing plugin ${plg}${packageName}${db}...`);
|
|
628
|
+
const { spawnCommand } = await import('./spawn.js');
|
|
629
|
+
if (await spawnCommand('npm', ['install', '-g', packageName, '--omit=dev', '--verbose'], 'install', packageName)) {
|
|
630
|
+
this.matterbridge.restartRequired = true;
|
|
631
|
+
this.matterbridge.fixedRestartRequired = true;
|
|
632
|
+
packageName = packageName.replace(/@.*$/, ''); // Remove @version if present
|
|
633
|
+
if (packageName !== 'matterbridge') {
|
|
634
|
+
if (!this.has(packageName))
|
|
635
|
+
await this.add(packageName);
|
|
636
|
+
const plugin = this.get(packageName);
|
|
637
|
+
if (plugin && !plugin.loaded)
|
|
638
|
+
await this.load(plugin);
|
|
639
|
+
}
|
|
640
|
+
else {
|
|
641
|
+
if (this.matterbridge.restartMode !== '') {
|
|
642
|
+
await this.matterbridge.shutdownProcess();
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
this.log.info(`Installed plugin ${plg}${packageName}${db} successfully`);
|
|
646
|
+
return true;
|
|
647
|
+
}
|
|
648
|
+
else {
|
|
649
|
+
this.log.error(`Failed to install plugin ${plg}${packageName}${er}`);
|
|
650
|
+
return false;
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
/**
|
|
654
|
+
* Uninstalls a package globally using npm.
|
|
655
|
+
*
|
|
656
|
+
* @param {string} packageName - The name of the package to uninstall.
|
|
657
|
+
* @returns {Promise<boolean>} A promise that resolves to true if the uninstallation was successful, false otherwise.
|
|
658
|
+
*/
|
|
659
|
+
async uninstall(packageName) {
|
|
660
|
+
this.log.debug(`Uninstalling plugin ${plg}${packageName}${db}...`);
|
|
661
|
+
const { spawnCommand } = await import('./spawn.js');
|
|
662
|
+
packageName = packageName.replace(/@.*$/, '');
|
|
663
|
+
if (packageName === 'matterbridge')
|
|
664
|
+
return false;
|
|
665
|
+
if (this.has(packageName)) {
|
|
666
|
+
const plugin = this.get(packageName);
|
|
667
|
+
if (plugin && plugin.loaded)
|
|
668
|
+
await this.shutdown(plugin, 'Matterbridge is uninstalling the plugin');
|
|
669
|
+
await this.remove(packageName);
|
|
670
|
+
}
|
|
671
|
+
if (await spawnCommand('npm', ['uninstall', '-g', packageName, '--verbose'], 'uninstall', packageName)) {
|
|
672
|
+
this.log.info(`Uninstalled plugin ${plg}${packageName}${db} successfully`);
|
|
673
|
+
return true;
|
|
674
|
+
}
|
|
675
|
+
else {
|
|
676
|
+
this.log.error(`Failed to uninstall plugin ${plg}${packageName}${er}`);
|
|
677
|
+
return false;
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
/**
|
|
681
|
+
* Get the author of a plugin from its package.json.
|
|
682
|
+
*
|
|
683
|
+
* @param {Record<string, string | number | Record<string, string | number | object>>} packageJson - The package.json object of the plugin.
|
|
684
|
+
* @returns {string} The author of the plugin, or 'Unknown author' if not found.
|
|
685
|
+
*/
|
|
686
|
+
getAuthor(packageJson) {
|
|
687
|
+
if (packageJson.author && typeof packageJson.author === 'string')
|
|
688
|
+
return packageJson.author;
|
|
689
|
+
else if (packageJson.author && typeof packageJson.author === 'object' && packageJson.author.name && typeof packageJson.author.name === 'string')
|
|
690
|
+
return packageJson.author.name;
|
|
691
|
+
return 'Unknown author';
|
|
692
|
+
}
|
|
693
|
+
/**
|
|
694
|
+
* Get the homepage of a plugin from its package.json.
|
|
695
|
+
*
|
|
696
|
+
* @param {Record<string, string | number | Record<string, string | number | object>>} packageJson - The package.json object of the plugin.
|
|
697
|
+
* @returns {string | undefined} The homepage of the plugin, or undefined if not found.
|
|
698
|
+
*/
|
|
699
|
+
getHomepage(packageJson) {
|
|
700
|
+
if (packageJson.homepage && typeof packageJson.homepage === 'string' && packageJson.homepage.includes('http')) {
|
|
701
|
+
return packageJson.homepage.replace('git+', '').replace('.git', '');
|
|
702
|
+
}
|
|
703
|
+
else if (packageJson.repository &&
|
|
704
|
+
typeof packageJson.repository === 'object' &&
|
|
705
|
+
packageJson.repository.url &&
|
|
706
|
+
typeof packageJson.repository.url === 'string' &&
|
|
707
|
+
packageJson.repository.url.includes('http')) {
|
|
708
|
+
return packageJson.repository.url.replace('git+', '').replace('.git', '');
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* Get the help URL of a plugin from its package.json.
|
|
713
|
+
*
|
|
714
|
+
* @param {Record<string, string | number | Record<string, string | number | object>>} packageJson - The package.json object of the plugin.
|
|
715
|
+
* @returns {string | undefined} The URL to the help page or to the README file, or undefined if not found.
|
|
716
|
+
*/
|
|
717
|
+
getHelp(packageJson) {
|
|
718
|
+
// If there's a help field that looks like a URL, return it.
|
|
719
|
+
if (packageJson.help && typeof packageJson.help === 'string' && packageJson.help.startsWith('http')) {
|
|
720
|
+
return packageJson.help;
|
|
721
|
+
}
|
|
722
|
+
else if (packageJson.repository &&
|
|
723
|
+
typeof packageJson.repository === 'object' &&
|
|
724
|
+
packageJson.repository.url &&
|
|
725
|
+
typeof packageJson.repository.url === 'string' &&
|
|
726
|
+
packageJson.repository.url.includes('http')) {
|
|
727
|
+
return packageJson.repository.url.replace('git+', '').replace('.git', '') + '/blob/main/README.md';
|
|
728
|
+
}
|
|
729
|
+
else if (packageJson.homepage && typeof packageJson.homepage === 'string' && packageJson.homepage.includes('http')) {
|
|
730
|
+
return packageJson.homepage.replace('git+', '').replace('.git', '');
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
/**
|
|
734
|
+
* Get the changelog URL of a plugin from its package.json.
|
|
735
|
+
*
|
|
736
|
+
* @param {Record<string, string | number | Record<string, string | number | object>>} packageJson - The package.json object of the plugin.
|
|
737
|
+
* @returns {string | undefined} The URL to the CHANGELOG file, or undefined if not found.
|
|
738
|
+
*/
|
|
739
|
+
getChangelog(packageJson) {
|
|
740
|
+
// If there's a changelog field that looks like a URL, return it.
|
|
741
|
+
if (packageJson.changelog && typeof packageJson.changelog === 'string' && packageJson.changelog.startsWith('http')) {
|
|
742
|
+
return packageJson.changelog;
|
|
743
|
+
}
|
|
744
|
+
else if (packageJson.repository &&
|
|
745
|
+
typeof packageJson.repository === 'object' &&
|
|
746
|
+
packageJson.repository.url &&
|
|
747
|
+
typeof packageJson.repository.url === 'string' &&
|
|
748
|
+
packageJson.repository.url.includes('http')) {
|
|
749
|
+
return packageJson.repository.url.replace('git+', '').replace('.git', '') + '/blob/main/CHANGELOG.md';
|
|
750
|
+
}
|
|
751
|
+
else if (packageJson.homepage && typeof packageJson.homepage === 'string' && packageJson.homepage.includes('http')) {
|
|
752
|
+
return packageJson.homepage.replace('git+', '').replace('.git', '');
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
/**
|
|
756
|
+
* Get the first funding URL(s) of a plugin from its package.json.
|
|
757
|
+
*
|
|
758
|
+
* @param {Record<string, any>} packageJson - The package.json object of the plugin.
|
|
759
|
+
* @returns {string | undefined} The first funding URLs, or undefined if not found.
|
|
760
|
+
*/
|
|
761
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
762
|
+
getFunding(packageJson) {
|
|
763
|
+
const funding = packageJson.funding;
|
|
764
|
+
if (!funding)
|
|
765
|
+
return undefined;
|
|
766
|
+
if (typeof funding === 'string' && !funding.startsWith('http'))
|
|
767
|
+
return;
|
|
768
|
+
if (typeof funding === 'string' && funding.startsWith('http'))
|
|
769
|
+
return funding;
|
|
770
|
+
// Normalize funding into an array.
|
|
771
|
+
const fundingEntries = Array.isArray(funding) ? funding : [funding];
|
|
772
|
+
for (const entry of fundingEntries) {
|
|
773
|
+
if (entry && typeof entry === 'string' && entry.startsWith('http')) {
|
|
774
|
+
// If the funding entry is a string, assume it is a URL.
|
|
775
|
+
return entry;
|
|
776
|
+
}
|
|
777
|
+
else if (entry && typeof entry === 'object' && typeof entry.url === 'string' && entry.url.startsWith('http')) {
|
|
778
|
+
// If it's an object with a 'url' property, use that.
|
|
779
|
+
return entry.url;
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
/**
|
|
784
|
+
* Parses the plugin package.json and returns it.
|
|
785
|
+
* It will also log warnings and errors for missing or invalid fields.
|
|
786
|
+
* It will return null if critical errors are found.
|
|
787
|
+
*
|
|
788
|
+
* @param {Plugin | PluginName} plugin - The plugin to load the package from.
|
|
789
|
+
* @returns {Promise<Record<string, string | number | object> | null>} A promise that resolves to the parsed package.json object or null if it could not be parsed.
|
|
790
|
+
*/
|
|
791
|
+
async parse(plugin) {
|
|
792
|
+
const { promises } = await import('node:fs');
|
|
793
|
+
if (typeof plugin === 'string') {
|
|
794
|
+
const p = this._plugins.get(plugin);
|
|
795
|
+
if (!p) {
|
|
796
|
+
this.log.error(`Plugin ${plg}${plugin}${er} not found`);
|
|
797
|
+
return null;
|
|
798
|
+
}
|
|
799
|
+
plugin = p;
|
|
800
|
+
}
|
|
801
|
+
try {
|
|
802
|
+
this.log.debug(`Parsing package.json of plugin ${plg}${plugin.name}${db}`);
|
|
803
|
+
const packageJson = JSON.parse(await promises.readFile(plugin.path, 'utf8'));
|
|
804
|
+
if (!packageJson.name)
|
|
805
|
+
this.log.warn(`Plugin ${plg}${plugin.name}${wr} has no name in package.json`);
|
|
806
|
+
if (!packageJson.version)
|
|
807
|
+
this.log.warn(`Plugin ${plg}${plugin.name}${wr} has no version in package.json`);
|
|
808
|
+
if (!packageJson.description)
|
|
809
|
+
this.log.warn(`Plugin ${plg}${plugin.name}${wr} has no description in package.json`);
|
|
810
|
+
if (!packageJson.author)
|
|
811
|
+
this.log.warn(`Plugin ${plg}${plugin.name}${wr} has no author in package.json`);
|
|
812
|
+
if (!packageJson.homepage)
|
|
813
|
+
this.log.info(`Plugin ${plg}${plugin.name}${nf} has no homepage in package.json`);
|
|
814
|
+
if (!packageJson.type || packageJson.type !== 'module')
|
|
815
|
+
this.log.error(`Plugin ${plg}${plugin.name}${er} is not a module`);
|
|
816
|
+
if (!packageJson.main)
|
|
817
|
+
this.log.error(`Plugin ${plg}${plugin.name}${er} has no main entrypoint in package.json`);
|
|
818
|
+
plugin.name = packageJson.name || 'Unknown name';
|
|
819
|
+
plugin.version = packageJson.version || '1.0.0';
|
|
820
|
+
plugin.description = packageJson.description || 'Unknown description';
|
|
821
|
+
plugin.author = this.getAuthor(packageJson);
|
|
822
|
+
plugin.homepage = this.getHomepage(packageJson);
|
|
823
|
+
plugin.help = this.getHelp(packageJson);
|
|
824
|
+
plugin.changelog = this.getChangelog(packageJson);
|
|
825
|
+
plugin.funding = this.getFunding(packageJson);
|
|
826
|
+
if (!plugin.type)
|
|
827
|
+
this.log.warn(`Plugin ${plg}${plugin.name}${wr} has no type`);
|
|
828
|
+
// Check for @project-chip and @matter packages in dependencies and devDependencies
|
|
829
|
+
const checkForProjectChipPackages = (dependencies) => {
|
|
830
|
+
return Object.keys(dependencies).filter((pkg) => pkg.startsWith('@project-chip') || pkg.startsWith('@matter'));
|
|
831
|
+
};
|
|
832
|
+
const projectChipDependencies = checkForProjectChipPackages(packageJson.dependencies || {});
|
|
833
|
+
if (projectChipDependencies.length > 0) {
|
|
834
|
+
this.log.error(`Found @project-chip packages "${projectChipDependencies.join(', ')}" in plugin ${plg}${plugin.name}${er} dependencies.`);
|
|
835
|
+
this.log.error(`Please open an issue on the plugin repository to remove them.`);
|
|
836
|
+
this.server.request({
|
|
837
|
+
type: 'frontend_snackbarmessage',
|
|
838
|
+
src: 'plugins',
|
|
839
|
+
dst: 'frontend',
|
|
840
|
+
params: { message: `Found not allowed package in plugin ${packageJson.name} package.json`, timeout: 30, severity: 'error' },
|
|
841
|
+
});
|
|
842
|
+
return null;
|
|
843
|
+
}
|
|
844
|
+
const projectChipDevDependencies = checkForProjectChipPackages(packageJson.devDependencies || {});
|
|
845
|
+
if (projectChipDevDependencies.length > 0) {
|
|
846
|
+
this.log.error(`Found @project-chip packages "${projectChipDevDependencies.join(', ')}" in plugin ${plg}${plugin.name}${er} devDependencies.`);
|
|
847
|
+
this.log.error(`Please open an issue on the plugin repository to remove them.`);
|
|
848
|
+
this.server.request({
|
|
849
|
+
type: 'frontend_snackbarmessage',
|
|
850
|
+
src: 'plugins',
|
|
851
|
+
dst: 'frontend',
|
|
852
|
+
params: { message: `Found not allowed package in plugin ${packageJson.name} package.json`, timeout: 30, severity: 'error' },
|
|
853
|
+
});
|
|
854
|
+
return null;
|
|
855
|
+
}
|
|
856
|
+
const projectChipPeerDependencies = checkForProjectChipPackages(packageJson.peerDependencies || {});
|
|
857
|
+
if (projectChipPeerDependencies.length > 0) {
|
|
858
|
+
this.log.error(`Found @project-chip packages "${projectChipPeerDependencies.join(', ')}" in plugin ${plg}${plugin.name}${er} peerDependencies.`);
|
|
859
|
+
this.log.error(`Please open an issue on the plugin repository to remove them.`);
|
|
860
|
+
this.server.request({
|
|
861
|
+
type: 'frontend_snackbarmessage',
|
|
862
|
+
src: 'plugins',
|
|
863
|
+
dst: 'frontend',
|
|
864
|
+
params: { message: `Found not allowed package in plugin ${packageJson.name} package.json`, timeout: 30, severity: 'error' },
|
|
865
|
+
});
|
|
866
|
+
return null;
|
|
867
|
+
}
|
|
868
|
+
// Check for matterbridge package in dependencies and devDependencies
|
|
869
|
+
const checkForMatterbridgePackage = (dependencies) => {
|
|
870
|
+
return Object.keys(dependencies).filter((pkg) => pkg === 'matterbridge');
|
|
871
|
+
};
|
|
872
|
+
const matterbridgeDependencies = checkForMatterbridgePackage(packageJson.dependencies || {});
|
|
873
|
+
if (matterbridgeDependencies.length > 0) {
|
|
874
|
+
this.log.error(`Found matterbridge package in the plugin ${plg}${plugin.name}${er} dependencies.`);
|
|
875
|
+
this.log.error(`Please open an issue on the plugin repository to remove them.`);
|
|
876
|
+
this.server.request({
|
|
877
|
+
type: 'frontend_snackbarmessage',
|
|
878
|
+
src: 'plugins',
|
|
879
|
+
dst: 'frontend',
|
|
880
|
+
params: { message: `Found not allowed package in plugin ${packageJson.name} package.json`, timeout: 30, severity: 'error' },
|
|
881
|
+
});
|
|
882
|
+
return null;
|
|
883
|
+
}
|
|
884
|
+
const matterbridgeDevDependencies = checkForMatterbridgePackage(packageJson.devDependencies || {});
|
|
885
|
+
if (matterbridgeDevDependencies.length > 0) {
|
|
886
|
+
this.log.error(`Found matterbridge package in the plugin ${plg}${plugin.name}${er} devDependencies.`);
|
|
887
|
+
this.log.error(`Please open an issue on the plugin repository to remove them.`);
|
|
888
|
+
this.server.request({
|
|
889
|
+
type: 'frontend_snackbarmessage',
|
|
890
|
+
src: 'plugins',
|
|
891
|
+
dst: 'frontend',
|
|
892
|
+
params: { message: `Found not allowed package in plugin ${packageJson.name} package.json`, timeout: 30, severity: 'error' },
|
|
893
|
+
});
|
|
894
|
+
return null;
|
|
895
|
+
}
|
|
896
|
+
const matterbridgePeerDependencies = checkForMatterbridgePackage(packageJson.peerDependencies || {});
|
|
897
|
+
if (matterbridgePeerDependencies.length > 0) {
|
|
898
|
+
this.log.error(`Found matterbridge package in the plugin ${plg}${plugin.name}${er} peerDependencies.`);
|
|
899
|
+
this.log.error(`Please open an issue on the plugin repository to remove them.`);
|
|
900
|
+
this.server.request({
|
|
901
|
+
type: 'frontend_snackbarmessage',
|
|
902
|
+
src: 'plugins',
|
|
903
|
+
dst: 'frontend',
|
|
904
|
+
params: { message: `Found not allowed package in plugin ${packageJson.name} package.json`, timeout: 30, severity: 'error' },
|
|
905
|
+
});
|
|
906
|
+
return null;
|
|
907
|
+
}
|
|
908
|
+
return packageJson;
|
|
909
|
+
}
|
|
910
|
+
catch (err) {
|
|
911
|
+
logError(this.log, `Failed to parse package.json of plugin ${plg}${plugin.name}${er}`, err);
|
|
912
|
+
this.server.request({
|
|
913
|
+
type: 'frontend_snackbarmessage',
|
|
914
|
+
src: 'plugins',
|
|
915
|
+
dst: 'frontend',
|
|
916
|
+
params: { message: `Failed to parse package.json of plugin ${plugin.name}`, timeout: 30, severity: 'error' },
|
|
917
|
+
});
|
|
918
|
+
plugin.error = true;
|
|
919
|
+
return null;
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
/**
|
|
923
|
+
* Enables a plugin by its name or path.
|
|
924
|
+
*
|
|
925
|
+
* This method enables a plugin by setting its `enabled` property to `true` and saving the updated
|
|
926
|
+
* plugin information to storage. It first checks if the plugin is already registered in the `_plugins` map.
|
|
927
|
+
* If not, it attempts to resolve the plugin's `package.json` file to retrieve its name and enable it.
|
|
928
|
+
*
|
|
929
|
+
* @param {string} nameOrPath - The name or path of the plugin to enable.
|
|
930
|
+
* @returns {Promise<Plugin | null>} A promise that resolves to the enabled plugin object, or null if the plugin could not be enabled.
|
|
931
|
+
*/
|
|
932
|
+
async enable(nameOrPath) {
|
|
933
|
+
const { promises } = await import('node:fs');
|
|
934
|
+
if (!nameOrPath)
|
|
935
|
+
return null;
|
|
936
|
+
if (this._plugins.has(nameOrPath)) {
|
|
937
|
+
const plugin = this._plugins.get(nameOrPath);
|
|
938
|
+
plugin.enabled = true;
|
|
939
|
+
this.log.info(`Enabled plugin ${plg}${plugin.name}${nf}`);
|
|
940
|
+
await this.saveToStorage();
|
|
941
|
+
this.emit('enabled', plugin.name);
|
|
942
|
+
return plugin;
|
|
943
|
+
}
|
|
944
|
+
const packageJsonPath = await this.resolve(nameOrPath);
|
|
945
|
+
if (!packageJsonPath) {
|
|
946
|
+
this.log.error(`Failed to enable plugin ${plg}${nameOrPath}${er}: package.json not found`);
|
|
947
|
+
return null;
|
|
948
|
+
}
|
|
949
|
+
try {
|
|
950
|
+
const packageJson = JSON.parse(await promises.readFile(packageJsonPath, 'utf8'));
|
|
951
|
+
const plugin = this._plugins.get(packageJson.name);
|
|
952
|
+
if (!plugin) {
|
|
953
|
+
this.log.error(`Failed to enable plugin ${plg}${nameOrPath}${er}: plugin not registered`);
|
|
954
|
+
return null;
|
|
955
|
+
}
|
|
956
|
+
plugin.enabled = true;
|
|
957
|
+
this.log.info(`Enabled plugin ${plg}${plugin.name}${nf}`);
|
|
958
|
+
await this.saveToStorage();
|
|
959
|
+
this.emit('enabled', plugin.name);
|
|
960
|
+
return plugin;
|
|
961
|
+
}
|
|
962
|
+
catch (err) {
|
|
963
|
+
logError(this.log, `Failed to parse package.json of plugin ${plg}${nameOrPath}${er}`, err);
|
|
964
|
+
return null;
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
/**
|
|
968
|
+
* Disables a plugin by its name or path.
|
|
969
|
+
*
|
|
970
|
+
* This method disables a plugin by setting its `enabled` property to `false` and saving the updated
|
|
971
|
+
* plugin information to storage. It first checks if the plugin is already registered in the `_plugins` map.
|
|
972
|
+
* If not, it attempts to resolve the plugin's `package.json` file to retrieve its name and disable it.
|
|
973
|
+
*
|
|
974
|
+
* @param {string} nameOrPath - The name or path of the plugin to enable.
|
|
975
|
+
* @returns {Promise<Plugin | null>} A promise that resolves to the disabled plugin object, or null if the plugin could not be disabled.
|
|
976
|
+
*/
|
|
977
|
+
async disable(nameOrPath) {
|
|
978
|
+
const { promises } = await import('node:fs');
|
|
979
|
+
if (!nameOrPath)
|
|
980
|
+
return null;
|
|
981
|
+
if (this._plugins.has(nameOrPath)) {
|
|
982
|
+
const plugin = this._plugins.get(nameOrPath);
|
|
983
|
+
plugin.enabled = false;
|
|
984
|
+
this.log.info(`Disabled plugin ${plg}${plugin.name}${nf}`);
|
|
985
|
+
await this.saveToStorage();
|
|
986
|
+
this.emit('disabled', plugin.name);
|
|
987
|
+
return plugin;
|
|
988
|
+
}
|
|
989
|
+
const packageJsonPath = await this.resolve(nameOrPath);
|
|
990
|
+
if (!packageJsonPath) {
|
|
991
|
+
this.log.error(`Failed to disable plugin ${plg}${nameOrPath}${er}: package.json not found`);
|
|
992
|
+
return null;
|
|
993
|
+
}
|
|
994
|
+
try {
|
|
995
|
+
const packageJson = JSON.parse(await promises.readFile(packageJsonPath, 'utf8'));
|
|
996
|
+
const plugin = this._plugins.get(packageJson.name);
|
|
997
|
+
if (!plugin) {
|
|
998
|
+
this.log.error(`Failed to disable plugin ${plg}${nameOrPath}${er}: plugin not registered`);
|
|
999
|
+
return null;
|
|
1000
|
+
}
|
|
1001
|
+
plugin.enabled = false;
|
|
1002
|
+
this.log.info(`Disabled plugin ${plg}${plugin.name}${nf}`);
|
|
1003
|
+
await this.saveToStorage();
|
|
1004
|
+
this.emit('disabled', plugin.name);
|
|
1005
|
+
return plugin;
|
|
1006
|
+
}
|
|
1007
|
+
catch (err) {
|
|
1008
|
+
logError(this.log, `Failed to parse package.json of plugin ${plg}${nameOrPath}${er}`, err);
|
|
1009
|
+
return null;
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
/**
|
|
1013
|
+
* Removes a plugin by its name or path.
|
|
1014
|
+
*
|
|
1015
|
+
* This method removes a plugin from the `_plugins` map and saves the updated plugin information to storage.
|
|
1016
|
+
* It first checks if the plugin is already registered in the `_plugins` map. If not, it attempts to resolve
|
|
1017
|
+
* the plugin's `package.json` file to retrieve its name and remove it.
|
|
1018
|
+
*
|
|
1019
|
+
* @param {string} nameOrPath - The name or path of the plugin to remove.
|
|
1020
|
+
* @returns {Promise<Plugin | null>} A promise that resolves to the removed plugin object, or null if the plugin could not be removed.
|
|
1021
|
+
*/
|
|
1022
|
+
async remove(nameOrPath) {
|
|
1023
|
+
const { promises } = await import('node:fs');
|
|
1024
|
+
if (!nameOrPath)
|
|
1025
|
+
return null;
|
|
1026
|
+
if (this._plugins.has(nameOrPath)) {
|
|
1027
|
+
const plugin = this._plugins.get(nameOrPath);
|
|
1028
|
+
this._plugins.delete(nameOrPath);
|
|
1029
|
+
this.log.info(`Removed plugin ${plg}${plugin.name}${nf}`);
|
|
1030
|
+
await this.saveToStorage();
|
|
1031
|
+
this.emit('removed', plugin.name);
|
|
1032
|
+
return plugin;
|
|
1033
|
+
}
|
|
1034
|
+
const packageJsonPath = await this.resolve(nameOrPath);
|
|
1035
|
+
if (!packageJsonPath) {
|
|
1036
|
+
this.log.error(`Failed to remove plugin ${plg}${nameOrPath}${er}: package.json not found`);
|
|
1037
|
+
return null;
|
|
1038
|
+
}
|
|
1039
|
+
try {
|
|
1040
|
+
const packageJson = JSON.parse(await promises.readFile(packageJsonPath, 'utf8'));
|
|
1041
|
+
const plugin = this._plugins.get(packageJson.name);
|
|
1042
|
+
if (!plugin) {
|
|
1043
|
+
this.log.error(`Failed to remove plugin ${plg}${nameOrPath}${er}: plugin not registered`);
|
|
1044
|
+
return null;
|
|
1045
|
+
}
|
|
1046
|
+
this._plugins.delete(packageJson.name);
|
|
1047
|
+
this.log.info(`Removed plugin ${plg}${plugin.name}${nf}`);
|
|
1048
|
+
await this.saveToStorage();
|
|
1049
|
+
this.emit('removed', plugin.name);
|
|
1050
|
+
return plugin;
|
|
1051
|
+
}
|
|
1052
|
+
catch (err) {
|
|
1053
|
+
logError(this.log, `Failed to parse package.json of plugin ${plg}${nameOrPath}${er}`, err);
|
|
1054
|
+
return null;
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
/**
|
|
1058
|
+
* Adds a plugin by its name or path.
|
|
1059
|
+
*
|
|
1060
|
+
* This method adds a plugin to the plugins map and saves the updated plugin information to storage.
|
|
1061
|
+
* It first resolves the plugin's `package.json` file to retrieve its details. If the plugin is already
|
|
1062
|
+
* registered, it logs an info message and returns null. Otherwise, it registers the plugin, enables it,
|
|
1063
|
+
* and saves the updated plugin information to storage.
|
|
1064
|
+
*
|
|
1065
|
+
* @param {string} nameOrPath - The name or path of the plugin to add.
|
|
1066
|
+
* @returns {Promise<Plugin | null>} A promise that resolves to the added plugin object, or null if the plugin could not be added.
|
|
1067
|
+
*/
|
|
1068
|
+
async add(nameOrPath) {
|
|
1069
|
+
const { promises } = await import('node:fs');
|
|
1070
|
+
if (!nameOrPath)
|
|
1071
|
+
return null;
|
|
1072
|
+
const packageJsonPath = await this.resolve(nameOrPath);
|
|
1073
|
+
if (!packageJsonPath) {
|
|
1074
|
+
this.log.error(`Failed to add plugin ${plg}${nameOrPath}${er}: package.json not found`);
|
|
1075
|
+
return null;
|
|
1076
|
+
}
|
|
1077
|
+
try {
|
|
1078
|
+
const packageJson = JSON.parse(await promises.readFile(packageJsonPath, 'utf8'));
|
|
1079
|
+
if (this._plugins.get(packageJson.name)) {
|
|
1080
|
+
this.log.info(`Plugin ${plg}${nameOrPath}${nf} already registered`);
|
|
1081
|
+
return null;
|
|
1082
|
+
}
|
|
1083
|
+
this._plugins.set(packageJson.name, {
|
|
1084
|
+
name: packageJson.name,
|
|
1085
|
+
enabled: true,
|
|
1086
|
+
path: packageJsonPath,
|
|
1087
|
+
type: 'AnyPlatform',
|
|
1088
|
+
version: packageJson.version,
|
|
1089
|
+
description: packageJson.description,
|
|
1090
|
+
author: this.getAuthor(packageJson),
|
|
1091
|
+
homepage: this.getHomepage(packageJson),
|
|
1092
|
+
help: this.getHelp(packageJson),
|
|
1093
|
+
changelog: this.getChangelog(packageJson),
|
|
1094
|
+
funding: this.getFunding(packageJson),
|
|
1095
|
+
});
|
|
1096
|
+
this.log.info(`Added plugin ${plg}${packageJson.name}${nf}`);
|
|
1097
|
+
await this.saveToStorage();
|
|
1098
|
+
const plugin = this._plugins.get(packageJson.name);
|
|
1099
|
+
this.emit('added', packageJson.name);
|
|
1100
|
+
return plugin;
|
|
1101
|
+
}
|
|
1102
|
+
catch (err) {
|
|
1103
|
+
logError(this.log, `Failed to parse package.json of plugin ${plg}${nameOrPath}${er}`, err);
|
|
1104
|
+
return null;
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
/**
|
|
1108
|
+
* Loads a plugin and returns the corresponding MatterbridgePlatform instance.
|
|
1109
|
+
*
|
|
1110
|
+
* @param {Plugin | PluginName} plugin - The plugin to load.
|
|
1111
|
+
* @param {boolean} start - Optional flag indicating whether to start the plugin after loading. Default is false.
|
|
1112
|
+
* @param {string} message - Optional message to pass to the plugin when starting.
|
|
1113
|
+
* @param {boolean} configure - Optional flag indicating whether to configure the plugin after loading. Default is false.
|
|
1114
|
+
* @returns {Promise<MatterbridgePlatform | undefined>} A Promise that resolves to the loaded MatterbridgePlatform instance or undefined.
|
|
1115
|
+
*/
|
|
1116
|
+
async load(plugin, start = false, message = '', configure = false) {
|
|
1117
|
+
const { promises } = await import('node:fs');
|
|
1118
|
+
const { default: path } = await import('node:path');
|
|
1119
|
+
if (typeof plugin === 'string') {
|
|
1120
|
+
const p = this._plugins.get(plugin);
|
|
1121
|
+
if (!p) {
|
|
1122
|
+
this.log.error(`Plugin ${plg}${plugin}${er} not found`);
|
|
1123
|
+
return undefined;
|
|
1124
|
+
}
|
|
1125
|
+
plugin = p;
|
|
1126
|
+
}
|
|
1127
|
+
if (!plugin.enabled) {
|
|
1128
|
+
this.log.error(`Plugin ${plg}${plugin.name}${er} not enabled`);
|
|
1129
|
+
return undefined;
|
|
1130
|
+
}
|
|
1131
|
+
if (plugin.platform) {
|
|
1132
|
+
this.log.error(`Plugin ${plg}${plugin.name}${er} already loaded`);
|
|
1133
|
+
return plugin.platform;
|
|
1134
|
+
}
|
|
1135
|
+
this.log.info(`Loading plugin ${plg}${plugin.name}${nf} type ${typ}${plugin.type}${nf}`);
|
|
1136
|
+
try {
|
|
1137
|
+
// Load the package.json of the plugin
|
|
1138
|
+
const packageJson = JSON.parse(await promises.readFile(plugin.path, 'utf8'));
|
|
1139
|
+
// Resolve the main module path relative to package.json
|
|
1140
|
+
const pluginEntry = path.resolve(path.dirname(plugin.path), packageJson.main);
|
|
1141
|
+
// Dynamically import the plugin
|
|
1142
|
+
const { pathToFileURL } = await import('node:url');
|
|
1143
|
+
const pluginUrl = pathToFileURL(pluginEntry);
|
|
1144
|
+
this.log.debug(`Importing plugin ${plg}${plugin.name}${db} from ${pluginUrl.href}`);
|
|
1145
|
+
const pluginInstance = (await import(pluginUrl.href));
|
|
1146
|
+
this.log.debug(`Imported plugin ${plg}${plugin.name}${db} from ${pluginUrl.href}`);
|
|
1147
|
+
// Call the default export function of the plugin, passing this MatterBridge instance, the log and the config
|
|
1148
|
+
if (pluginInstance.default) {
|
|
1149
|
+
const config = await this.loadConfig(plugin);
|
|
1150
|
+
// Preset the plugin properties here in case the plugin throws an error during loading. In this case the user can change the config and restart the plugin.
|
|
1151
|
+
plugin.name = packageJson.name;
|
|
1152
|
+
plugin.description = packageJson.description ?? 'No description';
|
|
1153
|
+
plugin.version = packageJson.version;
|
|
1154
|
+
plugin.author = this.getAuthor(packageJson);
|
|
1155
|
+
plugin.configJson = config;
|
|
1156
|
+
plugin.schemaJson = await this.loadSchema(plugin);
|
|
1157
|
+
config.name = packageJson.name;
|
|
1158
|
+
config.version = packageJson.version;
|
|
1159
|
+
const log = new AnsiLogger({
|
|
1160
|
+
logName: plugin.description,
|
|
1161
|
+
logTimestampFormat: 4 /* TimestampFormat.TIME_MILLIS */,
|
|
1162
|
+
logLevel: config.debug ? "debug" /* LogLevel.DEBUG */ : this.matterbridge.logLevel,
|
|
1163
|
+
});
|
|
1164
|
+
const platform = pluginInstance.default(this.matterbridge.getPlatformMatterbridge(), log, config);
|
|
1165
|
+
assertMatterbridgePlatform(platform, `Plugin ${plugin.name} does not export a valid MatterbridgePlatform`);
|
|
1166
|
+
if (!isMatterbridgeAccessoryPlatform(platform) && !isMatterbridgeDynamicPlatform(platform)) {
|
|
1167
|
+
// @ts-expect-error - override the type guard to call the shutdown method even if the platform is not valid
|
|
1168
|
+
await platform.onShutdown?.();
|
|
1169
|
+
throw new Error(`Plugin ${plugin.name} does not export a valid MatterbridgeAccessoryPlatform or MatterbridgeDynamicPlatform`);
|
|
1170
|
+
}
|
|
1171
|
+
config.type = platform.type;
|
|
1172
|
+
platform.name = packageJson.name;
|
|
1173
|
+
platform.config = config;
|
|
1174
|
+
platform.version = packageJson.version;
|
|
1175
|
+
platform.isLoaded = true;
|
|
1176
|
+
// @ts-expect-error - setMatterNode is intentionally private
|
|
1177
|
+
platform.setMatterNode?.(this.matterbridge.addBridgedEndpoint.bind(this.matterbridge), this.matterbridge.removeBridgedEndpoint.bind(this.matterbridge), this.matterbridge.removeAllBridgedEndpoints.bind(this.matterbridge), this.matterbridge.addVirtualEndpoint.bind(this.matterbridge));
|
|
1178
|
+
plugin.name = packageJson.name;
|
|
1179
|
+
plugin.description = packageJson.description ?? 'No description';
|
|
1180
|
+
plugin.version = packageJson.version;
|
|
1181
|
+
plugin.author = this.getAuthor(packageJson);
|
|
1182
|
+
plugin.homepage = this.getHomepage(packageJson);
|
|
1183
|
+
plugin.help = this.getHelp(packageJson);
|
|
1184
|
+
plugin.changelog = this.getChangelog(packageJson);
|
|
1185
|
+
plugin.funding = this.getFunding(packageJson);
|
|
1186
|
+
plugin.type = platform.type;
|
|
1187
|
+
plugin.platform = platform;
|
|
1188
|
+
plugin.loaded = true;
|
|
1189
|
+
plugin.registeredDevices = 0;
|
|
1190
|
+
await this.saveToStorage(); // Save the plugin to storage
|
|
1191
|
+
this.log.notice(`Loaded plugin ${plg}${plugin.name}${nt} type ${typ}${platform.type}${nt} (entrypoint ${UNDERLINE}${pluginEntry}${UNDERLINEOFF})`);
|
|
1192
|
+
this.emit('loaded', plugin.name);
|
|
1193
|
+
if (start)
|
|
1194
|
+
await this.start(plugin, message, false);
|
|
1195
|
+
if (configure)
|
|
1196
|
+
await this.configure(plugin);
|
|
1197
|
+
return platform;
|
|
1198
|
+
}
|
|
1199
|
+
else {
|
|
1200
|
+
this.log.error(`Plugin ${plg}${plugin.name}${er} does not provide a default export`);
|
|
1201
|
+
plugin.error = true;
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
catch (err) {
|
|
1205
|
+
inspectError(this.log, `Failed to load plugin ${plg}${plugin.name}${er}`, err);
|
|
1206
|
+
plugin.error = true;
|
|
1207
|
+
}
|
|
1208
|
+
return undefined;
|
|
1209
|
+
}
|
|
1210
|
+
/**
|
|
1211
|
+
* Starts a plugin.
|
|
1212
|
+
*
|
|
1213
|
+
* @param {Plugin | PluginName} plugin - The plugin to start.
|
|
1214
|
+
* @param {string} [message] - Optional message to pass to the plugin's onStart method.
|
|
1215
|
+
* @param {boolean} [configure] - Indicates whether to configure the plugin after starting (default false).
|
|
1216
|
+
* @returns {Promise<Plugin | undefined>} A promise that resolves when the plugin is started successfully, or rejects with an error if starting the plugin fails.
|
|
1217
|
+
*/
|
|
1218
|
+
async start(plugin, message, configure = false) {
|
|
1219
|
+
if (typeof plugin === 'string') {
|
|
1220
|
+
const p = this._plugins.get(plugin);
|
|
1221
|
+
if (!p) {
|
|
1222
|
+
this.log.error(`Plugin ${plg}${plugin}${er} not found`);
|
|
1223
|
+
return undefined;
|
|
1224
|
+
}
|
|
1225
|
+
plugin = p;
|
|
1226
|
+
}
|
|
1227
|
+
if (!plugin.loaded) {
|
|
1228
|
+
this.log.error(`Plugin ${plg}${plugin.name}${er} not loaded`);
|
|
1229
|
+
return undefined;
|
|
1230
|
+
}
|
|
1231
|
+
if (!plugin.platform) {
|
|
1232
|
+
this.log.error(`Plugin ${plg}${plugin.name}${er} no platform found`);
|
|
1233
|
+
return undefined;
|
|
1234
|
+
}
|
|
1235
|
+
if (plugin.started) {
|
|
1236
|
+
this.log.error(`Plugin ${plg}${plugin.name}${er} already started`);
|
|
1237
|
+
return undefined;
|
|
1238
|
+
}
|
|
1239
|
+
this.log.info(`Starting plugin ${plg}${plugin.name}${nf} type ${typ}${plugin.type}${nf}`);
|
|
1240
|
+
try {
|
|
1241
|
+
await plugin.platform.onStart(message);
|
|
1242
|
+
this.log.notice(`Started plugin ${plg}${plugin.name}${nt} type ${typ}${plugin.type}${nt}`);
|
|
1243
|
+
plugin.started = true;
|
|
1244
|
+
plugin.platform.isStarted = true;
|
|
1245
|
+
await this.saveConfigFromPlugin(plugin);
|
|
1246
|
+
this.emit('started', plugin.name);
|
|
1247
|
+
if (configure)
|
|
1248
|
+
await this.configure(plugin);
|
|
1249
|
+
return plugin;
|
|
1250
|
+
}
|
|
1251
|
+
catch (err) {
|
|
1252
|
+
plugin.error = true;
|
|
1253
|
+
logError(this.log, `Failed to start plugin ${plg}${plugin.name}${er}`, err);
|
|
1254
|
+
}
|
|
1255
|
+
return undefined;
|
|
1256
|
+
}
|
|
1257
|
+
/**
|
|
1258
|
+
* Configures a plugin.
|
|
1259
|
+
*
|
|
1260
|
+
* @param {Plugin | PluginName} plugin - The plugin to configure.
|
|
1261
|
+
* @returns {Promise<Plugin | undefined>} A promise that resolves when the plugin is configured successfully, or rejects with an error if configuration fails.
|
|
1262
|
+
*/
|
|
1263
|
+
async configure(plugin) {
|
|
1264
|
+
if (typeof plugin === 'string') {
|
|
1265
|
+
const p = this._plugins.get(plugin);
|
|
1266
|
+
if (!p) {
|
|
1267
|
+
this.log.error(`Plugin ${plg}${plugin}${er} not found`);
|
|
1268
|
+
return undefined;
|
|
1269
|
+
}
|
|
1270
|
+
plugin = p;
|
|
1271
|
+
}
|
|
1272
|
+
if (!plugin.loaded) {
|
|
1273
|
+
this.log.error(`Plugin ${plg}${plugin.name}${er} not loaded`);
|
|
1274
|
+
return undefined;
|
|
1275
|
+
}
|
|
1276
|
+
if (!plugin.started) {
|
|
1277
|
+
this.log.error(`Plugin ${plg}${plugin.name}${er} not started`);
|
|
1278
|
+
return undefined;
|
|
1279
|
+
}
|
|
1280
|
+
if (!plugin.platform) {
|
|
1281
|
+
this.log.error(`Plugin ${plg}${plugin.name}${er} no platform found`);
|
|
1282
|
+
return undefined;
|
|
1283
|
+
}
|
|
1284
|
+
if (plugin.configured) {
|
|
1285
|
+
this.log.debug(`Plugin ${plg}${plugin.name}${db} already configured`);
|
|
1286
|
+
return undefined;
|
|
1287
|
+
}
|
|
1288
|
+
this.log.info(`Configuring plugin ${plg}${plugin.name}${nf} type ${typ}${plugin.type}${nf}`);
|
|
1289
|
+
try {
|
|
1290
|
+
await plugin.platform.onConfigure();
|
|
1291
|
+
this.log.notice(`Configured plugin ${plg}${plugin.name}${nt} type ${typ}${plugin.type}${nt}`);
|
|
1292
|
+
plugin.configured = true;
|
|
1293
|
+
plugin.platform.isConfigured = true;
|
|
1294
|
+
this.emit('configured', plugin.name);
|
|
1295
|
+
return plugin;
|
|
1296
|
+
}
|
|
1297
|
+
catch (err) {
|
|
1298
|
+
plugin.error = true;
|
|
1299
|
+
logError(this.log, `Failed to configure plugin ${plg}${plugin.name}${er}`, err);
|
|
1300
|
+
}
|
|
1301
|
+
return undefined;
|
|
1302
|
+
}
|
|
1303
|
+
/**
|
|
1304
|
+
* Shuts down a plugin.
|
|
1305
|
+
*
|
|
1306
|
+
* This method shuts down a plugin by calling its `onShutdown` method and resetting its state.
|
|
1307
|
+
* It logs the shutdown process and optionally removes all devices associated with the plugin.
|
|
1308
|
+
*
|
|
1309
|
+
* @param {Plugin | PluginName} plugin - The plugin to shut down.
|
|
1310
|
+
* @param {string} [reason] - The reason for shutting down the plugin.
|
|
1311
|
+
* @param {boolean} [removeAllDevices] - Whether to remove all devices associated with the plugin.
|
|
1312
|
+
* @param {boolean} [force] - Whether to force the shutdown even if the plugin is not loaded or started.
|
|
1313
|
+
* @returns {Promise<Plugin | undefined>} A promise that resolves to the shut down plugin object, or undefined if the shutdown failed.
|
|
1314
|
+
*/
|
|
1315
|
+
async shutdown(plugin, reason, removeAllDevices = false, force = false) {
|
|
1316
|
+
if (typeof plugin === 'string') {
|
|
1317
|
+
const p = this._plugins.get(plugin);
|
|
1318
|
+
if (!p) {
|
|
1319
|
+
this.log.error(`Plugin ${plg}${plugin}${er} not found`);
|
|
1320
|
+
return undefined;
|
|
1321
|
+
}
|
|
1322
|
+
plugin = p;
|
|
1323
|
+
}
|
|
1324
|
+
this.log.debug(`Shutting down plugin ${plg}${plugin.name}${db}`);
|
|
1325
|
+
if (!plugin.loaded) {
|
|
1326
|
+
this.log.debug(`Plugin ${plg}${plugin.name}${db} not loaded`);
|
|
1327
|
+
if (!force)
|
|
1328
|
+
return undefined;
|
|
1329
|
+
}
|
|
1330
|
+
if (!plugin.started) {
|
|
1331
|
+
this.log.debug(`Plugin ${plg}${plugin.name}${db} not started`);
|
|
1332
|
+
if (!force)
|
|
1333
|
+
return undefined;
|
|
1334
|
+
}
|
|
1335
|
+
if (!plugin.configured) {
|
|
1336
|
+
this.log.debug(`Plugin ${plg}${plugin.name}${db} not configured`);
|
|
1337
|
+
}
|
|
1338
|
+
if (!plugin.platform) {
|
|
1339
|
+
this.log.debug(`Plugin ${plg}${plugin.name}${db} no platform found`);
|
|
1340
|
+
return undefined;
|
|
1341
|
+
}
|
|
1342
|
+
this.log.info(`Shutting down plugin ${plg}${plugin.name}${nf}: ${reason}...`);
|
|
1343
|
+
try {
|
|
1344
|
+
plugin.platform.isShuttingDown = true;
|
|
1345
|
+
await plugin.platform.onShutdown(reason);
|
|
1346
|
+
plugin.platform.isReady = false;
|
|
1347
|
+
plugin.platform.isLoaded = false;
|
|
1348
|
+
plugin.platform.isStarted = false;
|
|
1349
|
+
plugin.platform.isConfigured = false;
|
|
1350
|
+
plugin.platform.isShuttingDown = false;
|
|
1351
|
+
plugin.locked = undefined;
|
|
1352
|
+
plugin.error = undefined;
|
|
1353
|
+
plugin.loaded = undefined;
|
|
1354
|
+
plugin.started = undefined;
|
|
1355
|
+
plugin.configured = undefined;
|
|
1356
|
+
plugin.platform = undefined;
|
|
1357
|
+
if (removeAllDevices) {
|
|
1358
|
+
this.log.info(`Removing all endpoints for plugin ${plg}${plugin.name}${nf}: ${reason}...`);
|
|
1359
|
+
await this.matterbridge.removeAllBridgedEndpoints(plugin.name);
|
|
1360
|
+
}
|
|
1361
|
+
plugin.registeredDevices = undefined;
|
|
1362
|
+
this.log.notice(`Shutdown of plugin ${plg}${plugin.name}${nt} completed`);
|
|
1363
|
+
this.emit('shutdown', plugin.name);
|
|
1364
|
+
return plugin;
|
|
1365
|
+
}
|
|
1366
|
+
catch (err) {
|
|
1367
|
+
logError(this.log, `Failed to shut down plugin ${plg}${plugin.name}${er}`, err);
|
|
1368
|
+
}
|
|
1369
|
+
return undefined;
|
|
1370
|
+
}
|
|
1371
|
+
/**
|
|
1372
|
+
* Loads the configuration for a plugin.
|
|
1373
|
+
* If the configuration file exists, it reads the file and returns the parsed JSON data.
|
|
1374
|
+
* If the configuration file does not exist, it creates a new file with default configuration and returns it.
|
|
1375
|
+
* If any error occurs during file access or creation, it logs an error and return un empty config.
|
|
1376
|
+
*
|
|
1377
|
+
* @param {Plugin} plugin - The plugin for which to load the configuration.
|
|
1378
|
+
* @returns {Promise<PlatformConfig>} A promise that resolves to the loaded or created configuration.
|
|
1379
|
+
*/
|
|
1380
|
+
async loadConfig(plugin) {
|
|
1381
|
+
const { default: path } = await import('node:path');
|
|
1382
|
+
const { promises } = await import('node:fs');
|
|
1383
|
+
const configFile = path.join(this.matterbridge.matterbridgeDirectory, `${plugin.name}.config.json`);
|
|
1384
|
+
const defaultConfigFile = plugin.path.replace('package.json', `${plugin.name}.config.json`);
|
|
1385
|
+
try {
|
|
1386
|
+
await promises.access(configFile);
|
|
1387
|
+
const data = await promises.readFile(configFile, 'utf8');
|
|
1388
|
+
const config = JSON.parse(data);
|
|
1389
|
+
this.log.debug(`Loaded config file ${configFile} for plugin ${plg}${plugin.name}${db}.`);
|
|
1390
|
+
// this.log.debug(`Loaded config file ${configFile} for plugin ${plg}${plugin.name}${db}.\nConfig:${rs}\n`, config);
|
|
1391
|
+
// The first time a plugin is added to the system, the config file is created with the plugin name and type "AnyPlatform".
|
|
1392
|
+
config.name = plugin.name;
|
|
1393
|
+
config.type = plugin.type;
|
|
1394
|
+
if (config.debug === undefined)
|
|
1395
|
+
config.debug = false;
|
|
1396
|
+
if (config.unregisterOnShutdown === undefined)
|
|
1397
|
+
config.unregisterOnShutdown = false;
|
|
1398
|
+
return config;
|
|
1399
|
+
}
|
|
1400
|
+
catch (err) {
|
|
1401
|
+
const nodeErr = err;
|
|
1402
|
+
if (nodeErr.code === 'ENOENT') {
|
|
1403
|
+
this.log.debug(`Config file ${configFile} for plugin ${plg}${plugin.name}${db} does not exist, creating new config file...`);
|
|
1404
|
+
let config;
|
|
1405
|
+
try {
|
|
1406
|
+
await promises.access(defaultConfigFile);
|
|
1407
|
+
const data = await promises.readFile(defaultConfigFile, 'utf8');
|
|
1408
|
+
config = JSON.parse(data);
|
|
1409
|
+
this.log.debug(`Loaded default config file ${defaultConfigFile} for plugin ${plg}${plugin.name}${db}.`);
|
|
1410
|
+
}
|
|
1411
|
+
catch (_err) {
|
|
1412
|
+
this.log.debug(`Default config file ${defaultConfigFile} for plugin ${plg}${plugin.name}${db} does not exist, creating new config file...`);
|
|
1413
|
+
config = { name: plugin.name, type: plugin.type, version: '1.0.0', debug: false, unregisterOnShutdown: false };
|
|
1414
|
+
}
|
|
1415
|
+
try {
|
|
1416
|
+
await promises.writeFile(configFile, JSON.stringify(config, null, 2), 'utf8');
|
|
1417
|
+
this.log.debug(`Created config file ${configFile} for plugin ${plg}${plugin.name}${db}.`);
|
|
1418
|
+
// this.log.debug(`Created config file ${configFile} for plugin ${plg}${plugin.name}${db}.\nConfig:${rs}\n`, config);
|
|
1419
|
+
return config;
|
|
1420
|
+
}
|
|
1421
|
+
catch (err) {
|
|
1422
|
+
logError(this.log, `Error creating config file ${configFile} for plugin ${plg}${plugin.name}${er}`, err);
|
|
1423
|
+
return config;
|
|
1424
|
+
}
|
|
1425
|
+
}
|
|
1426
|
+
else {
|
|
1427
|
+
logError(this.log, `Error accessing config file ${configFile} for plugin ${plg}${plugin.name}${er}`, err);
|
|
1428
|
+
return { name: plugin.name, type: plugin.type, version: '1.0.0', debug: false, unregisterOnShutdown: false };
|
|
1429
|
+
}
|
|
1430
|
+
}
|
|
1431
|
+
}
|
|
1432
|
+
/**
|
|
1433
|
+
* Saves the configuration of a plugin to a file.
|
|
1434
|
+
*
|
|
1435
|
+
* This method saves the configuration of the specified plugin to a JSON file in the matterbridge directory.
|
|
1436
|
+
* If the plugin's configuration is not found, it logs an error and rejects the promise. If the configuration
|
|
1437
|
+
* is successfully saved, it logs a debug message. If an error occurs during the file write operation, it logs
|
|
1438
|
+
* the error and rejects the promise.
|
|
1439
|
+
*
|
|
1440
|
+
* @param {Plugin} plugin - The plugin whose configuration is to be saved.
|
|
1441
|
+
* @param {boolean} [restartRequired] - Indicates whether a restart is required after saving the configuration.
|
|
1442
|
+
* @returns {Promise<void>} A promise that resolves when the configuration is successfully saved, or rejects if an error occurs.
|
|
1443
|
+
* @throws {Error} If the plugin's configuration is not found.
|
|
1444
|
+
*/
|
|
1445
|
+
async saveConfigFromPlugin(plugin, restartRequired = false) {
|
|
1446
|
+
const { default: path } = await import('node:path');
|
|
1447
|
+
const { promises } = await import('node:fs');
|
|
1448
|
+
if (!plugin.platform?.config) {
|
|
1449
|
+
this.log.error(`Error saving config file for plugin ${plg}${plugin.name}${er}: config not found`);
|
|
1450
|
+
return Promise.reject(new Error(`Error saving config file for plugin ${plg}${plugin.name}${er}: config not found`));
|
|
1451
|
+
}
|
|
1452
|
+
const configFile = path.join(this.matterbridge.matterbridgeDirectory, `${plugin.name}.config.json`);
|
|
1453
|
+
try {
|
|
1454
|
+
await promises.writeFile(configFile, JSON.stringify(plugin.platform.config, null, 2), 'utf8');
|
|
1455
|
+
plugin.configJson = plugin.platform.config;
|
|
1456
|
+
if (restartRequired)
|
|
1457
|
+
plugin.restartRequired = true;
|
|
1458
|
+
this.log.debug(`Saved config file ${configFile} for plugin ${plg}${plugin.name}${db}`);
|
|
1459
|
+
// this.log.debug(`Saved config file ${configFile} for plugin ${plg}${plugin.name}${db}.\nConfig:${rs}\n`, plugin.platform.config);
|
|
1460
|
+
return Promise.resolve();
|
|
1461
|
+
}
|
|
1462
|
+
catch (err) {
|
|
1463
|
+
logError(this.log, `Error saving config file ${configFile} for plugin ${plg}${plugin.name}${er}`, err);
|
|
1464
|
+
return Promise.reject(err);
|
|
1465
|
+
}
|
|
1466
|
+
}
|
|
1467
|
+
/**
|
|
1468
|
+
* Saves the configuration of a plugin from a JSON object to a file.
|
|
1469
|
+
*
|
|
1470
|
+
* This method saves the provided configuration of the specified plugin to a JSON file in the matterbridge directory.
|
|
1471
|
+
* It first checks if the configuration data is valid by ensuring it contains the correct name and type, and matches
|
|
1472
|
+
* the plugin's name. If the configuration data is invalid, it logs an error and returns. If the configuration is
|
|
1473
|
+
* successfully saved, it updates the plugin's `configJson` property and logs a debug message. If an error occurs
|
|
1474
|
+
* during the file write operation, it logs the error and returns.
|
|
1475
|
+
*
|
|
1476
|
+
* @param {Plugin} plugin - The plugin whose configuration is to be saved.
|
|
1477
|
+
* @param {PlatformConfig} config - The configuration data to be saved.
|
|
1478
|
+
* @param {boolean} [restartRequired] - Indicates whether a restart is required after saving the configuration.
|
|
1479
|
+
* @returns {Promise<void>} A promise that resolves when the configuration is successfully saved, or returns if an error occurs.
|
|
1480
|
+
*/
|
|
1481
|
+
async saveConfigFromJson(plugin, config, restartRequired = false) {
|
|
1482
|
+
const { default: path } = await import('node:path');
|
|
1483
|
+
const { promises } = await import('node:fs');
|
|
1484
|
+
if (!config.name || !config.type || config.name !== plugin.name) {
|
|
1485
|
+
this.log.error(`Error saving config file for plugin ${plg}${plugin.name}${er}. Wrong config data content:${rs}\n`, config);
|
|
1486
|
+
return;
|
|
1487
|
+
}
|
|
1488
|
+
const configFile = path.join(this.matterbridge.matterbridgeDirectory, `${plugin.name}.config.json`);
|
|
1489
|
+
try {
|
|
1490
|
+
await promises.writeFile(configFile, JSON.stringify(config, null, 2), 'utf8');
|
|
1491
|
+
plugin.configJson = config;
|
|
1492
|
+
if (restartRequired)
|
|
1493
|
+
plugin.restartRequired = true;
|
|
1494
|
+
if (plugin.platform) {
|
|
1495
|
+
plugin.platform.config = config;
|
|
1496
|
+
plugin.platform.onConfigChanged(config).catch((err) => this.log.error(`Error calling onConfigChanged for plugin ${plg}${plugin.name}${er}: ${err}`));
|
|
1497
|
+
}
|
|
1498
|
+
this.log.debug(`Saved config file ${configFile} for plugin ${plg}${plugin.name}${db}`);
|
|
1499
|
+
// this.log.debug(`Saved config file ${configFile} for plugin ${plg}${plugin.name}${db}.\nConfig:${rs}\n`, config);
|
|
1500
|
+
}
|
|
1501
|
+
catch (err) {
|
|
1502
|
+
logError(this.log, `Error saving config file ${configFile} for plugin ${plg}${plugin.name}${er}`, err);
|
|
1503
|
+
return;
|
|
1504
|
+
}
|
|
1505
|
+
}
|
|
1506
|
+
/**
|
|
1507
|
+
* Loads the schema for a plugin.
|
|
1508
|
+
*
|
|
1509
|
+
* This method attempts to load the schema file for the specified plugin. If the schema file is found,
|
|
1510
|
+
* it reads and parses the file, updates the schema's title and description, and logs the process.
|
|
1511
|
+
* If the schema file is not found, it logs the event and loads a default schema for the plugin.
|
|
1512
|
+
*
|
|
1513
|
+
* @param {Plugin} plugin - The plugin whose schema is to be loaded.
|
|
1514
|
+
* @returns {Promise<PlatformSchema>} A promise that resolves to the loaded schema object, or the default schema if the schema file is not found.
|
|
1515
|
+
*/
|
|
1516
|
+
async loadSchema(plugin) {
|
|
1517
|
+
const { promises } = await import('node:fs');
|
|
1518
|
+
const schemaFile = plugin.path.replace('package.json', `${plugin.name}.schema.json`);
|
|
1519
|
+
try {
|
|
1520
|
+
await promises.access(schemaFile);
|
|
1521
|
+
const data = await promises.readFile(schemaFile, 'utf8');
|
|
1522
|
+
const schema = JSON.parse(data);
|
|
1523
|
+
schema.title = plugin.description;
|
|
1524
|
+
schema.description = plugin.name + ' v. ' + plugin.version + ' by ' + plugin.author;
|
|
1525
|
+
this.log.debug(`Loaded schema file ${schemaFile} for plugin ${plg}${plugin.name}${db}.`);
|
|
1526
|
+
// this.log.debug(`Loaded schema file ${schemaFile} for plugin ${plg}${plugin.name}${db}.\nSchema:${rs}\n`, schema);
|
|
1527
|
+
return schema;
|
|
1528
|
+
}
|
|
1529
|
+
catch (_err) {
|
|
1530
|
+
this.log.debug(`Schema file ${schemaFile} for plugin ${plg}${plugin.name}${db} not found. Loading default schema.`);
|
|
1531
|
+
return this.getDefaultSchema(plugin);
|
|
1532
|
+
}
|
|
1533
|
+
}
|
|
1534
|
+
/**
|
|
1535
|
+
* Returns the default schema for a plugin.
|
|
1536
|
+
*
|
|
1537
|
+
* This method generates a default schema object for the specified plugin. The schema includes
|
|
1538
|
+
* metadata such as the plugin's title, description, version, and author. It also defines the
|
|
1539
|
+
* properties of the schema, including the plugin's name, type, debug flag, and unregisterOnShutdown flag.
|
|
1540
|
+
*
|
|
1541
|
+
* @param {Plugin} plugin - The plugin for which the default schema is to be generated.
|
|
1542
|
+
* @returns {PlatformSchema} The default schema object for the plugin.
|
|
1543
|
+
*/
|
|
1544
|
+
getDefaultSchema(plugin) {
|
|
1545
|
+
return {
|
|
1546
|
+
title: plugin.description,
|
|
1547
|
+
description: plugin.name + ' v. ' + plugin.version + ' by ' + plugin.author,
|
|
1548
|
+
type: 'object',
|
|
1549
|
+
properties: {
|
|
1550
|
+
name: {
|
|
1551
|
+
description: 'Plugin name',
|
|
1552
|
+
type: 'string',
|
|
1553
|
+
readOnly: true,
|
|
1554
|
+
},
|
|
1555
|
+
type: {
|
|
1556
|
+
description: 'Plugin type',
|
|
1557
|
+
type: 'string',
|
|
1558
|
+
readOnly: true,
|
|
1559
|
+
},
|
|
1560
|
+
debug: {
|
|
1561
|
+
description: 'Enable the debug for the plugin (development only)',
|
|
1562
|
+
type: 'boolean',
|
|
1563
|
+
default: false,
|
|
1564
|
+
},
|
|
1565
|
+
unregisterOnShutdown: {
|
|
1566
|
+
description: 'Unregister all devices on shutdown (development only)',
|
|
1567
|
+
type: 'boolean',
|
|
1568
|
+
default: false,
|
|
1569
|
+
},
|
|
1570
|
+
},
|
|
1571
|
+
};
|
|
1572
|
+
}
|
|
1573
|
+
}
|
|
1574
|
+
//# sourceMappingURL=pluginManager.js.map
|