import { createDriver } from '../drivers/factory'; (async () => { const d = createDriver(); await d.createSession(); const KW = process.env.LIGHT_CARD || 'Strip Light 4E'; const W = 1080, H = 2400; const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); const status = async () => { const src = await d.getSource(); const texts = Array.from(src.matchAll(/text="([^"]+)"/g)).map((m) => m[1]); const i = texts.findIndex((t) => t.includes(KW)); return i >= 0 ? texts[i + 1] || '' : ''; }; const bri = async () => { const m = (await status()).match(/(\d+)\s*%/); return m ? +m[1] : -1; }; try { await d.dismissPopupIfPresent(); await d.goBackToHomepage(); await sleep(800); let el: string | null = null; for (let i = 0; i < 10; i++) { el = await d.findElementRaw('-android uiautomator', `new UiSelector().textContains("${KW}")`).catch(() => null); if (el) break; await d.swipe(540, 1500, 540, 700, 0.4).catch(() => {}); await sleep(800); } if (!el) throw new Error('找不到卡片'); const r = await d.getElementRect(el); await d.tap(r.x + r.width / 2, r.y + r.height / 2); await sleep(2500); // 开灯(动态电源键) const src0 = await d.getSource(); const pw = Array.from(src0.matchAll(/clickable="true"[^>]*?bounds="\[(\d+),(\d+)\]\[(\d+),(\d+)\]"/g)) .map((m) => ({ w: +m[3] - +m[1], h: +m[4] - +m[2], cx: (+m[1] + +m[3]) / 2, cy: (+m[2] + +m[4]) / 2 })) .filter((b) => b.w >= 150 && b.w <= 320 && b.h >= 150 && b.h <= 320 && Math.abs(b.cx - W / 2) < W * 0.15 && b.cy > H * 0.4); if (await bri() < 0 && pw.length) { await d.tap(Math.round(pw[0].cx), Math.round(pw[0].cy)); await sleep(3500); } console.log('开灯后状态=' + JSON.stringify(await status())); // 列出所有滑条 track const src = await d.getSource(); const tracks = Array.from(src.matchAll(/class="android\.view\.View"[^>]*?bounds="\[(\d+),(\d+)\]\[(\d+),(\d+)\]"/g)) .map((m) => ({ x1: +m[1], y1: +m[2], x2: +m[3], y2: +m[4] })) .filter((b) => b.x2 - b.x1 > W * 0.8 && b.y2 - b.y1 > 40 && b.y2 - b.y1 < 280 && b.y1 > H * 0.4) .map((b) => ({ left: b.x1 + 10, right: b.x2 - 10, y: Math.round((b.y1 + b.y2) / 2) })) .sort((a, b) => a.y - b.y); console.log('滑条数=' + tracks.length + ' y=' + JSON.stringify(tracks.map((t) => t.y))); // 逐条:从中点拖到低(0.25)读%,再拖到高(0.75)读% — 找亮度条+方向 for (const t of tracks) { const mid = Math.round((t.left + t.right) / 2); const loX = Math.round(t.left + 0.25 * (t.right - t.left)); const hiX = Math.round(t.left + 0.75 * (t.right - t.left)); // 起点尝试:先按当前亮度反算滑块位置(若读得到),否则用中点 const cur = await bri(); const span = t.right - t.left; const startX = cur >= 0 ? Math.round(t.left + cur / 100 * span) : mid; await d.swipe(startX, t.y, loX, t.y, 0.7); await sleep(2000); const aLo = await bri(); await d.swipe(loX, t.y, hiX, t.y, 0.7); await sleep(2000); const aHi = await bri(); console.log(`track y=${t.y}: 拖低→${aLo}% 拖高→${aHi}% ${aHi > aLo && aHi > 0 ? '★亮度条(正向)' : ''}`); } } catch (e: any) { console.error('探针失败:', e.message); } finally { await d.destroySession(); } })();