matterbridge
Version:
Matterbridge plugin manager for Matter
313 lines • 12 kB
TypeScript
/**
* This file contains the class Frontend.
*
* @file frontend.ts
* @author Luca Liguori
* @created 2025-01-13
* @version 1.2.0
* @license Apache-2.0
*
* Copyright 2025, 2026, 2027 Luca Liguori.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import EventEmitter from 'node:events';
import { LogLevel } from 'node-ansi-logger';
import { Matterbridge } from './matterbridge.js';
/**
* Websocket message ID for logging.
*
* @constant {number}
*/
export declare const WS_ID_LOG = 0;
/**
* Websocket message ID indicating a refresh is needed.
*
* @constant {number}
*/
export declare const WS_ID_REFRESH_NEEDED = 1;
/**
* Websocket message ID indicating a restart is needed.
*
* @constant {number}
*/
export declare const WS_ID_RESTART_NEEDED = 2;
/**
* Websocket message ID indicating a cpu update.
*
* @constant {number}
*/
export declare const WS_ID_CPU_UPDATE = 3;
/**
* Websocket message ID indicating a memory update.
*
* @constant {number}
*/
export declare const WS_ID_MEMORY_UPDATE = 4;
/**
* Websocket message ID indicating an uptime update.
*
* @constant {number}
*/
export declare const WS_ID_UPTIME_UPDATE = 5;
/**
* Websocket message ID indicating a snackbar message.
*
* @constant {number}
*/
export declare const WS_ID_SNACKBAR = 6;
/**
* Websocket message ID indicating matterbridge has un update available.
*
* @constant {number}
*/
export declare const WS_ID_UPDATE_NEEDED = 7;
/**
* Websocket message ID indicating a state update.
*
* @constant {number}
*/
export declare const WS_ID_STATEUPDATE = 8;
/**
* Websocket message ID indicating to close a permanent snackbar message.
*
* @constant {number}
*/
export declare const WS_ID_CLOSE_SNACKBAR = 9;
/**
* Websocket message ID indicating a shelly system update.
* check:
* curl -k http://127.0.0.1:8101/api/updates/sys/check
* perform:
* curl -k http://127.0.0.1:8101/api/updates/sys/perform
*
* @constant {number}
*/
export declare const WS_ID_SHELLY_SYS_UPDATE = 100;
/**
* Websocket message ID indicating a shelly main update.
* check:
* curl -k http://127.0.0.1:8101/api/updates/main/check
* perform:
* curl -k http://127.0.0.1:8101/api/updates/main/perform
*
* @constant {number}
*/
export declare const WS_ID_SHELLY_MAIN_UPDATE = 101;
/**
* Represents the Matterbridge events.
*/
interface FrontendEvents {
server_listening: [protocol: string, port: number, address?: string];
server_error: [error: Error];
server_stopped: [];
websocket_server_listening: [host: string];
websocket_server_stopped: [];
}
export declare class Frontend extends EventEmitter<FrontendEvents> {
private matterbridge;
private log;
private port;
private expressApp;
private httpServer;
private httpsServer;
private webSocketServer;
constructor(matterbridge: Matterbridge);
set logLevel(logLevel: LogLevel);
start(port?: number): Promise<void>;
stop(): Promise<void>;
private formatMemoryUsage;
private formatOsUpTime;
/**
* Retrieves the api settings data.
*
* @returns {Promise<{ matterbridgeInformation: MatterbridgeInformation, systemInformation: SystemInformation }>} A promise that resolve in the api settings object.
*/
private getApiSettings;
/**
* Retrieves the reachable attribute.
*
* @param {MatterbridgeEndpoint} device - The MatterbridgeEndpoint object.
* @returns {boolean} The reachable attribute.
*/
private getReachability;
/**
* Retrieves the power source attribute.
*
* @param {MatterbridgeEndpoint} endpoint - The MatterbridgeDevice to retrieve the power source from.
* @returns {'ac' | 'dc' | 'ok' | 'warning' | 'critical' | undefined} The power source attribute.
*/
private getPowerSource;
/**
* Retrieves the commissioned status, matter pairing codes, fabrics and sessions from a given device in server mode.
*
* @param {MatterbridgeEndpoint} device - The MatterbridgeEndpoint to retrieve the data from.
* @returns {ApiDevicesMatter | undefined} An ApiDevicesMatter object or undefined if not found.
*/
private getMatterDataFromDevice;
/**
* Retrieves the cluster text description from a given device.
* The output is a string with the attributes description of the cluster servers in the device to show in the frontend.
*
* @param {MatterbridgeEndpoint} device - The MatterbridgeEndpoint to retrieve the cluster text from.
* @returns {string} The attributes description of the cluster servers in the device.
*/
private getClusterTextFromDevice;
/**
* Retrieves the registered plugins sanitized for res.json().
*
* @returns {BaseRegisteredPlugin[]} An array of BaseRegisteredPlugin.
*/
private getPlugins;
/**
* Retrieves the devices from Matterbridge.
*
* @param {string} [pluginName] - The name of the plugin to filter devices by.
* @returns {Promise<ApiDevices[]>} A promise that resolves to an array of ApiDevices for the frontend.
*/
private getDevices;
/**
* Retrieves the clusters from a given plugin and endpoint number.
*
* Response for /api/clusters
*
* @param {string} pluginName - The name of the plugin.
* @param {number} endpointNumber - The endpoint number.
* @returns {ApiClustersResponse | undefined} A promise that resolves to the clusters or undefined if not found.
*/
private getClusters;
/**
* Handles incoming websocket messages for the Matterbridge frontend.
*
* @param {WebSocket} client - The websocket client that sent the message.
* @param {WebSocket.RawData} message - The raw data of the message received from the client.
* @returns {Promise<void>} A promise that resolves when the message has been handled.
*/
private wsMessageHandler;
/**
* Sends a WebSocket log message to all connected clients. The function is called by AnsiLogger.setGlobalCallback.
*
* @param {string} level - The logger level of the message: debug info notice warn error fatal...
* @param {string} time - The time string of the message
* @param {string} name - The logger name of the message
* @param {string} message - The content of the message.
*
* @remarks
* The function removes ANSI escape codes, leading asterisks, non-printable characters, and replaces all occurrences of \t and \n.
* It also replaces all occurrences of \" with " and angle-brackets with < and >.
* The function sends the message to all connected clients.
*/
wssSendMessage(level: string, time: string, name: string, message: string): void;
/**
* Sends a need to refresh WebSocket message to all connected clients.
*
* @param {string} changed - The changed value. If null, the whole page will be refreshed.
* possible values:
* - 'matterbridgeLatestVersion'
* - 'matterbridgeDevVersion'
* - 'matterbridgeAdvertise'
* - 'online'
* - 'offline'
* - 'reachability'
* - 'settings'
* - 'plugins'
* - 'pluginsRestart'
* - 'devices'
* - 'fabrics'
* - 'sessions'
*/
wssSendRefreshRequired(changed?: string | null): void;
/**
* Sends a need to restart WebSocket message to all connected clients.
*
* @param {boolean} snackbar - If true, a snackbar message will be sent to all connected clients. Default is true.
* @param {boolean} fixed - If true, the restart is fixed and will not be reset by plugin restarts. Default is false.
*/
wssSendRestartRequired(snackbar?: boolean, fixed?: boolean): void;
/**
* Sends a no need to restart WebSocket message to all connected clients.
*
* @param {boolean} snackbar - If true, the snackbar message will be cleared from all connected clients.
*/
wssSendRestartNotRequired(snackbar?: boolean): void;
/**
* Sends a need to update WebSocket message to all connected clients.
*
* @param {boolean} devVersion - If true, the update is for a development version.
*/
wssSendUpdateRequired(devVersion?: boolean): void;
/**
* Sends a cpu update message to all connected clients.
*
* @param {number} cpuUsage - The CPU usage percentage to send.
*/
wssSendCpuUpdate(cpuUsage: number): void;
/**
* Sends a memory update message to all connected clients.
*
* @param {string} totalMemory - The total memory in bytes.
* @param {string} freeMemory - The free memory in bytes.
* @param {string} rss - The resident set size in bytes.
* @param {string} heapTotal - The total heap memory in bytes.
* @param {string} heapUsed - The used heap memory in bytes.
* @param {string} external - The external memory in bytes.
* @param {string} arrayBuffers - The array buffers memory in bytes.
*/
wssSendMemoryUpdate(totalMemory: string, freeMemory: string, rss: string, heapTotal: string, heapUsed: string, external: string, arrayBuffers: string): void;
/**
* Sends an uptime update message to all connected clients.
*
* @param {string} systemUptime - The system uptime in a human-readable format.
* @param {string} processUptime - The process uptime in a human-readable format.
*/
wssSendUptimeUpdate(systemUptime: string, processUptime: string): void;
/**
* Sends an open snackbar message to all connected clients.
*
* @param {string} message - The message to send.
* @param {number} timeout - The timeout in seconds for the snackbar message.
* @param {'info' | 'warning' | 'error' | 'success'} severity - The severity of the snackbar message (default info).
*/
wssSendSnackbarMessage(message: string, timeout?: number, severity?: 'info' | 'warning' | 'error' | 'success'): void;
/**
* Sends a close snackbar message to all connected clients.
*
* @param {string} message - The message to send.
*/
wssSendCloseSnackbarMessage(message: string): void;
/**
* Sends an attribute update message to all connected WebSocket clients.
*
* @param {string | undefined} plugin - The name of the plugin.
* @param {string | undefined} serialNumber - The serial number of the device.
* @param {string | undefined} uniqueId - The unique identifier of the device.
* @param {string} cluster - The cluster name where the attribute belongs.
* @param {string} attribute - The name of the attribute that changed.
* @param {number | string | boolean} value - The new value of the attribute.
*
* @remarks
* This method logs a debug message and sends a JSON-formatted message to all connected WebSocket clients
* with the updated attribute information.
*/
wssSendAttributeChangedMessage(plugin: string | undefined, serialNumber: string | undefined, uniqueId: string | undefined, cluster: string, attribute: string, value: number | string | boolean): void;
/**
* Sends a message to all connected clients.
*
* @param {number} id - The message id.
* @param {string} method - The message method.
* @param {Record<string, string | number | boolean>} params - The message parameters.
*/
wssBroadcastMessage(id: number, method?: string, params?: Record<string, string | number | boolean>): void;
}
export {};
//# sourceMappingURL=frontend.d.ts.map