100 lines
3.4 KiB
Python
100 lines
3.4 KiB
Python
import requests
|
|
import time
|
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
# 从你的 main.py 中导入DeviceAutomation类
|
|
from main import DeviceAutomation
|
|
import globals.apis as apis
|
|
from datetime import datetime
|
|
|
|
## --- 配置区 ---
|
|
API_URL = "http://your-api-server.com/api/tasks" # 替换为真实的接口地址
|
|
MAX_WORKERS = 3 # 最大并发线程数,建议根据网络带宽调整
|
|
DEFAULT_PORT = "6666"
|
|
|
|
def get_remote_tasks():
|
|
"""
|
|
从接口获取数据
|
|
"""
|
|
try:
|
|
# 如果是真实接口,取消下面两行的注释
|
|
# response = requests.get(API_URL, timeout=10)
|
|
# return response.json()
|
|
accounts = apis.get_accounts_from_server("68c0dbfdb7cbcd616e7c5ab5")
|
|
if not accounts:
|
|
print("❌ 未从服务器获取到账户信息,终止流程")
|
|
return {}
|
|
|
|
filtered_accounts = [account for account in accounts if account.get('is_ok') == 1]
|
|
# print("✅ 获取账户信息成功", filtered_accounts)
|
|
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
# 从原始 accounts 数据中筛选有 device_ip 和 device_port 且不为 None 的账户
|
|
device_dict = {}
|
|
|
|
for account in filtered_accounts:
|
|
device_ip = account.get('device_ip')
|
|
device_port = account.get('device_port')
|
|
|
|
# 检查 device_ip 和 device_port 是否都存在且不为 None
|
|
if device_ip and device_port:
|
|
# 拼接为 ip:port 格式
|
|
key = f"{device_ip}:{device_port}"
|
|
# 添加当前时间
|
|
device_dict[key] = current_time
|
|
|
|
# 结果
|
|
print(device_dict)
|
|
return device_dict
|
|
# # 模拟数据供测试
|
|
# return {
|
|
# "192.168.1.45:5555": "2026-02-03 10:20:00",
|
|
# "192.168.1.100:5556": "2026-02-03 10:20:20",
|
|
# "192.168.31.12:5557": "2026-02-03 10:21:00"
|
|
# }
|
|
except Exception as e:
|
|
print(f"❌ 获取接口任务失败: {e}")
|
|
return {}
|
|
|
|
def run_task(address, target_time):
|
|
"""
|
|
单个线程的任务包装器
|
|
"""
|
|
# 格式化账号/设备名:确保带上端口号
|
|
device_address = address
|
|
|
|
print(f"🕒 [等待/启动] 设备: {device_address} | 预定时间: {target_time}")
|
|
|
|
try:
|
|
# 创建DeviceAutomation实例并执行上传逻辑
|
|
automation = DeviceAutomation(device_address)
|
|
result = automation.handle_app_state()
|
|
return f"✅ {device_address} 完成: {result}"
|
|
except Exception as e:
|
|
return f"❌ {device_address} 报错: {str(e)}"
|
|
|
|
def monitor_center():
|
|
"""调度中心"""
|
|
tasks_data = get_remote_tasks()
|
|
|
|
if not tasks_data:
|
|
print("📭 接口未返回任何任务,程序退出。")
|
|
return
|
|
|
|
print(f"🗂️ 发现 {len(tasks_data)} 个待处理账号,开始建立线程池...")
|
|
|
|
# 使用线程池并发执行
|
|
with ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
|
|
# 提交所有任务
|
|
future_to_device = {
|
|
executor.submit(run_task, acc, t): acc
|
|
for acc, t in tasks_data.items()
|
|
}
|
|
|
|
# 实时打印完成情况
|
|
for future in as_completed(future_to_device):
|
|
print(future.result())
|
|
|
|
if __name__ == "__main__":
|
|
print("🚀 自动化调度程序启动...")
|
|
monitor_center()
|
|
print("🏁 所有并发任务处理序列结束。") |