28 lines
1.5 KiB
TypeScript
28 lines
1.5 KiB
TypeScript
import { createDriver } from '../drivers/factory';
|
|
import { enterPairingMode } from '../utils/common/relay.helper';
|
|
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
|
const dump = async (d: any): Promise<string[]> => [...new Set((Array.from((await d.getSource()).matchAll(/text="([^"]+)"/g)) as any[]).map((m) => m[1]).filter((t: string) => t.trim() && t.length < 30))].slice(0, 18) as string[];
|
|
(async () => {
|
|
const d = createDriver();
|
|
await d.createSession();
|
|
try {
|
|
console.log('当前页: ' + JSON.stringify(await dump(d)));
|
|
await enterPairingMode('robot_s1p').catch((e) => console.log('relay 失败:', e.message)); // ch11+12 5s
|
|
console.log('已按 ch11+12 5s');
|
|
await sleep(2000);
|
|
// 点 Next 进搜索
|
|
for (const t of ['Next', 'Connect device', 'Connect Device']) {
|
|
const b = await d.findElementRaw('-android uiautomator', `new UiSelector().text("${t}")`).catch(() => null);
|
|
if (b) { await d.tapElement(b); console.log('点了 ' + t); break; }
|
|
}
|
|
// 轮询搜索结果
|
|
for (let i = 0; i < 14; i++) {
|
|
await sleep(5000);
|
|
const t = await dump(d);
|
|
console.log(`+${(i + 1) * 5}s: ${JSON.stringify(t)}`);
|
|
if (/Configure Wi-?Fi|SSID|Select Device|Pick a room|added successfully|S1 Plus/i.test(t.join(' ')) && !/Search for a device/i.test(t.join(' '))) { console.log('==发现/进配网=='); break; }
|
|
}
|
|
} catch (e: any) { console.error('FAIL:', e.message); }
|
|
finally { await d.destroySession(); }
|
|
})();
|