/**
 * This file contains the class Matterbridge.
 *
 * @file matterbridge.ts
 * @author Luca Liguori
 * @created 2023-12-29
 * @version 1.6.0
 * @license Apache-2.0
 *
 * Copyright 2023, 2024, 2025 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 { AnsiLogger, LogLevel } from 'node-ansi-logger';
import { NodeStorageManager, NodeStorage } from 'node-persist-manager';
import { DeviceTypeId, Endpoint, LogLevel as MatterLogLevel, VendorId, StorageContext, StorageManager, StorageService, Environment, ServerNode, SessionsBehavior } from '@matter/main';
import { DeviceCertification, ExposedFabricInformation } from '@matter/main/protocol';
import { AggregatorEndpoint } from '@matter/main/endpoints';
import { MatterbridgeInformation, RegisteredPlugin, SanitizedExposedFabricInformation, SanitizedSession, SystemInformation } from './matterbridgeTypes.js';
import { PluginManager } from './pluginManager.js';
import { DeviceManager } from './deviceManager.js';
import { MatterbridgeEndpoint } from './matterbridgeEndpoint.js';
import { Frontend } from './frontend.js';
/**
 * Represents the Matterbridge events.
 */
interface MatterbridgeEvents {
    shutdown: [];
    restart: [];
    update: [];
    initialize_started: [];
    initialize_completed: [];
    online: [nodeid: string];
    offline: [nodeid: string];
    bridge_started: [];
    childbridge_started: [];
    cleanup_started: [];
    cleanup_completed: [];
    startmemorycheck: [];
    stopmemorycheck: [];
    startinspector: [];
    stopinspector: [];
    takeheapsnapshot: [];
    triggergarbagecollection: [];
}
/**
 * Represents the Matterbridge application.
 */
export declare class Matterbridge extends EventEmitter<MatterbridgeEvents> {
    systemInformation: SystemInformation;
    matterbridgeInformation: MatterbridgeInformation;
    homeDirectory: string;
    rootDirectory: string;
    matterbridgeDirectory: string;
    matterbridgePluginDirectory: string;
    matterbridgeCertDirectory: string;
    globalModulesDirectory: string;
    matterbridgeVersion: string;
    matterbridgeLatestVersion: string;
    matterbridgeDevVersion: string;
    bridgeMode: 'bridge' | 'childbridge' | 'controller' | '';
    restartMode: 'service' | 'docker' | '';
    profile: string | undefined;
    shutdown: boolean;
    private readonly failCountLimit;
    log: AnsiLogger;
    matterbridgeLoggerFile: string;
    matterLoggerFile: string;
    plugins: PluginManager;
    devices: DeviceManager;
    frontend: Frontend;
    nodeStorageName: string;
    nodeStorage: NodeStorageManager | undefined;
    nodeContext: NodeStorage | undefined;
    hasCleanupStarted: boolean;
    private initialized;
    private execRunningCount;
    private startMatterInterval;
    private startMatterIntervalMs;
    private checkUpdateInterval;
    private checkUpdateTimeout;
    private configureTimeout;
    private reachabilityTimeout;
    private endAdvertiseTimeout;
    private sigintHandler;
    private sigtermHandler;
    private exceptionHandler;
    private rejectionHandler;
    environment: Environment;
    matterStorageName: string;
    matterStorageService: StorageService | undefined;
    matterStorageManager: StorageManager | undefined;
    matterbridgeContext: StorageContext | undefined;
    controllerContext: StorageContext | undefined;
    mdnsInterface: string | undefined;
    ipv4address: string | undefined;
    ipv6address: string | undefined;
    port: number | undefined;
    passcode: number | undefined;
    discriminator: number | undefined;
    certification: DeviceCertification.Configuration | undefined;
    serverNode: ServerNode<ServerNode.RootEndpoint> | undefined;
    aggregatorNode: Endpoint<AggregatorEndpoint> | undefined;
    aggregatorVendorId: VendorId;
    aggregatorVendorName: string;
    aggregatorProductId: number;
    aggregatorProductName: string;
    aggregatorDeviceType: DeviceTypeId;
    aggregatorSerialNumber: string | undefined;
    aggregatorUniqueId: string | undefined;
    protected static instance: Matterbridge | undefined;
    protected constructor();
    /**
     * Retrieves the list of Matterbridge devices.
     *
     * @returns {MatterbridgeEndpoint[]} An array of MatterbridgeDevice objects.
     */
    getDevices(): MatterbridgeEndpoint[];
    /**
     * Retrieves the list of registered plugins.
     *
     * @returns {RegisteredPlugin[]} An array of RegisteredPlugin objects.
     */
    getPlugins(): RegisteredPlugin[];
    /**
     * Set the logger logLevel for the Matterbridge classes and call onChangeLoggerLevel() for each plugin.
     *
     * @param {LogLevel} logLevel The logger logLevel to set.
     */
    setLogLevel(logLevel: LogLevel): Promise<void>;
    /**
     * Loads an instance of the Matterbridge class.
     * If an instance already exists, return that instance.
     *
     * @param {boolean} initialize - Whether to initialize the Matterbridge instance after loading. Defaults to false.
     * @returns {Matterbridge} A promise that resolves to the Matterbridge instance.
     */
    static loadInstance(initialize?: boolean): Promise<Matterbridge>;
    /**
     * Call cleanup() and dispose MdnsService.
     *
     * @param {number} [timeout] - The timeout duration to wait for the cleanup to complete in milliseconds. Default is 1000.
     * @param {number} [pause] - The pause duration after the cleanup in milliseconds. Default is 250.
     *
     * @deprecated This method is deprecated and is ONLY used for jest tests.
     */
    destroyInstance(timeout?: number, pause?: number): Promise<void>;
    /**
     * Initializes the Matterbridge application.
     *
     * @remarks
     * This method performs the necessary setup and initialization steps for the Matterbridge application.
     * It displays the help information if the 'help' parameter is provided, sets up the logger, checks the
     * node version, registers signal handlers, initializes storage, and parses the command line.
     *
     * @returns {Promise<void>} A Promise that resolves when the initialization is complete.
     */
    initialize(): Promise<void>;
    /**
     * Parses the command line arguments and performs the corresponding actions.
     *
     * @private
     * @returns {Promise<void>} A promise that resolves when the command line arguments have been processed, or the process exits.
     */
    private parseCommandLine;
    /**
     * Asynchronously loads and starts the registered plugins.
     *
     * This method is responsible for initializing and starting all enabled plugins.
     * It ensures that each plugin is properly loaded and started before the bridge starts.
     *
     * @returns {Promise<void>} A promise that resolves when all plugins have been loaded and started.
     */
    private startPlugins;
    /**
     * Registers the process handlers for uncaughtException, unhandledRejection, SIGINT and SIGTERM.
     * When either of these signals are received, the cleanup method is called with an appropriate message.
     */
    private registerProcessHandlers;
    /**
     * Deregisters the process uncaughtException, unhandledRejection, SIGINT and SIGTERM signal handlers.
     */
    private deregisterProcessHandlers;
    /**
     * Logs the node and system information.
     */
    private logNodeAndSystemInfo;
    /**
     * Creates a MatterLogger function to show the matter.js log messages in AnsiLogger (for the frontend).
     *
     * @returns {Function} The MatterLogger function.
     */
    private createMatterLogger;
    /**
     * Creates a Matter File Logger.
     *
     * @param {string} filePath - The path to the log file.
     * @param {boolean} [unlink] - Whether to unlink the log file before creating a new one.
     * @returns {Function} - A function that logs formatted messages to the log file.
     */
    createMatterFileLogger(filePath: string, unlink?: boolean): Promise<(level: MatterLogLevel, formattedLog: string) => Promise<void>>;
    /**
     * Restarts the process by exiting the current instance and loading a new instance (/api/restart).
     *
     * @returns {Promise<void>} A promise that resolves when the restart is completed.
     */
    restartProcess(): Promise<void>;
    /**
     * Shut down the process (/api/shutdown).
     *
     * @returns {Promise<void>} A promise that resolves when the shutdown is completed.
     */
    shutdownProcess(): Promise<void>;
    /**
     * Update matterbridge and shut down the process (virtual device 'Update Matterbridge').
     *
     * @returns {Promise<void>} A promise that resolves when the update is completed.
     */
    updateProcess(): Promise<void>;
    /**
     * Unregister all devices and shut down the process (/api/unregister).
     *
     * @param {number} [timeout] - The timeout duration to wait for the message exchange to complete in milliseconds. Default is 1000.
     *
     * @returns {Promise<void>} A promise that resolves when the cleanup is completed.
     */
    unregisterAndShutdownProcess(timeout?: number): Promise<void>;
    /**
     * Reset commissioning and shut down the process (/api/reset).
     *
     * @returns {Promise<void>} A promise that resolves when the cleanup is completed.
     */
    shutdownProcessAndReset(): Promise<void>;
    /**
     * Factory reset and shut down the process (/api/factory-reset).
     *
     * @returns {Promise<void>} A promise that resolves when the cleanup is completed.
     */
    shutdownProcessAndFactoryReset(): Promise<void>;
    /**
     * Cleans up the Matterbridge instance.
     *
     * @param {string} message - The cleanup message.
     * @param {boolean} [restart] - Indicates whether to restart the instance after cleanup. Default is `false`.
     * @param {number} [timeout] - The timeout duration to wait for the message exchange to complete in milliseconds. Default is 1000.
     *
     * @returns {Promise<void>} A promise that resolves when the cleanup is completed.
     */
    protected cleanup(message: string, restart?: boolean, timeout?: number): Promise<void>;
    /**
     * Creates and configures the server node for a single not bridged device.
     *
     * @param {RegisteredPlugin} plugin - The plugin to configure.
     * @param {MatterbridgeEndpoint} device - The device to associate with the plugin.
     * @returns {Promise<void>} A promise that resolves when the server node for the accessory plugin is created and configured.
     */
    private createDeviceServerNode;
    /**
     * Creates and configures the server node for an accessory plugin for a given device.
     *
     * @param {RegisteredPlugin} plugin - The plugin to configure.
     * @param {MatterbridgeEndpoint} device - The device to associate with the plugin.
     * @returns {Promise<void>} A promise that resolves when the server node for the accessory plugin is created and configured.
     */
    private createAccessoryPlugin;
    /**
     * Creates and configures the server node and the aggregator node for a dynamic plugin.
     *
     * @param {RegisteredPlugin} plugin - The plugin to configure.
     * @returns {Promise<void>} A promise that resolves when the server node and the aggregator node for the dynamic plugin is created and configured.
     */
    private createDynamicPlugin;
    /**
     * Starts the Matterbridge in bridge mode.
     *
     * @private
     * @returns {Promise<void>} A promise that resolves when the Matterbridge is started.
     */
    private startBridge;
    /**
     * Starts the Matterbridge in childbridge mode.
     *
     * @param {number} [delay] - The delay before starting the childbridge. Default is 1000 milliseconds.
     *
     * @returns {Promise<void>} A promise that resolves when the Matterbridge is started.
     */
    private startChildbridge;
    /**
     * Starts the Matterbridge controller.
     *
     * @private
     * @returns {Promise<void>} A promise that resolves when the Matterbridge is started.
     */
    protected startController(): Promise<void>;
    /**                                                                                                                                   */
    /**                                                     Matter.js methods                                                             */
    /**                                                                                                                                   */
    /**
     * Starts the matter storage with name Matterbridge, create the matterbridge context and performs a backup.
     *
     * @returns {Promise<void>} - A promise that resolves when the storage is started.
     */
    private startMatterStorage;
    /**
     * Makes a backup copy of the specified matter storage directory.
     *
     * @param {string} storageName - The name of the storage directory to be backed up.
     * @param {string} backupName - The name of the backup directory to be created.
     * @private
     * @returns {Promise<void>} A promise that resolves when the has been done.
     */
    private backupMatterStorage;
    /**
     * Stops the matter storage.
     *
     * @returns {Promise<void>} A promise that resolves when the storage is stopped.
     */
    private stopMatterStorage;
    /**
     * Creates a server node storage context.
     *
     * @param {string} pluginName - The name of the plugin.
     * @param {string} deviceName - The name of the device.
     * @param {DeviceTypeId} deviceType - The device type of the device.
     * @param {number} vendorId - The vendor ID.
     * @param {string} vendorName - The vendor name.
     * @param {number} productId - The product ID.
     * @param {string} productName - The product name.
     * @param {string} [serialNumber] - The serial number of the device (optional).
     * @param {string} [uniqueId] - The unique ID of the device (optional).
     * @returns {Promise<StorageContext>} The storage context for the commissioning server.
     */
    private createServerNodeContext;
    /**
     * Creates a server node.
     *
     * @param {StorageContext} storageContext - The storage context for the server node.
     * @param {number} [port] - The port number for the server node. Defaults to 5540.
     * @param {number} [passcode] - The passcode for the server node. Defaults to 20242025.
     * @param {number} [discriminator] - The discriminator for the server node. Defaults to 3850.
     * @returns {Promise<ServerNode<ServerNode.RootEndpoint>>} A promise that resolves to the created server node.
     */
    private createServerNode;
    /**
     * Starts the 15 minutes timer to advice that advertising for the specified server node is ended.
     *
     * @param {ServerNode} [matterServerNode] - The server node to start.
     */
    private startEndAdvertiseTimer;
    /**
     * Starts the specified server node.
     *
     * @param {ServerNode} [matterServerNode] - The server node to start.
     * @returns {Promise<void>} A promise that resolves when the server node has started.
     */
    private startServerNode;
    /**
     * Stops the specified server node.
     *
     * @param {ServerNode} matterServerNode - The server node to stop.
     * @param {number} [timeout] - The timeout in milliseconds for stopping the server node. Defaults to 30 seconds.
     * @returns {Promise<void>} A promise that resolves when the server node has stopped.
     */
    private stopServerNode;
    /**
     * Advertises the specified server node.
     *
     * @param {ServerNode} [matterServerNode] - The server node to advertise.
     * @returns {Promise<{ qrPairingCode: string, manualPairingCode: string } | undefined>} A promise that resolves to the pairing codes if the server node is advertised, or undefined if not.
     */
    advertiseServerNode(matterServerNode?: ServerNode): Promise<{
        qrPairingCode: string;
        manualPairingCode: string;
    } | undefined>;
    /**
     * Stop advertise the specified server node.
     *
     * @param {ServerNode} [matterServerNode] - The server node to advertise.
     * @returns {Promise<void>} A promise that resolves when the server node has stopped advertising.
     */
    stopAdvertiseServerNode(matterServerNode?: ServerNode): Promise<void>;
    /**
     * Creates an aggregator node with the specified storage context.
     *
     * @param {StorageContext} storageContext - The storage context for the aggregator node.
     * @returns {Promise<Endpoint<AggregatorEndpoint>>} A promise that resolves to the created aggregator node.
     */
    private createAggregatorNode;
    /**
     * Adds a MatterbridgeEndpoint to the specified plugin.
     *
     * @param {string} pluginName - The name of the plugin.
     * @param {MatterbridgeEndpoint} device - The device to add as a bridged endpoint.
     * @returns {Promise<void>} A promise that resolves when the bridged endpoint has been added.
     */
    addBridgedEndpoint(pluginName: string, device: MatterbridgeEndpoint): Promise<void>;
    /**
     * Removes a MatterbridgeEndpoint from the specified plugin.
     *
     * @param {string} pluginName - The name of the plugin.
     * @param {MatterbridgeEndpoint} device - The device to remove as a bridged endpoint.
     * @returns {Promise<void>} A promise that resolves when the bridged endpoint has been removed.
     */
    removeBridgedEndpoint(pluginName: string, device: MatterbridgeEndpoint): Promise<void>;
    /**
     * Removes all bridged endpoints from the specified plugin.
     *
     * @param {string} pluginName - The name of the plugin.
     * @param {number} [delay] - The delay in milliseconds between removing each bridged endpoint (default: 0).
     * @returns {Promise<void>} A promise that resolves when all bridged endpoints have been removed.
     *
     * @remarks
     * This method iterates through all devices in the DeviceManager and removes each bridged endpoint associated with the specified plugin.
     * It also applies a delay between each removal if specified.
     * The delay is useful to allow the controllers to receive a single subscription for each device removed.
     */
    removeAllBridgedEndpoints(pluginName: string, delay?: number): Promise<void>;
    /**
     * Subscribes to the attribute change event for the given device and plugin.
     * Specifically, it listens for changes in the 'reachable' attribute of the
     * BridgedDeviceBasicInformationServer cluster server of the bridged device or BasicInformationServer cluster server of server node.
     *
     * @param {RegisteredPlugin} plugin - The plugin associated with the device.
     * @param {MatterbridgeEndpoint} device - The device to subscribe to attribute changes for.
     * @returns {Promise<void>} A promise that resolves when the subscription is set up.
     */
    private subscribeAttributeChanged;
    /**
     * Sanitizes the fabric information by converting bigint properties to strings because `res.json` doesn't support bigint.
     *
     * @param {ExposedFabricInformation[]} fabricInfo - The array of exposed fabric information objects.
     * @returns {SanitizedExposedFabricInformation[]} An array of sanitized exposed fabric information objects.
     */
    sanitizeFabricInformations(fabricInfo: ExposedFabricInformation[]): SanitizedExposedFabricInformation[];
    /**
     * Sanitizes the session information by converting bigint properties to strings because `res.json` doesn't support bigint.
     *
     * @param {SessionsBehavior.Session[]} sessions - The array of session information objects.
     * @returns {SanitizedSession[]} An array of sanitized session information objects.
     */
    sanitizeSessionInformation(sessions: SessionsBehavior.Session[]): SanitizedSession[];
    /**
     * Sets the reachability of the specified aggregator node bridged devices and trigger.
     *
     * @param {Endpoint<AggregatorEndpoint>} aggregatorNode - The aggregator node to set the reachability for.
     * @param {boolean} reachable - A boolean indicating the reachability status to set.
     */
    private setAggregatorReachability;
    getVendorIdName: (vendorId: number | undefined) => string;
}
export {};
//# sourceMappingURL=matterbridge.d.ts.map