feat(production): 修改订单清除按钮功能---除了清除报警、也复位状态,让订单能继续执行

This commit is contained in:
lidg
2026-11-09 11:05:12 +08:00
parent 6a70f1943f
commit 0ed952f917
15 changed files with 691 additions and 49 deletions
+96
View File
@@ -100,6 +100,7 @@ void DeviceService::startPolling()
DeviceService::~DeviceService()
{
m_pollTimer->stop();
++m_resetGeneration;
m_terminationActive = true;
m_terminationCallback = OperationCallback();
++m_visionGeneration;
@@ -446,6 +447,7 @@ void DeviceService::terminate(const OperationCallback &callback)
}
m_terminationActive = true;
++m_resetGeneration;
m_terminationCallback = callback;
m_terminationSignals.clear();
m_terminationSignalIndex = 0;
@@ -478,6 +480,97 @@ void DeviceService::terminate(const OperationCallback &callback)
clearNextTerminationSignal();
}
void DeviceService::resetProductionSignals(const OperationCallback &callback)
{
if (m_terminationActive) {
if (callback) callback(false, "PLC termination or reset is already in progress");
return;
}
const bool simulation = ConfigManager::instance().simulationMode();
if (!simulation && !ConfigManager::instance().productionEnabled()) {
if (callback) callback(false, "Production control is disabled by configuration");
return;
}
const ModbusAddressMap addresses = m_modbus->config().addressMap;
const VisionConfig vision = ConfigManager::instance().visionConfig();
if (addresses.clearAlarmCommand < 0 || addresses.clearAlarmCommand > 65535) {
if (callback) callback(false, "Invalid clear-alarm coil address");
return;
}
QList<int> resetSignals;
const ProductionAction actions[] = {PhotoMoveAction, RackPick, FrontLoad, Inspect,
FrontUnload, Flip, ReverseFlip, BackLoad, BackUnload, RackPlace};
for (const ProductionAction action : actions) {
int command = -1;
int feedback = -1;
actionAddresses(action, addresses, vision, &command, &feedback);
for (const int address : {command, feedback}) {
if (!resetSignals.contains(address)) resetSignals.append(address);
}
}
if (!resetSignals.contains(vision.comparisonPickCoil)) resetSignals.append(vision.comparisonPickCoil);
for (const int address : resetSignals) {
if (address < 0 || address > 65535 || address == addresses.clearAlarmCommand) {
if (callback) callback(false, QString("Invalid production reset coil M%1").arg(address));
return;
}
}
m_terminationActive = true;
m_terminationCallback = callback;
const quint64 generation = ++m_resetGeneration;
++m_visionGeneration;
m_pollTimer->stop();
m_resetSignals = resetSignals;
m_resetSignalIndex = 0;
m_modbus->abortAll("PLC production reset requested");
m_statusRequestPending = false;
m_controlRequestPending = false;
m_productionActionActive = false;
m_productionActionBusy = false;
if (simulation) {
finishTermination(true, QString());
return;
}
m_modbus->writeCoil(addresses.clearAlarmCommand, true,
[this, generation, addresses](bool success, const QString &error) {
if (generation != m_resetGeneration || !m_terminationActive) return;
if (!success) {
finishTermination(false, QString("Clear alarm M%1 failed: %2")
.arg(addresses.clearAlarmCommand).arg(error));
return;
}
clearNextResetSignal(generation);
});
}
void DeviceService::clearNextResetSignal(quint64 generation)
{
if (generation != m_resetGeneration || !m_terminationActive) return;
if (m_resetSignalIndex >= m_resetSignals.size()) {
finishTermination(true, QString());
return;
}
const int address = m_resetSignals.at(m_resetSignalIndex++);
m_modbus->writeCoil(address, false,
[this, generation, address](bool written, const QString &error) {
if (generation != m_resetGeneration || !m_terminationActive) return;
if (!written) {
finishTermination(false, QString("Reset M%1 failed: %2").arg(address).arg(error));
return;
}
m_modbus->readCoil(address,
[this, generation, address](bool read, bool value, const QString &readError) {
if (generation != m_resetGeneration || !m_terminationActive) return;
if (!read || value) {
finishTermination(false, QString("Reset M%1 not confirmed: %2")
.arg(address).arg(read ? "coil remains set" : readError));
return;
}
clearNextResetSignal(generation);
});
});
}
void DeviceService::clearNextTerminationSignal()
{
if (!m_terminationActive) {
@@ -517,10 +610,13 @@ void DeviceService::finishTermination(bool success, const QString &error)
}
const OperationCallback callback = m_terminationCallback;
++m_resetGeneration;
m_terminationCallback = OperationCallback();
m_terminationActive = false;
m_terminationSignals.clear();
m_terminationSignalIndex = 0;
m_resetSignals.clear();
m_resetSignalIndex = 0;
m_controlRequestPending = false;
m_productionActionActive = false;
m_productionActionBusy = false;
+5
View File
@@ -84,6 +84,7 @@ public:
void setInspectionMode(const QString &mode,
const OperationCallback &callback);
void terminate(const OperationCallback &callback);
void resetProductionSignals(const OperationCallback &callback);
void beginProductionAction(ProductionAction action, int trayNo,
int slotNo, const VisionWriteCallback &callback,
const std::function<bool()> &beforeSend = {});
@@ -121,6 +122,7 @@ private:
const OperationCallback &callback);
void clearActionSignals(const OperationCallback &callback);
void clearNextTerminationSignal();
void clearNextResetSignal(quint64 generation);
void finishTermination(bool success, const QString &error);
void finishActionPoll(bool completed, const ActionCallback &callback);
bool checkVisionAccess(QString *error) const;
@@ -140,6 +142,9 @@ private:
QList<QPair<int, int>> m_terminationSignals;
int m_terminationSignalIndex;
OperationCallback m_terminationCallback;
QList<int> m_resetSignals;
int m_resetSignalIndex = 0;
quint64 m_resetGeneration = 0;
quint64 m_visionGeneration = 0;
};
+341 -21
View File
@@ -98,6 +98,8 @@ ProductionService::Snapshot ProductionService::snapshot() const
result.stateName = stateName();
result.terminationRequired = m_terminationRequired;
result.visionRecoveryRequired = m_visionNeedsReconcile;
result.resetInProgress = m_resetInProgress;
result.resetRequired = m_resetRequired;
result.error = m_lastError;
return result;
}
@@ -110,6 +112,7 @@ void ProductionService::publishState()
ProductionService::~ProductionService()
{
m_destroying = true;
m_timer->stop();
m_state = Terminating;
m_stopRequested.store(true);
@@ -201,6 +204,7 @@ void ProductionService::requestStopImmediate()
bool ProductionService::saveVisionRecoveryState(bool pending, QString *error)
{
const QJsonObject state{{"version", 1}, {"pending", pending},
{"reset_pending", pending && m_resetRequired},
{"run_id", m_runId}, {"workpiece_id", m_currentWorkpiece.id},
{"updated_at", QDateTime::currentDateTimeUtc().toString(Qt::ISODateWithMs)}};
const QByteArray bytes = QJsonDocument(state).toJson(QJsonDocument::Compact);
@@ -220,11 +224,13 @@ void ProductionService::loadVisionRecoveryState()
const QJsonObject state = opened ? QJsonDocument::fromJson(file.readAll()).object() : QJsonObject();
const bool valid = state.value("version").toDouble() == 1 && state.value("pending").isBool();
if (valid && !state.value("pending").toBool()) return;
m_resetRequired = state.value("reset_pending").toBool();
m_visionNeedsReconcile = true;
m_visionSuspended = true;
m_visionStage = VisionStage::Fault;
m_lastError = valid ? "上次视觉事务尚未完成,请核对机器人和 PLC 后恢复"
: "视觉恢复记录无法读取或已损坏,请核对机器人和 PLC 后恢复";
m_lastError = m_resetRequired ? "上次复位未完成,请点击清除报警并选择复位"
: (valid ? "上次视觉事务尚未完成,请核对机器人和 PLC 后恢复"
: "视觉恢复记录无法读取或已损坏,请核对机器人和 PLC 后恢复");
}
void ProductionService::publishVisionControl()
@@ -546,6 +552,10 @@ void ProductionService::visionFault(const QString &reason)
bool ProductionService::start(int orderId, QString *error)
{
if (m_alarmOperationBusy || m_resetRequired || m_shutdownRequested) {
if (error) *error = "清警或复位尚未完成,不能启动订单";
return false;
}
if (!m_initialized && !restoreActiveRun(error)) return false;
if (!ProductionRepository().checkActionSchema(error)) return false;
const VisionConfig vision = ConfigManager::instance().visionConfig();
@@ -698,6 +708,10 @@ bool ProductionService::start(int orderId, QString *error)
bool ProductionService::pause(QString *error)
{
if (m_resetInProgress) {
if (error) *error = "正在复位,不能暂停生产";
return false;
}
if (!isActive()) {
if (error) {
*error = "Production service is not active";
@@ -775,6 +789,10 @@ void ProductionService::abortVisionForPause()
bool ProductionService::resume(QString *error)
{
if (m_alarmOperationBusy || m_resetRequired || m_shutdownRequested) {
if (error) *error = "清警或复位尚未完成,不能继续生产";
return false;
}
if (m_state != Paused || m_starting || m_recoveryCheckPending) {
if (error) *error = "当前订单不能继续或正在处理其他恢复操作";
return false;
@@ -800,7 +818,8 @@ bool ProductionService::resume(QString *error)
const quint64 generation = ++m_actionGeneration;
m_deviceService->requestStatus(
[this, generation](bool success, const DeviceStatus &, const QString &statusError) {
if (generation != m_actionGeneration || stopRequested()) { m_starting = false; return; }
if (generation != m_actionGeneration) return;
if (stopRequested()) { m_starting = false; return; }
QString error;
if (!success || !checkRecoverySafety(&error)) {
m_starting = false;
@@ -809,7 +828,8 @@ bool ProductionService::resume(QString *error)
}
m_deviceService->setInspectionMode(m_inspectionMode,
[this, generation](bool modeSuccess, const QString &modeError) {
if (generation != m_actionGeneration || stopRequested()) { m_starting = false; return; }
if (generation != m_actionGeneration) return;
if (stopRequested()) { m_starting = false; return; }
QString error;
if (!modeSuccess || !checkRecoverySafety(&error)
|| (m_currentWorkpiece.processStep == "RACK_PICKING" && !pickTargetValid())) {
@@ -837,6 +857,15 @@ bool ProductionService::resume(QString *error)
bool ProductionService::stopOrder(int orderId, QString *error)
{
if (m_resetInProgress) {
requestStopImmediate();
if (error) *error = "正在复位,已请求中止,请等待复位结果";
return false;
}
if (m_alarmOperationBusy || m_resetRequired) {
if (error) *error = "清警或复位尚未完成,请等待结果或重试复位";
return false;
}
if (orderId <= 0) {
if (error) *error = "没有可终止的订单";
return false;
@@ -882,6 +911,15 @@ bool ProductionService::stopConfirmedOrder(int runId, int orderId, QString *erro
bool ProductionService::stop(QString *error, bool finishOrder)
{
if (m_resetInProgress) {
requestStopImmediate();
if (error) *error = "正在复位,已请求中止,请等待复位结果";
return false;
}
if ((m_alarmOperationBusy && !m_shutdownRequested) || m_resetRequired) {
if (error) *error = "清警或复位尚未完成,请等待结果或重试复位";
return false;
}
if (m_state == Terminating) return true;
if (m_state == Idle || m_state == Completed) {
if (error) {
@@ -991,6 +1029,17 @@ void ProductionService::shutdown(const DeviceService::OperationCallback &callbac
m_shutdownRequested = true;
m_shutdownCallback = callback;
// The reset callback owns the current device operation. It will finish
// shutdown cleanup without committing the old task or clearing its marker.
if (m_resetInProgress) return;
if (m_resetRequired) {
m_timer->stop();
finishRecoveryCheck();
m_deviceService->terminate([this](bool stopped, const QString &stopError) {
if (!m_destroying) finishShutdown(stopped, stopError);
});
return;
}
if (orphanVisionLock) {
finishRecoveryCheck();
m_timer->stop();
@@ -1031,26 +1080,250 @@ void ProductionService::shutdown(const DeviceService::OperationCallback &callbac
bool ProductionService::clearAlarm(QString *error)
{
Q_UNUSED(error)
if (m_alarmOperationBusy || m_shutdownRequested || m_state == Terminating) {
if (error) *error = "清警、复位或退出操作正在进行,请稍后重试";
return false;
}
m_alarmOperationBusy = true;
emit alarmOperationBusyChanged(true);
m_deviceService->clearAlarm(
[this](bool success, const QString &operationError) {
if (!success) {
emit productionError(operationError);
return;
}
QString persistenceError;
if (!ProductionRepository().clearActiveAlarms(
QString(), "OPERATOR", &persistenceError)) {
emit productionError(persistenceError);
return;
}
if (m_destroying) return;
QString resultError = operationError;
if (success)
success = ProductionRepository().clearActiveAlarms(
QString(), "OPERATOR", &resultError);
m_alarmOperationBusy = false;
emit alarmOperationBusyChanged(false);
emit alarmOperationFinished(false, success, success
? "报警已清除,生产任务及运行状态未重置" : resultError);
});
return true;
}
QJsonObject ProductionService::resetContext(QString *error)
{
m_resetContext = QJsonObject();
QString contextError;
ProductionRunSnapshot run;
Order order;
Order preserved;
if (!ProductionRepository().findActiveRun(&run, &contextError)
|| (run.id > 0 && !OrderRepository().findOrder(run.orderId, &order, &contextError))
|| !OrderRepository().findUnfinishedOrder(&preserved, &contextError)) {
if (error) *error = contextError;
return QJsonObject{{"valid", false}, {"error", contextError}};
}
m_resetContext = QJsonObject{{"valid", true}, {"run_id", run.id},
{"order_id", run.orderId}, {"order_no", order.no},
{"context_token", QUuid::createUuid().toString(QUuid::WithoutBraces)}};
if (preserved.status == "NOT_START" && preserved.id != run.orderId)
m_resetContext.insert("preserved_order_no", preserved.no);
return m_resetContext;
}
bool ProductionService::resetProduction(const QJsonObject &context, QString *error)
{
if (m_alarmOperationBusy || m_shutdownRequested || m_state == Terminating
|| (m_externalStopRequested && m_externalStopRequested->load())) {
if (error) *error = "清警、复位或退出操作正在进行,请稍后重试";
return false;
}
if (!context.value("field_confirmed").toBool() || m_resetContext.isEmpty()
|| !context.value("valid").toBool()
|| context.value("context_token") != m_resetContext.value("context_token")
|| context.value("run_id") != m_resetContext.value("run_id")
|| context.value("order_id") != m_resetContext.value("order_id")) {
if (error) *error = "复位确认已过期或尚未确认现场已停止,请重新选择";
return false;
}
ProductionRepository runs;
ProductionRunSnapshot run;
if (!runs.findActiveRun(&run, error)) return false;
const int confirmedRun = context.value("run_id").toInt();
const int confirmedOrder = context.value("order_id").toInt();
if (run.id > 0 && (run.id != confirmedRun || run.orderId != confirmedOrder)) {
if (error) *error = "活动生产任务已改变,请重新确认复位目标";
return false;
}
m_resetContext = QJsonObject();
m_resetInProgress = true;
m_alarmOperationBusy = true;
m_resetRequired = true;
m_terminationRequired = true;
m_state = Terminating;
// Consume an earlier ordinary stop, but never the worker's exit flag.
// Shutdown can be queued behind this reset after setting that atomic flag.
m_stopRequested.exchange(false);
m_timer->stop();
m_deviceService->stopPolling();
++m_actionGeneration;
++m_visionGeneration;
finishRecoveryCheck();
invalidateVision("操作员正在复位自动线");
m_actionCancelRequested = false;
m_deferredCancelCallback = DeviceService::OperationCallback();
m_runId = run.id;
m_orderId = run.orderId;
m_currentWorkpiece = Workpiece();
m_currentWorkpiece.processStep = run.processStep;
emit alarmOperationBusyChanged(true);
publishState();
QString resetError;
if (!saveVisionRecoveryState(true, &resetError)) {
finishReset(false, resetError);
return true;
}
if (run.id > 0) {
DatabaseManager &database = DatabaseManager::instance();
Order order;
QString latest;
bool active = false;
ProductionRunSnapshot lockedRun;
if (!database.transaction(&resetError)
|| !OrderRepository().findOrderForUpdate(run.orderId, &order, &resetError)
|| !runs.lockOrderRuns(run.orderId, &latest, &active, &resetError)
|| !runs.findActiveRun(&lockedRun, &resetError)
|| lockedRun.id != run.id || lockedRun.orderId != run.orderId
|| !runs.updateRun(run.id, "STOPPING", "TERMINATING", run.processStep,
run.currentWorkpieceId, run.timeoutState, run.recoveryRetryCount,
"操作员请求复位", &resetError)
|| !database.commit(&resetError)) {
database.rollback();
finishReset(false, resetError.isEmpty() ? "复位目标已改变" : resetError);
return true;
}
}
m_deviceService->resetProductionSignals(
[this](bool success, const QString &deviceError) {
if (m_destroying) return;
if (m_shutdownRequested) {
finishReset(false, "退出请求已中止复位,旧任务保留待下次复位");
m_deviceService->terminate([this](bool stopped, const QString &stopError) {
if (!m_destroying) finishShutdown(stopped, stopError);
});
return;
}
QString resetError = deviceError;
if (stopRequested()) {
finishReset(false, "收到新的终止请求,复位已中止,请重新确认");
return;
}
if (success) success = finalizeRun(false, true, &resetError, true);
ProductionRunSnapshot remaining;
if (success) {
success = ProductionRepository().findActiveRun(&remaining, &resetError);
if (success && remaining.id > 0) {
success = false;
resetError = "仍存在活动生产任务,不能完成复位";
}
}
if (success && stopRequested()) {
success = false;
resetError = "收到新的终止请求,请重新确认并完成复位";
}
if (success) success = saveVisionRecoveryState(false, &resetError);
finishReset(success, resetError);
});
return true;
}
void ProductionService::finishReset(bool success, const QString &error)
{
if (success) {
clearResetState();
} else {
m_lastError = error.isEmpty() ? "自动线复位失败,请重试" : error;
m_state = Error;
m_initialized = false;
m_visionNeedsReconcile = true;
}
m_resetInProgress = false;
m_alarmOperationBusy = false;
publishState();
publishVisionControl();
if (!m_shutdownRequested) m_deviceService->startPolling();
emit alarmOperationBusyChanged(false);
emit alarmOperationFinished(true, success, success
? "复位完成,当前待机,可启动订单" : m_lastError);
}
void ProductionService::clearResetState()
{
m_runId = 0;
m_orderId = 0;
m_currentWorkpiece = Workpiece();
m_checkpoint = ProductionActionCheckpoint();
m_currentTrayNo = 0;
m_currentSlotNo = 0;
m_timeoutState = Idle;
m_recoveryRetryCount = 0;
m_frontRawResult = 0;
m_frontResultState.clear();
m_inspectionMode.clear();
m_actionPending = false;
m_actionStarting = false;
m_actionPollPending = false;
m_actionCompleted = false;
m_actionError.clear();
m_actionCancelRequested = false;
m_deferredCancelCallback = DeviceService::OperationCallback();
m_inspectionStatusPending = false;
m_inspectionStatusReady = false;
m_inspectionRawResult = 0;
m_inspectionStatusError.clear();
m_starting = false;
m_plcStarted = false;
m_tickRunning = false;
m_pauseAfterCurrentAction = false;
m_recoveryCheckPending = false;
m_manualContext = QJsonObject();
m_recoveryKeepPaused = false;
m_recoveryIdleUnlock = false;
m_recoveryAlarmCode.clear();
m_targetReady = false;
m_targetWorkpieceId = 0;
m_targetSession.clear();
m_targetUsesVision = false;
m_forceNormalPick = false;
m_restoredRun = false;
m_visionStage = VisionStage::Idle;
m_visionRequest = QJsonObject();
m_visionResult = QJsonObject();
m_visionWorkpieceId = 0;
m_visionTrayNo = 0;
m_visionSession.clear();
m_visionIoPending = false;
m_visionSuspended = false;
m_visionCommitStarted = false;
m_visionNeedsReconcile = false;
m_visionTimer.invalidate();
m_heartbeatTimer.invalidate();
clearProcessTimer();
m_lastError.clear();
m_persistenceUncertain = false;
m_terminationRequired = false;
m_resetRequired = false;
m_initialized = true;
m_state = Idle;
m_deviceStatus = DeviceStatus();
m_deviceStatusValid = false;
emit deviceStatusChanged(m_deviceStatus);
emit currentWorkpieceChanged(0, QString());
emit currentWorkpieceSnapshotChanged(m_currentWorkpiece);
emit currentPositionChanged(0, 0);
reportVision("待机", "复位完成,可以启动新订单");
}
bool ProductionService::restoreActiveRun(QString *error)
{
if (m_resetInProgress || m_resetRequired) {
if (error) *error = "上次复位尚未完成,请通过清除报警重新复位";
publishState();
return false;
}
if (m_initialized) { publishState(); return true; }
QString restoreError;
const bool success = restoreRun(&restoreError);
@@ -1257,6 +1530,10 @@ void ProductionService::finishRecoveryCheck(const QString &error)
bool ProductionService::recover(QString *error)
{
if (m_alarmOperationBusy || m_resetRequired || m_shutdownRequested) {
if (error) *error = "清警或复位尚未完成,不能恢复旧任务";
return false;
}
if (m_terminationRequired) {
if (error) *error = "上次终止尚未完成,请重试终止,不能恢复生产";
return false;
@@ -1420,6 +1697,10 @@ bool ProductionService::saveManualDecision(const QJsonObject &decision, QString
bool ProductionService::submitRecoveryDecision(const QJsonObject &decision, QString *error)
{
if (m_alarmOperationBusy || m_resetRequired || m_shutdownRequested) {
if (error) *error = "清警或复位尚未完成,原恢复确认已失效";
return false;
}
if (!m_recoveryCheckPending || m_manualContext.isEmpty()) {
if (error) *error = "人工确认已失效,请重新点击恢复/重试";
if (!m_recoveryCheckPending) emit recoveryBusyChanged(false);
@@ -1674,6 +1955,10 @@ void ProductionService::resumeRecoveredState(State state)
bool ProductionService::markCurrentWorkpieceNg(QString *error)
{
if (m_alarmOperationBusy || m_resetRequired || m_shutdownRequested) {
if (error) *error = "清警或复位尚未完成,不能修改当前工件";
return false;
}
if (m_terminationRequired) {
if (error) *error = "终止尚未完成,请重试终止,不能继续处理工件";
return false;
@@ -2213,8 +2498,10 @@ void ProductionService::cancelPendingProductionAction(
return;
}
const quint64 generation = m_actionGeneration;
m_deviceService->cancelProductionAction(
[this, callback](bool success, const QString &error) {
[this, callback, generation](bool success, const QString &error) {
if (generation != m_actionGeneration) return;
if (success) {
m_actionPending = false;
m_actionCompleted = false;
@@ -2246,8 +2533,10 @@ void ProductionService::tryDeferredActionCancel()
return;
}
const quint64 generation = m_actionGeneration;
m_deviceService->cancelProductionAction(
[this, callback](bool success, const QString &error) {
[this, callback, generation](bool success, const QString &error) {
if (generation != m_actionGeneration) return;
if (success) {
m_actionPending = false;
m_actionCompleted = false;
@@ -2595,12 +2884,41 @@ bool ProductionService::finishCurrentWorkpiece(const QString &state,
return true;
}
bool ProductionService::finalizeRun(bool completed, bool finishOrder, QString *error)
bool ProductionService::finalizeRun(bool completed, bool finishOrder, QString *error, bool resetting)
{
DatabaseManager &database = DatabaseManager::instance();
if (!database.transaction(error)) return false;
OrderRepository orders;
ProductionRepository runs;
if (resetting) {
ProductionRunSnapshot active;
if (!runs.findActiveRun(&active, error)) {
database.rollback();
return false;
}
if (active.id == 0) {
// An earlier commit may already have deleted or closed the target.
// Never infer a new target from an unfinished order or stale IDs.
LogRecord log;
log.type = "操作日志";
log.module = "订单管理";
log.action = "复位自动线";
log.content = "无活动任务,清理遗留状态";
log.status = "成功";
if (!runs.clearActiveAlarms(QString(), "OPERATOR", error)
|| !LogRepository().insertLog(log, error)
|| !database.commit(error)) {
database.rollback();
return false;
}
return true;
}
if (active.id != m_runId || active.orderId != m_orderId) {
database.rollback();
if (error) *error = "活动生产任务已改变,不能复位其他订单";
return false;
}
}
Order order;
ProductionRunSnapshot run;
QString latestState;
@@ -2642,8 +2960,9 @@ bool ProductionService::finalizeRun(bool completed, bool finishOrder, QString *e
LogRecord log;
log.type = "操作日志";
log.module = "订单管理";
log.action = completed ? "完成订单" : (finishOrder ? "终止订单(剩余工件不再执行)" : "取消订单");
log.action = resetting ? "复位自动线" : (completed ? "完成订单" : (finishOrder ? "终止订单(剩余工件不再执行)" : "取消订单"));
log.content = log.action + ":" + order.no;
if (resetting) log.content += QString(";运行 ID:%1").arg(m_runId);
if (removeTerminatedOrder)
log.content += ";无测量记录,自动删除订单及关联数据并释放料位";
log.status = "成功";
@@ -2656,7 +2975,8 @@ bool ProductionService::finalizeRun(bool completed, bool finishOrder, QString *e
|| !runs.insertStateHistory(m_runId, m_orderId, m_currentWorkpiece.id,
stateName(), completed ? "COMPLETED" : "IDLE", m_currentWorkpiece.processStep,
completed ? "SYSTEM" : "OPERATOR", completed ? "COMPLETE" : "STOP", m_lastError, error)
|| !LogRepository().insertLog(log, error)) {
|| !LogRepository().insertLog(log, error)
|| (resetting && !runs.clearActiveAlarms(QString(), "OPERATOR", error))) {
database.rollback();
return false;
}
+14 -1
View File
@@ -58,6 +58,8 @@ public:
QString stateName;
bool terminationRequired = false;
bool visionRecoveryRequired = false;
bool resetInProgress = false;
bool resetRequired = false;
QString error;
};
Snapshot snapshot() const;
@@ -79,6 +81,8 @@ public:
bool stopOrder(int orderId, QString *error = nullptr);
bool stopConfirmedOrder(int runId, int orderId, QString *error = nullptr);
bool clearAlarm(QString *error = nullptr);
QJsonObject resetContext(QString *error = nullptr);
bool resetProduction(const QJsonObject &context, QString *error = nullptr);
bool recover(QString *error = nullptr);
bool submitRecoveryDecision(const QJsonObject &decision, QString *error = nullptr);
bool markCurrentWorkpieceNg(QString *error = nullptr);
@@ -109,6 +113,8 @@ signals:
void recoveryCompleted(const QString &mode);
void recoveryDecisionRequired(const QJsonObject &context);
void recoveryBusyChanged(bool busy);
void alarmOperationBusyChanged(bool busy);
void alarmOperationFinished(bool resetRequested, bool success, const QString &message);
void visionMeasureRequested(const QJsonObject &request);
void visionCancelRequested(const QString &requestId);
void visionCycleChanged(const QJsonObject &cycle);
@@ -169,7 +175,9 @@ private:
bool finishCurrentWorkpiece(const QString &state, const QString &processStep,
QString *error);
bool finishOrder(QString *error);
bool finalizeRun(bool completed, bool finishOrder, QString *error);
bool finalizeRun(bool completed, bool finishOrder, QString *error, bool resetting = false);
void finishReset(bool success, const QString &error);
void clearResetState();
void tickSimulation();
void tickProduction();
bool processProductionStep(State state, QString *error);
@@ -223,6 +231,11 @@ private:
State m_state;
bool m_initialized = false;
bool m_terminationRequired = false;
bool m_resetInProgress = false;
bool m_resetRequired = false;
bool m_alarmOperationBusy = false;
bool m_destroying = false;
QJsonObject m_resetContext;
State m_timeoutState;
int m_runId;
int m_orderId;
+25 -2
View File
@@ -52,6 +52,10 @@ void ProductionWorker::ensureService()
this, &ProductionWorker::recoveryDecisionRequired);
connect(m_service, &ProductionService::recoveryBusyChanged,
this, &ProductionWorker::recoveryBusyChanged);
connect(m_service, &ProductionService::alarmOperationBusyChanged,
this, &ProductionWorker::alarmOperationBusyChanged);
connect(m_service, &ProductionService::alarmOperationFinished,
this, &ProductionWorker::alarmOperationFinished);
}
void ProductionWorker::requestStopImmediate()
@@ -123,7 +127,22 @@ void ProductionWorker::requestClearAlarm()
{
ensureService();
QString error;
reportFailure(m_service->clearAlarm(&error), error);
if (!m_service->clearAlarm(&error))
emit alarmOperationFinished(false, false, error);
}
void ProductionWorker::requestResetContext()
{
ensureService();
emit resetContextReady(m_service->resetContext());
}
void ProductionWorker::requestReset(const QJsonObject &context)
{
ensureService();
QString error;
if (!m_service->resetProduction(context, &error))
emit alarmOperationFinished(true, false, error);
}
void ProductionWorker::requestRecover()
@@ -160,10 +179,14 @@ void ProductionWorker::requestRestore()
void ProductionWorker::requestShutdown()
{
ensureService();
const bool resetShutdown = m_service->snapshot().resetRequired;
m_service->shutdown(
[this](bool success, const QString &error) {
[this, resetShutdown](bool success, const QString &error) {
if (!success) {
reportFailure(false, error);
// Reset has already persisted its unfinished marker. Report
// cleanup failure but let the worker exit with that marker.
if (resetShutdown) emit shutdownReady();
return;
}
emit shutdownReady();
+5
View File
@@ -21,6 +21,8 @@ public slots:
void requestStop();
void requestStopOrder(int runId, int orderId);
void requestClearAlarm();
void requestResetContext();
void requestReset(const QJsonObject &context);
void requestRecover();
void requestRecoveryDecision(const QJsonObject &decision);
void requestMarkNg();
@@ -47,6 +49,9 @@ signals:
void recoveryCompleted(const QString &mode);
void recoveryDecisionRequired(const QJsonObject &context);
void recoveryBusyChanged(bool busy);
void resetContextReady(const QJsonObject &context);
void alarmOperationBusyChanged(bool busy);
void alarmOperationFinished(bool resetRequested, bool success, const QString &message);
void shutdownReady();
void visionMeasureRequested(const QJsonObject &request);
void visionCancelRequested(const QString &requestId);