Files

161 lines
6.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""v1.0.63 回归:离线演示模式(测试数据外置 + 模拟秤循环 + 假识别)
现场诉求:「把现场设置离线模式,并且添加测试数据我先测试一下界面」。
A. 离线待办数据从 data/mock_rows.json 读(换数据不用重打包)
B. 文件缺失/写坏 → 回退内置 4 条,界面照样起
C. 模拟秤自动循环:空 → 放货 → 取走
D. 假识别:mock 模式按名单轮着认;真实现场永远不走这条路
E. 全链路:拉待办 → 提交 → 该行从待办消失
"""
import io
import json
import os
import sys
import tempfile
import time
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 core.flow import FlowEngine # noqa: E402
from core.api import client as C # noqa: E402
ok = fail = 0
def check(name, cond, extra=''):
global ok, fail
if cond:
ok += 1
print(' ✔ %s' % name)
else:
fail += 1
print(' ✘ %s %s' % (name, extra))
print('A 离线待办数据(data/mock_rows.json)')
rows = C._mock_rows_file('picking')
check('读到 40 行', rows and len(rows) == 40, len(rows or []))
custs = sorted({r['customer_name'] for r in rows})
check('4 个客户', custs == ['李府家宴', '王记饭店', '赵家厨房', '鲜果铺'], custs)
check('每行都有 id / 品名 / 数量',
all(r.get('id') and r.get('item_name')
and float(r.get('required_qty') or 0) > 0 for r in rows))
check('带标准名(下单名 ≠ 标准名的行)',
any(r.get('standard_name') and r['standard_name'] != r['item_name']
for r in rows))
check('有按件计量的行(测免称重)',
any(str(r.get('unit')) == '件' for r in rows),
[r['item_name'] for r in rows if r.get('unit') == '件'])
ins = C._mock_rows_file('inspection')
check('入库段 6 行', ins and len(ins) == 6, len(ins or []))
print('B 文件缺失/写坏要能回退内置数据')
old = C.MOCK_ROWS_FILE
try:
C.MOCK_ROWS_FILE = os.path.join(tempfile.gettempdir(), 'no_such_rows.json')
check('文件不存在 → None(调用方回退内置)',
C._mock_rows_file('picking') is None)
bad = os.path.join(tempfile.gettempdir(), '_bad_rows.json')
with io.open(bad, 'w', encoding='utf-8') as f:
f.write('{"picking": [')
C.MOCK_ROWS_FILE = bad
check('JSON 写坏 → None,不抛异常', C._mock_rows_file('picking') is None)
with io.open(bad, 'w', encoding='utf-8') as f:
json.dump({'picking': [
{'item_name': '莲藕', 'required_qty': 3},
{'unit': '斤'}, # 没品名:跳过
'not-a-dict', # 不是对象:跳过
]}, f, ensure_ascii=False)
got = C._mock_rows_file('picking')
check('坏行跳过、好行补默认值',
len(got) == 1 and got[0]['id'].startswith('mock-')
and got[0]['unit'] == '斤' and got[0]['spec'] == '散装', got)
finally:
C.MOCK_ROWS_FILE = old
print('C 模拟秤自动循环(空 → 放货 → 取走)')
CFG = {
'flow': {'min_scale_kg': 0.05, 'submit_on_remove': True,
'demo_recog': True},
'scale': {'simulate': True, 'sim_loop_kg': 3.0, 'sim_loop_hold_s': 2.5,
'sim_loop_keep_s': 7.0, 'sim_loop_idle_s': 3.0},
'camera': {'simulate': True},
'vision': {'min_confidence': 60},
'server': {'mock': True},
}
eng = FlowEngine(CFG, lambda n, p=None: None)
eng._running = False
now = time.monotonic()
eng.scale._sim_target = None
eng.scale._sim_auto(now - 1.0) # 周期第 1 秒:还没放货
check('开头空秤(不放货)', eng.scale._sim_target is None, eng.scale._sim_target)
eng.scale._sim_auto(now - 5.0) # 第 5 秒:货在秤上
check('放货段 = 3.0 kg', eng.scale._sim_target == 3.0, eng.scale._sim_target)
eng.scale._sim_auto(now - 11.0) # 第 11 秒:已取走
check('取走段归 0', eng.scale._sim_target is None, eng.scale._sim_target)
check('周期总长 = hold+keep+idle',
abs((2.5 + 7.0 + 3.0) - 12.5) < 1e-9)
CFG['scale']['sim_loop_kg'] = 0 # 关掉自动循环
eng.scale._sim_target = 1.23
eng.scale._sim_auto(now - 5.0)
check('sim_loop_kg=0 不干预(外部 sim_put 仍可用)',
eng.scale._sim_target == 1.23, eng.scale._sim_target)
CFG['scale']['sim_loop_kg'] = 3.0
print('D 假识别只在离线模式生效')
R = [
{'id': 'd1', 'item_name': '上海青', 'unit': '斤', 'required_qty': 6},
{'id': 'd2', 'item_name': '莲藕', 'unit': '斤', 'required_qty': 20},
{'id': 'd3', 'item_name': '金针菇', 'unit': '件', 'required_qty': 3},
]
eng.rows = R
eng.scope_who = None
eng._frozen_kg = 3.0 # 3kg = 6 斤 → 应命中 6 斤那行
check('按秤上重量挑应拣量最接近的一行',
eng._demo_recognize(None) == '上海青', eng._demo_recognize(None))
eng._frozen_kg = 10.0 # 20 斤 → 莲藕
check('换重量就换命中行', eng._demo_recognize(None) == '莲藕',
eng._demo_recognize(None))
eng._frozen_kg = 0 # 没重量:按名单顺序轮
eng._demo_i = 0
got = [eng._demo_recognize(None) for _ in range(4)]
check('没重量时轮着给(不重复同一件)', len(set(got[:3])) == 3, got)
check('名单为空 → None(走人工)',
(setattr(eng, 'rows', []), eng._demo_recognize(None))[1] is None)
eng.rows = R
eng.api.mock = False
check('真实现场(mock=False)永不假识别',
eng._demo_recognize(None) is None)
eng.api.mock = True
eng.cfg['flow']['demo_recog'] = False
check('flow.demo_recog=False 可关掉假识别',
eng._demo_recognize(None) is None)
eng.cfg['flow']['demo_recog'] = True
print('E 全链路:拉测试待办 → 提交 → 该行消失')
api = C.ApiClient({'server': {'mock': True}, 'account': {'username': 'demo'}})
api.login()
pend = api.pending('picking')
check('登录后拉到 40 行待办', len(pend['rows']) == 40, len(pend['rows']))
first = pend['rows'][0]
api.commit_picking(first, 6.0, 3.0, 'op-demo-1', False)
pend2 = api.pending('picking')
check('提交后剩 39 行', len(pend2['rows']) == 39, len(pend2['rows']))
check('提交掉的那行不在了',
first['id'] not in {r['id'] for r in pend2['rows']})
print('')
print('通过 %d / 失败 %d' % (ok, fail))
sys.exit(1 if fail else 0)