189 lines
6.3 KiB
C++
189 lines
6.3 KiB
C++
#ifndef PRODUCTIONSERVICE_H
|
|
#define PRODUCTIONSERVICE_H
|
|
|
|
#include "deviceservice.h"
|
|
|
|
#include "../models/workpiece.h"
|
|
|
|
#include <QElapsedTimer>
|
|
#include <QObject>
|
|
#include <atomic>
|
|
|
|
class QTimer;
|
|
|
|
class ProductionService : public QObject
|
|
{
|
|
Q_OBJECT
|
|
|
|
public:
|
|
enum State
|
|
{
|
|
Idle,
|
|
Preparing,
|
|
WaitingDeviceReady,
|
|
RackPicking,
|
|
FrontLoading,
|
|
Loading,
|
|
FrontChecking,
|
|
FrontUnloading,
|
|
Flipping,
|
|
BackLoading,
|
|
BackChecking,
|
|
BackUnloading,
|
|
RackPlacing,
|
|
Judging,
|
|
Unloading,
|
|
Paused,
|
|
Error,
|
|
Completed,
|
|
TimeoutWarning,
|
|
TimeoutPaused,
|
|
RecoveryChecking,
|
|
ManualConfirm,
|
|
Retrying,
|
|
Terminating
|
|
};
|
|
Q_ENUM(State)
|
|
|
|
static ProductionService &instance();
|
|
|
|
State state() const;
|
|
QString stateName() const;
|
|
int orderId() const;
|
|
int currentTrayNo() const;
|
|
int currentSlotNo() const;
|
|
QString inspectionMode() const;
|
|
Workpiece currentWorkpiece() const;
|
|
|
|
bool start(int orderId, QString *error = nullptr);
|
|
bool pause(QString *error = nullptr);
|
|
bool resume(QString *error = nullptr);
|
|
bool stop(QString *error = nullptr);
|
|
bool reset(QString *error = nullptr);
|
|
bool clearAlarm(QString *error = nullptr);
|
|
bool recover(QString *error = nullptr);
|
|
bool retry(QString *error = nullptr);
|
|
bool markCurrentWorkpieceNg(QString *error = nullptr);
|
|
bool restoreActiveRun(QString *error = nullptr);
|
|
void shutdown(const DeviceService::OperationCallback &callback);
|
|
void setExternalStopFlag(std::atomic_bool *flag);
|
|
void requestStopImmediate();
|
|
|
|
signals:
|
|
void stateChanged(ProductionService::State state, const QString &name);
|
|
void deviceStatusChanged(const DeviceStatus &status);
|
|
void deviceCommunicationError(const QString &error);
|
|
void currentWorkpieceChanged(int workpieceId, const QString &barcode);
|
|
void currentWorkpieceSnapshotChanged(const Workpiece &workpiece);
|
|
void currentPositionChanged(int trayNo, int slotNo);
|
|
void productionFinished(int orderId);
|
|
void productionError(const QString &error);
|
|
void alarmRaised(const QString &code, const QString &message);
|
|
void processElapsedChanged(const QString &processStep, int elapsedMs);
|
|
void timeoutWarning(const QString &state, int elapsedMs, int timeoutMs);
|
|
void timeoutRecoveryRequired(const QString &state, const QString &message);
|
|
void recoveryCompleted(const QString &mode);
|
|
|
|
private slots:
|
|
void tick();
|
|
void onDeviceStatusChanged(const DeviceStatus &status);
|
|
void onCommunicationError(const QString &error);
|
|
|
|
public:
|
|
explicit ProductionService(QObject *parent = nullptr);
|
|
|
|
private:
|
|
ProductionService(const ProductionService &) = delete;
|
|
ProductionService &operator=(const ProductionService &) = delete;
|
|
|
|
void setState(State state);
|
|
bool claimNextWorkpiece(QString *error);
|
|
bool updateCurrentProcessStep(const QString &processStep, QString *error,
|
|
bool forceRestart = false);
|
|
bool finishCurrentWorkpiece(const QString &state, const QString &processStep,
|
|
QString *error);
|
|
bool finishOrder(QString *error);
|
|
void tickSimulation();
|
|
void tickProduction();
|
|
bool processProductionStep(State state, QString *error);
|
|
bool recoverFromPlcStep(QString *error);
|
|
bool processProductionAction(DeviceService::ProductionAction action,
|
|
bool *completed, QString *error);
|
|
void cancelPendingProductionAction(
|
|
const DeviceService::OperationCallback &callback);
|
|
void tryDeferredActionCancel();
|
|
bool executeCurrentAction(DeviceService::ProductionAction action,
|
|
const QString &nextProcessStep,
|
|
State nextState, QString *error);
|
|
bool recordInspection(const QString &side, int rawResult,
|
|
const QString &resultState, QString *error);
|
|
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();
|
|
void clearProcessTimer();
|
|
int currentProcessElapsedMs() const;
|
|
void emitProcessElapsed();
|
|
void persistRecoveryAction(const QString &action, const QString &message);
|
|
bool isTimedState(State state) const;
|
|
QString stateNameForState(State state) const;
|
|
QString processStepForState(State state) const;
|
|
State stateForProcessStep(const QString &processStep) const;
|
|
QString runStateForState(State state) const;
|
|
State stateFromName(const QString &name) const;
|
|
void persistAlarm(const QString &code, const QString &message);
|
|
void persistDeviceStatus(const DeviceStatus &status);
|
|
void fail(const QString &error);
|
|
void finishShutdown(bool success, const QString &error);
|
|
bool isActive() const;
|
|
bool stopRequested() const;
|
|
|
|
QTimer *m_timer;
|
|
DeviceService *m_deviceService;
|
|
QElapsedTimer m_stateTimer;
|
|
QElapsedTimer m_processTimer;
|
|
QElapsedTimer m_heartbeatTimer;
|
|
State m_state;
|
|
State m_timeoutState;
|
|
int m_runId;
|
|
int m_orderId;
|
|
Workpiece m_currentWorkpiece;
|
|
DeviceStatus m_deviceStatus;
|
|
bool m_deviceStatusValid;
|
|
int m_recoveryRetryCount;
|
|
int m_processElapsedMs;
|
|
int m_currentTrayNo;
|
|
int m_currentSlotNo;
|
|
int m_frontRawResult;
|
|
bool m_actionPending;
|
|
bool m_actionStarting;
|
|
bool m_actionPollPending;
|
|
bool m_actionCompleted;
|
|
QString m_actionError;
|
|
bool m_actionCancelRequested;
|
|
DeviceService::OperationCallback m_deferredCancelCallback;
|
|
bool m_inspectionStatusPending;
|
|
bool m_inspectionStatusReady;
|
|
QString m_inspectionStatusError;
|
|
bool m_starting;
|
|
bool m_plcStarted;
|
|
bool m_tickRunning;
|
|
DeviceService::ProductionAction m_pendingAction;
|
|
QString m_inspectionMode;
|
|
QString m_frontResultState;
|
|
QString m_currentProcessStep;
|
|
QString m_lastError;
|
|
std::atomic_bool m_stopRequested;
|
|
std::atomic_bool *m_externalStopRequested;
|
|
bool m_shutdownRequested;
|
|
DeviceService::OperationCallback m_shutdownCallback;
|
|
};
|
|
|
|
Q_DECLARE_METATYPE(ProductionService::State)
|
|
Q_DECLARE_METATYPE(Workpiece)
|
|
|
|
#endif // PRODUCTIONSERVICE_H
|