Skip to content

Commit

Permalink
Release 1.1.12
Browse files Browse the repository at this point in the history
  • Loading branch information
Luligu committed Mar 22, 2024
1 parent 801bb6a commit b26c184
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ All notable changes to this project will be documented in this file.

### Added

- [platform]: Added async getConfig() and async setConfig() to store plugin config.
- [platform]: Added async loadConfig() and async saveConfig() to store plugin config.
- [platform]: Added a config (JSON) property to platforms to store plugin config.

## [1.1.11] - 2024-03-19

Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ Add your plugin logic in platform.ts.

## MatterbridgeDynamicPlatform and MatterbridgeAccessoryPlatform api

### name: string
The plugin name.

### type: string
The plugin platform type.

### config: object
The plugin config (loaded before onStart() is called and save after onShutdown() is called).

### async onStart(reason?: string)
The method onStart() is where you have to create your MatterbridgeDevice and add all needed clusters and command handlers.

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "matterbridge",
"version": "1.1.11",
"version": "1.1.12",
"description": "Matterbridge",
"author": "https://github.com/Luligu",
"license": "Apache-2.0",
Expand Down Expand Up @@ -66,7 +66,7 @@
},
"dependencies": {
"@project-chip/matter-node.js": "^0.7.5",
"express": "^4.19.0",
"express": "^4.19.1",
"matter-history": "^1.0.12",
"node-ansi-logger": "^1.9.2",
"node-persist-manager": "^1.0.5"
Expand Down
4 changes: 4 additions & 0 deletions src/matterbridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ interface MatterbridgePlatform {
onStart(reason?: string): Promise<this>;
onConfigure(): Promise<this>;
onShutdown(reason?: string): Promise<this>;
loadConfig(): Promise<void>;
saveConfig(): Promise<void>;
matterbridge: Matterbridge;
log: AnsiLogger;
name: string;
Expand Down Expand Up @@ -568,6 +570,7 @@ export class Matterbridge extends EventEmitter {
// Calling the shutdown functions with a reason
for (const plugin of this.registeredPlugins) {
if (plugin.platform) await plugin.platform.onShutdown('Matterbridge is closing: ' + message);
if (plugin.platform) await plugin.platform.saveConfig();
}

// Set reachability to false
Expand Down Expand Up @@ -1026,6 +1029,7 @@ export class Matterbridge extends EventEmitter {
plugin.registeredDevices = 0;
plugin.addedDevices = 0;
await this.nodeContext?.set<RegisteredPlugin[]>('plugins', this.getBaseRegisteredPlugins());
await platform.loadConfig();
this.log.info(`Loaded plugin ${plg}${plugin.name}${db} type ${typ}${platform.type}${db} (entrypoint ${UNDERLINE}${pluginEntry}${UNDERLINEOFF})`);
if (start) this.startPlugin(plugin, message); // No await do it asyncronously
return Promise.resolve(platform);
Expand Down
13 changes: 8 additions & 5 deletions src/matterbridgePlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,30 @@ export class MatterbridgePlatform {
protected log: AnsiLogger;
protected name = ''; // Will be set by the loadPlugin() method using the package.json value.
protected type = ''; // Will be set by the extending classes.
protected config: Config = {};

constructor(matterbridge: Matterbridge, log: AnsiLogger) {
this.matterbridge = matterbridge;
this.log = log;
}

async getConfig(): Promise<Config> {
private async loadConfig(): Promise<void> {
const configFile = path.join(this.matterbridge.matterbridgeDirectory, `${this.name}.config.json`);
try {
await fs.access(configFile);
this.log.debug(`Config file found: ${configFile}.`);
const data = await fs.readFile(configFile, 'utf8');
return JSON.parse(data) as Config;
this.config = JSON.parse(data) as Config;
return;
} catch (err) {
if (err instanceof Error) {
const nodeErr = err as NodeJS.ErrnoException;
if (nodeErr.code === 'ENOENT') {
try {
await this.writeFile(configFile, JSON.stringify({ name: this.name, type: this.type }, null, 2));
this.log.info(`Created config file: ${configFile}.`);
return { name: this.name, type: this.type };
this.config = { name: this.name, type: this.type };
return;
} catch (err) {
this.log.error(`Error creating config file ${configFile}: ${err}`);
return Promise.reject(err);
Expand All @@ -76,10 +79,10 @@ export class MatterbridgePlatform {
}
}

async setConfig(config: Config): Promise<void> {
private async saveConfig(): Promise<void> {
const configFile = path.join(this.matterbridge.matterbridgeDirectory, `${this.name}.config.json`);
try {
await this.writeFile(configFile, JSON.stringify(config));
await this.writeFile(configFile, JSON.stringify(this.config));
} catch (err) {
this.log.error(`Error setting config: ${err}`);
return Promise.reject(err);
Expand Down

0 comments on commit b26c184

Please sign in to comment.