2023-10-12 11:35:27 +02:00
|
|
|
import util from 'node:util'
|
|
|
|
import child_process from 'node:child_process'
|
2023-10-13 11:01:59 +02:00
|
|
|
import path from 'node:path'
|
|
|
|
import fs from 'node:fs'
|
|
|
|
import dayjs from 'dayjs'
|
2023-09-18 09:07:28 +02:00
|
|
|
import { Adb } from '@devicefarmer/adbkit'
|
2023-10-19 05:44:17 +02:00
|
|
|
import appStore from '@electron/helpers/store.js'
|
2023-10-17 09:27:08 +02:00
|
|
|
import { adbPath } from '@electron/configs/index.js'
|
2023-10-25 12:17:19 +02:00
|
|
|
import { uniq } from 'lodash-es'
|
2023-10-17 09:27:08 +02:00
|
|
|
|
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-26 11:50:45 +02:00
|
|
|
appStore.onDidChange('scrcpy.common.adbPath', async (value, oldValue) => {
|
|
|
|
console.log('onDidChange.scrcpy.common.adbPath', value)
|
2023-10-20 08:23:48 +02:00
|
|
|
|
|
|
|
if (value === oldValue) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value === client?.options?.bin) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-10-19 05:44:17 +02:00
|
|
|
if (client) {
|
|
|
|
await client.kill().catch(e => console.warn(e))
|
|
|
|
client = null
|
|
|
|
}
|
2023-10-20 08:23:48 +02:00
|
|
|
|
2023-10-24 10:46:51 +02:00
|
|
|
client = Adb.createClient({ bin: value || adbPath })
|
2023-10-19 05:44:17 +02:00
|
|
|
})
|
|
|
|
|
2023-10-11 10:42:47 +02:00
|
|
|
const shell = async command => exec(`${adbPath} ${command}`)
|
2023-10-17 11:54:29 +02:00
|
|
|
const getDevices = async () => client.listDevicesWithPaths()
|
2023-10-25 12:17:19 +02:00
|
|
|
const deviceShell = async (id, command) => {
|
|
|
|
const res = await client.getDevice(id).shell(command).then(Adb.util.readAll)
|
|
|
|
return res.toString()
|
|
|
|
}
|
2023-10-17 11:54:29 +02:00
|
|
|
const kill = async (...params) => client.kill(...params)
|
|
|
|
const connect = async (...params) => client.connect(...params)
|
|
|
|
const disconnect = async (...params) => client.disconnect(...params)
|
2023-09-18 09:07:28 +02:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-17 11:54:29 +02:00
|
|
|
const tcpip = async (id, port = 5555) => client.getDevice(id).tcpip(port)
|
2023-10-11 10:42:47 +02:00
|
|
|
|
2023-10-13 11:01:59 +02:00
|
|
|
const screencap = async (deviceId, options = {}) => {
|
|
|
|
let fileStream = null
|
|
|
|
try {
|
|
|
|
const device = client.getDevice(deviceId)
|
|
|
|
fileStream = await device.screencap()
|
|
|
|
console.log('fileStream', fileStream)
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
console.warn(error?.message || error)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fileStream) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
const fileName = `Screencap-${dayjs().format('YYYY-MM-DD-HH-mm-ss')}.png`
|
|
|
|
const savePath = options.savePath || path.resolve('../', fileName)
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fileStream
|
|
|
|
.pipe(fs.createWriteStream(savePath))
|
|
|
|
.on('finish', () => {
|
|
|
|
resolve(true)
|
|
|
|
})
|
|
|
|
.on('error', (error) => {
|
|
|
|
console.warn(error?.message || error)
|
|
|
|
reject(false)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-10-17 11:54:29 +02:00
|
|
|
const install = async (id, path) => client.getDevice(id).install(path)
|
|
|
|
|
2023-10-20 12:17:58 +02:00
|
|
|
const version = async () => client.version()
|
|
|
|
|
2023-10-25 12:17:19 +02:00
|
|
|
const display = async (deviceId) => {
|
|
|
|
let value = []
|
|
|
|
try {
|
|
|
|
const res = await deviceShell(deviceId, 'dumpsys display')
|
|
|
|
|
|
|
|
const regex = /Display Id=(\d+)/g
|
|
|
|
|
|
|
|
const match = res.match(regex) || []
|
|
|
|
|
|
|
|
const mapValue = match.map(item => item.split('=')[1])
|
|
|
|
|
|
|
|
value = uniq(mapValue)
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
console.error(error?.message || error)
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('display.deviceId.value', value)
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
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 () => {
|
2023-10-26 11:50:45 +02:00
|
|
|
const binPath = appStore.get('scrcpy.common.adbPath') || adbPath
|
2023-10-20 08:23:48 +02:00
|
|
|
|
|
|
|
client = Adb.createClient({
|
|
|
|
bin: binPath,
|
|
|
|
})
|
|
|
|
|
2023-10-19 05:44:17 +02:00
|
|
|
console.log('client', client)
|
2023-09-18 09:07:28 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
shell,
|
2023-10-11 10:42:47 +02:00
|
|
|
getDevices,
|
|
|
|
deviceShell,
|
2023-09-18 09:07:28 +02:00
|
|
|
kill,
|
|
|
|
connect,
|
|
|
|
disconnect,
|
2023-10-11 10:42:47 +02:00
|
|
|
getDeviceIP,
|
|
|
|
tcpip,
|
2023-10-13 11:01:59 +02:00
|
|
|
screencap,
|
2023-10-17 11:54:29 +02:00
|
|
|
install,
|
2023-10-20 12:17:58 +02:00
|
|
|
version,
|
2023-10-25 12:17:19 +02:00
|
|
|
display,
|
2023-10-13 11:01:59 +02:00
|
|
|
watch,
|
2023-09-18 09:07:28 +02:00
|
|
|
}
|
|
|
|
}
|