Skip to content

Commit

Permalink
add API: check Android env (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
yumiguan authored Apr 29, 2020
1 parent 098b8ae commit c35c4ea
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 9 deletions.
6 changes: 6 additions & 0 deletions frontend/src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ const errorHandler = (error) => {

axios.interceptors.response.use(successHandler, errorHandler)

export const checkEnv = () => {
return axios({
url: API_PREFIX + '/check_env'
})
}

export const getDevices = () => {
return axios({
url: API_PREFIX + '/devices'
Expand Down
14 changes: 12 additions & 2 deletions frontend/src/components/Android.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import ScreenShot from '@/components/ScreenShot.vue'
import PackageLaunch from '@/components/PackageLaunch.vue'
import PackageInstall from '@/components/PackageInstall.vue'
import PackageBoard from '@/components/PackageBoard.vue'
import { checkEnv } from '@/api'
export default {
components: {
Expand All @@ -60,8 +61,7 @@ export default {
}
},
created () {
this.$store.dispatch('loadDevices')
this.$io.on('device', this.getDevices)
this.checkEnvironment()
this.$bus.$on('msg.success', this.successMessage)
this.$bus.$on('msg.info', this.infoMessage)
this.$bus.$on('msg.error', this.errorMessage)
Expand All @@ -72,6 +72,16 @@ export default {
}
},
methods: {
checkEnvironment () {
checkEnv()
.then(
this.$store.dispatch('loadDevices'),
this.$io.on('android-device', this.getDevices)
)
.catch(error => {
this.$bus.$emit('msg.error', error.data.message)
})
},
getDevices () {
this.$store.dispatch('loadDevices')
},
Expand Down
5 changes: 2 additions & 3 deletions lyrebird_android/android_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class AndroidHomeError(Exception):
def check_android_home():
global adb
android_home = os.environ.get('ANDROID_HOME')
if not android_home or android_home == '':
raise AndroidHomeError('Not set env : ANDROID_HOME')
if not android_home:
raise AndroidHomeError('Environment variable ANDROID_HOME not found!')
if not os.path.exists(android_home):
raise AndroidHomeError('ANDROID_HOME %s not exists' % android_home)
if not os.path.isdir(android_home):
Expand Down Expand Up @@ -392,7 +392,6 @@ def adb_command_executor(self, command):


def devices():
check_android_home()
res = subprocess.run(f'{adb} devices -l', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = res.stdout.decode()
err_str = res.stderr.decode()
Expand Down
7 changes: 7 additions & 0 deletions lyrebird_android/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
if not os.path.exists(tmp_dir):
os.makedirs(tmp_dir)

def check_env():
msg = device_service.check_env()
if device_service.status == device_service.RUNNING:
return make_ok_response()
else:
return make_fail_response(msg)

def device_list():
device_list = device_service.devices_to_dict()
return make_ok_response(device_list=device_list)
Expand Down
16 changes: 13 additions & 3 deletions lyrebird_android/device_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,25 @@ def __init__(self):
self.reset_screenshot_dir()
logger.debug('DeviceService OnCreate')

def check_env(self):
try:
android_helper.check_android_home()
self.status = self.RUNNING
logger.debug('Android device listener start')
except Exception as e:
self.status = self.STOP
msg = e.args[0]
logger.error(msg)
return msg

def devices_to_dict(self):
json_obj = {}
for device_id in self.devices:
json_obj[device_id] = self.devices[device_id].to_dict()
return json_obj

def run(self):
self.status = self.RUNNING
logger.debug('Android device listener start')
self.check_env()
while self.status == self.RUNNING:
try:
self.handle()
Expand All @@ -59,7 +69,7 @@ def handle(self):
self.devices[_device_id].stop_log()
self.devices = devices

lyrebird.emit('device')
lyrebird.emit('android-device')
self.publish_devices_package_info(self.devices, config.load().package_name)

@staticmethod
Expand Down
2 changes: 2 additions & 0 deletions lyrebird_android/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
id='android',
name='Android',
api=[
# check env
('/api/check_env', apis.check_env, ['GET']),
# 获取设备列表
('/api/devices', apis.device_list, ['GET']),
# 应用详情
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name='lyrebird-android',
version='0.5.5',
version='0.5.6',
packages=['lyrebird_android'],
url='https://github.com/meituan/lyrebird-android',
author='HBQA',
Expand Down

0 comments on commit c35c4ea

Please sign in to comment.