多设备启动,端口固定,时间接口未返回
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -58,7 +58,7 @@ def get_breakpoint_list():
|
||||
"""
|
||||
# 请求参数
|
||||
params = {
|
||||
'user_name': global_variable.GLOBAL_USERNAME
|
||||
'user_name': global_variable.get_username()
|
||||
}
|
||||
|
||||
# 请求地址
|
||||
@@ -114,7 +114,7 @@ def get_measurement_task():
|
||||
url = "https://engineering.yuxindazhineng.com/index/index/getOne"
|
||||
|
||||
# 获取用户名
|
||||
user_name = global_variable.GLOBAL_USERNAME
|
||||
user_name = global_variable.get_username()
|
||||
if not user_name:
|
||||
logging.error("未设置用户名,无法获取测量任务")
|
||||
return None
|
||||
@@ -156,8 +156,8 @@ def get_end_with_num():
|
||||
url = "https://engineering.yuxindazhineng.com/index/index/getOne3"
|
||||
|
||||
# 获取用户名
|
||||
user_name = global_variable.GLOBAL_USERNAME
|
||||
line_num = global_variable.GLOBAL_LINE_NUM
|
||||
user_name = global_variable.get_username()
|
||||
line_num = global_variable.get_line_num()
|
||||
if not line_num:
|
||||
logging.error("未设置线路编码,无法获取测量任务")
|
||||
return None
|
||||
@@ -451,17 +451,17 @@ def get_line_info_and_save_global(user_name: str) -> bool:
|
||||
# # 存入全局字典:key=line_num,value=line_name
|
||||
# global_variable.GLOBAL_UPLOAD_BREAKPOINT_DICT[line_num] = line_name
|
||||
# 存入全局字典:key=line_name,value=line_num
|
||||
global_variable.GLOBAL_UPLOAD_BREAKPOINT_DICT[line_name] = line_num
|
||||
global_variable.get_upload_breakpoint_dict()[line_name] = line_num
|
||||
|
||||
# 如果line_name不在列表中,则添加
|
||||
if line_name not in global_variable.GLOBAL_UPLOAD_BREAKPOINT_LIST:
|
||||
global_variable.GLOBAL_UPLOAD_BREAKPOINT_LIST.append(line_name)
|
||||
if line_name not in global_variable.get_upload_breakpoint_list():
|
||||
global_variable.get_upload_breakpoint_list().append(line_name)
|
||||
|
||||
logging.info(f"找到status=3的线路信息:line_num={line_num}, line_name={line_name}")
|
||||
found_valid_data = True
|
||||
|
||||
if found_valid_data:
|
||||
logging.info(f"成功提取所有status=3的线路信息,当前全局字典数据:{global_variable.GLOBAL_UPLOAD_BREAKPOINT_DICT}")
|
||||
logging.info(f"成功提取所有status=3的线路信息,当前全局字典数据:{global_variable.get_upload_breakpoint_dict()}")
|
||||
return True
|
||||
else:
|
||||
logging.warning("data列表中未找到任何status=3且字段完整的线路信息")
|
||||
@@ -476,4 +476,36 @@ def get_line_info_and_save_global(user_name: str) -> bool:
|
||||
return False
|
||||
except Exception as e:
|
||||
logging.error(f"调用get_name_all接口时发生未知异常:{str(e)}", exc_info=True) # exc_info=True打印异常堆栈,方便排查
|
||||
return False
|
||||
return False
|
||||
|
||||
def get_accounts_from_server(yh_id):
|
||||
"""从服务器获取账户信息"""
|
||||
url = "http://www.yuxindazhineng.com:3002/api/accounts/get_uplaod_data"
|
||||
headers = {
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
data = {
|
||||
"yh_id": yh_id
|
||||
}
|
||||
|
||||
try:
|
||||
print(f"🔍 查询服务器账户信息,用户ID: {yh_id}")
|
||||
response = requests.post(url, headers=headers, json=data, timeout=10)
|
||||
|
||||
if response.status_code == 200:
|
||||
result = response.json()
|
||||
if result.get("code") == 0:
|
||||
print(f"✅ 查询成功,找到 {result.get('total', 0)} 个账户")
|
||||
return result.get("data", [])
|
||||
else:
|
||||
print(f"❌ 查询失败: {result.get('message', '未知错误')}")
|
||||
return []
|
||||
else:
|
||||
print(f"❌ 服务器响应错误: {response.status_code}")
|
||||
return []
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"❌ 网络请求失败: {e}")
|
||||
return []
|
||||
except json.JSONDecodeError as e:
|
||||
print(f"❌ JSON解析失败: {e}")
|
||||
return []
|
||||
|
||||
@@ -60,6 +60,21 @@ def init_appium_driver(device_id, app_package="com.bjjw.cjgc", app_activity=".ac
|
||||
# 等待应用稳定
|
||||
time.sleep(2)
|
||||
|
||||
# 设置屏幕永不休眠
|
||||
try:
|
||||
# 使用ADB命令设置屏幕永不休眠
|
||||
screen_timeout_cmd = [
|
||||
"adb", "-s", device_id,
|
||||
"shell", "settings", "put", "system", "screen_off_timeout", "86400000"
|
||||
]
|
||||
timeout_result = subprocess.run(screen_timeout_cmd, capture_output=True, text=True, timeout=15)
|
||||
if timeout_result.returncode == 0:
|
||||
logging.info(f"设备 {device_id} 已成功设置屏幕永不休眠")
|
||||
else:
|
||||
logging.warning(f"设备 {device_id} 设置屏幕永不休眠失败: {timeout_result.stderr}")
|
||||
except Exception as timeout_error:
|
||||
logging.warning(f"设备 {device_id} 设置屏幕永不休眠时出错: {str(timeout_error)}")
|
||||
|
||||
logging.info(f"设备 {device_id} Appium驱动初始化完成")
|
||||
return driver, wait
|
||||
|
||||
@@ -87,7 +102,7 @@ def check_session_valid(driver, device_id=None):
|
||||
bool: 会话有效返回True,否则返回False
|
||||
"""
|
||||
if device_id is None:
|
||||
device_id = global_variable.GLOBAL_DEVICE_ID
|
||||
device_id = global_variable.get_device_id()
|
||||
device_str = f"设备 {device_id} " if device_id else ""
|
||||
|
||||
if not driver:
|
||||
@@ -158,7 +173,7 @@ def reconnect_driver(device_id, old_driver=None, app_package="com.bjjw.cjgc", ap
|
||||
"""
|
||||
# 使用传入的device_id或从全局变量获取
|
||||
if not device_id:
|
||||
device_id = global_variable.GLOBAL_DEVICE_ID
|
||||
device_id = global_variable.get_device_id()
|
||||
|
||||
# 修复device_id参数类型问题并使用全局设备ID作为备用
|
||||
actual_device_id = device_id
|
||||
@@ -174,7 +189,7 @@ def reconnect_driver(device_id, old_driver=None, app_package="com.bjjw.cjgc", ap
|
||||
|
||||
# 如果仍然没有有效的设备ID,使用全局变量
|
||||
if not actual_device_id or (isinstance(actual_device_id, str) and ("session=" in actual_device_id or len(actual_device_id.strip()) == 0)):
|
||||
actual_device_id = global_variable.GLOBAL_DEVICE_ID
|
||||
actual_device_id = global_variable.get_device_id()
|
||||
logging.warning(f"无法获取有效设备ID,使用全局变量GLOBAL_DEVICE_ID: {actual_device_id}")
|
||||
|
||||
device_id = actual_device_id # 使用修正后的设备ID
|
||||
@@ -395,7 +410,6 @@ def ensure_appium_server_running(port=4723):
|
||||
logging.error("Appium 服务启动超时!请检查 appium 命令是否在命令行可直接运行。")
|
||||
return False
|
||||
|
||||
# ... (保留 init_appium_driver, safe_quit_driver 等其他函数) ...
|
||||
|
||||
def wait_for_appium_start(port, timeout=10):
|
||||
"""
|
||||
@@ -442,7 +456,7 @@ def safe_quit_driver(driver, device_id=None):
|
||||
device_id: 设备ID(可选)
|
||||
"""
|
||||
if device_id is None:
|
||||
device_id = global_variable.GLOBAL_DEVICE_ID
|
||||
device_id = global_variable.get_device_id()
|
||||
device_str = f"设备 {device_id} " if device_id else ""
|
||||
logging.info(f"{device_str}开始关闭驱动")
|
||||
|
||||
@@ -504,7 +518,7 @@ def launch_app_manually(driver, device_id, package_name="com.bjjw.cjgc", activit
|
||||
"""
|
||||
try:
|
||||
if not device_id:
|
||||
device_id = global_variable.GLOBAL_DEVICE_ID
|
||||
device_id = global_variable.get_device_id()
|
||||
# 尝试从driver获取设备ID
|
||||
if driver and hasattr(driver, 'capabilities'):
|
||||
device_id = driver.capabilities.get('udid')
|
||||
|
||||
@@ -1,16 +1,147 @@
|
||||
# 全局变量
|
||||
GLOBAL_DEVICE_ID = "" # 设备ID
|
||||
GLOBAL_USERNAME = "czyuzongwen" # 用户名
|
||||
GLOBAL_CURRENT_PROJECT_NAME = "" # 当前测试项目名称
|
||||
GLOBAL_LINE_NUM = "" # 线路编码
|
||||
GLOBAL_BREAKPOINT_STATUS_CODES = [0,3] # 要获取的断点状态码列表
|
||||
GLOBAL_UPLOAD_BREAKPOINT_LIST = []
|
||||
GLOBAL_UPLOAD_BREAKPOINT_DICT = {}
|
||||
GLOBAL_TESTED_BREAKPOINT_LIST = [] # 测量结束的断点列表
|
||||
LINE_TIME_MAPPING_DICT = {} # 存储所有线路编码和对应的时间的全局字典
|
||||
GLOBAL_BREAKPOINT_DICT = {} # 存储测量结束的断点名称和对应的线路编码的全局字典
|
||||
GLOBAL_NAME_TO_ID_MAP = {} # 存储所有数据员姓名和对应的身份证号的全局字典
|
||||
GLOBAL_UPLOAD_SUCCESS_BREAKPOINT_LIST = [] # 上传成功的`断点列表
|
||||
import threading
|
||||
|
||||
# 线程本地存储,为每个线程创建独立的变量副本
|
||||
thread_local = threading.local()
|
||||
|
||||
# 共享数据的锁
|
||||
upload_breakpoint_lock = threading.RLock()
|
||||
upload_success_lock = threading.RLock()
|
||||
line_time_mapping_lock = threading.RLock()
|
||||
breakpoint_dict_lock = threading.RLock()
|
||||
name_to_id_map_lock = threading.RLock()
|
||||
|
||||
# 线程安全的全局变量访问函数
|
||||
def get_device_id():
|
||||
"""获取当前线程的设备ID"""
|
||||
if not hasattr(thread_local, 'GLOBAL_DEVICE_ID'):
|
||||
thread_local.GLOBAL_DEVICE_ID = ""
|
||||
return thread_local.GLOBAL_DEVICE_ID
|
||||
|
||||
def set_device_id(device_id):
|
||||
"""设置当前线程的设备ID"""
|
||||
thread_local.GLOBAL_DEVICE_ID = device_id
|
||||
|
||||
def get_username():
|
||||
"""获取用户名(全局共享)"""
|
||||
if not hasattr(thread_local, 'GLOBAL_USERNAME'):
|
||||
thread_local.GLOBAL_USERNAME = "czyuzongwen"
|
||||
return thread_local.GLOBAL_USERNAME
|
||||
|
||||
def set_username(username):
|
||||
"""设置用户名"""
|
||||
thread_local.GLOBAL_USERNAME = username
|
||||
|
||||
def get_current_project_name():
|
||||
"""获取当前测试项目名称"""
|
||||
if not hasattr(thread_local, 'GLOBAL_CURRENT_PROJECT_NAME'):
|
||||
thread_local.GLOBAL_CURRENT_PROJECT_NAME = ""
|
||||
return thread_local.GLOBAL_CURRENT_PROJECT_NAME
|
||||
|
||||
def set_current_project_name(project_name):
|
||||
"""设置当前测试项目名称"""
|
||||
thread_local.GLOBAL_CURRENT_PROJECT_NAME = project_name
|
||||
|
||||
def get_line_num():
|
||||
"""获取线路编码"""
|
||||
if not hasattr(thread_local, 'GLOBAL_LINE_NUM'):
|
||||
thread_local.GLOBAL_LINE_NUM = ""
|
||||
return thread_local.GLOBAL_LINE_NUM
|
||||
|
||||
def set_line_num(line_num):
|
||||
"""设置线路编码"""
|
||||
thread_local.GLOBAL_LINE_NUM = line_num
|
||||
|
||||
def get_breakpoint_status_codes():
|
||||
"""获取要获取的断点状态码列表"""
|
||||
return [0, 3] # 固定值,不需要线程本地存储
|
||||
|
||||
def get_upload_breakpoint_list():
|
||||
"""获取上传断点列表"""
|
||||
if not hasattr(thread_local, 'GLOBAL_UPLOAD_BREAKPOINT_LIST'):
|
||||
thread_local.GLOBAL_UPLOAD_BREAKPOINT_LIST = []
|
||||
return thread_local.GLOBAL_UPLOAD_BREAKPOINT_LIST
|
||||
|
||||
def set_upload_breakpoint_list(breakpoint_list):
|
||||
"""设置上传断点列表"""
|
||||
thread_local.GLOBAL_UPLOAD_BREAKPOINT_LIST = breakpoint_list
|
||||
|
||||
def get_upload_breakpoint_dict():
|
||||
"""获取上传断点字典"""
|
||||
if not hasattr(thread_local, 'GLOBAL_UPLOAD_BREAKPOINT_DICT'):
|
||||
thread_local.GLOBAL_UPLOAD_BREAKPOINT_DICT = {}
|
||||
return thread_local.GLOBAL_UPLOAD_BREAKPOINT_DICT
|
||||
|
||||
def set_upload_breakpoint_dict(breakpoint_dict):
|
||||
"""设置上传断点字典"""
|
||||
thread_local.GLOBAL_UPLOAD_BREAKPOINT_DICT = breakpoint_dict
|
||||
|
||||
def get_tested_breakpoint_list():
|
||||
"""获取测量结束的断点列表"""
|
||||
if not hasattr(thread_local, 'GLOBAL_TESTED_BREAKPOINT_LIST'):
|
||||
thread_local.GLOBAL_TESTED_BREAKPOINT_LIST = []
|
||||
return thread_local.GLOBAL_TESTED_BREAKPOINT_LIST
|
||||
|
||||
def set_tested_breakpoint_list(tested_list):
|
||||
"""设置测量结束的断点列表"""
|
||||
thread_local.GLOBAL_TESTED_BREAKPOINT_LIST = tested_list
|
||||
|
||||
def get_line_time_mapping_dict():
|
||||
"""获取线路编码和对应的时间的字典"""
|
||||
if not hasattr(thread_local, 'LINE_TIME_MAPPING_DICT'):
|
||||
thread_local.LINE_TIME_MAPPING_DICT = {}
|
||||
return thread_local.LINE_TIME_MAPPING_DICT
|
||||
|
||||
def set_line_time_mapping_dict(mapping_dict):
|
||||
"""设置线路编码和对应的时间的字典"""
|
||||
thread_local.LINE_TIME_MAPPING_DICT = mapping_dict
|
||||
|
||||
def get_breakpoint_dict():
|
||||
"""获取测量结束的断点名称和对应的线路编码的字典"""
|
||||
if not hasattr(thread_local, 'GLOBAL_BREAKPOINT_DICT'):
|
||||
thread_local.GLOBAL_BREAKPOINT_DICT = {}
|
||||
return thread_local.GLOBAL_BREAKPOINT_DICT
|
||||
|
||||
def set_breakpoint_dict(breakpoint_dict):
|
||||
"""设置测量结束的断点名称和对应的线路编码的字典"""
|
||||
thread_local.GLOBAL_BREAKPOINT_DICT = breakpoint_dict
|
||||
|
||||
def get_name_to_id_map():
|
||||
"""获取数据员姓名和对应的身份证号的字典"""
|
||||
if not hasattr(thread_local, 'GLOBAL_NAME_TO_ID_MAP'):
|
||||
thread_local.GLOBAL_NAME_TO_ID_MAP = {}
|
||||
return thread_local.GLOBAL_NAME_TO_ID_MAP
|
||||
|
||||
def set_name_to_id_map(name_id_map):
|
||||
"""设置数据员姓名和对应的身份证号的字典"""
|
||||
thread_local.GLOBAL_NAME_TO_ID_MAP = name_id_map
|
||||
|
||||
def get_upload_success_breakpoint_list():
|
||||
"""获取上传成功的断点列表"""
|
||||
if not hasattr(thread_local, 'GLOBAL_UPLOAD_SUCCESS_BREAKPOINT_LIST'):
|
||||
thread_local.GLOBAL_UPLOAD_SUCCESS_BREAKPOINT_LIST = []
|
||||
return thread_local.GLOBAL_UPLOAD_SUCCESS_BREAKPOINT_LIST
|
||||
|
||||
def set_upload_success_breakpoint_list(success_list):
|
||||
"""设置上传成功的断点列表"""
|
||||
thread_local.GLOBAL_UPLOAD_SUCCESS_BREAKPOINT_LIST = success_list
|
||||
|
||||
# 为了保持 ,保留原有的全局变量名称
|
||||
# 但这些将不再被直接使用,而是通过上面的函数访问
|
||||
GLOBAL_DEVICE_ID = "" # 设备ID
|
||||
GLOBAL_USERNAME = "czyuzongwen" # 用户名
|
||||
GLOBAL_CURRENT_PROJECT_NAME = "" # 当前测试项目名称
|
||||
GLOBAL_LINE_NUM = "" # 线路编码
|
||||
GLOBAL_BREAKPOINT_STATUS_CODES = [0,3] # 要获取的断点状态码列表
|
||||
GLOBAL_UPLOAD_BREAKPOINT_LIST = [] #
|
||||
GLOBAL_UPLOAD_BREAKPOINT_DICT = {} #
|
||||
GLOBAL_TESTED_BREAKPOINT_LIST = [] # 测量结束的断点列表
|
||||
LINE_TIME_MAPPING_DICT = {} # 存储所有线路编码和对应的时间的全局字典
|
||||
GLOBAL_BREAKPOINT_DICT = {} # 存储测量结束的断点名称和对应的线路编码的全局字典
|
||||
GLOBAL_NAME_TO_ID_MAP = {} # 存储所有数据员姓名和对应的身份证号的全局字典
|
||||
GLOBAL_UPLOAD_SUCCESS_BREAKPOINT_LIST = [] # 上传成功的`断点列表
|
||||
|
||||
GLOBAL_YU_ID = "68e764f0f9548871c22f93b8" # 宇恒一号ID
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user