import { Logger } from 'pino'; import { TrafficSniffer } from '../trackers/traffic-sniffer.js'; import type { ITransportController, ITransportInfo, ITransportStatus, ITransport, TTransportType, TDeviceStateHandler, TPortStateHandler, TRSMode, IScanOptions, IScanReport, IPollingTaskOptions, IPollingQueueInfo, IPollingManagerConfig, INodeSerialTransportOptions, IWebSerialTransportOptions, IWebSerialPort, ITransportControllerOptions } from '../../types/public.js'; import type { TPollingAction, TPollingBulkAction } from '../../types/public.js'; /** * The main controller for managing Modbus transports. * It serves as an orchestrator for transport creation, device state tracking, * routing requests to the correct bus, and managing polling tasks. * * This class implements a thread-safe approach using Mutex for CRUD operations. */ declare class TransportController implements ITransportController { private readonly _mutex; logger: Logger; private readonly _registry; private readonly _router; private readonly _stateManager; private readonly _pollingProxy; private readonly _scanService; private _sniffer; /** * Returns the traffic sniffer if enabled, allowing inspection of raw binary traffic. */ get sniffer(): TrafficSniffer | null; /** * Initializes a new TransportController. * * @param {ITransportControllerOptions} options - Configuration for the controller (e.g., enable sniffer). */ constructor(options?: ITransportControllerOptions); /** * Silences the internal logger. */ disableLogger(): void; /** * Enables the internal logger with 'info' level. */ enableLogger(): void; /** * Creates a pino logger instance with pretty printing for non-production environments. * @private */ private _createLogger; /** Pauses the currently active scan operation. */ pauseScan(): void; /** Resumes a previously paused scan operation. */ resumeScan(): void; /** Aborts the currently active scan operation. */ stopScan(): void; /** * Performs a scan on a Serial/RTU port to discover Modbus devices. * * @param {IScanOptions} options - Scan parameters (baud rates, slave IDs, etc.). * @returns {Promise} Result of the discovery process. */ scanRtuPort(options: IScanOptions): Promise; /** * Performs a scan on a Network/TCP port to discover Modbus units. * * @param {IScanOptions} options - Scan parameters (hosts, ports, unit IDs). * @returns {Promise} Result of the discovery process. */ scanTcpPort(options: IScanOptions): Promise; /** * Adds and initializes a new transport. * Validates RSMode constraints (e.g., RS232 only allows one device). * * @param {string} id - Unique identifier for the transport. * @param {TTransportType} type - Transport type (node-rtu, node-tcp, etc.). * @param {object} options - Connection parameters. * @param {object} [reconnectOptions] - Settings for automatic reconnection. * @param {IPollingManagerConfig} [pollingConfig] - Custom settings for the internal polling manager. * @throws {Error} If transport ID exists or RSMode constraints are violated. */ addTransport(id: string, type: TTransportType, options: INodeSerialTransportOptions | (IWebSerialTransportOptions & { port: IWebSerialPort; }), reconnectOptions?: { maxReconnectAttempts?: number; reconnectInterval?: number; }, pollingConfig?: IPollingManagerConfig): Promise; /** * Removes a transport, stops its polling, and cleans up its resources. * * @param {string} id - The ID of the transport to remove. */ removeTransport(id: string): Promise; /** * Retrieves the raw ITransport instance for a given ID. */ getTransport(id: string): ITransport | null; /** * Returns a list of all registered transport information. */ listTransports(): ITransportInfo[]; /** Attempts to connect all registered transports. */ connectAll(): Promise; /** Disconnects all registered transports. */ disconnectAll(): Promise; /** * Connects a specific transport. * @param {string} id - Transport identifier. */ connectTransport(id: string): Promise; /** * Disconnects a specific transport and stops its polling tasks. */ disconnectTransport(id: string): Promise; /** * Finds an appropriate transport instance for a specific Slave ID and RS mode. * @param {number} slaveId - The Modbus slave ID. * @param {TRSMode} requiredRSMode - The required communication mode. */ getTransportForSlave(slaveId: number, requiredRSMode: TRSMode): ITransport | null; /** * Assigns a Slave ID to a transport. * Checks for RS232 limitations (max 1 device). */ assignSlaveIdToTransport(transportId: string, slaveId: number): Promise; /** * Unassigns a Slave ID from a transport. * If the transport becomes empty, it is automatically removed. */ removeSlaveIdFromTransport(transportId: string, slaveId: number): Promise; /** * Re-initializes a transport with new options without removing its ID from the registry. * Useful for changing baud rate or IP address on the fly. */ reloadTransport(id: string, options: INodeSerialTransportOptions | (IWebSerialTransportOptions & { port: IWebSerialPort; })): Promise; /** * Low-level method to write raw data to a transport and optionally read a response. * This call is wrapped in the transport's polling manager to ensure synchronized access to the port. * * @param {string} transportId - Target transport. * @param {Uint8Array} data - Raw binary data to write. * @param {number} [readLength=0] - Number of bytes to read after writing. * @param {number} [timeout=3000] - Read timeout. * @returns {Promise} The read response or an empty array. */ writeToPort(transportId: string, data: Uint8Array, readLength?: number, timeout?: number): Promise; /** Sets a global handler for device state changes. */ setDeviceStateHandler(handler: TDeviceStateHandler): void; /** Sets a global handler for port state changes. */ setPortStateHandler(handler: TPortStateHandler): void; /** Sets a device state handler for a specific transport only. */ setDeviceStateHandlerForTransport(transportId: string, handler: TDeviceStateHandler): Promise; /** Sets a port state handler for a specific transport only. */ setPortStateHandlerForTransport(transportId: string, handler: TPortStateHandler): Promise; /** Adds a recurring polling task to a transport. */ addPollingTask(transportId: string, options: IPollingTaskOptions): void; /** Removes a specific polling task. */ removePollingTask(transportId: string, taskId: string): void; /** Updates options for an existing polling task. */ updatePollingTask(transportId: string, taskId: string, options: Partial): Promise; /** Performs an action (start/stop/pause/resume) on a polling task. Accepts string or EPollingAction. */ controlTask(transportId: string, taskId: string, action: TPollingAction): void; /** Performs a bulk action on all polling tasks of a transport. Accepts string or EPollingBulkAction. */ controlPolling(transportId: string, action: TPollingBulkAction): void; /** Returns queue info and current task states for a transport. */ getPollingQueueInfo(transportId: string): IPollingQueueInfo; /** Executes a one-off function with immediate priority on the transport's queue. */ executeImmediate(transportId: string, fn: () => Promise): Promise; /** * Retrieves status for one or all transports. * @param {string} [id] - Optional ID to get status for a single transport. */ getStatus(id?: string): ITransportStatus | Record; /** Returns the count of transports currently in 'connected' state. */ getActiveTransportCount(): number; /** * Maps internal transport info to a public status object. * @private */ private _buildStatus; /** * Shuts down the controller, disconnecting all transports and clearing all polling tasks. */ destroy(): Promise; /** Internal disconnect logic used by reload and destroy. @private */ private _disconnectTransportInternal; /** Internal removal logic used by removeSlaveIdFromTransport. @private */ private _removeTransportInternal; /** Internal handler for device state changes received from ITransport. @private */ private _onDeviceStateChange; /** Internal handler for port/transport status changes received from ITransport. @private */ private _onPortStateChange; } export = TransportController;