Skip to content

Commit

Permalink
feat: refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
conganhhcmus committed Jul 3, 2024
1 parent 2a64261 commit f61caa7
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 91 deletions.
3 changes: 0 additions & 3 deletions exec/.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
IS_BUILDED=FALSE
PORT=9090
ADB_PORT=5037
ADB_HOST=127.0.0.1
MONKEY_PORT=1080
49 changes: 1 addition & 48 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
}
},
"dependencies": {
"@devicefarmer/adbkit": "^3.2.6",
"appium": "^2.11.1",
"bluebird": "^3.7.2",
"buffer": "^6.0.3",
Expand Down Expand Up @@ -61,4 +60,4 @@
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.0.4"
}
}
}
53 changes: 29 additions & 24 deletions src/lib/adb.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
const { Adb } = require('@devicefarmer/adbkit')
const Bluebird = require('bluebird')
const { runExecAsync, runSpawn } = require('../utils/shell')

const { isMacOS } = require('../utils/platform')
const { runExecAsync } = require('../utils/shell')
const fs = require('fs')

const client = Adb.createClient({ port: process.env.ADB_PORT || 5037, host: process.env.ADB_HOST || '127.0.0.1' })
const getDeviceNameById = async (deviceId) => {
switch (process.platform) {
case 'darwin':
const output = await runExecAsync(`adb -s ${deviceId} emu avd name`)
return output.match(/([^\r\n|OK]+)/g)[0].replaceAll('_', ' ')
default:
return deviceId
}
}

class ADBHelper {
static getDevices = async () => {
const devices = await client.listDevices()
return await Bluebird.map(devices, async (_) => {
if (isMacOS) {
const output = await runExecAsync(`adb -s ${_.id} emu avd name`)
return {
id: _.id,
name: output.match(/([^\r\n|OK]+)/g)[0].replaceAll('_', ' '),
}
}
return {
id: _.id,
name: _.id,
}
})
const ignoreText = ['device', 'offline']
const output = await runExecAsync(`adb devices`)
const deviceIds = output
.substring(output.indexOf('\n') + 1)
.match(/[\S]+/g)
.filter((text) => !ignoreText.includes(text))

return await Bluebird.map(deviceIds, async (id) => ({
id: id,
name: await getDeviceNameById(id),
}))
}

static screenCap = async (deviceId, path) => {
const device = client.getDevice(deviceId)
const data = await device.screencap().then(Adb.util.readAll)
static screenCap = async (deviceId, path) => await runExecAsync(`adb -s ${deviceId} exec-out screencap -p > ${path}`)

static screenRecord = (deviceId, outputHandler = null) => {
const streamProcess = runSpawn(`adb -s ${deviceId} exec-out screenrecord --output-format=h264 -`)
streamProcess.stdout.on('data', (data) => {
outputHandler && outputHandler(data)
})

fs.writeFileSync(path, Buffer.from(data), 'binary')
return streamProcess
}
}

Expand Down
10 changes: 0 additions & 10 deletions src/utils/platform.js

This file was deleted.

7 changes: 3 additions & 4 deletions src/websocket.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const { runSpawn } = require('./utils/shell')
const { ADBHelper } = require('./lib/adb')

function getLiveScreen(ws, req) {
const deviceId = req.params.id

const streamProcess = runSpawn(`adb -s ${deviceId} exec-out screenrecord --output-format=h264 -`)
let chunk = Buffer.from([])
streamProcess.stdout.on('data', (data) => {

const streamProcess = ADBHelper.screenRecord(deviceId, (data) => {
if (isBeginChunk(data)) {
if (chunk.length > 0) {
if (ws.readyState == 2 || ws.readyState == 3) {
Expand Down

0 comments on commit f61caa7

Please sign in to comment.