结合自动到轨迹
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
# screenshot_page.py
|
||||
from socket import TCP_NODELAY
|
||||
import subprocess
|
||||
import logging
|
||||
import time
|
||||
@@ -543,9 +544,10 @@ class ScreenshotPage:
|
||||
# 获取项目名称
|
||||
project_name = global_variable.get_current_project_name() or "默认项目"
|
||||
|
||||
# 获取当前日期(如果没有提供)
|
||||
if not date_str:
|
||||
date_str = datetime.now().strftime("%Y%m%d")
|
||||
# 获取当前日期
|
||||
date_str = datetime.now().strftime("%Y%m%d")
|
||||
# if not date_str:
|
||||
# date_str = datetime.now().strftime("%Y%m%d")
|
||||
|
||||
# 获取当前时间(如果没有提供),并确保格式合法(不含冒号)
|
||||
if not time_str:
|
||||
@@ -610,14 +612,28 @@ class ScreenshotPage:
|
||||
"""
|
||||
try:
|
||||
# D盘txt文件路径
|
||||
txt_file_path = "D:\\uploadInfo\\screenshot_status.txt"
|
||||
# txt_file_path = "D:\\uploadInfo\\screenshot_status.txt"
|
||||
date_str = datetime.now().strftime("%Y%m%d")
|
||||
project_name = global_variable.get_current_project_name() or "默认项目"
|
||||
# 创建D盘下的截图目录结构:D:\uploadInfo\picture\项目名\年月日
|
||||
screenshots_dir = os.path.join("D:\\", "uploadInfo", "picture", project_name, date_str)
|
||||
|
||||
# 确保目录存在
|
||||
try:
|
||||
os.makedirs(screenshots_dir, exist_ok=True)
|
||||
self.logger.info(f"截图数据保存目录: {screenshots_dir}")
|
||||
txt_file_path = os.path.join(screenshots_dir, "screenshot_status.txt")
|
||||
except Exception as dir_error:
|
||||
self.logger.error(f"创建截图目录失败: {str(dir_error)}")
|
||||
return False
|
||||
|
||||
# 获取当前时间
|
||||
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
# 状态信息
|
||||
status = "成功" if success else "失败"
|
||||
status_line = f"{current_time} - {breakpoint_name} - 截图{status}\n"
|
||||
logo = "✅" if success else "❌"
|
||||
status_line = f"{logo}{current_time} - {breakpoint_name} - 截图{status}\n"
|
||||
|
||||
# 追加写入文件
|
||||
with open(txt_file_path, 'a', encoding='utf-8') as f:
|
||||
@@ -1031,16 +1047,22 @@ class ScreenshotPage:
|
||||
|
||||
# 获取列表元素
|
||||
list_view = self.driver.find_element(AppiumBy.ID, "com.bjjw.cjgc:id/auto_data_list")
|
||||
self.logger.info(f"时间戳1: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}")
|
||||
|
||||
same_content_count = 0
|
||||
|
||||
# 初始化第一次的子元素文本
|
||||
initial_child_elements = list_view.find_elements(AppiumBy.CLASS_NAME, "android.widget.TextView")
|
||||
current_child_texts = "|".join([
|
||||
elem.text.strip() for elem in initial_child_elements
|
||||
if elem.text and elem.text.strip()
|
||||
])
|
||||
|
||||
self.logger.info(f"时间戳2: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}")
|
||||
# current_child_texts = "|".join([
|
||||
# elem.text.strip() for elem in initial_child_elements
|
||||
# if elem.text and elem.text.strip()
|
||||
# ])
|
||||
current_child_texts = "|".join(
|
||||
elem_text.strip() for elem in initial_child_elements
|
||||
if (elem_text := elem.text) and elem_text.strip()
|
||||
)
|
||||
self.logger.info(f"时间戳3: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}")
|
||||
for i in range(max_swipes):
|
||||
# 执行下滑操作
|
||||
self.driver.execute_script("mobile: scrollGesture", {
|
||||
@@ -1050,14 +1072,12 @@ class ScreenshotPage:
|
||||
'duration': 500
|
||||
})
|
||||
|
||||
# time.sleep(0.5)
|
||||
|
||||
# 获取滑动后的子元素文本
|
||||
new_child_elements = list_view.find_elements(AppiumBy.CLASS_NAME, "android.widget.TextView")
|
||||
new_child_texts = "|".join([
|
||||
elem.text.strip() for elem in new_child_elements
|
||||
if elem.text and elem.text.strip()
|
||||
])
|
||||
new_child_texts = "|".join(
|
||||
elem_text.strip() for elem in new_child_elements
|
||||
if (elem_text := elem.text) and elem_text.strip()
|
||||
)
|
||||
|
||||
# 判断内容是否变化:若连续3次相同,认为到达底部
|
||||
if new_child_texts == current_child_texts:
|
||||
@@ -1354,7 +1374,7 @@ class ScreenshotPage:
|
||||
# 获取当前年月日(例如:20251023)
|
||||
current_date = datetime.now().strftime("%Y%m%d")
|
||||
# 拼接格式:年月日.1.用户名.txt
|
||||
filename = f"{current_date}.1.{global_variable.get_username()}.txt"
|
||||
filename = f"{current_date}_1_{global_variable.get_username()}.txt"
|
||||
file_path = os.path.join(log_directory, filename)
|
||||
|
||||
# 轮询等待文件出现
|
||||
@@ -1426,7 +1446,7 @@ class ScreenshotPage:
|
||||
self.logger.error(f"设备 {device_id} 加载线路时间映射字典失败")
|
||||
return False
|
||||
|
||||
# time.sleep(5)
|
||||
time.sleep(5)
|
||||
|
||||
# 循环检查数据数量是否一致,直到获取到完整数据
|
||||
retry_count = 0
|
||||
@@ -1466,7 +1486,7 @@ class ScreenshotPage:
|
||||
# 检查GLOBAL_BREAKPOINT_DICT是否为空,如果为空则初始化一些测试数据
|
||||
if not global_variable.get_upload_breakpoint_dict():
|
||||
self.logger.warning("global_variable.get_upload_breakpoint_dict()为空,正在初始化测试数据")
|
||||
# 添加一些测试断点数据,实际使用时应该从其他地方加载
|
||||
# 添加一些测试断点数据,实际使用时是接口返回
|
||||
# 注意:这里的值应该是字典,与section_mileage_config_page.py中的数据结构保持一致
|
||||
global_variable.set_breakpoint_dict({
|
||||
"CZSCZQ-3-康定2号隧道-DK297+201-DK297+199-山区": {
|
||||
@@ -1479,83 +1499,158 @@ class ScreenshotPage:
|
||||
}
|
||||
})
|
||||
|
||||
# 开始循环
|
||||
breakpoint_names = list(global_variable.get_upload_breakpoint_dict().keys())
|
||||
self.logger.info(f"设备 {device_id} 获取的断点名称列表: {breakpoint_names}")
|
||||
processed_breakpoints = [] # 创建一个列表,用于记录已处理的断点
|
||||
# 开始循环处理断点
|
||||
# 检测当前页面获取到的标段是否在breakpoint_names中
|
||||
max_scroll_attempts = 100 # 最大滚动尝试次数
|
||||
scroll_count = 0
|
||||
previous_items = set() # 记录前一次获取的项目集合,用于检测是否到达边界
|
||||
found_any = False # 标记是否找到并处理了任何断点
|
||||
max_retry_attempts = 3 # 每个断点的最大重试次数
|
||||
all_success = True
|
||||
for breakpoint_name in global_variable.get_upload_breakpoint_dict().keys():
|
||||
self.logger.info(f"开始处理要平差截图的断点 {breakpoint_name}")
|
||||
# 把断点名称给find_keyword
|
||||
if not self.find_keyword(breakpoint_name):
|
||||
self.logger.error(f"设备 {device_id} 未找到包含 {breakpoint_name} 的文件名")
|
||||
all_success = False
|
||||
continue
|
||||
|
||||
if not self.handle_measurement_dialog():
|
||||
self.logger.error(f"设备 {device_id} 处理测量弹窗失败")
|
||||
all_success = False
|
||||
continue
|
||||
|
||||
if not self.check_apply_btn():
|
||||
self.logger.error(f"设备 {device_id} 检查平差处理按钮失败")
|
||||
all_success = False
|
||||
self.click_back_button(device_id)
|
||||
continue
|
||||
|
||||
# 根据断点名称在get_upload_breakpoint_dict()中获取线路编码
|
||||
line_code = global_variable.get_upload_breakpoint_dict().get(breakpoint_name)
|
||||
if not line_code:
|
||||
self.logger.error(f"设备 {device_id} 未找到断点 {breakpoint_name} 对应的线路编码")
|
||||
all_success = False
|
||||
continue
|
||||
|
||||
# 根据线路编码查找对应的时间
|
||||
date_str, time_str = self.get_line_end_time(line_code)
|
||||
if not time_str or not date_str:
|
||||
self.logger.error(f"设备 {device_id} 未找到线路 {line_code} 对应的时间")
|
||||
all_success = False
|
||||
continue
|
||||
|
||||
# 修改时间
|
||||
if not self.set_device_time(device_id, time_str, date_str):
|
||||
self.logger.error(f"设备 {device_id} 设置设备时间失败")
|
||||
all_success = False
|
||||
continue
|
||||
|
||||
# 滑动列表到底部
|
||||
if not self.scroll_list_to_bottom(device_id):
|
||||
self.logger.error(f"设备 {device_id} 下滑列表到底部失败")
|
||||
all_success = False
|
||||
continue
|
||||
while scroll_count < max_scroll_attempts:
|
||||
# 获取当前页面中的所有项目
|
||||
current_items = self.get_current_items()
|
||||
self.logger.info(f"当前页面找到 {len(current_items)} 个项目")
|
||||
|
||||
# 2. 点击最后一个spinner
|
||||
if not self.click_last_spinner_with_retry(device_id):
|
||||
self.logger.error(f"设备 {device_id} 点击最后一个spinner失败")
|
||||
all_success = False
|
||||
continue
|
||||
|
||||
# 3. 再下滑一次
|
||||
if not self.scroll_down_once(device_id):
|
||||
self.logger.warning(f"设备 {device_id} 再次下滑失败,但继续执行")
|
||||
|
||||
# 平差处理完成后截图
|
||||
time.sleep(0.2) # 等待平差处理按钮点击后的界面变化
|
||||
logging.info("断点保存到上传列表成功,开始截图")
|
||||
# png_time = date_str + " " + time_str
|
||||
if not self.take_screenshot(breakpoint_name, date_str, time_str):
|
||||
self.logger.error(f"设备 {device_id} 截图失败")
|
||||
self.write_screenshot_status(breakpoint_name, success=False)
|
||||
all_success = False
|
||||
else:
|
||||
self.write_screenshot_status(breakpoint_name, success=True)
|
||||
# 检查当前页面的项目是否在breakpoint_names中
|
||||
for item in current_items:
|
||||
# 检查项目是否在breakpoint_names中且未处理
|
||||
if item in breakpoint_names and item not in processed_breakpoints:
|
||||
self.logger.info(f"找到目标断点: {item}")
|
||||
found_any = True
|
||||
line_code = None
|
||||
# 获取线路编码
|
||||
try:
|
||||
line_code = global_variable.get_upload_breakpoint_dict()[item]
|
||||
except KeyError:
|
||||
self.logger.error(f"未找到断点 {item} 的线路编码")
|
||||
continue
|
||||
|
||||
# 点击返回按钮并处理弹窗
|
||||
if not self.execute_back_navigation_steps(device_id):
|
||||
self.logger.error(f"设备 {device_id} 处理返回按钮确认失败")
|
||||
all_success = False
|
||||
# 启用WiFi
|
||||
# if not self.enable_wifi(device_id):
|
||||
# self.logger.error(f"设备 {device_id} 启用WiFi失败")
|
||||
# return False
|
||||
# 重试处理断点
|
||||
retry_count = 0
|
||||
while retry_count < max_retry_attempts:
|
||||
self.logger.info(f"开始处理断点 {item} (第 {retry_count + 1} 次尝试)")
|
||||
|
||||
# 点击目标项目
|
||||
if not self.click_item_by_text(item):
|
||||
self.logger.error(f"点击目标断点失败: {item}")
|
||||
retry_count += 1
|
||||
all_success = False
|
||||
continue
|
||||
|
||||
if not self.handle_measurement_dialog():
|
||||
self.logger.error(f"设备 {device_id} 处理测量弹窗失败")
|
||||
retry_count += 1
|
||||
all_success = False
|
||||
continue
|
||||
|
||||
if not self.check_apply_btn():
|
||||
self.logger.error(f"设备 {device_id} 检查平差处理按钮失败")
|
||||
retry_count += 1
|
||||
all_success = False
|
||||
self.click_back_button(device_id)
|
||||
continue
|
||||
|
||||
# 根据线路编码查找对应的时间
|
||||
date_str, time_str = self.get_line_end_time(line_code)
|
||||
if not time_str or not date_str:
|
||||
self.logger.error(f"设备 {device_id} 未找到线路 {line_code} 对应的时间")
|
||||
retry_count += 1
|
||||
all_success = False
|
||||
continue
|
||||
|
||||
# 修改时间
|
||||
if not self.set_device_time(device_id, time_str, date_str):
|
||||
self.logger.error(f"设备 {device_id} 设置设备时间失败")
|
||||
retry_count += 1
|
||||
all_success = False
|
||||
continue
|
||||
|
||||
# 滑动列表到底部
|
||||
if not self.scroll_list_to_bottom(device_id):
|
||||
self.logger.error(f"设备 {device_id} 下滑列表到底部失败")
|
||||
retry_count += 1
|
||||
all_success = False
|
||||
continue
|
||||
|
||||
# 2. 点击最后一个spinner
|
||||
if not self.click_last_spinner_with_retry(device_id):
|
||||
self.logger.error(f"设备 {device_id} 点击最后一个spinner失败")
|
||||
retry_count += 1
|
||||
all_success = False
|
||||
continue
|
||||
|
||||
# 3. 再下滑一次
|
||||
while True:
|
||||
if self.scroll_down_once(device_id):
|
||||
self.logger.warning(f"设备 {device_id} 下滑成功,跳出下滑一次循环")
|
||||
break
|
||||
else:
|
||||
self.logger.warning(f"设备 {device_id} 下滑失败,循环执行下滑一次")
|
||||
|
||||
|
||||
# 平差处理完成后截图
|
||||
time.sleep(0.2) # 等待平差处理按钮点击后的界面变化
|
||||
logging.info("断点保存到上传列表成功,开始截图")
|
||||
# png_time = date_str + " " + time_str
|
||||
if not self.take_screenshot(item, date_str, time_str):
|
||||
self.logger.error(f"设备 {device_id} 截图失败")
|
||||
self.write_screenshot_status(item, success=False)
|
||||
retry_count += 1
|
||||
all_success = False
|
||||
else:
|
||||
self.write_screenshot_status(item, success=True)
|
||||
|
||||
# 点击返回按钮并处理弹窗
|
||||
if not self.execute_back_navigation_steps(device_id):
|
||||
self.logger.error(f"设备 {device_id} 处理返回按钮确认失败")
|
||||
retry_count += 1
|
||||
all_success = False
|
||||
|
||||
# 成功处理完一个断点,添加到已处理列表
|
||||
processed_breakpoints.append(item)
|
||||
if item in processed_breakpoints: # 跳过重复处理
|
||||
self.logger.info(f"成功处理断点: {item}")
|
||||
break # 跳出重试循环
|
||||
|
||||
# 检查是否重试次数用完仍未成功
|
||||
if retry_count >= max_retry_attempts:
|
||||
self.logger.error(f"断点 {item} 经过 {max_retry_attempts} 次重试后仍然失败")
|
||||
# 重新获取当前页面项目,因为可能有新的项目加载
|
||||
break
|
||||
|
||||
|
||||
# # 根据截图结果更新time.txt文件状态
|
||||
# status = "ok" if all_success else "true"
|
||||
# # self.update_upload_info_status(status)
|
||||
# username = global_variable.get_username()
|
||||
# self.update_file_status(username, "running", status)
|
||||
# self.logger.info(f"{username} 截图完成状态为 {status}")
|
||||
|
||||
# self.logger.info(f"设备 {device_id} 截图页面操作执行完成,状态: {status}")
|
||||
# return True
|
||||
|
||||
|
||||
# 向下滑动列表以加载更多项目
|
||||
if not self.scroll_list(direction="down"):
|
||||
self.logger.error("向下滑动列表失败")
|
||||
break
|
||||
|
||||
# 检查是否到达底部:连续两次获取的项目相同
|
||||
if current_items == previous_items and len(current_items) > 0:
|
||||
self.logger.info("连续两次获取的项目相同,已到达列表底部")
|
||||
break
|
||||
|
||||
# 更新前一次项目集合
|
||||
previous_items = current_items.copy()
|
||||
|
||||
scroll_count += 1
|
||||
self.logger.info(f"第 {scroll_count} 次向下滑动,继续查找...")
|
||||
|
||||
# 根据截图结果更新time.txt文件状态
|
||||
status = "ok" if all_success else "true"
|
||||
# self.update_upload_info_status(status)
|
||||
@@ -1564,7 +1659,109 @@ class ScreenshotPage:
|
||||
self.logger.info(f"{username} 截图完成状态为 {status}")
|
||||
|
||||
self.logger.info(f"设备 {device_id} 截图页面操作执行完成,状态: {status}")
|
||||
return True
|
||||
# return True
|
||||
# 检查是否所有断点都处理完成
|
||||
if len(processed_breakpoints) == len(breakpoint_names):
|
||||
self.logger.info(f"设备 {device_id} 平差页面操作执行完成")
|
||||
return True
|
||||
elif found_any:
|
||||
self.logger.warning(f"设备 {device_id} 部分断点处理完成,已成功处理 {len(processed_breakpoints)}/{len(breakpoint_names)} 个断点")
|
||||
return True
|
||||
else:
|
||||
self.logger.warning(f"设备 {device_id} 未找到任何目标断点")
|
||||
return True
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# # 开始循环
|
||||
# all_success = True
|
||||
# for breakpoint_name in global_variable.get_upload_breakpoint_dict().keys():
|
||||
# self.logger.info(f"开始处理要平差截图的断点 {breakpoint_name}")
|
||||
# # 把断点名称给find_keyword
|
||||
# if not self.find_keyword(breakpoint_name):
|
||||
# self.logger.error(f"设备 {device_id} 未找到包含 {breakpoint_name} 的文件名")
|
||||
# all_success = False
|
||||
# continue
|
||||
|
||||
# if not self.handle_measurement_dialog():
|
||||
# self.logger.error(f"设备 {device_id} 处理测量弹窗失败")
|
||||
# all_success = False
|
||||
# continue
|
||||
|
||||
# if not self.check_apply_btn():
|
||||
# self.logger.error(f"设备 {device_id} 检查平差处理按钮失败")
|
||||
# all_success = False
|
||||
# self.click_back_button(device_id)
|
||||
# continue
|
||||
|
||||
# # 根据断点名称在get_upload_breakpoint_dict()中获取线路编码
|
||||
# line_code = global_variable.get_upload_breakpoint_dict().get(breakpoint_name)
|
||||
# if not line_code:
|
||||
# self.logger.error(f"设备 {device_id} 未找到断点 {breakpoint_name} 对应的线路编码")
|
||||
# all_success = False
|
||||
# continue
|
||||
|
||||
# # 根据线路编码查找对应的时间
|
||||
# date_str, time_str = self.get_line_end_time(line_code)
|
||||
# if not time_str or not date_str:
|
||||
# self.logger.error(f"设备 {device_id} 未找到线路 {line_code} 对应的时间")
|
||||
# all_success = False
|
||||
# continue
|
||||
|
||||
# # 修改时间
|
||||
# if not self.set_device_time(device_id, time_str, date_str):
|
||||
# self.logger.error(f"设备 {device_id} 设置设备时间失败")
|
||||
# all_success = False
|
||||
# continue
|
||||
|
||||
# # 滑动列表到底部
|
||||
# if not self.scroll_list_to_bottom(device_id):
|
||||
# self.logger.error(f"设备 {device_id} 下滑列表到底部失败")
|
||||
# all_success = False
|
||||
# continue
|
||||
|
||||
# # 2. 点击最后一个spinner
|
||||
# if not self.click_last_spinner_with_retry(device_id):
|
||||
# self.logger.error(f"设备 {device_id} 点击最后一个spinner失败")
|
||||
# all_success = False
|
||||
# continue
|
||||
|
||||
# # 3. 再下滑一次
|
||||
# if not self.scroll_down_once(device_id):
|
||||
# self.logger.warning(f"设备 {device_id} 再次下滑失败,但继续执行")
|
||||
|
||||
# # 平差处理完成后截图
|
||||
# time.sleep(0.2) # 等待平差处理按钮点击后的界面变化
|
||||
# logging.info("断点保存到上传列表成功,开始截图")
|
||||
# # png_time = date_str + " " + time_str
|
||||
# if not self.take_screenshot(breakpoint_name, date_str, time_str):
|
||||
# self.logger.error(f"设备 {device_id} 截图失败")
|
||||
# self.write_screenshot_status(breakpoint_name, success=False)
|
||||
# all_success = False
|
||||
# else:
|
||||
# self.write_screenshot_status(breakpoint_name, success=True)
|
||||
|
||||
# # 点击返回按钮并处理弹窗
|
||||
# if not self.execute_back_navigation_steps(device_id):
|
||||
# self.logger.error(f"设备 {device_id} 处理返回按钮确认失败")
|
||||
# all_success = False
|
||||
|
||||
# 启用WiFi
|
||||
# if not self.enable_wifi(device_id):
|
||||
# self.logger.error(f"设备 {device_id} 启用WiFi失败")
|
||||
# return False
|
||||
|
||||
# # 根据截图结果更新time.txt文件状态
|
||||
# status = "ok" if all_success else "true"
|
||||
# # self.update_upload_info_status(status)
|
||||
# username = global_variable.get_username()
|
||||
# self.update_file_status(username, "running", status)
|
||||
# self.logger.info(f"{username} 截图完成状态为 {status}")
|
||||
|
||||
# self.logger.info(f"设备 {device_id} 截图页面操作执行完成,状态: {status}")
|
||||
# return True
|
||||
except Exception as e:
|
||||
self.logger.error(f"设备 {device_id} 执行截图页面操作时出错: {str(e)}")
|
||||
# 保存错误截图
|
||||
|
||||
Reference in New Issue
Block a user