CCL_QMS检验
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

585 lines
22 KiB

3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
  1. # -*- coding: utf-8 -*-
  2. import json
  3. import os
  4. import queue
  5. import shutil
  6. import sys
  7. import threading
  8. import time
  9. from datetime import datetime
  10. from pathlib import Path
  11. from tkinter import Tk, Label, Entry, Button, StringVar, END, DISABLED, NORMAL, filedialog, messagebox, simpledialog
  12. from tkinter.scrolledtext import ScrolledText
  13. import requests
  14. def get_runtime_dir():
  15. if getattr(sys, "frozen", False):
  16. return Path(sys.executable).resolve().parent
  17. return Path(__file__).resolve().parent
  18. def _ensure_windows_run_registry(entry_name, exe_path):
  19. import winreg
  20. run_key_path = r"Software\Microsoft\Windows\CurrentVersion\Run"
  21. target_value = "\"%s\"" % exe_path
  22. try:
  23. with winreg.OpenKey(
  24. winreg.HKEY_CURRENT_USER,
  25. run_key_path,
  26. 0,
  27. winreg.KEY_READ | winreg.KEY_SET_VALUE
  28. ) as key:
  29. try:
  30. old_value, _ = winreg.QueryValueEx(key, entry_name)
  31. except FileNotFoundError:
  32. old_value = None
  33. if old_value and str(old_value).strip().strip("\"").lower() == exe_path.lower():
  34. return False, "系统自启动项已存在,无需重复写入。"
  35. winreg.SetValueEx(key, entry_name, 0, winreg.REG_SZ, target_value)
  36. if old_value:
  37. return True, "检测到自启动路径变化,已自动更新。"
  38. return True, "首次启动已写入系统自启动项,下次开机会自动运行。"
  39. except Exception as e:
  40. return False, "写入系统自启动项失败: %s" % e
  41. def _cleanup_legacy_startup_items(legacy_entry_name, app_data):
  42. if app_data:
  43. startup_dir = Path(app_data) / "Microsoft" / "Windows" / "Start Menu" / "Programs" / "Startup"
  44. legacy_script = startup_dir / (legacy_entry_name + ".vbs")
  45. try:
  46. if legacy_script.exists():
  47. legacy_script.unlink()
  48. except Exception:
  49. pass
  50. try:
  51. import winreg
  52. run_key_path = r"Software\Microsoft\Windows\CurrentVersion\Run"
  53. with winreg.OpenKey(
  54. winreg.HKEY_CURRENT_USER,
  55. run_key_path,
  56. 0,
  57. winreg.KEY_SET_VALUE
  58. ) as key:
  59. try:
  60. winreg.DeleteValue(key, legacy_entry_name)
  61. except FileNotFoundError:
  62. pass
  63. except Exception:
  64. pass
  65. def ensure_windows_startup():
  66. if os.name != "nt":
  67. return False, "当前系统非Windows,跳过自启动注册。"
  68. if not getattr(sys, "frozen", False):
  69. return False, "当前为源码运行,跳过自启动注册。"
  70. exe_path = str(Path(sys.executable).resolve())
  71. entry_name = "QMSFileCollector"
  72. app_data = os.getenv("APPDATA", "").strip()
  73. _cleanup_legacy_startup_items("CKPClientFileCollector", app_data)
  74. if app_data:
  75. startup_dir = Path(app_data) / "Microsoft" / "Windows" / "Start Menu" / "Programs" / "Startup"
  76. startup_script = startup_dir / (entry_name + ".vbs")
  77. script_content = (
  78. "Set WshShell = CreateObject(\"WScript.Shell\")\n"
  79. "WshShell.Run Chr(34) & \"%s\" & Chr(34), 0\n"
  80. "Set WshShell = Nothing\n"
  81. ) % exe_path.replace("\"", "\"\"")
  82. try:
  83. startup_dir.mkdir(parents=True, exist_ok=True)
  84. old_content = ""
  85. if startup_script.exists():
  86. old_content = startup_script.read_text(encoding="utf-8")
  87. if old_content == script_content:
  88. return False, "启动文件夹自启动项已存在,无需重复写入。"
  89. startup_script.write_text(script_content, encoding="utf-8")
  90. if old_content:
  91. return True, "检测到启动文件夹自启动路径变化,已自动更新。"
  92. return True, "首次启动已写入启动文件夹自启动项,下次开机会自动运行。"
  93. except Exception as startup_error:
  94. try:
  95. changed, reg_msg = _ensure_windows_run_registry(entry_name, exe_path)
  96. return changed, "写入启动文件夹失败,已回退注册表方式: %s" % reg_msg
  97. except Exception as reg_error:
  98. return False, "写入启动文件夹和注册表均失败: %s; %s" % (startup_error, reg_error)
  99. try:
  100. return _ensure_windows_run_registry(entry_name, exe_path)
  101. except Exception as e:
  102. return False, "未获取到APPDATA且注册表写入失败: %s" % e
  103. class CollectorApp:
  104. def __init__(self, root):
  105. self.root = root
  106. self.root.title("文件采集客户端")
  107. self.root.geometry("1280x720")
  108. self.root.resizable(True, True)
  109. self.config_path = get_runtime_dir() / "collector_config.json"
  110. self.log_queue = queue.Queue()
  111. self.running = False
  112. self.worker_thread = None
  113. self.stop_event = threading.Event()
  114. self.active_rows = []
  115. self.upload_state = {}
  116. self.notice_ts = {}
  117. self.base_url_var = StringVar()
  118. self.poll_seconds_var = StringVar(value="10")
  119. self.row_vars = []
  120. for _ in range(3):
  121. self.row_vars.append({
  122. "site": StringVar(),
  123. "bu_no": StringVar(),
  124. "equipment_no": StringVar(),
  125. "source_path": StringVar(),
  126. "backup_path": StringVar(),
  127. })
  128. self._build_ui()
  129. self._init_windows_startup()
  130. self._load_config()
  131. self.root.after(200, self._flush_logs)
  132. def _init_windows_startup(self):
  133. changed, message = ensure_windows_startup()
  134. if changed:
  135. self.log(message)
  136. return
  137. if getattr(sys, "frozen", False) and ("失败" in message):
  138. self.log(message)
  139. def _build_ui(self):
  140. Label(self.root, text="系统地址").place(x=20, y=20)
  141. Entry(self.root, textvariable=self.base_url_var, width=92).place(x=85, y=20)
  142. Button(self.root, text="弹框设置地址", command=self.popup_set_base_url).place(x=860, y=16)
  143. Button(self.root, text="保存配置", command=self.save_config).place(x=970, y=16)
  144. Label(self.root, text="轮询秒").place(x=20, y=58)
  145. Entry(self.root, textvariable=self.poll_seconds_var, width=10).place(x=85, y=58)
  146. Button(self.root, text="开始同步", command=self.start_collect).place(x=180, y=54)
  147. Button(self.root, text="停止同步", command=self.stop_collect).place(x=265, y=54)
  148. Label(self.root, text="配置说明:最多3行,每行需填写 site + buNo + equipmentNo + 本地目录;备份目录可选(为空则自动使用本地目录_bak)").place(x=360, y=58)
  149. Label(self.root, text="").place(x=20, y=100)
  150. Label(self.root, text="Site").place(x=80, y=100)
  151. Label(self.root, text="BuNo").place(x=220, y=100)
  152. Label(self.root, text="EquipmentNo").place(x=360, y=100)
  153. Label(self.root, text="本地目录").place(x=530, y=100)
  154. Label(self.root, text="备份目录").place(x=890, y=100)
  155. for idx, row in enumerate(self.row_vars):
  156. y = 130 + idx * 40
  157. Label(self.root, text=str(idx + 1)).place(x=26, y=y)
  158. Entry(self.root, textvariable=row["site"], width=16).place(x=80, y=y)
  159. Entry(self.root, textvariable=row["bu_no"], width=16).place(x=220, y=y)
  160. Entry(self.root, textvariable=row["equipment_no"], width=18).place(x=360, y=y)
  161. Entry(self.root, textvariable=row["source_path"], width=40).place(x=530, y=y)
  162. Button(self.root, text="选择目录", command=lambda x=idx: self.choose_source_dir(x)).place(x=815, y=y - 4)
  163. Entry(self.root, textvariable=row["backup_path"], width=30).place(x=890, y=y)
  164. Button(self.root, text="选择目录", command=lambda x=idx: self.choose_backup_dir(x)).place(x=1110, y=y - 4)
  165. self.log_text = ScrolledText(self.root, width=165, height=23)
  166. self.log_text.place(x=20, y=280)
  167. self.log_text.configure(state=DISABLED)
  168. def popup_set_base_url(self):
  169. new_url = simpledialog.askstring(
  170. "设置目标地址",
  171. "请输入xujie-sys地址,例如:http://172.26.58.88:8080",
  172. initialvalue=self.base_url_var.get().strip()
  173. )
  174. if new_url is not None:
  175. self.base_url_var.set(new_url.strip())
  176. self.log("已设置目标地址: %s" % new_url.strip())
  177. def choose_source_dir(self, row_index):
  178. path = filedialog.askdirectory(title="选择第%d行本地目录" % (row_index + 1))
  179. if path:
  180. row = self.row_vars[row_index]
  181. row["source_path"].set(path)
  182. if not row["backup_path"].get().strip():
  183. row["backup_path"].set(self._default_backup_path(path))
  184. def choose_backup_dir(self, row_index):
  185. path = filedialog.askdirectory(title="选择第%d行备份目录" % (row_index + 1))
  186. if path:
  187. self.row_vars[row_index]["backup_path"].set(path)
  188. def _default_backup_path(self, source_path):
  189. source_text = str(source_path).strip()
  190. if not source_text:
  191. return ""
  192. source = Path(source_text)
  193. return str(source.parent / (source.name + "_bak"))
  194. def _load_config(self):
  195. if not self.config_path.exists():
  196. self.log("未找到本地配置,请先录入并保存。")
  197. return
  198. try:
  199. config = json.loads(self.config_path.read_text(encoding="utf-8"))
  200. self.base_url_var.set(config.get("base_url", ""))
  201. self.poll_seconds_var.set(str(config.get("poll_seconds", "10")))
  202. rows = config.get("rows")
  203. # 兼容旧版本单行配置
  204. if not isinstance(rows, list):
  205. rows = [{
  206. "site": config.get("site", ""),
  207. "bu_no": config.get("bu_no", ""),
  208. "equipment_no": config.get("equipment_no", ""),
  209. "source_path": config.get("source_path", ""),
  210. "backup_path": config.get("backup_path", ""),
  211. }]
  212. for idx in range(min(3, len(rows))):
  213. item = rows[idx] if isinstance(rows[idx], dict) else {}
  214. source_path = str(item.get("source_path", "")).strip()
  215. backup_path = str(item.get("backup_path", "")).strip()
  216. if source_path and not backup_path:
  217. backup_path = self._default_backup_path(source_path)
  218. self.row_vars[idx]["site"].set(str(item.get("site", "")).strip())
  219. self.row_vars[idx]["bu_no"].set(str(item.get("bu_no", "")).strip())
  220. self.row_vars[idx]["equipment_no"].set(str(item.get("equipment_no", "")).strip())
  221. self.row_vars[idx]["source_path"].set(source_path)
  222. self.row_vars[idx]["backup_path"].set(backup_path)
  223. self.log("配置加载完成。")
  224. if self._can_auto_start():
  225. self.start_collect(auto_mode=True)
  226. except Exception as e:
  227. self.log("读取配置失败: %s" % e)
  228. def save_config(self):
  229. try:
  230. rows = self._get_valid_rows(require_complete=True, raise_on_empty=False)
  231. except ValueError as e:
  232. messagebox.showwarning("提示", str(e))
  233. return
  234. data = {
  235. "base_url": self.base_url_var.get().strip(),
  236. "poll_seconds": self.poll_seconds_var.get().strip(),
  237. "rows": rows,
  238. }
  239. try:
  240. self.config_path.write_text(json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8")
  241. self.log("配置已保存: %s" % self.config_path)
  242. except Exception as e:
  243. messagebox.showerror("错误", "保存配置失败: %s" % e)
  244. return
  245. if self.running:
  246. self.active_rows = rows
  247. self.log("采集配置已更新,下一轮轮询自动生效。")
  248. elif self._can_auto_start():
  249. self.start_collect(auto_mode=True)
  250. def _can_auto_start(self):
  251. if not self._base_url():
  252. return False
  253. if not self._is_poll_seconds_valid():
  254. return False
  255. try:
  256. rows = self._get_valid_rows(require_complete=True, raise_on_empty=True)
  257. return len(rows) > 0
  258. except Exception:
  259. return False
  260. def _is_poll_seconds_valid(self):
  261. try:
  262. return int(self.poll_seconds_var.get().strip()) > 0
  263. except Exception:
  264. return False
  265. def _base_url(self):
  266. return self.base_url_var.get().strip().rstrip("/")
  267. def _get_valid_rows(self, require_complete, raise_on_empty):
  268. rows = []
  269. for idx, row in enumerate(self.row_vars):
  270. site = row["site"].get().strip()
  271. bu_no = row["bu_no"].get().strip()
  272. equipment_no = row["equipment_no"].get().strip()
  273. source_path = row["source_path"].get().strip()
  274. backup_path = row["backup_path"].get().strip()
  275. filled_count = 0
  276. for value in (site, bu_no, equipment_no, source_path, backup_path):
  277. if value:
  278. filled_count += 1
  279. if filled_count == 0:
  280. continue
  281. required_count = 0
  282. for value in (site, bu_no, equipment_no, source_path):
  283. if value:
  284. required_count += 1
  285. if require_complete and required_count < 4:
  286. raise ValueError("%d行配置未填写完整,请补全site、buNo、equipmentNo、本地目录。" % (idx + 1))
  287. if required_count == 4:
  288. if not backup_path:
  289. backup_path = self._default_backup_path(source_path)
  290. if os.path.normcase(os.path.normpath(source_path)) == os.path.normcase(os.path.normpath(backup_path)):
  291. raise ValueError("%d行配置错误:本地目录和备份目录不能相同。" % (idx + 1))
  292. rows.append({
  293. "row_index": idx + 1,
  294. "site": site,
  295. "bu_no": bu_no,
  296. "equipment_no": equipment_no,
  297. "source_path": source_path,
  298. "backup_path": backup_path,
  299. })
  300. if raise_on_empty and len(rows) == 0:
  301. raise ValueError("请至少填写一行完整采集配置。")
  302. return rows
  303. def start_collect(self, auto_mode=False):
  304. if self.running:
  305. if not auto_mode:
  306. self.log("采集任务已在运行中。")
  307. return
  308. if not self._base_url():
  309. if auto_mode:
  310. self.log("自动启动失败:系统地址为空。")
  311. else:
  312. messagebox.showwarning("提示", "请先设置系统地址")
  313. return
  314. try:
  315. poll_seconds = int(self.poll_seconds_var.get().strip() or "10")
  316. if poll_seconds <= 0:
  317. raise ValueError("轮询秒必须大于0")
  318. except Exception:
  319. if auto_mode:
  320. self.log("自动启动失败:轮询秒无效。")
  321. else:
  322. messagebox.showwarning("提示", "轮询秒必须是大于0的整数")
  323. return
  324. try:
  325. rows = self._get_valid_rows(require_complete=True, raise_on_empty=True)
  326. except ValueError as e:
  327. if auto_mode:
  328. self.log("自动启动失败:%s" % e)
  329. else:
  330. messagebox.showwarning("提示", str(e))
  331. return
  332. self.active_rows = rows
  333. # 每次手动/自动启动都重置一次状态,避免停启后误判“已同步”。
  334. self.upload_state = {}
  335. self.notice_ts = {}
  336. self.stop_event = threading.Event()
  337. self.running = True
  338. self.worker_thread = threading.Thread(target=self._worker_loop, args=(self.stop_event,), daemon=True)
  339. self.worker_thread.start()
  340. if auto_mode:
  341. self.log("配置有效,已自动启动轮询同步。")
  342. else:
  343. self.log("采集任务已启动。")
  344. def stop_collect(self):
  345. if not self.running:
  346. self.log("采集任务未运行。")
  347. return
  348. self.running = False
  349. self.stop_event.set()
  350. self.log("正在停止采集任务...")
  351. def _worker_loop(self, stop_event):
  352. while not stop_event.is_set():
  353. rows = list(self.active_rows)
  354. for row in rows:
  355. if stop_event.is_set():
  356. break
  357. self._sync_one_row(row, stop_event)
  358. try:
  359. poll_seconds = int(self.poll_seconds_var.get().strip() or "10")
  360. except Exception:
  361. poll_seconds = 10
  362. for _ in range(max(1, poll_seconds)):
  363. if stop_event.is_set():
  364. break
  365. time.sleep(1)
  366. if self.stop_event is stop_event:
  367. self.running = False
  368. self.worker_thread = None
  369. self.log("采集任务已停止。")
  370. def _sync_one_row(self, row, stop_event):
  371. row_index = row["row_index"]
  372. source_path = row["source_path"]
  373. path_key = "row%d_missing" % row_index
  374. empty_key = "row%d_empty" % row_index
  375. if not os.path.isdir(source_path):
  376. self._log_with_interval(path_key, "%d行目录不存在: %s" % (row_index, source_path), 60)
  377. return
  378. files = []
  379. for name in os.listdir(source_path):
  380. file_path = Path(source_path) / name
  381. if file_path.is_file():
  382. files.append(file_path)
  383. self._cleanup_row_upload_state(row_index, files)
  384. if not files:
  385. self._log_with_interval(empty_key, "%d行目录为空,等待新文件..." % row_index, 60)
  386. return
  387. files.sort(key=lambda p: p.stat().st_mtime)
  388. for file_path in files:
  389. if stop_event.is_set():
  390. return
  391. signature = self._file_signature(file_path)
  392. if signature is None:
  393. continue
  394. state_key = "%d|%s" % (row_index, str(file_path).lower())
  395. if self.upload_state.get(state_key) == signature:
  396. continue
  397. if self._upload_file(row, file_path):
  398. if self._backup_and_remove_file(row, file_path):
  399. # 文件已从源目录移走,立即清理状态,允许后续同名新文件再次同步。
  400. self.upload_state.pop(state_key, None)
  401. else:
  402. self.upload_state.pop(state_key, None)
  403. def _cleanup_row_upload_state(self, row_index, files):
  404. prefix = "%d|" % row_index
  405. current_keys = set()
  406. for file_path in files:
  407. current_keys.add("%d|%s" % (row_index, str(file_path).lower()))
  408. stale_keys = []
  409. for key in self.upload_state.keys():
  410. if key.startswith(prefix) and key not in current_keys:
  411. stale_keys.append(key)
  412. for key in stale_keys:
  413. self.upload_state.pop(key, None)
  414. def _file_signature(self, file_path):
  415. try:
  416. stat = file_path.stat()
  417. return stat.st_mtime_ns, stat.st_size
  418. except Exception as e:
  419. self.log("读取文件状态失败: %s, 原因: %s" % (file_path, e))
  420. return None
  421. def _get_backup_dir(self, row):
  422. backup_path = str(row.get("backup_path", "")).strip()
  423. if backup_path:
  424. return Path(backup_path)
  425. return Path(self._default_backup_path(row["source_path"]))
  426. def _backup_and_remove_file(self, row, file_path):
  427. source_path = row["source_path"]
  428. row_index = row["row_index"]
  429. backup_dir = self._get_backup_dir(row)
  430. if os.path.normcase(os.path.normpath(str(source_path))) == os.path.normcase(os.path.normpath(str(backup_dir))):
  431. self.log("%d行备份目录不能与本地目录相同: %s" % (row_index, backup_dir))
  432. return False
  433. try:
  434. backup_dir.mkdir(parents=True, exist_ok=True)
  435. except Exception as e:
  436. self.log("%d行创建备份目录失败: %s, 原因: %s" % (row_index, backup_dir, e))
  437. return False
  438. target = backup_dir / file_path.name
  439. if target.exists():
  440. timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
  441. target = backup_dir / ("%s_%s%s" % (file_path.stem, timestamp, file_path.suffix))
  442. try:
  443. shutil.move(str(file_path), str(target))
  444. self.log("%d行源文件已转移至备份: %s -> %s" % (row_index, file_path.name, target))
  445. return True
  446. except Exception as e:
  447. self.log("%d行源文件备份转移失败: %s, 原因: %s" % (row_index, file_path.name, e))
  448. return False
  449. def _upload_file(self, row, file_path):
  450. url = self._base_url() + "/collector/client/upload"
  451. data = {
  452. "site": row["site"],
  453. "buNo": row["bu_no"],
  454. "equipmentNo": row["equipment_no"],
  455. }
  456. try:
  457. with open(file_path, "rb") as fp:
  458. files = {"file": (file_path.name, fp, "application/octet-stream")}
  459. resp = requests.post(url, data=data, files=files, timeout=180)
  460. resp.raise_for_status()
  461. result = resp.json()
  462. if result.get("code") == 0:
  463. response_data = result.get("data") or {}
  464. server_path = response_data.get("savedFullPath", "")
  465. self.log("%d行上传成功: %s -> %s" % (row["row_index"], file_path.name, server_path))
  466. return True
  467. self.log("%d行上传失败: %s, 原因: %s" % (
  468. row["row_index"], file_path.name, result.get("msg")))
  469. return False
  470. except Exception as e:
  471. self.log("%d行上传异常: %s, 原因: %s" % (row["row_index"], file_path.name, e))
  472. return False
  473. def _log_with_interval(self, key, message, seconds):
  474. now = time.time()
  475. last = self.notice_ts.get(key, 0)
  476. if now - last >= seconds:
  477. self.notice_ts[key] = now
  478. self.log(message)
  479. def log(self, message):
  480. self.log_queue.put("[%s] %s" % (datetime.now().strftime("%H:%M:%S"), message))
  481. def _flush_logs(self):
  482. while not self.log_queue.empty():
  483. line = self.log_queue.get()
  484. self.log_text.configure(state=NORMAL)
  485. self.log_text.insert(END, line + "\n")
  486. self.log_text.see(END)
  487. self.log_text.configure(state=DISABLED)
  488. self.root.after(200, self._flush_logs)
  489. if __name__ == "__main__":
  490. app_root = Tk()
  491. app = CollectorApp(app_root)
  492. app_root.mainloop()