diff --git a/frontend/src/api.js b/frontend/src/api.js index 01fcdd6..a037d42 100644 --- a/frontend/src/api.js +++ b/frontend/src/api.js @@ -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' diff --git a/frontend/src/components/iOS.vue b/frontend/src/components/iOS.vue index 3b744aa..e230b80 100644 --- a/frontend/src/components/iOS.vue +++ b/frontend/src/components/iOS.vue @@ -24,6 +24,7 @@ import DeviceList from '@/components/DeviceList.vue' import DeviceInfo from '@/components/DeviceInfo.vue' import ScreenShot from '@/components/ScreenShot.vue' import PackageInfo from '@/components/PackageInfo.vue' +import { checkEnv } from '@/api' export default { components: { @@ -38,8 +39,7 @@ export default { } }, created () { - this.$store.dispatch('loadDevices') - this.$io.on('device_ios', 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) @@ -50,6 +50,16 @@ export default { } }, methods: { + checkEnvironment () { + checkEnv() + .then( + this.$store.dispatch('loadDevices'), + this.$io.on('ios-device', this.getDevices) + ) + .catch(error => { + this.$bus.$emit('msg.error', error.data.message) + }) + }, getDevices () { this.$store.dispatch('loadDevices') }, diff --git a/lyrebird_ios/apis.py b/lyrebird_ios/apis.py index 99b7605..f035628 100644 --- a/lyrebird_ios/apis.py +++ b/lyrebird_ios/apis.py @@ -98,8 +98,11 @@ def get_screenshot_image(device_id): return None def check_env(): - from .ios_helper import error_msg - return jsonify(error_msg) + msg = device_service.check_env() + if device_service.status == device_service.RUNNING: + return make_ok_response() + else: + return make_fail_response(msg) def _get_ip(): diff --git a/lyrebird_ios/device_service.py b/lyrebird_ios/device_service.py index 0750b7c..4ee4e92 100644 --- a/lyrebird_ios/device_service.py +++ b/lyrebird_ios/device_service.py @@ -22,6 +22,16 @@ def __init__(self): self.devices = {} self.reset_screenshot_dir() + def check_env(self): + error_message = ios_helper.check_environment() + if not error_message: + self.status = self.RUNNING + _log.debug('iOS device listener start') + else: + self.status = self.STOP + _log.error(error_message) + return error_message + def devices_to_dict(self): json_obj = {} for device_id in self.devices: @@ -29,7 +39,7 @@ def devices_to_dict(self): return json_obj def run(self): - self.status = self.RUNNING + self.check_env() while self.status == self.RUNNING: try: self.handle() @@ -46,7 +56,7 @@ def handle(self): self.devices = devices self.publish_devices_info_event(self.devices, self.get_default_app_name()) - lyrebird.emit('device_ios') + lyrebird.emit('ios-device') def start_log_recorder(self, device_id): for _device_id in self.devices: diff --git a/lyrebird_ios/ios_helper.py b/lyrebird_ios/ios_helper.py index 62ca7db..b1e4d81 100644 --- a/lyrebird_ios/ios_helper.py +++ b/lyrebird_ios/ios_helper.py @@ -22,7 +22,6 @@ screenshot_dir = os.path.abspath(os.path.join(storage, 'screenshot')) PLIST_PATH = os.path.join(storage, 'plist') -error_msg = None ios_driver = wda_helper.Helper() @@ -31,18 +30,18 @@ def check_environment(): 检查用户环境,第三方依赖是否正确安装。 :return: """ - global idevice_id, idevicescreenshot, ideviceinfo, error_msg + global idevice_id, idevicescreenshot, ideviceinfo if not os.path.exists('/usr/local/bin/ideviceinfo'): - _log.error('ideviceinfo command not found, check your libimobiledevice') + return 'ideviceinfo command not found, check your libimobiledevice' else: p = subprocess.Popen('/usr/local/bin/ideviceinfo', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) err = p.stderr.read().decode() if len(err): - _log.error(f'Something wrong with the ideviceinfo program: {err}') + return f'Something wrong with the ideviceinfo program: {err}' if not os.path.exists('/usr/local/bin/idevicescreenshot'): - _log.debug('ideviceinfo command not found, check your libimobiledevice') + return 'ideviceinfo command not found, check your libimobiledevice' idevice_id = '/usr/local/bin/idevice_id' ideviceinfo = '/usr/local/bin/ideviceinfo' @@ -264,8 +263,6 @@ def devices(): :type dict :return: online_devices object of online devices """ - check_environment() - res = subprocess.run(f'{idevice_id} -l', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) output = res.stdout.decode() err_str = res.stderr.decode() diff --git a/lyrebird_ios/manifest.py b/lyrebird_ios/manifest.py index b8e0618..b22d2db 100644 --- a/lyrebird_ios/manifest.py +++ b/lyrebird_ios/manifest.py @@ -22,7 +22,7 @@ # get application list ('/api/apps/', apis.app_list, ['GET']), # environment check - ('/api/check-env', apis.check_env, ['GET']), + ('/api/check_env', apis.check_env, ['GET']), # get config ('/api/conf', apis.conf, ['GET']) ], diff --git a/setup.py b/setup.py index 7fb02f5..ea557d0 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setup( name='lyrebird-ios', - version='0.3.0', + version='0.3.1', packages=['lyrebird_ios'], url='https://github.com/meituan/lyrebird-ios', author='HBQA',