Skip to content

Commit

Permalink
fix: 🔧 修复 ADB 环境变量配置错误导致无法连接的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
viarotel committed Sep 16, 2023
1 parent 6dd2db9 commit 9ef7203
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import './ipc/index.js'
function createWindow() {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 900,
height: 670,
minWidth: 900,
minHeight: 700,
show: false,
autoHideMenuBar: true,
...(process.platform === 'linux' ? { icon } : {}),
Expand Down
12 changes: 7 additions & 5 deletions src/preload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { electronAPI } from '@electron-toolkit/preload'
import { Adb } from '@devicefarmer/adbkit'

import scrcpyPath from '../../resources/core/scrcpy.exe?asset&asarUnpack'
import adbPath from '../../resources/core/adb.exe?asset&asarUnpack'
import { addContext } from './helpers/index.js'

const util = require('node:util')
Expand All @@ -17,7 +18,7 @@ addContext('electron', electronAPI)
addContext('api', api)

addContext('adbkit', () => {
const client = Adb.createClient()
const client = Adb.createClient({ bin: adbPath })
console.log('client', client)

const getDevices = async () => await client.listDevicesWithPaths()
Expand Down Expand Up @@ -49,9 +50,9 @@ addContext('adbkit', () => {
return close
}

// window.addEventListener('beforeunload', () => {
// kill()
// })
window.addEventListener('beforeunload', () => {
kill()
})

return {
getDevices,
Expand All @@ -64,7 +65,8 @@ addContext('adbkit', () => {
})

addContext('scrcpy', () => {
const shell = command => exec(`${scrcpyPath} ${command}`)
const shell = command =>
exec(`${scrcpyPath} ${command}`, { env: { ...process.env, ADB: adbPath } })

return {
shell,
Expand Down
4 changes: 3 additions & 1 deletion src/renderer/src/components/Advanced/index.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<template>
<div class=""></div>
<div class="">
[WIP]
</div>
</template>

<script>
Expand Down
23 changes: 21 additions & 2 deletions src/renderer/src/components/Wired/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
</el-button>
</div>
<div class="pt-4 flex-1 h-0 overflow-hidden">
<el-table v-loading="loading" :data="deviceList" style="width: 100%" border height="100%">
<el-table
v-loading="loading"
:element-loading-text="loadingText"
:data="deviceList"
style="width: 100%"
border
height="100%"
>
<template #empty>
<el-empty description="设备列表为空" />
</template>
Expand All @@ -30,7 +37,14 @@
<el-table-column label="操作" width="300" align="center">
<template #default="{ row }">
<el-button type="primary" :loading="row.$loading" @click="handleStart(row)">
{{ row.$loading ? '运行中' : '连接设备' }}
{{ row.$loading ? '镜像中' : '开始镜像' }}
</el-button>
<el-button
:disabled="!row.$loading"
type="default"
@click="handleScreenUp(row)"
>
点亮屏幕
</el-button>
</template>
</el-table-column>
Expand All @@ -46,6 +60,7 @@ export default {
data() {
return {
loading: false,
loadingText: '初始化中...',
deviceList: [],
}
},
Expand All @@ -57,6 +72,9 @@ export default {
})
},
methods: {
handleScreenUp(row) {
this.$adb.shell(row.id, 'input keyevent KEYCODE_POWER')
},
handleReset() {
this.$electron.ipcRenderer.send('restart-app')
},
Expand Down Expand Up @@ -92,6 +110,7 @@ export default {
this.deviceList = []
}
this.loading = false
this.loadingText = '正在获取设备列表...'
},
},
}
Expand Down
33 changes: 28 additions & 5 deletions src/renderer/src/components/Wireless/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@
</el-button>
</div>
<div class="pt-4 flex-1 h-0 overflow-hidden">
<el-table v-loading="loading" :data="deviceList" style="width: 100%" border height="100%">
<el-table
v-loading="loading"
:element-loading-text="loadingText"
:data="deviceList"
style="width: 100%"
border
height="100%"
>
<template #empty>
<el-empty description="设备列表为空" />
</template>
Expand All @@ -44,9 +51,17 @@
<el-table-column label="操作" width="300" align="center">
<template #default="{ row }">
<el-button type="primary" :loading="row.$loading" @click="handleStart(row)">
{{ row.$loading ? '运行中' : '连接设备' }}
{{ row.$loading ? '镜像中' : '开始镜像' }}
</el-button>
<el-button type="danger" :loading="row.$stopLoading" @click="handleStop(row)">
<el-button :disabled="!row.$loading" type="default" @click="handleScreenUp(row)">
点亮屏幕
</el-button>
<el-button
:disabled="!row.$loading"
type="danger"
:loading="row.$stopLoading"
@click="handleStop(row)"
>
{{ row.$stopLoading ? '断开中' : '断开连接' }}
</el-button>
</template>
Expand All @@ -58,16 +73,19 @@

<script>
import { isIPWithPort, sleep } from '@renderer/utils/index.js'
import storage from '@renderer/utils/storages'
export default {
data() {
const adbCache = storage.get('adbCache') || {}
return {
loading: false,
loadingText: '初始化中...',
connectLoading: false,
deviceList: [],
formData: {
host: '',
port: null,
host: adbCache.host,
port: adbCache.port,
},
}
},
Expand All @@ -79,6 +97,9 @@ export default {
})
},
methods: {
handleScreenUp(row) {
this.$adb.shell(row.id, 'input keyevent KEYCODE_POWER')
},
handleReset() {
this.$electron.ipcRenderer.send('restart-app')
},
Expand All @@ -91,6 +112,7 @@ export default {
try {
await this.$adb.connect(this.formData.host, this.formData.port || 5555)
this.$message.success('连接设备成功')
storage.set('adbCache', this.formData)
}
catch (error) {
if (error.message)
Expand Down Expand Up @@ -144,6 +166,7 @@ export default {
this.deviceList = []
}
this.loading = false
this.loadingText = '正在获取设备列表...'
},
},
}
Expand Down
29 changes: 29 additions & 0 deletions src/renderer/src/utils/storages/cookieStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import jsCookie from 'js-cookie'

/**
* 操作 Cookie
* @method set 设置
* @method get 获取
* @method remove 移除
* @method clear 移除全部
*/
export default {
// 设置
set(key, val) {
jsCookie.set(key, JSON.stringify(val))
},
// 获取
get(key) {
const json = jsCookie.get(key)
try {
return JSON.parse(json)
}
catch (error) {
return json
}
},
// 移除
remove(key) {
jsCookie.remove(key)
},
}
7 changes: 7 additions & 0 deletions src/renderer/src/utils/storages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import defaultStorage from './localStorage'

// export { default as localStorage } from './localStorage'
// export { default as sessionStorage } from './sessionStorage'
// export { default as cookieStorage } from './cookieStorage'

export default defaultStorage
31 changes: 31 additions & 0 deletions src/renderer/src/utils/storages/localStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* window.localStorage
* @method set 设置
* @method get 获取
* @method remove 移除
* @method clear 移除全部
*/
export default {
// 设置
set(key, val) {
window.localStorage.setItem(key, JSON.stringify(val))
},
// 获取
get(key) {
const json = window.localStorage.getItem(key)
try {
return JSON.parse(json)
}
catch (error) {
return json
}
},
// 移除
remove(key) {
window.localStorage.removeItem(key)
},
// 移除全部
clear() {
window.localStorage.clear()
},
}
31 changes: 31 additions & 0 deletions src/renderer/src/utils/storages/sessionStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* window.sessionStorage
* @method set 设置
* @method get 获取
* @method remove 移除
* @method clear 移除全部
*/
export default {
// 设置
set(key, val) {
window.sessionStorage.setItem(key, JSON.stringify(val))
},
// 获取
get(key) {
const json = window.sessionStorage.getItem(key)
try {
return JSON.parse(json)
}
catch (error) {
return json
}
},
// 移除
remove(key) {
window.sessionStorage.removeItem(key)
},
// 移除全部
clear() {
window.sessionStorage.clear()
},
}

0 comments on commit 9ef7203

Please sign in to comment.