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
+87 -52
View File
@@ -523,35 +523,37 @@ void DeviceService::finishTermination(bool success, const QString &error)
void DeviceService::beginProductionAction(
ProductionAction action, int trayNo, int slotNo,
const OperationCallback &callback, const std::function<bool()> &beforeSend)
const VisionWriteCallback &callback, const std::function<bool()> &beforeSend)
{
if (m_terminationActive) {
if (callback) {
callback(false, "PLC termination is in progress");
callback(VisionWriteOutcome::NotSent, "PLC termination is in progress");
}
return;
}
if (ConfigManager::instance().simulationMode()) {
const bool permitted = !beforeSend || beforeSend();
if (callback) {
callback(true, QString());
callback(permitted ? VisionWriteOutcome::Confirmed : VisionWriteOutcome::NotSent,
permitted ? QString() : "Production action invalidated before transmission");
}
return;
}
if (!ConfigManager::instance().productionEnabled()) {
if (callback) {
callback(false, "Production control is disabled by configuration");
callback(VisionWriteOutcome::NotSent, "Production control is disabled by configuration");
}
return;
}
if (m_productionActionActive || m_productionActionBusy) {
if (callback) {
callback(false, "A PLC production action is already active");
callback(VisionWriteOutcome::NotSent, "A PLC production action is already active");
}
return;
}
if (trayNo <= 0 || slotNo < 1 || slotNo > 49) {
if (callback) {
callback(false, "PLC tray or slot position is invalid");
callback(VisionWriteOutcome::NotSent, "PLC tray or slot position is invalid");
}
return;
}
@@ -562,7 +564,7 @@ void DeviceService::beginProductionAction(
&m_productionCommandAddress,
&m_productionFeedbackAddress)) {
if (callback) {
callback(false, "Unknown PLC production action");
callback(VisionWriteOutcome::NotSent, "Unknown PLC production action");
}
return;
}
@@ -576,50 +578,40 @@ void DeviceService::beginProductionAction(
if (!success) {
m_productionActionBusy = false;
if (callback) {
callback(false, error);
callback(VisionWriteOutcome::NotSent, error);
}
return;
}
if (status.command || status.feedback) {
m_productionActionBusy = false;
if (callback) {
callback(false,
callback(VisionWriteOutcome::NotSent,
"PLC command or feedback is still active before a new action");
}
return;
}
auto sendCommand =
[this, callback](bool success, const QString &error) {
if (!success) {
m_productionActionBusy = false;
if (callback) {
callback(false, error);
}
return;
}
m_productionActionActive = true;
[this, callback](VisionWriteOutcome outcome, const QString &error) {
// An unacknowledged command may already be running. Do not
// permit another action until it has been reconciled.
m_productionActionActive = outcome != VisionWriteOutcome::NotSent;
m_productionActionBusy = false;
if (callback) {
callback(true, QString());
callback(outcome, error);
}
};
auto writeCommand = [this, sendCommand, beforeSend](bool success,
const QString &error) {
if (!success) {
sendCommand(false, error);
sendCommand(VisionWriteOutcome::NotSent, error);
return;
}
if (beforeSend) {
m_modbus->writeVisionCoil(m_productionCommandAddress, true,
[sendCommand](VisionWriteOutcome outcome, const QString &error) {
sendCommand(outcome == VisionWriteOutcome::Confirmed, error);
}, beforeSend);
} else {
m_modbus->writeCoil(m_productionCommandAddress, true, sendCommand);
}
// Action commands must never use the legacy automatic retry
// path, even when no persistence hook is supplied.
m_modbus->writeVisionCoil(m_productionCommandAddress, true,
sendCommand, beforeSend);
};
if (action == PhotoMoveAction || action == RackPick
@@ -706,7 +698,9 @@ void DeviceService::adoptProductionAction(ProductionAction action)
m_productionActionActive = true;
}
void DeviceService::pollProductionAction(const ActionCallback &callback)
void DeviceService::pollProductionAction(
const ActionCallback &callback,
const std::function<bool(QString *)> &beforeClear)
{
if (m_terminationActive) {
if (callback) {
@@ -715,9 +709,9 @@ void DeviceService::pollProductionAction(const ActionCallback &callback)
return;
}
if (ConfigManager::instance().simulationMode()) {
if (callback) {
callback(true, true, QString());
}
finishProductionAction([callback](bool success, const QString &error) {
if (callback) callback(success, success, error);
}, beforeClear);
return;
}
if (m_productionActionBusy) {
@@ -736,7 +730,7 @@ void DeviceService::pollProductionAction(const ActionCallback &callback)
m_productionActionBusy = true;
m_modbus->readCoil(
m_productionFeedbackAddress,
[this, callback](bool success, bool feedback,
[this, callback, beforeClear](bool success, bool feedback,
const QString &error) {
if (!success) {
m_productionActionBusy = false;
@@ -753,23 +747,62 @@ void DeviceService::pollProductionAction(const ActionCallback &callback)
return;
}
clearActionSignals(
[this, callback](bool cleared, const QString &clearError) {
m_productionActionBusy = false;
if (cleared) {
m_productionActionActive = false;
m_productionCommandAddress = -1;
m_productionFeedbackAddress = -1;
if (callback) {
callback(true, true, QString());
}
} else if (callback) {
callback(false, false, clearError);
}
});
QString checkpointError;
const bool saved = beforeClear && beforeClear(&checkpointError);
m_productionActionBusy = false;
if (!saved) {
if (!beforeClear)
checkpointError = "Action completion checkpoint hook is missing";
// An empty error explicitly defers completion while the
// caller gathers asynchronous inspection data.
if (callback) callback(checkpointError.isEmpty(), false, checkpointError);
return;
}
finishProductionAction(
[callback](bool cleared, const QString &clearError) {
if (callback) callback(cleared, cleared, clearError);
}, [](QString *) { return true; });
});
}
void DeviceService::finishProductionAction(
const OperationCallback &callback,
const std::function<bool(QString *)> &beforeClear)
{
if (m_terminationActive || m_productionActionBusy) {
if (callback) callback(false, "PLC production action is busy or terminating");
return;
}
const bool simulation = ConfigManager::instance().simulationMode();
if (!simulation && !m_productionActionActive) {
if (callback) callback(false, "No PLC production action is active");
return;
}
m_productionActionBusy = true;
QString checkpointError;
if (!beforeClear || !beforeClear(&checkpointError)) {
m_productionActionBusy = false;
if (callback) callback(false, checkpointError.isEmpty()
? "Action completion checkpoint was not saved"
: checkpointError);
return;
}
auto finish = [this, callback](bool success, const QString &error) {
m_productionActionBusy = false;
if (success) {
m_productionActionActive = false;
m_productionCommandAddress = -1;
m_productionFeedbackAddress = -1;
}
if (callback) callback(success, error);
};
if (simulation) {
finish(true, QString());
return;
}
clearActionSignals(finish);
}
void DeviceService::cancelProductionAction(const OperationCallback &callback)
{
if (m_terminationActive) {
@@ -830,14 +863,16 @@ void DeviceService::clearSignals(int commandAddress, int feedbackAddress,
commandAddress, false,
[this, feedbackAddress, callback](bool commandCleared,
const QString &commandError) {
if (!commandCleared) {
if (callback) callback(false, commandError);
return;
}
m_modbus->writeCoil(
feedbackAddress, false,
[callback, commandCleared, commandError](
[callback](
bool feedbackCleared, const QString &feedbackError) {
if (callback) {
callback(commandCleared && feedbackCleared,
commandCleared ? feedbackError
: commandError);
callback(feedbackCleared, feedbackError);
}
});
});
+5 -2
View File
@@ -84,9 +84,12 @@ public:
const OperationCallback &callback);
void terminate(const OperationCallback &callback);
void beginProductionAction(ProductionAction action, int trayNo,
int slotNo, const OperationCallback &callback,
int slotNo, const VisionWriteCallback &callback,
const std::function<bool()> &beforeSend = {});
void pollProductionAction(const ActionCallback &callback);
void pollProductionAction(const ActionCallback &callback,
const std::function<bool(QString *)> &beforeClear);
void finishProductionAction(const OperationCallback &callback,
const std::function<bool(QString *)> &beforeClear);
void inspectProductionAction(ProductionAction action,
const ActionStatusCallback &callback);
void adoptProductionAction(ProductionAction action);
File diff suppressed because it is too large Load Diff
+35 -6
View File
@@ -4,6 +4,7 @@
#include "deviceservice.h"
#include "../models/workpiece.h"
#include "../repositories/productionrepository.h"
#include <QElapsedTimer>
#include <QObject>
@@ -64,6 +65,7 @@ public:
bool stop(QString *error = nullptr);
bool clearAlarm(QString *error = nullptr);
bool recover(QString *error = nullptr);
bool submitRecoveryDecision(const QJsonObject &decision, QString *error = nullptr);
bool markCurrentWorkpieceNg(QString *error = nullptr);
bool restoreActiveRun(QString *error = nullptr);
void shutdown(const DeviceService::OperationCallback &callback);
@@ -89,6 +91,8 @@ signals:
void timeoutWarning(const QString &state, int elapsedMs, int timeoutMs);
void timeoutRecoveryRequired(const QString &state, const QString &message);
void recoveryCompleted(const QString &mode);
void recoveryDecisionRequired(const QJsonObject &context);
void recoveryBusyChanged(bool busy);
void visionMeasureRequested(const QJsonObject &request);
void visionCancelRequested(const QString &requestId);
void visionCycleChanged(const QJsonObject &cycle);
@@ -118,14 +122,29 @@ private:
void reportVision(const QString &phase, const QString &message, bool invalidated = false);
void invalidateVision(const QString &reason);
void visionFault(const QString &reason);
bool recoverVision(QString *error);
void publishVisionControl();
bool saveVisionRecoveryState(bool pending, QString *error);
void loadVisionRecoveryState();
ProductionService(const ProductionService &) = delete;
ProductionService &operator=(const ProductionService &) = delete;
void setState(State state);
bool setState(State state);
QString actionTypeForState(State state) const;
QString nextStepForState(State state) const;
bool ensureActionCheckpoint(State state, QString *error);
bool saveActionPhase(const QString &phase, QString *error);
bool saveActionCompletion(ProductionActionCheckpoint *action,
const QString &nextStep, int nextWorkpieceId,
QString *error);
bool actionCallbackCurrent(quint64 generation, const QString &token, int attempt) const;
bool loadRecoverySnapshot(QString *error);
void finishRecoveryCheck(const QString &error = QString());
void requestManualDecision(State state, const ProductionActionStatus &status,
const QString &reason, const QStringList &options);
void resumeRecoveredState(State state);
bool saveManualDecision(const QJsonObject &decision, QString *error);
void checkVisionRecovery(const QJsonObject &decision = QJsonObject());
bool pickTargetValid() const;
bool claimNextWorkpiece(QString *error);
bool updateCurrentProcessStep(const QString &processStep, QString *error,
bool forceRestart = false);
@@ -157,7 +176,6 @@ private:
QString resultStateForRawResult(int rawResult) const;
bool ensureOrderRunning(QString *error);
bool checkRecoverySafety(QString *error) const;
bool mapPlcStepToState(State *state, QString *processStep) const;
void startProcessTimer(const QString &processStep);
void pauseProcessTimer();
void resumeProcessTimer();
@@ -199,18 +217,31 @@ private:
bool m_actionStarting;
bool m_actionPollPending;
bool m_actionCompleted;
bool m_actionWasSent;
QString m_actionError;
bool m_actionCancelRequested;
DeviceService::OperationCallback m_deferredCancelCallback;
bool m_inspectionStatusPending;
bool m_inspectionStatusReady;
int m_inspectionRawResult = 0;
QString m_inspectionStatusError;
bool m_starting;
bool m_plcStarted;
bool m_tickRunning;
bool m_pauseAfterCurrentAction;
bool m_recoveryCheckPending;
quint64 m_actionGeneration = 0;
quint64 m_recoveryGeneration = 0;
ProductionActionCheckpoint m_checkpoint;
QJsonObject m_manualContext;
bool m_recoveryKeepPaused = false;
bool m_recoveryIdleUnlock = false;
bool m_targetReady = false;
int m_targetWorkpieceId = 0;
QString m_targetSession;
bool m_targetUsesVision = false;
bool m_forceNormalPick = false;
bool m_restoredRun = false;
bool m_persistenceUncertain = false;
DeviceService::ProductionAction m_pendingAction;
QString m_inspectionMode;
QString m_frontResultState;
@@ -235,8 +266,6 @@ private:
bool m_visionSuspended = false;
bool m_visionCommitStarted = false;
bool m_visionNeedsReconcile = false;
bool m_visionRecoveryChecking = false;
bool m_visionFaultCleanupPending = false;
QString m_visionStatePath;
};
+11
View File
@@ -45,6 +45,10 @@ void ProductionWorker::ensureService()
this, &ProductionWorker::timeoutRecoveryRequired);
connect(m_service, &ProductionService::recoveryCompleted,
this, &ProductionWorker::recoveryCompleted);
connect(m_service, &ProductionService::recoveryDecisionRequired,
this, &ProductionWorker::recoveryDecisionRequired);
connect(m_service, &ProductionService::recoveryBusyChanged,
this, &ProductionWorker::recoveryBusyChanged);
}
void ProductionWorker::requestStopImmediate()
@@ -119,6 +123,13 @@ void ProductionWorker::requestRecover()
reportFailure(m_service->recover(&error), error);
}
void ProductionWorker::requestRecoveryDecision(const QJsonObject &decision)
{
ensureService();
QString error;
reportFailure(m_service->submitRecoveryDecision(decision, &error), error);
}
void ProductionWorker::requestMarkNg()
{
ensureService();
+3
View File
@@ -21,6 +21,7 @@ public slots:
void requestStop();
void requestClearAlarm();
void requestRecover();
void requestRecoveryDecision(const QJsonObject &decision);
void requestMarkNg();
void requestRestore();
void requestShutdown();
@@ -42,6 +43,8 @@ signals:
void timeoutWarning(const QString &state, int elapsedMs, int timeoutMs);
void timeoutRecoveryRequired(const QString &state, const QString &message);
void recoveryCompleted(const QString &mode);
void recoveryDecisionRequired(const QJsonObject &context);
void recoveryBusyChanged(bool busy);
void shutdownReady();
void visionMeasureRequested(const QJsonObject &request);
void visionCancelRequested(const QString &requestId);
+5 -2
View File
@@ -280,7 +280,7 @@ bool WorkpieceService::updateState(int id, const QString &state,
bool WorkpieceService::updateProcessStepWithRecord(
int id, const QString &processStep, const QString &inspectionMode,
int plcStep, const QString &source, int durationMs, QString *error,
bool forceRestart)
bool forceRestart, const std::function<bool(QString *)> &beforeCommit)
{
if (id <= 0 || processStep.trimmed().isEmpty()) {
if (error) {
@@ -344,6 +344,7 @@ bool WorkpieceService::updateProcessStepWithRecord(
QString("工件 %1 工序更新为 %2")
.arg(workpiece.barcode, processStep),
&transactionError)
|| (beforeCommit && !beforeCommit(&transactionError))
|| !database.commit(&transactionError)) {
database.rollback();
if (error) {
@@ -358,7 +359,8 @@ bool WorkpieceService::completeWithInspection(
int id, const QString &state, const QString &processStep,
const QString &previousProcessStep, int durationMs, int rawResult,
const QString &resultState, int plcStep, const QString &inspectionMode,
const QString &source, QString *error)
const QString &source, QString *error,
const std::function<bool(QString *)> &beforeCommit)
{
if (id <= 0) {
if (error) {
@@ -408,6 +410,7 @@ bool WorkpieceService::completeWithInspection(
QString("工件 %1 检测结果为 %2")
.arg(workpiece.barcode, resultState),
&transactionError)
|| (beforeCommit && !beforeCommit(&transactionError))
|| !database.commit(&transactionError)) {
database.rollback();
if (error) {
+5 -2
View File
@@ -4,6 +4,7 @@
#include "../models/workpiece.h"
#include <QList>
#include <functional>
class WorkpieceService
{
@@ -25,7 +26,8 @@ public:
const QString &source,
int durationMs,
QString *error = nullptr,
bool forceRestart = false);
bool forceRestart = false,
const std::function<bool(QString *)> &beforeCommit = {});
bool recordInspection(int id, const QString &side, int rawResult,
const QString &resultState, int plcStep,
const QString &inspectionMode, const QString &source,
@@ -38,7 +40,8 @@ public:
const QString &resultState, int plcStep,
const QString &inspectionMode,
const QString &source,
QString *error = nullptr);
QString *error = nullptr,
const std::function<bool(QString *)> &beforeCommit = {});
private:
WorkpieceService() = default;