| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- /**
- * 运行时 API:设备操作(adb)、日志、桩函数
- */
- const path = require('path')
- const fs = require('fs')
- const { spawnSync } = require('child_process')
- const config = require('./compiler-config.js')
- const { sendSystemButton } = require(path.join(config.projectRoot, 'nodejs', 'adb', 'adb-sys-btn.js'))
- function runAdb(action, args = [], deviceId = '') {
- const r = spawnSync('node', [config.adbInteractPath, action, ...args, deviceId], { encoding: 'utf-8', timeout: 10000 })
- return { success: r.status === 0, error: r.stderr }
- }
- const sendTap = (device, x, y) => runAdb('tap', [String(x), String(y)], device)
- const sendSwipe = (device, x1, y1, x2, y2, duration) =>
- runAdb('swipe-coords', [String(x1), String(y1), String(x2), String(y2), String(duration || 300)], device)
- const sendKeyEvent = (device, keyCode) => runAdb('keyevent', [String(keyCode)], device)
- const sendText = (device, text) => runAdb('text', [String(text)], device)
- function appendLog(folderPath, message) {
- if (!folderPath || typeof folderPath !== 'string') return Promise.resolve()
- const logDir = path.resolve(folderPath)
- const logFile = path.join(logDir, 'log.txt')
- fs.mkdirSync(logDir, { recursive: true })
- fs.appendFileSync(logFile, message + '\n')
- return Promise.resolve()
- }
- const _stub = (name) => ({ success: false, error: `${name} 需在主进程实现` })
- function createElectronAPI(overrides = {}) {
- return {
- sendTap,
- sendSwipe,
- sendKeyEvent,
- sendText,
- sendSystemKey: (device, keyCode) => sendSystemButton(String(keyCode), device),
- matchImageAndGetCoordinate: null,
- findTextAndGetCoordinate: () => _stub('findTextAndGetCoordinate'),
- appendLog,
- readTextFile: null,
- writeTextFile: null,
- saveChatHistory: () => _stub('saveChatHistory'),
- readChatHistory: () => _stub('readChatHistory'),
- readAllChatHistory: () => _stub('readAllChatHistory'),
- saveChatHistorySummary: () => _stub('saveChatHistorySummary'),
- getChatHistorySummary: () => _stub('getChatHistorySummary'),
- saveChatHistoryTxt: () => _stub('saveChatHistoryTxt'),
- extractChatHistory: () => _stub('extractChatHistory'),
- readLastMessage: () => _stub('readLastMessage'),
- ocrLastMessage: () => _stub('ocrLastMessage'),
- getCachedScreenshot: () => _stub('getCachedScreenshot'),
- captureScreenshot: () => _stub('captureScreenshot'),
- sendScroll: () => _stub('sendScroll'),
- matchImageRegionLocation: () => _stub('matchImageRegionLocation'),
- ...overrides,
- }
- }
- module.exports = {
- runAdb,
- sendTap,
- sendSwipe,
- sendKeyEvent,
- sendText,
- appendLog,
- createElectronAPI,
- }
|