补货时把原货抬起来调整位置会被当成取走、按短重量入账。 空秤窗口 0.3s→3s、残重 3s→10s;期间货放回秤上即取消, 状态卡提示行显示到期时间。分拣仍必须人工确认。
332 lines
11 KiB
Python
332 lines
11 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""分拣回归:拿走货提交 + 分拣人工确认成功后打印标签
|
||
|
||
现场逻辑(与操作员确认过):
|
||
- 分拣 AI 命中后先进入待确认;只有人工点击「确认分拣」才提交并在成功后打印。
|
||
- 分拣选中、改判、重量变化、拿走货都不提前提交或打印;重量变化只更新数量。
|
||
- 入库保留原有拿走货提交逻辑。
|
||
- 留档照片用识别时抓拍(拿走后拍必是空画面);重量复核跳过。
|
||
"""
|
||
import os
|
||
import sys
|
||
import time
|
||
import types
|
||
|
||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||
os.chdir(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||
|
||
from core.flow import FlowEngine
|
||
|
||
ok = fail = 0
|
||
|
||
|
||
def check(name, cond, extra=''):
|
||
global ok, fail
|
||
if cond:
|
||
ok += 1
|
||
print(' ✓', name)
|
||
else:
|
||
fail += 1
|
||
print(' ✗', name, extra)
|
||
|
||
|
||
class Fake(FlowEngine):
|
||
"""只搭被测路径需要的属性(不连设备、不起线程)"""
|
||
|
||
def __init__(self, station='inspection', flow_cfg=None):
|
||
self.cfg = {'flow': dict(flow_cfg or {}), 'vision': {}}
|
||
self.station = station
|
||
self.state = 'IDLE'
|
||
self.rows = []
|
||
self.server_config = {}
|
||
self._focus = None
|
||
self._frozen_kg = None
|
||
self._frozen_row = None
|
||
self._cycle_recog_path = None
|
||
self._cycle_conf = None
|
||
self._cycle_detected = ''
|
||
self._aborted = False
|
||
self._running = True
|
||
self._count_mode = False
|
||
self._count_qty = 0.0
|
||
self._last_ok = None
|
||
self._printed_rid = None
|
||
self._pre_pick_rid = None
|
||
self._remove_submit = False
|
||
self._confirm_seen_present = False
|
||
self._confirm_absent_at = 0.0
|
||
self.logs = []
|
||
self.prints = []
|
||
self.focus_calls = []
|
||
self.submit_calls = []
|
||
self.aborts = []
|
||
self.confirms = []
|
||
|
||
# -- 引擎钩子(记录而不是真干)
|
||
def emit(self, ev, payload=None):
|
||
if ev == 'log':
|
||
self.logs.append(str(payload))
|
||
|
||
def _print_async(self, row, business):
|
||
self.prints.append((row.get('id'), round(float(business), 3)))
|
||
|
||
def _set_state(self, s):
|
||
self.state = s
|
||
|
||
def _capture_worker(self, weight):
|
||
# _begin_cycle launches this in a daemon thread; device I/O isn't relevant here.
|
||
return
|
||
|
||
def _focus_payload(self):
|
||
return dict(self._focus or {})
|
||
|
||
def _to_focus(self, row, business, **kw):
|
||
self.focus_calls.append((row.get('id') if row else None,
|
||
float(business or 0.0)))
|
||
FlowEngine._to_focus(self, row, business, **kw)
|
||
|
||
def _start_submit(self, row, business, source):
|
||
self.submit_calls.append((row.get('id'), float(business), source))
|
||
|
||
def _abort_cycle(self, msg):
|
||
self.aborts.append(msg)
|
||
self._auto_round = False
|
||
self._plan = []
|
||
self.emit('log', msg)
|
||
FlowEngine._abort_cycle(self, msg)
|
||
|
||
def confirm_focus(self, qty=None, auto_remove=False):
|
||
self.confirms.append(auto_remove)
|
||
return FlowEngine.confirm_focus(self, qty, auto_remove=auto_remove)
|
||
|
||
def _clear_focus(self):
|
||
self._focus = None
|
||
|
||
|
||
def mrow(rid='r1', name='青笋', unit='斤', qty=10.0):
|
||
return {'id': rid, 'item_name': name, 'unit': unit,
|
||
'prepare_qty': qty, 'required_qty': qty}
|
||
|
||
|
||
print('一、submit_on_remove 配置开关')
|
||
e = Fake()
|
||
check('默认开(不写配置也是拿走即提交)', e.submit_on_remove() is True)
|
||
e = Fake(flow_cfg={'submit_on_remove': False})
|
||
check('显式关掉生效', e.submit_on_remove() is False)
|
||
|
||
print('二、_confirm_tick:拿走即提交(v1.0.69 口径:空秤稳 3 秒才提交,'
|
||
'抬货调整/补货放回秤上即取消)')
|
||
e = Fake()
|
||
e.state = 'CONFIRM'
|
||
e._focus = {'mode': 'weigh', 'row_id': 'r1'}
|
||
e._frozen_kg = 9.0
|
||
e._frozen_row = mrow()
|
||
e.rows = [mrow()]
|
||
t0 = time.monotonic()
|
||
e._confirm_tick(t0, {'present': True, 'weight': 9.0})
|
||
check('货在秤上:不提交,记见过货', e.confirms == []
|
||
and e._confirm_seen_present is True)
|
||
e._confirm_tick(t0 + 0.1, {'present': True, 'weight': 8.8})
|
||
check('读数掉一点但还在秤上:不提交', e.confirms == [])
|
||
e._confirm_tick(t0 + 0.5, {'present': False, 'weight': 0.0})
|
||
check('归零瞬间:先不提交', e.confirms == [])
|
||
e._confirm_tick(t0 + 3.4, {'present': False, 'weight': 0.0})
|
||
check('空秤未满 3 秒:不提交', e.confirms == [])
|
||
e._confirm_tick(t0 + 3.6, {'present': False, 'weight': 0.0})
|
||
check('空秤满 3 秒 → confirm_focus(auto_remove=True)',
|
||
e.confirms == [True])
|
||
|
||
# 现场:补货时把原货抬起来调整位置,再放回去 —— 不能算取走
|
||
e = Fake()
|
||
e.state = 'CONFIRM'
|
||
e._focus = {'mode': 'weigh', 'row_id': 'r1'}
|
||
e._frozen_kg = 9.0
|
||
e._frozen_row = mrow()
|
||
t0 = time.monotonic()
|
||
e._confirm_tick(t0, {'present': True, 'weight': 9.0})
|
||
e._confirm_tick(t0 + 0.5, {'present': False, 'weight': 0.0})
|
||
e._confirm_tick(t0 + 2.0, {'present': True, 'weight': 9.4})
|
||
check('抬起 1.5 秒又放回:不提交', e.confirms == [])
|
||
check('放回后取货计时清零', not e._confirm_gone_at,
|
||
e._confirm_gone_at)
|
||
|
||
e = Fake()
|
||
e.state = 'CONFIRM'
|
||
e._focus = {'mode': 'weigh'}
|
||
e._frozen_kg = 9.0
|
||
e._frozen_row = mrow()
|
||
e._confirm_tick(time.monotonic() - 3.0, {'present': False, 'weight': 0.0})
|
||
e._confirm_tick(time.monotonic(), {'present': False, 'weight': 0.0})
|
||
check('进选中态就没见过货且空秤 2.5 秒 → 作废本轮', len(e.aborts) == 1)
|
||
|
||
e = Fake()
|
||
e.state = 'CONFIRM'
|
||
e._focus = {'mode': 'count'}
|
||
e._frozen_kg = 0.0
|
||
e._confirm_tick(time.monotonic(), {'present': False, 'weight': 0.0})
|
||
check('按件计量行:不走拿走即提交', e.confirms == [] and e.aborts == [])
|
||
|
||
e = Fake(flow_cfg={'submit_on_remove': False})
|
||
e.state = 'CONFIRM'
|
||
e._focus = {'mode': 'weigh'}
|
||
e._frozen_kg = 9.0
|
||
e._frozen_row = mrow()
|
||
e._confirm_tick(time.monotonic(), {'present': False, 'weight': 0.0})
|
||
check('开关关掉:CONFIRM 态不自动提交', e.confirms == [])
|
||
|
||
print('三、分拣选择和重量变化不触发打印')
|
||
e = Fake(station='picking')
|
||
e._aborted = False
|
||
e._running = True
|
||
e._to_focus(mrow('r1'), 9.0, mode='weigh', auto=True)
|
||
check('分拣 AI 选中只进入待确认,不打印',
|
||
e.prints == [] and e.state == 'CONFIRM')
|
||
check('进入确认态重置在秤标记', e._confirm_seen_present is False)
|
||
|
||
e = Fake(station='picking')
|
||
e.state = 'CONFIRM'
|
||
e._focus = {'mode': 'weigh', 'row_id': 'r1', 'business': 18.0}
|
||
e._frozen_kg = 9.0
|
||
e._confirm_live_kg = 9.0
|
||
e._confirm_settled_kg = 9.0
|
||
e._confirm_move_at = time.monotonic() - 2.0
|
||
e._scale_says_stable = lambda delta: True
|
||
e.rows = [mrow('r1')]
|
||
t0 = time.monotonic()
|
||
e._confirm_tick(t0, {'present': True, 'weight': 9.2})
|
||
e._confirm_tick(t0 + 1.0, {'present': True, 'weight': 9.2})
|
||
check('稳定增重更新待确认数量到 18.4 斤、不打印',
|
||
abs(e._focus['business'] - 18.4) < 0.001 and e.prints == [])
|
||
|
||
e = Fake(station='inspection')
|
||
e._to_focus(mrow('r1'), 9.0, mode='weigh', auto=True)
|
||
check('入库选中不打印', e.prints == [])
|
||
|
||
print('四、改判只更新待确认行,不重打')
|
||
e = Fake(station='picking')
|
||
e.state = 'CONFIRM'
|
||
e._focus = {'mode': 'weigh', 'row_id': 'r1', 'business': 18.0,
|
||
'auto': True, 'candidates': ['r1']}
|
||
e._frozen_kg = 9.0
|
||
e._frozen_row = mrow('r1')
|
||
e.rows = [mrow('r1', '青笋'), mrow('r2', '芹菜', qty=5.0)]
|
||
okk, msg = e.pick_focus('r2')
|
||
check('改判成功', okk is True, msg)
|
||
check('改判只更新目标行且不打印', e._focus['row_id'] == 'r2'
|
||
and e.prints == [])
|
||
|
||
print('五、confirm_focus 拿走自动提交的来源与标记')
|
||
e = Fake(station='inspection')
|
||
e.state = 'CONFIRM'
|
||
e._focus = {'mode': 'weigh', 'row_id': 'r1', 'business': 18.0, 'auto': True}
|
||
e._frozen_kg = 9.0
|
||
e.rows = [mrow()]
|
||
e._clear_focus = lambda: None
|
||
okk, msg = e.confirm_focus(auto_remove=True)
|
||
check('提交发出,来源带「拿走自动提交」',
|
||
okk is True and e.submit_calls
|
||
and '拿走自动提交' in e.submit_calls[0][2], (e.submit_calls, msg))
|
||
check('复核特例标记 _remove_submit 置位', e._remove_submit is True)
|
||
|
||
print('六、_snapshot_ok:拿走即提交的复核特例')
|
||
e = Fake()
|
||
e._remove_submit = True
|
||
e._count_mode = False
|
||
e.rows = [mrow()]
|
||
check('货已离秤也放行(跳过重量复核)', e._snapshot_ok(mrow()) is True)
|
||
e.rows = [mrow('r9')]
|
||
check('目标行已不在待办 → 拦下', e._snapshot_ok(mrow()) is False)
|
||
e2 = Fake()
|
||
e2._remove_submit = False
|
||
e2._count_mode = False
|
||
|
||
|
||
class NoScale:
|
||
def state(self, cfg):
|
||
raise RuntimeError('不应走到秤复核')
|
||
|
||
|
||
e2.scale = NoScale()
|
||
try:
|
||
e2._snapshot_ok(mrow())
|
||
check('正常提交仍走重量复核(未误跳过)', False)
|
||
except RuntimeError:
|
||
check('正常提交仍走重量复核(未误跳过)', True)
|
||
|
||
print('七、空秤预选(preselect)')
|
||
e = Fake()
|
||
e.rows = [mrow()]
|
||
check('待机态可预选', e.preselect('r1') is True
|
||
and e._pre_pick_rid == 'r1')
|
||
check('预选日志给操作员看(不带 dev 前缀)',
|
||
any('已预选' in s for s in e.logs))
|
||
e.state = 'CONFIRM'
|
||
check('非待机态不可预选', e.preselect('r1') is False)
|
||
|
||
print('八、_decide_auto:AI 命中覆盖手工预选')
|
||
e = Fake()
|
||
e.rows = [mrow('r1'), mrow('r2', '芹菜', qty=5.0)]
|
||
e._pre_pick_rid = 'r2'
|
||
e.state = 'IDLE'
|
||
e._decide_auto(mrow('r1'), 10.0, 'AI')
|
||
check('预选被消费', e._pre_pick_rid is None)
|
||
check('命中行与预选不同 → 留覆盖日志',
|
||
any('覆盖手工预选' in s for s in e.logs))
|
||
check('拿走即提交:进选中态而不是直接提交',
|
||
len(e.focus_calls) == 1 and e.submit_calls == [])
|
||
|
||
e = Fake()
|
||
e.rows = [mrow('r1')]
|
||
e._pre_pick_rid = 'r1'
|
||
e._decide_auto(mrow('r1'), 10.0, 'AI')
|
||
check('命中行=预选行:无覆盖日志', not any('覆盖手工预选' in s
|
||
for s in e.logs)
|
||
and e._pre_pick_rid is None)
|
||
|
||
e = Fake()
|
||
e.rows = [mrow('r1')]
|
||
e._pre_pick_rid = 'r1'
|
||
e._to_manual(None, [])
|
||
check('识别没命中(转人工页)→ 预选作废', e._pre_pick_rid is None)
|
||
|
||
e = Fake(station='picking', flow_cfg={'confirm_before_submit': False})
|
||
e.rows = [mrow('r1')]
|
||
e._decide_auto(mrow('r1'), 10.0, 'AI')
|
||
check('分拣不受自动提交配置影响:必须进入人工确认',
|
||
len(e.submit_calls) == 0 and len(e.focus_calls) == 1)
|
||
|
||
print('九、_frozen_shot:识别抓拍当留档')
|
||
import tempfile
|
||
fd, p = tempfile.mkstemp(suffix='.jpg')
|
||
os.write(fd, b'\xff\xd8fakejpeg')
|
||
os.close(fd)
|
||
e = Fake()
|
||
e._cycle_recog_path = p
|
||
shot = e._frozen_shot()
|
||
check('识别抓拍存在 → 合格留档 + sha256',
|
||
shot['qualified'] is True and len(shot['sha256']) == 64)
|
||
e._cycle_recog_path = 'X:/no/such.jpg'
|
||
shot = e._frozen_shot()
|
||
check('抓拍缺失 → 不合格(needs_photo 行不会拿空帧充数)',
|
||
shot['qualified'] is False)
|
||
os.remove(p)
|
||
|
||
print('十、周期复位')
|
||
e = Fake()
|
||
e._printed_rid = 'r1'
|
||
e._remove_submit = True
|
||
e._pre_pick_rid = 'r2'
|
||
e._begin_cycle(9.0)
|
||
check('_begin_cycle 重置拿走提交标记', e._remove_submit is False)
|
||
e = Fake()
|
||
e._printed_rid = 'r1'
|
||
e._pre_pick_rid = 'r2'
|
||
e._remove_submit = True
|
||
e._abort_cycle('作废')
|
||
check('_abort_cycle 清三个周期标记',
|
||
e._printed_rid is None and e._pre_pick_rid is None
|
||
and e._remove_submit is False)
|
||
|
||
print('\n%d 项通过,%d 项失败' % (ok, fail))
|
||
sys.exit(1 if fail else 0)
|