Skip to content

Commit

Permalink
v0.2.8 - solve dismount developer disk error (#27)
Browse files Browse the repository at this point in the history
* solve dismount developer disk error;

* remove unused API
  • Loading branch information
yumiguan authored Jun 20, 2019
1 parent d190e9d commit ae87d0f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 67 deletions.
7 changes: 3 additions & 4 deletions lyrebird_ios/device_service.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
import shutil
import lyrebird
from lyrebird import context
from . import ios_helper
from lyrebird import context
from lyrebird.log import get_logger

_log = get_logger()
Expand Down Expand Up @@ -57,10 +57,9 @@ def start_log_recorder(self, device_id):

def publish_devices_info_event(self, online_devices, app_name):
devices = []
for item in online_devices:
device_info = online_devices[item]
for device_id, device_info in online_devices.items():
message_info = {
'id': device_info.device_id,
'id': device_id,
'info': {
'name': device_info.device_name,
'model': device_info.model,
Expand Down
20 changes: 10 additions & 10 deletions lyrebird_ios/ios_helper.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os
import plistlib
import subprocess
import codecs
import json
import time
import codecs
import plistlib
import subprocess
import lyrebird
from lyrebird import context
from lyrebird.log import get_logger
Expand Down Expand Up @@ -277,19 +277,19 @@ def take_screen_shot(self):
if not os.path.exists(screenshot_dir):
os.makedirs(screenshot_dir)
file_name = self.model.replace(' ', '_')
timestrap = int(time.time())
screen_shot_file = os.path.abspath(os.path.join(screenshot_dir, f'{file_name}_{timestrap}.png'))
timestamp = int(time.time())
screen_shot_file = os.path.abspath(os.path.join(screenshot_dir, f'{file_name}_{timestamp}.png'))
p = subprocess.run(f'{idevicescreenshot} -u {self.device_id} {screen_shot_file}',
shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
err_str = p.stdout.decode()
if p.returncode == 0:
return dict({
'screen_shot_file': screen_shot_file,
'timestrap': timestrap
'timestamp': timestamp
})
else:
_log.error(f'{err_str}')
return ''
return {}

def to_dict(self):
device_info = {k: self.__dict__[k] for k in self.__dict__ if not k.startswith('_')}
Expand All @@ -302,9 +302,9 @@ def to_dict(self):

def devices():
"""
devices 用于返回在线的设备示例集合
:type 字典
:return: online_devices 对象 (在线的设备)
:type dict
:return: online_devices object of online devices
"""
check_environment()

Expand Down
69 changes: 17 additions & 52 deletions lyrebird_ios/ui.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from flask import request, jsonify, send_from_directory
from lyrebird import context
import lyrebird
import codecs
import time
import os
import time
import codecs
import socket
import lyrebird
from flask import request, jsonify, send_from_directory
from lyrebird import context
from lyrebird.log import get_logger
from .device_service import DeviceService

Expand All @@ -31,23 +31,6 @@ def index(self):
"""
return codecs.open(self.get_package_file_path('templates/index.html'), 'r', 'utf-8').read()

def info(self):
"""
获取设备信息
:return:
"""
device_info = {'device': None, 'app': None}
if len(device_service.devices) == 0:
return jsonify(device_info)
device = list(device_service.devices.values())[0]
device_prop = device.to_dict()
device_info['device'] = {'UDID': device_prop['device_id'], 'Model': device_prop['model'], 'Version': device_prop['os_version']}

plugin_conf = lyrebird.context.application.conf.get('plugin.ios', {})
default_bundle_id = plugin_conf.get('bundle_id', '')
device_info['app'] = device.get_app_info(default_bundle_id)
return jsonify(device_info)

def conf(self):
plugin_conf = lyrebird.context.application.conf.get('plugin.ios', {})
default_bundle_id = plugin_conf.get('bundle_id', '')
Expand All @@ -60,10 +43,11 @@ def device_detail(self, device_id):
return "\n".join(device_service.devices.get(device_id).device_info)

def get_app_info(self, device_id, bundle_id):
device = device_service.devices.get(device_id)
def send_device_event():
device_service.publish_devices_info_event(device, bundle_id)
device_service.publish_devices_info_event(device_service.devices, bundle_id)
lyrebird.add_background_task('SendDeviceEvent', send_device_event)

device = device_service.devices.get(device_id)
return jsonify(device.get_app_info(bundle_id))

def app_list(self, device_id):
Expand All @@ -77,7 +61,7 @@ def app_list(self, device_id):
def start_app(self, device_id, bundle_id):
device = device_service.devices.get(device_id)
port = lyrebird.context.application.conf.get('mock.port')
res = device.start_app(bundle_id, get_ip(), port)
res = device.start_app(bundle_id, _get_ip(), port)
if 'ConnectionResetError' in res and '54' in res:
return context.make_fail_response('WDA is not ready!')
return context.make_ok_response()
Expand All @@ -96,10 +80,11 @@ def logcat_start(self, device_id):
def take_screen_shot(self, device_id):
device = device_service.devices.get(device_id)
img_info = device.take_screen_shot()
timestrap = img_info.get('timestrap')
timestamp = img_info.get('timestamp')
if img_info.get('screen_shot_file'):
test = {'imgUrl': f'/ui/plugin/iOS/api/src/screenshot/{device_id}?time={timestrap}'}
return jsonify(test)
return jsonify({
'imgUrl': f'/ui/plugin/iOS/api/src/screenshot/{device_id}?time={timestamp}'
})
else:
return context.make_fail_response('Could not start screenshot service! '
'Please make sure the idevicescreenshot command works correctly')
Expand Down Expand Up @@ -128,8 +113,8 @@ def get_screen_shot(self, message):
def get_screenshot_image(self, device_id):
if request.args.get('time'):
model = device_service.devices.get(device_id).model.replace(' ', '_')
timestrap = request.args.get('time')
return send_from_directory(screenshot_dir, f'{model}_{timestrap}.png')
timestamp = request.args.get('time')
return send_from_directory(screenshot_dir, f'{model}_{timestamp}.png')
else:
return None

Expand All @@ -153,32 +138,12 @@ def check_env(self):
from .ios_helper import error_msg
return jsonify(error_msg)

def desc(self):
device_info = self.info().json
if device_info.get('device'):
return jsonify({
"code": 0,
"data": "\n\n【设备应用信息】\n" +
"设备类型: %s\n" % device_info.get('device').get('Model') +
"设备系统: %s\n\n" % device_info.get('device').get('Version') +
"应用名称: %s\n" % device_info.get('app').get('AppName') +
"Version: %s\n" % device_info.get('app').get('VersionNumber') +
"Build: %s\n" % device_info.get('app').get('BuildNumber') +
"BundleID: %s" % device_info.get('app').get('BundleID')
})
else:
return context.make_fail_response('No device found, is it plugged in?')

def on_create(self):
"""
插件初始化函数(必选)
"""
# 设置模板目录(可选,设置模板文件目录。默认值templates)

# for overbridge
self.add_url_rule('/api/info', view_func=self.info)
# for Bugit
self.add_url_rule('/api/desc', view_func=self.desc)
# 获取设备列表
self.add_url_rule('/api/devices', view_func=self.device_list)
# 设备详情
Expand Down Expand Up @@ -215,12 +180,12 @@ def get_title():
return 'iOS'


def get_ip():
def _get_ip():
"""
获取当前设备在网络中的ip地址
:return: IP地址字符串
"""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('google.com', 80))
s.connect(('bing.com', 80))
return s.getsockname()[0]
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name='lyrebird-ios',
version='0.2.7',
version='0.2.8',
packages=['lyrebird_ios'],
url='https://github.com/meituan/lyrebird-ios',
author='HBQA',
Expand Down

0 comments on commit ae87d0f

Please sign in to comment.