160 lines
4.8 KiB
Python
160 lines
4.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""离线演示模式实测:真引擎 + 假秤 + 假相机 + 假识别。
|
|
|
|
用现场的 config(已切 mock/simulate),只把窗口改成可截图的普通窗口。
|
|
按状态等点截图,而不是按秒数瞎截:
|
|
1 待机(40 行待办、4 页)
|
|
2 等确认(跳卡高亮 + 左下「确认」点亮 + 状态行结论)
|
|
3 手动点「确认」后(立刻提交、台账落一条)
|
|
4 下一轮自动提交(秤归零)
|
|
5 按客户筛选(4 个客户)
|
|
"""
|
|
import io
|
|
import json
|
|
import os
|
|
import sys
|
|
import time
|
|
import tkinter as tk
|
|
|
|
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
|
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
ROOT = os.path.dirname(HERE)
|
|
sys.path.insert(0, ROOT)
|
|
os.chdir(ROOT)
|
|
|
|
from PIL import ImageGrab # noqa: E402
|
|
from ui import app as A # noqa: E402
|
|
|
|
BASE = json.load(open(os.path.join(ROOT, 'data', '_diag', 'demo_config.json'),
|
|
encoding='utf-8'))
|
|
BASE.setdefault('ui', {})
|
|
BASE['ui'].update({'mode': 'window', 'fit_screen': False,
|
|
'borderless': False, 'todo_thumb': False,
|
|
'width': 1440, 'height': 900})
|
|
A.load_config = lambda path='config.json', _c=BASE: _c
|
|
|
|
OUT = os.path.join(ROOT, 'data', '_diag', '_offline_ui')
|
|
os.makedirs(OUT, exist_ok=True)
|
|
|
|
root = tk.Tk()
|
|
app = A.App(root)
|
|
root.geometry('1440x900+0+0')
|
|
app._login_user = '演示账号'
|
|
app._login_pwd = '123456'
|
|
app.engine.login() # mock 登录会把 login_ok 投进队列,由 _poll 处理
|
|
# 不能自己再调一次 _on_login_ok:那样 engine.start() 会被调两遍,
|
|
# 两个 tick 线程一起跑,同一件货会提交两次(第一版脚本就是这么踩的)。
|
|
t0 = time.time()
|
|
while time.time() - t0 < 8:
|
|
root.update()
|
|
if app.ready:
|
|
break
|
|
time.sleep(0.02)
|
|
print('ready=%s station=%s tick线程=%d' % (
|
|
app.ready, app.engine.station,
|
|
len([t for t in __import__('threading').enumerate()
|
|
if 'tick_loop' in str(getattr(t, '_target', ''))])))
|
|
|
|
|
|
def snap(name):
|
|
# 状态事件要等 _poll(120ms 一次)从队列里取出来才画到界面上,
|
|
# 所以这里先空转一会儿,否则截到的是上一个状态的画面。
|
|
t0 = time.time()
|
|
while time.time() - t0 < 0.6:
|
|
root.update()
|
|
time.sleep(0.02)
|
|
root.update_idletasks()
|
|
time.sleep(0.2)
|
|
x, y = root.winfo_rootx(), root.winfo_rooty()
|
|
w, h = root.winfo_width(), root.winfo_height()
|
|
ImageGrab.grab(bbox=(x, y, x + w, y + h)).save(os.path.join(OUT, name))
|
|
print(' saved', name, '| 界面状态行:',
|
|
app.lbl_hint.cget('text') if hasattr(app, 'lbl_hint') else '')
|
|
|
|
|
|
def wait_state(want, timeout=25.0):
|
|
"""跑到指定状态(或超时),返回是否等到"""
|
|
t0 = time.time()
|
|
while time.time() - t0 < timeout:
|
|
root.update()
|
|
if getattr(app.engine, 'state', '') == want:
|
|
return True
|
|
time.sleep(0.03)
|
|
return False
|
|
|
|
|
|
def info():
|
|
return (app.engine.state,
|
|
float(getattr(app.engine.scale, 'weight', 0) or 0),
|
|
len(getattr(app, '_all_rows', []) or []))
|
|
|
|
|
|
T0 = time.time()
|
|
# 先等第一轮走完,回到空秤待机,界面稳定后截「待机」
|
|
wait_state('CONFIRM', 15)
|
|
snap('1_等确认_确认按钮点亮.png')
|
|
print(' ', info())
|
|
|
|
# 手动点左下「确认」——不靠自动提交,验证按钮真的能记账
|
|
print('点「确认」按钮,btn_confirm 文字=', end='')
|
|
try:
|
|
print(app.btn_confirm.cget('text'))
|
|
except Exception as e:
|
|
print('?', e)
|
|
app._big_confirm()
|
|
t1 = time.time()
|
|
while time.time() - t1 < 10:
|
|
root.update()
|
|
if app.engine.state in ('DONE', 'IDLE'):
|
|
break
|
|
time.sleep(0.03)
|
|
snap('2_手动确认后_已提交.png')
|
|
print(' ', info())
|
|
|
|
# 等下一轮,这次不点按钮,让秤归零自动提交
|
|
wait_state('WEIGHING', 20)
|
|
ok = wait_state('CONFIRM', 25)
|
|
snap('3_第二轮_等确认.png')
|
|
print(' ', info())
|
|
app.engine.scale.sim_clear() # 模拟「把货拿走」→ 归零
|
|
t2 = time.time()
|
|
while time.time() - t2 < 12:
|
|
root.update()
|
|
if app.engine.state in ('DONE', 'IDLE'):
|
|
break
|
|
time.sleep(0.03)
|
|
snap('4_拿走货_自动提交.png')
|
|
print(' ', info())
|
|
|
|
# 按客户筛选:4 个客户
|
|
app._set_group_mode('customer')
|
|
for _ in range(10):
|
|
root.update()
|
|
time.sleep(0.03)
|
|
app._toggle_group_dd()
|
|
for _ in range(8):
|
|
root.update()
|
|
time.sleep(0.03)
|
|
snap('5_按客户筛选.png')
|
|
try:
|
|
app._close_group_dd()
|
|
except Exception:
|
|
pass
|
|
|
|
p = BASE.get('record_file') or 'data/records_demo.csv'
|
|
if os.path.isfile(p):
|
|
with io.open(p, encoding='utf-8') as f:
|
|
lines = [ln for ln in f if ln.strip()]
|
|
print('台账 %d 行(含表头):' % len(lines))
|
|
for ln in lines[-3:]:
|
|
print(' ', ln.strip()[:150])
|
|
else:
|
|
print('台账没生成')
|
|
|
|
try:
|
|
app.engine.stop()
|
|
except Exception:
|
|
pass
|
|
root.destroy()
|
|
print('done')
|