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

109 lines 4.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BatchOperationService = void 0; /** * Service class for handling batch operations with smart delays and error handling */ class BatchOperationService { /** * Execute multiple operations in sequence with smart delays to prevent KLAP -1012 errors * @param operations Array of operations to execute * @param options Configuration options for batch execution * @returns Array of results for each operation */ static async executeBatch(operations, options = {}) { const results = []; const defaultDelay = options.defaultDelay || 2000; for (let i = 0; i < operations.length; i++) { const operationItem = operations[i]; if (!operationItem) continue; const { operation, name, delayAfter } = operationItem; const startTime = Date.now(); try { console.log(`\n--- Executing batch operation: ${name} ---`); const data = await operation(); const duration = Date.now() - startTime; const result = { name, success: data.result, duration }; if (data.result) { result.data = data; } else if (data.errorInf) { result.error = data.errorInf; } results.push(result); // Add delay after operation (except for the last one) if (i < operations.length - 1) { const delay = delayAfter !== undefined ? delayAfter : defaultDelay; if (delay > 0) { console.log(`⏳ Waiting ${delay}ms before next operation...`); await new Promise(resolve => setTimeout(resolve, delay)); } } } catch (error) { const duration = Date.now() - startTime; results.push({ name, success: false, error: error, duration }); } } return results; } /** * Execute operations in parallel (use with caution for device operations) * @param operations Array of operations to execute in parallel * @param options Configuration options * @returns Array of results for each operation */ static async executeParallel(operations, options = {}) { const maxConcurrency = options.maxConcurrency || 3; const results = []; // Process operations in chunks to limit concurrency for (let i = 0; i < operations.length; i += maxConcurrency) { const chunk = operations.slice(i, i + maxConcurrency); const chunkPromises = chunk.map(async (operationItem) => { const { operation, name } = operationItem; const startTime = Date.now(); try { console.log(`\n--- Executing parallel operation: ${name} ---`); const data = await operation(); const duration = Date.now() - startTime; const result = { name, success: data.result, duration }; if (data.result) { result.data = data; } else if (data.errorInf) { result.error = data.errorInf; } return result; } catch (error) { const duration = Date.now() - startTime; return { name, success: false, error: error, duration }; } }); const chunkResults = await Promise.all(chunkPromises); results.push(...chunkResults); } return results; } } exports.BatchOperationService = BatchOperationService; //# sourceMappingURL=batch-operation-service.js.map