#!/usr/bin/env node /** * run-process.js * 接收两个参数:ip 数组 (JSON)、脚本名 * 异步根据每个 ip 执行脚本 * * 调用示例:node run-process.js '["192.168.2.5","192.168.2.6"]' 'RedNoteAIThumbsUp' */ const path = require('path') const fs = require('fs') const { parseWorkflow, executeActionSequence } = require('./ef-compiler/ef-compiler.js') const ipListJson = process.argv[2] const scriptName = process.argv[3] const ipList = JSON.parse(ipListJson) let shouldStop = false const folderPath = path.join(path.resolve(__dirname, '..', 'static'), 'process', scriptName) const { actions } = parseWorkflow(JSON.parse(fs.readFileSync(path.join(folderPath, 'process.json'), 'utf8'))) const resolution = { width: 1080, height: 1920 } /** 启动执行:遍历 ip 列表并异步执行脚本;任一台失败则停止全部并返回失败设备 IP */ async function start() { let failedIp = null const runOne = async (ip) => { if (shouldStop) return { ip, success: false, stopped: true } const result = await executeActionSequence(actions, `${ip}:5555`, folderPath, resolution, 1000, null, () => shouldStop) if (!result.success) { if (!failedIp) { failedIp = ip; shouldStop = true } } return { ip, success: result.success } } const results = await Promise.all(ipList.map(ip => runOne(ip))) const output = failedIp ? { success: false, failedIp, results } : { success: true, results } process.stdout.write(JSON.stringify(output) + '\n') process.exit(failedIp ? 1 : 0) } /** 停止执行(SIGTERM/SIGINT 时调用) */ function stop() { shouldStop = true } process.on('SIGTERM', () => { stop(); process.exit(130) }) process.on('SIGINT', () => { stop(); process.exit(130) }) start()