2023-10-12 11:35:27 +02:00
|
|
|
import util from 'node:util'
|
|
|
|
import child_process from 'node:child_process'
|
2023-09-18 09:07:28 +02:00
|
|
|
import { Adb } from '@devicefarmer/adbkit'
|
|
|
|
import adbPath from '@resources/core/adb.exe?asset&asarUnpack'
|
|
|
|
|
2023-10-12 11:35:27 +02:00
|
|
|
const exec = util.promisify(child_process.exec)
|
2023-10-07 07:22:40 +02:00
|
|
|
|
2023-09-18 09:07:28 +02:00
|
|
|
let client = null
|
|
|
|
|
|
|
|
window.addEventListener('beforeunload', () => {
|
|
|
|
if (client) {
|
|
|
|
client.kill()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-10-11 10:42:47 +02:00
|
|
|
const shell = async command => exec(`${adbPath} ${command}`)
|
2023-09-18 09:07:28 +02:00
|
|
|
const getDevices = async () => await client.listDevicesWithPaths()
|
2023-10-11 10:42:47 +02:00
|
|
|
const deviceShell = async (id, command) => await client.getDevice(id).shell(command)
|
2023-09-18 09:07:28 +02:00
|
|
|
const kill = async (...params) => await client.kill(...params)
|
|
|
|
const connect = async (...params) => await client.connect(...params)
|
|
|
|
const disconnect = async (...params) => await client.disconnect(...params)
|
|
|
|
|
2023-10-11 10:42:47 +02:00
|
|
|
const getDeviceIP = async (id) => {
|
|
|
|
try {
|
|
|
|
const { stdout } = await shell(`-s ${id} shell ip -f inet addr show wlan0`)
|
|
|
|
// console.log('stdout', stdout)
|
|
|
|
const reg = /inet ([0-9.]+)\/\d+/
|
|
|
|
const match = stdout.match(reg)
|
|
|
|
const value = match[1]
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const tcpip = async (id, port = 5555) => await client.getDevice(id).tcpip(port)
|
|
|
|
|
2023-09-18 09:07:28 +02:00
|
|
|
const watch = async (callback) => {
|
|
|
|
const tracker = await client.trackDevices()
|
2023-10-11 10:42:47 +02:00
|
|
|
tracker.on('add', async (ret) => {
|
|
|
|
const host = await getDeviceIP(ret.id)
|
|
|
|
callback('add', { ...ret, $host: host })
|
2023-09-18 09:07:28 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
tracker.on('remove', (device) => {
|
2023-10-07 07:22:40 +02:00
|
|
|
callback('remove', device)
|
2023-09-18 09:07:28 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
tracker.on('end', (ret) => {
|
2023-10-07 07:22:40 +02:00
|
|
|
callback('end', ret)
|
2023-09-18 09:07:28 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
tracker.on('error', (err) => {
|
2023-10-07 07:22:40 +02:00
|
|
|
callback('error', err)
|
2023-09-18 09:07:28 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
const close = () => tracker.end()
|
|
|
|
|
|
|
|
return close
|
|
|
|
}
|
|
|
|
|
|
|
|
export default () => {
|
|
|
|
client = Adb.createClient({ bin: adbPath })
|
|
|
|
console.log('client', client)
|
|
|
|
|
|
|
|
return {
|
|
|
|
shell,
|
2023-10-11 10:42:47 +02:00
|
|
|
getDevices,
|
|
|
|
deviceShell,
|
2023-09-18 09:07:28 +02:00
|
|
|
kill,
|
|
|
|
connect,
|
|
|
|
disconnect,
|
|
|
|
watch,
|
2023-10-11 10:42:47 +02:00
|
|
|
getDeviceIP,
|
|
|
|
tcpip,
|
2023-09-18 09:07:28 +02:00
|
|
|
}
|
|
|
|
}
|