feat(production): 持久化动作检查点并完善恢复重试流程
- production_runs 新增动作阶段、尝试次数、版本及人工确认字段 - 提供兼容 PostgreSQL 9.5 的幂等升级脚本和数据库结构检查 - 工序推进、动作完成检查点和审计记录使用同一事务 - 机器人命令单次发送,完成反馈先持久化再清握手 - 已完成动作恢复仅补握手或数据库记录,避免重复执行 - 恢复/重试按钮固定显示在清除报警前,加入明确人工处理选项 - 记录人工决定、操作人及现场核对信息,拒绝过期确认 - 区分普通暂停继续、故障恢复与终止后的视觉锁解除 - 补齐视觉目标跨重启校验、异步失败及迟到回调防护 - 检测结果关联动作和尝试次数,重试清除旧结果缓存
This commit is contained in:
@@ -5,6 +5,8 @@
|
||||
#include <QSqlError>
|
||||
#include <QSqlQuery>
|
||||
#include <QVariant>
|
||||
#include <QMap>
|
||||
#include <QRegularExpression>
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -145,11 +147,7 @@ bool ProductionRepository::findActiveRun(ProductionRunSnapshot *run, QString *er
|
||||
}
|
||||
|
||||
QSqlQuery query(DatabaseManager::instance().database());
|
||||
query.prepare("select id, order_id, current_workpiece_id, plan_num, "
|
||||
"start_tray_no, start_slot_no, inspection_mode, run_state, "
|
||||
"production_state, process_step, timeout_state, "
|
||||
"recovery_retry_count, last_error "
|
||||
"from production_runs "
|
||||
query.prepare("select * from production_runs "
|
||||
"where run_state in ('RUNNING', 'PAUSED', 'RECOVERY') "
|
||||
"order by id desc limit 1");
|
||||
if (!query.exec()) {
|
||||
@@ -174,13 +172,218 @@ bool ProductionRepository::findActiveRun(ProductionRunSnapshot *run, QString *er
|
||||
run->timeoutState = query.value("timeout_state").toString();
|
||||
run->recoveryRetryCount = query.value("recovery_retry_count").toInt();
|
||||
run->lastError = query.value("last_error").toString();
|
||||
run->action.token = query.value("action_token").toString();
|
||||
run->action.type = query.value("action_type").toString();
|
||||
run->action.phase = query.value("action_phase").toString();
|
||||
run->action.nextStep = query.value("action_next_step").toString();
|
||||
run->action.attempt = query.value("action_attempt").toInt();
|
||||
run->action.version = query.value("action_lock_version").toInt();
|
||||
run->action.command = query.value("action_command_value");
|
||||
run->action.feedback = query.value("action_feedback_value");
|
||||
run->action.manualDecision = query.value("manual_decision").toString();
|
||||
run->action.manualOperator = query.value("manual_operator").toString();
|
||||
run->action.manualDecidedAt = query.value("manual_decided_at").toDateTime();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ProductionRepository::findRun(int runId, ProductionRunSnapshot *run, QString *error)
|
||||
{
|
||||
if (!run || !ensureOpen(error)) return false;
|
||||
QSqlQuery query(DatabaseManager::instance().database());
|
||||
query.prepare("select * from production_runs where id = :id");
|
||||
query.bindValue(":id", runId);
|
||||
if (!query.exec()) { setError(query, error); return false; }
|
||||
if (!query.next()) { if (error) *error = "Production run no longer exists"; return false; }
|
||||
run->id = query.value("id").toInt();
|
||||
run->orderId = query.value("order_id").toInt();
|
||||
run->currentWorkpieceId = query.value("current_workpiece_id").toInt();
|
||||
run->planNum = query.value("plan_num").toInt();
|
||||
run->startTrayNo = query.value("start_tray_no").toInt();
|
||||
run->startSlotNo = query.value("start_slot_no").toInt();
|
||||
run->inspectionMode = query.value("inspection_mode").toString();
|
||||
run->runState = query.value("run_state").toString();
|
||||
run->productionState = query.value("production_state").toString();
|
||||
run->processStep = query.value("process_step").toString();
|
||||
run->timeoutState = query.value("timeout_state").toString();
|
||||
run->recoveryRetryCount = query.value("recovery_retry_count").toInt();
|
||||
run->lastError = query.value("last_error").toString();
|
||||
run->action.token = query.value("action_token").toString();
|
||||
run->action.type = query.value("action_type").toString();
|
||||
run->action.phase = query.value("action_phase").toString();
|
||||
run->action.nextStep = query.value("action_next_step").toString();
|
||||
run->action.attempt = query.value("action_attempt").toInt();
|
||||
run->action.version = query.value("action_lock_version").toInt();
|
||||
run->action.command = query.value("action_command_value");
|
||||
run->action.feedback = query.value("action_feedback_value");
|
||||
run->action.manualDecision = query.value("manual_decision").toString();
|
||||
run->action.manualOperator = query.value("manual_operator").toString();
|
||||
run->action.manualDecidedAt = query.value("manual_decided_at").toDateTime();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ProductionRepository::checkActionSchema(QString *error)
|
||||
{
|
||||
if (!ensureOpen(error)) return false;
|
||||
QSqlQuery query(DatabaseManager::instance().database());
|
||||
query.prepare("select action_token, action_type, action_phase, action_next_step, "
|
||||
"action_attempt, action_lock_version, action_command_value, action_feedback_value, "
|
||||
"manual_decision, manual_operator, manual_decided_at, action_updated_at "
|
||||
"from production_runs where 1 = 0");
|
||||
if (!query.exec()) {
|
||||
if (error) *error = "请先执行 db/migrations/2026-10-29-production-action-checkpoints.sql:" + query.lastError().text();
|
||||
return false;
|
||||
}
|
||||
if (DatabaseManager::instance().database().driverName() != "QPSQL") return true;
|
||||
const QMap<QString, QString> expectedTypes = {
|
||||
{"action_token", "character varying(64)"},
|
||||
{"action_type", "character varying(32)"},
|
||||
{"action_phase", "character varying(32)"},
|
||||
{"action_next_step", "character varying(64)"},
|
||||
{"action_attempt", "integer"},
|
||||
{"action_lock_version", "integer"},
|
||||
{"action_command_value", "boolean"},
|
||||
{"action_feedback_value", "boolean"},
|
||||
{"manual_decision", "character varying(32)"},
|
||||
{"manual_operator", "character varying(64)"},
|
||||
{"manual_decided_at", "timestamp without time zone"},
|
||||
{"action_updated_at", "timestamp without time zone"}
|
||||
};
|
||||
query.prepare("select a.attname, format_type(a.atttypid,a.atttypmod), a.attnotnull, "
|
||||
"pg_get_expr(d.adbin,d.adrelid) from pg_attribute a "
|
||||
"left join pg_attrdef d on d.adrelid=a.attrelid and d.adnum=a.attnum "
|
||||
"where a.attrelid='production_runs'::regclass and a.attnum>0 and not a.attisdropped");
|
||||
if (!query.exec()) { setError(query, error); return false; }
|
||||
int checked = 0;
|
||||
while (query.next()) {
|
||||
const QString name = query.value(0).toString();
|
||||
if (!expectedTypes.contains(name)) continue;
|
||||
const bool counter = name == "action_attempt" || name == "action_lock_version";
|
||||
if (query.value(1).toString() != expectedTypes.value(name)
|
||||
|| query.value(2).toBool() != counter
|
||||
|| (counter && query.value(3).toString() != "0")) {
|
||||
if (error) *error = "动作恢复字段类型、空值或默认值不匹配:" + name;
|
||||
return false;
|
||||
}
|
||||
++checked;
|
||||
}
|
||||
if (checked != expectedTypes.size()) {
|
||||
if (error) *error = "动作恢复数据库字段不完整";
|
||||
return false;
|
||||
}
|
||||
const QMap<QString, QString> expectedChecks = {
|
||||
{"ck_production_runs_action_type", "CHECK (action_type IS NULL OR action_type = ANY ARRAY['PHOTO_MOVE','VISION','RACK_PICK','FRONT_LOAD','INSPECT','FRONT_UNLOAD','FLIP','BACK_LOAD','BACK_UNLOAD','RACK_PLACE'])"},
|
||||
{"ck_production_runs_action_phase", "CHECK (action_phase IS NULL OR action_phase = ANY ARRAY['NOT_SENT','SENDING','COMMAND_CONFIRMED','FEEDBACK_RECEIVED','SIGNALS_CLEARED','STEP_COMMITTED','RESULT_WRITING','RESULT_CONFIRMED','MODE_WRITING','MODE_CONFIRMED'])"},
|
||||
{"ck_production_runs_manual_decision", "CHECK (manual_decision IS NULL OR manual_decision = ANY ARRAY['ACTION_COMPLETED','ACTION_NOT_EXECUTED','RETRY_VISION','NORMAL_PICK'])"},
|
||||
{"ck_production_runs_action_attempt", "CHECK (action_attempt >= 0)"},
|
||||
{"ck_production_runs_action_lock_version", "CHECK (action_lock_version >= 0)"}
|
||||
};
|
||||
auto normalizedCheck = [](QString definition) {
|
||||
definition.remove(QRegularExpression("::(?:character varying|text)(?:\\[\\])?"));
|
||||
definition.remove(QRegularExpression("[\\s()\\[\\]]"));
|
||||
return definition;
|
||||
};
|
||||
query.prepare("select conname, pg_get_constraintdef(oid) from pg_constraint "
|
||||
"where conrelid='production_runs'::regclass and contype='c' and convalidated");
|
||||
if (!query.exec()) { setError(query, error); return false; }
|
||||
checked = 0;
|
||||
while (query.next()) {
|
||||
const QString name = query.value(0).toString();
|
||||
if (!expectedChecks.contains(name)) continue;
|
||||
if (normalizedCheck(query.value(1).toString()) != normalizedCheck(expectedChecks.value(name))) {
|
||||
if (error) *error = "动作恢复约束定义不匹配:" + name;
|
||||
return false;
|
||||
}
|
||||
++checked;
|
||||
}
|
||||
if (checked != expectedChecks.size()) {
|
||||
if (error) *error = "动作恢复数据库约束不完整,请执行检查点升级 SQL";
|
||||
return false;
|
||||
}
|
||||
query.prepare("select count(*) from pg_index i join pg_attribute a "
|
||||
"on a.attrelid=i.indrelid and a.attnum=i.indkey[0] "
|
||||
"where i.indrelid='production_runs'::regclass and i.indisunique and i.indisvalid "
|
||||
"and i.indnatts=1 and a.attname='action_token' "
|
||||
"and pg_get_expr(i.indpred,i.indrelid)='(action_token IS NOT NULL)'");
|
||||
if (!query.exec() || !query.next() || query.value(0).toInt() < 1) {
|
||||
if (error) *error = "动作恢复唯一索引不完整,请执行检查点升级 SQL";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ProductionRepository::saveActionCheckpoint(int runId, int orderId, int workpieceId,
|
||||
ProductionActionCheckpoint *action, QString *error)
|
||||
{
|
||||
if (!action || !ensureOpen(error)) return false;
|
||||
QSqlQuery query(DatabaseManager::instance().database());
|
||||
query.prepare("update production_runs set action_token=:token, action_type=:type, "
|
||||
"action_phase=:phase, action_next_step=:next, action_attempt=:attempt, "
|
||||
"action_lock_version=action_lock_version+1, action_command_value=:command, "
|
||||
"action_feedback_value=:feedback, manual_decision=:decision, manual_operator=:operator, "
|
||||
"manual_decided_at=:decided, action_updated_at=now(), updated_at=now() "
|
||||
"where id=:id and order_id=:order_id and current_workpiece_id is not distinct from :workpiece "
|
||||
"and action_lock_version=:version");
|
||||
query.bindValue(":token", action->token);
|
||||
query.bindValue(":type", action->type);
|
||||
query.bindValue(":phase", action->phase);
|
||||
query.bindValue(":next", action->nextStep);
|
||||
query.bindValue(":attempt", action->attempt);
|
||||
query.bindValue(":command", action->command.isNull() ? QVariant(QVariant::Bool) : action->command);
|
||||
query.bindValue(":feedback", action->feedback.isNull() ? QVariant(QVariant::Bool) : action->feedback);
|
||||
query.bindValue(":decision", action->manualDecision);
|
||||
query.bindValue(":operator", action->manualOperator);
|
||||
query.bindValue(":decided", action->manualDecidedAt.isValid() ? QVariant(action->manualDecidedAt) : QVariant(QVariant::DateTime));
|
||||
query.bindValue(":id", runId);
|
||||
query.bindValue(":order_id", orderId);
|
||||
query.bindValue(":workpiece", nullableId(workpieceId));
|
||||
query.bindValue(":version", action->version);
|
||||
if (!query.exec()) { setError(query, error); return false; }
|
||||
if (query.numRowsAffected() != 1) {
|
||||
if (error) *error = "动作检查点已改变,请重新读取恢复状态";
|
||||
return false;
|
||||
}
|
||||
++action->version;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ProductionRepository::findInspectionResult(int workpieceId, const QString &side,
|
||||
int *rawResult, QString *resultState, QString *error,
|
||||
const QString &actionToken, int actionAttempt)
|
||||
{
|
||||
if (!rawResult || !resultState || (side != "FRONT" && side != "BACK")) {
|
||||
if (error) *error = "Invalid inspection result request";
|
||||
return false;
|
||||
}
|
||||
if (!ensureOpen(error)) return false;
|
||||
QSqlQuery query(DatabaseManager::instance().database());
|
||||
QString sql = "select raw_result, result_state from inspection_records "
|
||||
"where workpiece_id=:id and inspection_side=:side and inspection_state='COMPLETED' "
|
||||
"and created_at >= (select started_at from workpiece_process_records "
|
||||
"where workpiece_id=:id and process_step=:step order by attempt_no desc limit 1) ";
|
||||
if (!actionToken.isEmpty()) sql += "and message=:action_message ";
|
||||
sql += "order by attempt_no desc limit 1";
|
||||
query.prepare(sql);
|
||||
query.bindValue(":id", workpieceId);
|
||||
query.bindValue(":side", side);
|
||||
query.bindValue(":step", side == "FRONT" ? "FRONT_CHECKING" : "BACK_CHECKING");
|
||||
if (!actionToken.isEmpty())
|
||||
query.bindValue(":action_message", "action=" + actionToken + ";attempt=" + QString::number(actionAttempt));
|
||||
if (!query.exec()) { setError(query, error); return false; }
|
||||
if (!query.next()) { if (error) *error = "检测结果尚未持久化,不能认定检测完成"; return false; }
|
||||
if (query.value(0).isNull()) {
|
||||
if (error) *error = "检测结果为空,不能认定检测完成";
|
||||
return false;
|
||||
}
|
||||
*rawResult = query.value(0).toInt();
|
||||
*resultState = query.value(1).toString();
|
||||
return *resultState == "OK" || *resultState == "NG";
|
||||
}
|
||||
|
||||
bool ProductionRepository::insertStateHistory(
|
||||
int runId, int orderId, int workpieceId, const QString &fromState,
|
||||
const QString &toState, const QString &processStep, const QString &source,
|
||||
const QString &reason, const QString &message, QString *error)
|
||||
const QString &reason, const QString &message, QString *error,
|
||||
const QString &operatorName)
|
||||
{
|
||||
if (!ensureOpen(error)) {
|
||||
return false;
|
||||
@@ -189,9 +392,9 @@ bool ProductionRepository::insertStateHistory(
|
||||
QSqlQuery query(DatabaseManager::instance().database());
|
||||
query.prepare("insert into production_state_history "
|
||||
"(production_run_id, order_id, workpiece_id, from_state, "
|
||||
"to_state, process_step, source, reason, message) "
|
||||
"to_state, process_step, source, reason, message, operator_name) "
|
||||
"values (:run_id, :order_id, :workpiece_id, :from_state, "
|
||||
":to_state, :process_step, :source, :reason, :message)");
|
||||
":to_state, :process_step, :source, :reason, :message, :operator)");
|
||||
query.bindValue(":run_id", nullableId(runId));
|
||||
query.bindValue(":order_id", nullableId(orderId));
|
||||
query.bindValue(":workpiece_id", nullableId(workpieceId));
|
||||
@@ -201,6 +404,7 @@ bool ProductionRepository::insertStateHistory(
|
||||
query.bindValue(":source", source);
|
||||
query.bindValue(":reason", reason);
|
||||
query.bindValue(":message", message);
|
||||
query.bindValue(":operator", operatorName);
|
||||
if (!query.exec()) {
|
||||
setError(query, error);
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user