@webarray/esphome-native-api
Version:
TypeScript/Node.js client for ESPHome native API with encryption and deep sleep support
173 lines • 4.18 kB
TypeScript
/**
* Core connection handler for ESPHome Native API
* Manages TCP connection, reconnection, and message handling
*/
import { EventEmitter } from 'eventemitter3';
import { DecodedMessage } from '../utils/protocol';
import { ConnectionOptions, ConnectionState, MessageType, HealthMetrics, ConnectionHealth } from '../types';
export interface ConnectionEvents {
connect: () => void;
disconnect: (error?: Error) => void;
message: (message: DecodedMessage) => void;
error: (error: Error) => void;
stateChange: (state: ConnectionState) => void;
}
export declare class Connection extends EventEmitter<ConnectionEvents> {
private socket;
private protocol;
private state;
private options;
private timers;
private reconnectTimer?;
private pingTimer?;
private pingTimeoutTimer?;
private isReconnecting;
private isDestroyed;
private expectedDisconnect;
private hasDeepSleep;
private healthMetrics;
private pingLatencies;
private readonly maxLatencySamples;
constructor(options: ConnectionOptions);
/**
* Connect to the ESPHome device
*/
connect(): Promise<void>;
/**
* Establish the actual TCP connection
*/
private establishConnection;
/**
* Setup socket event handlers
*/
private setupSocketHandlers;
/**
* Handle incoming messages
*/
private handleMessage;
/**
* Send a message to the device
*/
sendMessage(type: MessageType, data: Buffer): void;
/**
* Handle disconnect request from device (deep sleep)
*/
private handleDisconnectRequest;
/**
* Disconnect from the device
*/
disconnect(): void;
/**
* Handle disconnection and potential reconnection
*/
private handleDisconnect;
/**
* Schedule a reconnection attempt
*/
private scheduleReconnect;
/**
* Check if this is an expected disconnect (e.g., deep sleep)
*/
isExpectedDisconnect(): boolean;
/**
* Start the ping timer
*/
private startPingTimer;
/**
* Start the ping timeout timer
*/
private startPingTimeoutTimer;
/**
* Handle pong response
*/
private handlePongResponse;
/**
* Stop the ping timer
*/
private stopPingTimer;
/**
* Stop the ping timeout timer
*/
private stopPingTimeoutTimer;
/**
* Clean up resources
*/
private cleanup;
/**
* Update connection state
*/
private updateState;
/**
* Destroy the connection
*/
destroy(): void;
/**
* Get the current connection state
*/
getState(): ConnectionState;
/**
* Check if connected
*/
isConnected(): boolean;
/**
* Check if authenticated
*/
isAuthenticated(): boolean;
/**
* Set authentication state
*/
setAuthenticated(authenticated: boolean): void;
/**
* Set API version
*/
setApiVersion(major: number, minor: number): void;
/**
* Set server info
*/
setServerInfo(info: string): void;
/**
* Enable/disable deep sleep mode
*/
setDeepSleepMode(enabled: boolean): void;
/**
* Get health metrics
*/
getHealthMetrics(): HealthMetrics;
/**
* Get connection health status
*/
getConnectionHealth(): ConnectionHealth;
/**
* Reset health metrics
*/
resetHealthMetrics(): void;
/**
* Calculate average ping latency
*/
private calculateAveragePingLatency;
/**
* Update health metrics on connect
*/
private updateHealthOnConnect;
/**
* Update health metrics on disconnect
*/
private updateHealthOnDisconnect;
/**
* Update health metrics on ping/pong
*/
private updateHealthOnPing;
/**
* Update health metrics on pong received
*/
private updateHealthOnPong;
/**
* Update health metrics on message sent
*/
private updateHealthOnMessageSent;
/**
* Update health metrics on message received
*/
private updateHealthOnMessageReceived;
}
//# sourceMappingURL=connection.d.ts.map