UNPKG

node-red-contrib-tplink-tapo-connect-api

Version:

This unofficial node-RED node allows connection to TP-Link Tapo devices. This project has been enhanced with AI support to enable new features. Starting with v0.50, we have added support for the KLAP protocol. To prioritize the operation of this node, we

129 lines 4.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConnectionManager = void 0; /** * Manages device connections and connection state * Single responsibility: Connection lifecycle management */ class ConnectionManager { constructor(ip, credentials, options = {}) { var _a, _b, _c; this.ip = ip; this.credentials = credentials; this.connectionState = { isConnected: false, lastActivity: Date.now(), retryCount: 0 }; this.options = { maxRetries: (_a = options.maxRetries) !== null && _a !== void 0 ? _a : 3, retryDelay: (_b = options.retryDelay) !== null && _b !== void 0 ? _b : 1000, timeout: (_c = options.timeout) !== null && _c !== void 0 ? _c : 10000 }; } /** * Establish connection to device */ async connect() { if (this.connectionState.isConnected) { return; } let lastError = null; for (let attempt = 0; attempt <= this.options.maxRetries; attempt++) { try { await this.performConnection(); this.connectionState = { isConnected: true, connectionTime: Date.now(), lastActivity: Date.now(), retryCount: 0 }; return; } catch (error) { lastError = error; this.connectionState.retryCount = attempt + 1; if (attempt < this.options.maxRetries) { await this.delay(this.options.retryDelay * (attempt + 1)); } } } throw new Error(`Failed to connect after ${this.options.maxRetries + 1} attempts: ${lastError === null || lastError === void 0 ? void 0 : lastError.message}`); } /** * Disconnect from device */ async disconnect() { if (!this.connectionState.isConnected) { return; } try { await this.performDisconnection(); } finally { this.connectionState = { isConnected: false, lastActivity: Date.now(), retryCount: 0 }; } } /** * Check if connection is active and healthy */ isConnected() { return this.connectionState.isConnected; } /** * Get current connection state */ getConnectionState() { return { ...this.connectionState }; } /** * Update last activity timestamp */ updateActivity() { this.connectionState.lastActivity = Date.now(); } /** * Check if connection has been idle for too long */ isIdle(maxIdleTime = 300000) { return Date.now() - this.connectionState.lastActivity > maxIdleTime; } /** * Reset connection state (useful for error recovery) */ reset() { this.connectionState = { isConnected: false, lastActivity: Date.now(), retryCount: 0 }; } /** * Actual connection implementation - no-op as connection is handled by protocol-specific auth classes */ async performConnection() { // Connection is handled by the protocol-specific authentication classes // This is just for tracking connection state return Promise.resolve(); } /** * Actual disconnection implementation - no-op as disconnection is handled by protocol-specific auth classes */ async performDisconnection() { // Disconnection is handled by the protocol-specific authentication classes // This is just for tracking connection state return Promise.resolve(); } /** * Utility method for delays */ delay(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } } exports.ConnectionManager = ConnectionManager; //# sourceMappingURL=connection-manager.js.map