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);
}
});
});