修改截图保存文件夹
This commit is contained in:
63
scheduler.py
63
scheduler.py
@@ -73,7 +73,7 @@ def parse_time_config():
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
# 匹配:用户名 时间 true (仅获取待处理任务)
|
||||
match = re.search(r'(\w+)\s+(\d{1,2}:\d{2}:\d{2})\s+true$', line)
|
||||
match = re.search(r'(\w+)\s+(\d{4}-\d{1,2}-\d{1,2}\s+\d{1,2}:\d{2}:\d{2})\s+true$', line)
|
||||
if match:
|
||||
username, scheduled_time = match.group(1), match.group(2)
|
||||
time_map[username] = scheduled_time
|
||||
@@ -81,6 +81,37 @@ def parse_time_config():
|
||||
print(f"❌ 解析 time.txt 失败: {e}")
|
||||
return time_map
|
||||
|
||||
def normalize_datetime(time_str):
|
||||
"""
|
||||
将时间字符串格式化为标准格式:YYYY-MM-DD HH:MM:SS
|
||||
补全单数字的月、日、时
|
||||
例如:2024-1-15 9:52:20 -> 2024-01-15 09:52:20
|
||||
"""
|
||||
try:
|
||||
# 分割日期和时间部分
|
||||
if ' ' in time_str:
|
||||
date_part, time_part = time_str.split(' ', 1)
|
||||
|
||||
# 补全日期部分的单数字
|
||||
date_parts = date_part.split('-')
|
||||
if len(date_parts) == 3:
|
||||
year = date_parts[0]
|
||||
month = date_parts[1].zfill(2) # 月补零
|
||||
day = date_parts[2].zfill(2) # 日补零
|
||||
date_part = f"{year}-{month}-{day}"
|
||||
|
||||
# 补全时间部分的单数字小时
|
||||
time_parts = time_part.split(':')
|
||||
if len(time_parts) >= 1:
|
||||
hour = time_parts[0].zfill(2) # 小时补零
|
||||
time_part = f"{hour}:{':'.join(time_parts[1:])}"
|
||||
|
||||
return f"{date_part} {time_part}"
|
||||
return time_str
|
||||
except Exception as e:
|
||||
print(f"⚠️ 时间格式标准化失败 ({time_str}): {e}")
|
||||
return time_str
|
||||
|
||||
def get_combined_tasks():
|
||||
"""
|
||||
结合接口(is_ok==1)和本地文件(true)筛选任务
|
||||
@@ -96,13 +127,14 @@ def get_combined_tasks():
|
||||
return {}
|
||||
|
||||
task_list = {}
|
||||
today = datetime.now().strftime("%Y-%m-%d")
|
||||
# today = datetime.now().strftime("%Y-%m-%d")
|
||||
|
||||
for account in accounts:
|
||||
if account.get('is_ok') == 1 or account.get('username') == "czyuzongwen":
|
||||
if account.get('is_ok') == 1:
|
||||
user = account.get('username')
|
||||
ip = account.get('device_ip')
|
||||
port = account.get('device_port')
|
||||
project_name = account.get('project_name')
|
||||
|
||||
# 只有在 time.txt 中是 true 的账号才会被加入
|
||||
if user in local_times and ip and port:
|
||||
@@ -110,21 +142,22 @@ def get_combined_tasks():
|
||||
# full_time = f"{today} {local_times[user]}"
|
||||
# 确保时间是两位数格式
|
||||
raw_time = local_times[user]
|
||||
# 将时间格式化为两位数:9:52:20 -> 09:52:20
|
||||
if ':' in raw_time:
|
||||
parts = raw_time.split(':')
|
||||
if len(parts[0]) == 1:
|
||||
raw_time = f"0{raw_time}" # 补齐前导零
|
||||
# # 将时间格式化为两位数:9:52:20 -> 09:52:20
|
||||
# if ':' in raw_time:
|
||||
# parts = raw_time.split(':')
|
||||
# if len(parts[0]) == 1:
|
||||
# raw_time = f"0{raw_time}" # 补齐前导零
|
||||
|
||||
full_time = f"{today} {raw_time}"
|
||||
task_list[address] = {"time": full_time, "user": user}
|
||||
# full_time = f"{today} {raw_time}"
|
||||
full_time = normalize_datetime(raw_time)
|
||||
task_list[address] = {"time": full_time, "user": user, "project_name": project_name}
|
||||
|
||||
return task_list
|
||||
except Exception as e:
|
||||
print(f"❌ 获取任务异常: {e}")
|
||||
return {}
|
||||
|
||||
def run_task(address, target_time, username):
|
||||
def run_task(address, target_time, username, project_name):
|
||||
"""
|
||||
单个执行线程:锁定状态 -> 等待 -> 执行 -> 完成
|
||||
"""
|
||||
@@ -133,7 +166,7 @@ def run_task(address, target_time, username):
|
||||
if not update_file_status(username, "true", "running"):
|
||||
return f"⏭️ {username} 状态已变更,跳过执行。"
|
||||
|
||||
print(f"🚀 [任务锁定] 设备: {address} | 用户: {username} | 计划时间: {target_time}")
|
||||
print(f"🚀 [任务锁定] 设备: {address} | 用户: {username} | 计划时间: {target_time} | 项目: {project_name}")
|
||||
|
||||
try:
|
||||
# 2. 计算并执行等待逻辑
|
||||
@@ -146,7 +179,7 @@ def run_task(address, target_time, username):
|
||||
|
||||
# 3. 调用 main.py 中的自动化逻辑
|
||||
print(f"▶️ [正在执行] {username} 开始自动化操作...")
|
||||
automation = DeviceAutomation(address)
|
||||
automation = DeviceAutomation(address, project_name)
|
||||
result = automation.handle_app_state()
|
||||
|
||||
# 4. 执行完成后,将状态从 running 改为 done
|
||||
@@ -169,9 +202,9 @@ def monitor_center():
|
||||
if tasks:
|
||||
print(f"📡 发现 {len(tasks)} 个符合条件且未跑过的任务,准备启动线程池...")
|
||||
with ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
|
||||
# 提交任务,将 address, time, username 传入
|
||||
# 提交任务,将 address, time, username, project_name 传入
|
||||
future_to_user = {
|
||||
executor.submit(run_task, addr, info['time'], info['user']): info['user']
|
||||
executor.submit(run_task, addr, info['time'], info['user'], info.get('project_name', '')): info['user']
|
||||
for addr, info in tasks.items()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user