feat(production): 持久化动作检查点并完善恢复重试流程

- production_runs 新增动作阶段、尝试次数、版本及人工确认字段
- 提供兼容 PostgreSQL 9.5 的幂等升级脚本和数据库结构检查
- 工序推进、动作完成检查点和审计记录使用同一事务
- 机器人命令单次发送,完成反馈先持久化再清握手
- 已完成动作恢复仅补握手或数据库记录,避免重复执行
- 恢复/重试按钮固定显示在清除报警前,加入明确人工处理选项
- 记录人工决定、操作人及现场核对信息,拒绝过期确认
- 区分普通暂停继续、故障恢复与终止后的视觉锁解除
- 补齐视觉目标跨重启校验、异步失败及迟到回调防护
- 检测结果关联动作和尝试次数,重试清除旧结果缓存
This commit is contained in:
lidg
2026-10-29 21:12:15 +08:00
parent df804963e5
commit 4046471c1a
15 changed files with 1763 additions and 659 deletions
+111
View File
@@ -177,6 +177,117 @@ on production_runs(order_id);
create index if not exists ix_production_runs_workpiece_id
on production_runs(current_workpiece_id);
-- Independent action recovery checkpoints (PostgreSQL 9.5 compatible).
-- Keep NULL phases for historical rows: NULL does not mean NOT_SENT.
do $$
declare
target_table regclass := 'production_runs'::regclass;
col record;
begin
for col in
select *
from (values
('action_token', 'varchar(64)'),
('action_type', 'varchar(32)'),
('action_phase', 'varchar(32)'),
('action_next_step', 'varchar(64)'),
('action_attempt', 'integer not null default 0'),
('action_lock_version', 'integer not null default 0'),
('action_command_value', 'boolean'),
('action_feedback_value', 'boolean'),
('manual_decision', 'varchar(32)'),
('manual_operator', 'varchar(64)'),
('manual_decided_at', 'timestamp'),
('action_updated_at', 'timestamp')
) as columns_to_add(column_name, column_type)
loop
if not exists (
select 1 from pg_attribute
where attrelid = target_table
and attname = col.column_name
and attnum > 0 and not attisdropped
) then
execute 'alter table ' || target_table::text ||
' add column ' || quote_ident(col.column_name) ||
' ' || col.column_type;
end if;
end loop;
end;
$$ language plpgsql;
comment on column production_runs.action_token
is '当前独立动作唯一标识;NULL表示尚无持久化动作检查点';
comment on column production_runs.action_type
is '当前独立动作类型';
comment on column production_runs.action_phase
is '当前动作持久化阶段;NULL表示未知,不可直接认定为未发送';
comment on column production_runs.action_next_step
is '当前动作完成后待提交的下一工序';
comment on column production_runs.action_attempt
is '当前动作尝试次数';
comment on column production_runs.action_lock_version
is '动作检查点乐观锁版本号';
comment on column production_runs.action_command_value
is '最近一次成功读取的动作命令值;NULL表示未知,恢复前必须重新读取PLC';
comment on column production_runs.action_feedback_value
is '最近一次成功读取的动作反馈值;NULL表示未知,恢复前必须重新读取PLC';
comment on column production_runs.manual_decision
is '当前动作最近一次人工确认决定;完整历史记录在production_state_history';
comment on column production_runs.manual_operator
is '当前动作最近一次人工确认操作人';
comment on column production_runs.manual_decided_at
is '当前动作最近一次人工确认时间';
comment on column production_runs.action_updated_at
is '当前动作检查点更新时间,由应用程序写入';
do $$
declare
target_table regclass := 'production_runs'::regclass;
begin
if not exists (
select 1 from pg_class i
join pg_class t on t.relnamespace = i.relnamespace
where t.oid = target_table
and i.relname = 'ux_production_runs_action_token'
) then
create unique index ux_production_runs_action_token
on production_runs(action_token)
where action_token is not null;
end if;
end;
$$ language plpgsql;
do $$
declare
target_table regclass := 'production_runs'::regclass;
rule record;
begin
for rule in
select *
from (values
('ck_production_runs_action_type',
'action_type is null or action_type in (''PHOTO_MOVE'', ''VISION'', ''RACK_PICK'', ''FRONT_LOAD'', ''INSPECT'', ''FRONT_UNLOAD'', ''FLIP'', ''BACK_LOAD'', ''BACK_UNLOAD'', ''RACK_PLACE'')'),
('ck_production_runs_action_phase',
'action_phase is null or action_phase in (''NOT_SENT'', ''SENDING'', ''COMMAND_CONFIRMED'', ''FEEDBACK_RECEIVED'', ''SIGNALS_CLEARED'', ''STEP_COMMITTED'', ''RESULT_WRITING'', ''RESULT_CONFIRMED'', ''MODE_WRITING'', ''MODE_CONFIRMED'')'),
('ck_production_runs_manual_decision',
'manual_decision is null or manual_decision in (''ACTION_COMPLETED'', ''ACTION_NOT_EXECUTED'', ''RETRY_VISION'', ''NORMAL_PICK'')'),
('ck_production_runs_action_attempt', 'action_attempt >= 0'),
('ck_production_runs_action_lock_version', 'action_lock_version >= 0')
) as constraints_to_add(constraint_name, check_expression)
loop
if not exists (
select 1 from pg_constraint
where conrelid = target_table
and conname = rule.constraint_name
) then
execute 'alter table ' || target_table::text ||
' add constraint ' || quote_ident(rule.constraint_name) ||
' check (' || rule.check_expression || ')';
end if;
end loop;
end;
$$ language plpgsql;
-- Production state history
create table if not exists production_state_history (
id bigserial primary key,