| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- import { ipcMain } from 'electron';
- import { exec } from 'child_process';
- import { promisify } from 'util';
- import { getCachedAdbPath } from '../config.js';
- const execAsync = promisify(exec);
- // 发送 tap 事件到设备
- export async function sendTap(ipPort, x, y) {
- if (!ipPort) {
- return { success: false, error: '缺少设备 ID' };
- }
- if (typeof x !== 'number' || typeof y !== 'number') {
- return { success: false, error: '坐标必须是数字' };
- }
- try {
- const adbPath = getCachedAdbPath();
- const command = `${adbPath} -s ${ipPort} shell input tap ${x} ${y}`;
- await execAsync(command, {
- timeout: 5000,
- maxBuffer: 1024 * 1024
- });
- return { success: true };
- } catch (error) {
- return { success: false, error: error.message };
- }
- }
- // 发送 swipe 事件到设备
- export async function sendSwipe(ipPort, x1, y1, x2, y2, duration = 300) {
- if (!ipPort) {
- return { success: false, error: '缺少设备 ID' };
- }
- if (typeof x1 !== 'number' || typeof y1 !== 'number' || typeof x2 !== 'number' || typeof y2 !== 'number') {
- return { success: false, error: '坐标必须是数字' };
- }
- try {
- const adbPath = getCachedAdbPath();
- const command = `${adbPath} -s ${ipPort} shell input swipe ${x1} ${y1} ${x2} ${y2} ${duration}`;
- await execAsync(command, {
- timeout: 5000,
- maxBuffer: 1024 * 1024
- });
- return { success: true };
- } catch (error) {
- return { success: false, error: error.message };
- }
- }
- // 注册 IPC 处理器
- export function registerIpcHandlers() {
- // IPC 处理程序:发送 tap 事件到设备
- ipcMain.handle('send-tap', async (event, ipPort, x, y) => {
- return await sendTap(ipPort, x, y);
- });
- // IPC 处理程序:发送 swipe 事件到设备
- ipcMain.handle('send-swipe', async (event, ipPort, x1, y1, x2, y2, duration = 300) => {
- return await sendSwipe(ipPort, x1, y1, x2, y2, duration);
- });
- }
|