2026-08-20 11:22:55 +08:00
|
|
|
#include "productionworker.h"
|
|
|
|
|
|
|
|
|
|
ProductionWorker::ProductionWorker(QObject *parent)
|
|
|
|
|
: QObject(parent)
|
|
|
|
|
, m_service(nullptr)
|
2026-08-23 09:41:44 +08:00
|
|
|
, m_stopRequested(false)
|
2026-08-20 11:22:55 +08:00
|
|
|
{
|
2026-11-06 15:49:33 +08:00
|
|
|
qRegisterMetaType<ProductionService::Snapshot>("ProductionService::Snapshot");
|
2026-11-09 18:46:26 +08:00
|
|
|
qRegisterMetaType<Workpiece>("Workpiece");
|
2026-08-20 11:22:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ProductionWorker::ensureService()
|
|
|
|
|
{
|
|
|
|
|
if (m_service) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_service = new ProductionService(this);
|
2026-11-06 15:49:33 +08:00
|
|
|
connect(m_service, &ProductionService::snapshotChanged,
|
|
|
|
|
this, &ProductionWorker::snapshotChanged);
|
2026-08-23 09:41:44 +08:00
|
|
|
m_service->setExternalStopFlag(&m_stopRequested);
|
2026-10-27 22:16:55 +08:00
|
|
|
connect(m_service, &ProductionService::visionMeasureRequested, this, &ProductionWorker::visionMeasureRequested);
|
|
|
|
|
connect(m_service, &ProductionService::visionCancelRequested, this, &ProductionWorker::visionCancelRequested);
|
|
|
|
|
connect(m_service, &ProductionService::visionCycleChanged, this, &ProductionWorker::visionCycleChanged);
|
|
|
|
|
connect(m_service, &ProductionService::visionProductionActiveChanged, this, &ProductionWorker::visionProductionActiveChanged);
|
2026-08-20 11:22:55 +08:00
|
|
|
connect(m_service, &ProductionService::stateChanged,
|
|
|
|
|
this, &ProductionWorker::stateChanged);
|
2026-08-23 09:41:44 +08:00
|
|
|
connect(m_service, &ProductionService::deviceStatusChanged,
|
|
|
|
|
this, &ProductionWorker::deviceStatusChanged);
|
|
|
|
|
connect(m_service, &ProductionService::deviceCommunicationError,
|
|
|
|
|
this, &ProductionWorker::deviceCommunicationError);
|
2026-08-20 11:22:55 +08:00
|
|
|
connect(m_service, &ProductionService::currentWorkpieceChanged,
|
|
|
|
|
this, &ProductionWorker::currentWorkpieceChanged);
|
|
|
|
|
connect(m_service, &ProductionService::currentWorkpieceSnapshotChanged,
|
|
|
|
|
this, &ProductionWorker::currentWorkpieceSnapshotChanged);
|
|
|
|
|
connect(m_service, &ProductionService::currentPositionChanged,
|
|
|
|
|
this, &ProductionWorker::currentPositionChanged);
|
|
|
|
|
connect(m_service, &ProductionService::productionFinished,
|
|
|
|
|
this, &ProductionWorker::productionFinished);
|
|
|
|
|
connect(m_service, &ProductionService::productionError,
|
|
|
|
|
this, &ProductionWorker::productionError);
|
|
|
|
|
connect(m_service, &ProductionService::alarmRaised,
|
|
|
|
|
this, &ProductionWorker::alarmRaised);
|
|
|
|
|
connect(m_service, &ProductionService::processElapsedChanged,
|
|
|
|
|
this, &ProductionWorker::processElapsedChanged);
|
|
|
|
|
connect(m_service, &ProductionService::timeoutWarning,
|
|
|
|
|
this, &ProductionWorker::timeoutWarning);
|
|
|
|
|
connect(m_service, &ProductionService::timeoutRecoveryRequired,
|
|
|
|
|
this, &ProductionWorker::timeoutRecoveryRequired);
|
|
|
|
|
connect(m_service, &ProductionService::recoveryCompleted,
|
|
|
|
|
this, &ProductionWorker::recoveryCompleted);
|
2026-10-29 21:12:15 +08:00
|
|
|
connect(m_service, &ProductionService::recoveryDecisionRequired,
|
|
|
|
|
this, &ProductionWorker::recoveryDecisionRequired);
|
|
|
|
|
connect(m_service, &ProductionService::recoveryBusyChanged,
|
|
|
|
|
this, &ProductionWorker::recoveryBusyChanged);
|
2026-11-09 11:05:12 +08:00
|
|
|
connect(m_service, &ProductionService::alarmOperationBusyChanged,
|
|
|
|
|
this, &ProductionWorker::alarmOperationBusyChanged);
|
|
|
|
|
connect(m_service, &ProductionService::alarmOperationFinished,
|
|
|
|
|
this, &ProductionWorker::alarmOperationFinished);
|
2026-08-20 11:22:55 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
void ProductionWorker::requestStopImmediate()
|
|
|
|
|
{
|
|
|
|
|
m_stopRequested.store(true);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-27 22:16:55 +08:00
|
|
|
void ProductionWorker::updateVisionAvailability(bool ready, bool busy, const QString &session)
|
|
|
|
|
{
|
|
|
|
|
ensureService();
|
|
|
|
|
m_service->onVisionAvailability(ready, busy, session);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ProductionWorker::receiveVisionResult(const QJsonObject &result)
|
|
|
|
|
{
|
|
|
|
|
ensureService();
|
|
|
|
|
m_service->onVisionResult(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ProductionWorker::requestVisionSnapshot()
|
|
|
|
|
{
|
|
|
|
|
ensureService();
|
|
|
|
|
m_service->publishVisionSnapshot();
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-20 11:22:55 +08:00
|
|
|
void ProductionWorker::reportFailure(bool success, const QString &error)
|
|
|
|
|
{
|
|
|
|
|
if (!success && !error.isEmpty()) {
|
|
|
|
|
emit productionError(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ProductionWorker::requestStart(int orderId)
|
|
|
|
|
{
|
|
|
|
|
ensureService();
|
|
|
|
|
QString error;
|
|
|
|
|
reportFailure(m_service->start(orderId, &error), error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ProductionWorker::requestPause()
|
|
|
|
|
{
|
|
|
|
|
ensureService();
|
|
|
|
|
QString error;
|
|
|
|
|
reportFailure(m_service->pause(&error), error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ProductionWorker::requestResume()
|
|
|
|
|
{
|
|
|
|
|
ensureService();
|
|
|
|
|
QString error;
|
|
|
|
|
reportFailure(m_service->resume(&error), error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ProductionWorker::requestStop()
|
|
|
|
|
{
|
|
|
|
|
ensureService();
|
|
|
|
|
QString error;
|
|
|
|
|
reportFailure(m_service->stop(&error), error);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-06 15:49:33 +08:00
|
|
|
void ProductionWorker::requestStopOrder(int runId, int orderId)
|
2026-10-30 16:43:24 +08:00
|
|
|
{
|
|
|
|
|
ensureService();
|
|
|
|
|
QString error;
|
2026-11-06 15:49:33 +08:00
|
|
|
reportFailure(m_service->stopConfirmedOrder(runId, orderId, &error), error);
|
2026-10-30 16:43:24 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
void ProductionWorker::requestClearAlarm()
|
|
|
|
|
{
|
|
|
|
|
ensureService();
|
|
|
|
|
QString error;
|
2026-11-09 11:05:12 +08:00
|
|
|
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);
|
2026-08-23 09:41:44 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-20 11:22:55 +08:00
|
|
|
void ProductionWorker::requestRecover()
|
|
|
|
|
{
|
|
|
|
|
ensureService();
|
|
|
|
|
QString error;
|
2026-11-06 15:49:33 +08:00
|
|
|
const bool success = m_service->snapshot().initialized
|
|
|
|
|
? m_service->recover(&error) : m_service->restoreActiveRun(&error);
|
|
|
|
|
reportFailure(success, error);
|
2026-08-20 11:22:55 +08:00
|
|
|
}
|
|
|
|
|
|
2026-10-29 21:12:15 +08:00
|
|
|
void ProductionWorker::requestRecoveryDecision(const QJsonObject &decision)
|
|
|
|
|
{
|
|
|
|
|
ensureService();
|
|
|
|
|
QString error;
|
|
|
|
|
reportFailure(m_service->submitRecoveryDecision(decision, &error), error);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-20 11:22:55 +08:00
|
|
|
void ProductionWorker::requestMarkNg()
|
|
|
|
|
{
|
|
|
|
|
ensureService();
|
|
|
|
|
QString error;
|
|
|
|
|
reportFailure(m_service->markCurrentWorkpieceNg(&error), error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ProductionWorker::requestRestore()
|
|
|
|
|
{
|
|
|
|
|
ensureService();
|
|
|
|
|
QString error;
|
|
|
|
|
reportFailure(m_service->restoreActiveRun(&error), error);
|
2026-11-06 15:49:33 +08:00
|
|
|
emit recoveryBusyChanged(false);
|
2026-08-20 11:22:55 +08:00
|
|
|
}
|
2026-08-23 09:41:44 +08:00
|
|
|
|
|
|
|
|
void ProductionWorker::requestShutdown()
|
|
|
|
|
{
|
|
|
|
|
ensureService();
|
2026-11-09 11:05:12 +08:00
|
|
|
const bool resetShutdown = m_service->snapshot().resetRequired;
|
2026-08-23 09:41:44 +08:00
|
|
|
m_service->shutdown(
|
2026-11-09 11:05:12 +08:00
|
|
|
[this, resetShutdown](bool success, const QString &error) {
|
2026-08-23 09:41:44 +08:00
|
|
|
if (!success) {
|
|
|
|
|
reportFailure(false, error);
|
2026-11-09 11:05:12 +08:00
|
|
|
// Reset has already persisted its unfinished marker. Report
|
|
|
|
|
// cleanup failure but let the worker exit with that marker.
|
|
|
|
|
if (resetShutdown) emit shutdownReady();
|
2026-08-23 09:41:44 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
emit shutdownReady();
|
|
|
|
|
});
|
|
|
|
|
}
|