订单流程修改--QThread事件循环导致的逻辑修改
This commit is contained in:
+672
-218
File diff suppressed because it is too large
Load Diff
+56
-17
@@ -3,7 +3,10 @@
|
||||
|
||||
#include <QString>
|
||||
#include <QtGlobal>
|
||||
#include <QList>
|
||||
#include <QObject>
|
||||
#include <QPair>
|
||||
#include <functional>
|
||||
|
||||
struct DeviceStatus
|
||||
{
|
||||
@@ -26,6 +29,12 @@ class DeviceService : public QObject
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
using OperationCallback = std::function<void(bool, const QString &)>;
|
||||
using StatusCallback =
|
||||
std::function<void(bool, const DeviceStatus &, const QString &)>;
|
||||
using ActionCallback =
|
||||
std::function<void(bool, bool, const QString &)>;
|
||||
|
||||
enum ProductionAction
|
||||
{
|
||||
RackPick,
|
||||
@@ -38,38 +47,68 @@ public:
|
||||
RackPlace
|
||||
};
|
||||
|
||||
static DeviceService &instance();
|
||||
explicit DeviceService(QObject *parent = nullptr);
|
||||
|
||||
void startPolling();
|
||||
void stopPolling();
|
||||
bool isPolling() const;
|
||||
|
||||
bool readStatus(DeviceStatus *status, QString *error = nullptr);
|
||||
bool start(QString *error = nullptr);
|
||||
bool pause(QString *error = nullptr);
|
||||
bool reset(QString *error = nullptr);
|
||||
bool clearAlarm(QString *error = nullptr);
|
||||
bool setInspectionMode(const QString &mode, QString *error = nullptr);
|
||||
bool executeProductionAction(ProductionAction action, int trayNo,
|
||||
int slotNo, QString *error = nullptr);
|
||||
void requestStatus(const StatusCallback &callback);
|
||||
void start(const OperationCallback &callback);
|
||||
void pause(const OperationCallback &callback);
|
||||
void reset(const OperationCallback &callback);
|
||||
void clearAlarm(const OperationCallback &callback);
|
||||
void setInspectionMode(const QString &mode,
|
||||
const OperationCallback &callback);
|
||||
void terminate(const OperationCallback &callback);
|
||||
void beginProductionAction(ProductionAction action, int trayNo,
|
||||
int slotNo, const OperationCallback &callback);
|
||||
void pollProductionAction(const ActionCallback &callback);
|
||||
void cancelProductionAction(const OperationCallback &callback);
|
||||
bool productionActionActive() const;
|
||||
bool productionActionBusy() const;
|
||||
|
||||
signals:
|
||||
void statusChanged(const DeviceStatus &status);
|
||||
void communicationError(const QString &error);
|
||||
|
||||
private:
|
||||
explicit DeviceService(QObject *parent = nullptr);
|
||||
DeviceService(const DeviceService &) = delete;
|
||||
DeviceService &operator=(const DeviceService &) = delete;
|
||||
|
||||
void pollStatus();
|
||||
DeviceStatus simulatedStatus() const;
|
||||
bool connectWithRetry(QString *error);
|
||||
bool readStatusOnce(DeviceStatus *status, QString *error);
|
||||
bool executeCommand(int commandAddress, int feedbackAddress, QString *error);
|
||||
bool waitForFeedback(int address, QString *error);
|
||||
void readStatusStep(int index, const DeviceStatus &status);
|
||||
void finishStatusRequest(bool success, const DeviceStatus &status,
|
||||
const QString &error);
|
||||
void executeCommand(int commandAddress, int feedbackAddress,
|
||||
const OperationCallback &callback,
|
||||
bool allowDuringTermination = false);
|
||||
void writeCommandAndWaitFeedback(int commandAddress, int feedbackAddress,
|
||||
const OperationCallback &callback,
|
||||
bool allowDuringTermination = false);
|
||||
void pollFeedback(int commandAddress, int feedbackAddress,
|
||||
const OperationCallback &callback,
|
||||
bool allowDuringTermination = false);
|
||||
void clearSignals(int commandAddress, int feedbackAddress,
|
||||
const OperationCallback &callback);
|
||||
void clearActionSignals(const OperationCallback &callback);
|
||||
void clearNextTerminationSignal();
|
||||
void finishTermination(bool success, const QString &error);
|
||||
void finishActionPoll(bool completed, const ActionCallback &callback);
|
||||
|
||||
QTimer *m_pollTimer;
|
||||
class ModbusTcpManager *m_modbus;
|
||||
bool m_statusRequestPending;
|
||||
QList<StatusCallback> m_statusCallbacks;
|
||||
bool m_controlRequestPending;
|
||||
bool m_productionActionActive;
|
||||
bool m_productionActionBusy;
|
||||
int m_productionCommandAddress;
|
||||
int m_productionFeedbackAddress;
|
||||
bool m_terminationActive;
|
||||
QList<QPair<int, int>> m_terminationSignals;
|
||||
int m_terminationSignalIndex;
|
||||
OperationCallback m_terminationCallback;
|
||||
int m_controlCommandAddress;
|
||||
int m_controlFeedbackAddress;
|
||||
};
|
||||
|
||||
#endif // DEVICESERVICE_H
|
||||
|
||||
+712
-187
File diff suppressed because it is too large
Load Diff
@@ -7,6 +7,7 @@
|
||||
|
||||
#include <QElapsedTimer>
|
||||
#include <QObject>
|
||||
#include <atomic>
|
||||
|
||||
class QTimer;
|
||||
|
||||
@@ -39,7 +40,8 @@ public:
|
||||
TimeoutPaused,
|
||||
RecoveryChecking,
|
||||
ManualConfirm,
|
||||
Retrying
|
||||
Retrying,
|
||||
Terminating
|
||||
};
|
||||
Q_ENUM(State)
|
||||
|
||||
@@ -58,13 +60,19 @@ public:
|
||||
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);
|
||||
@@ -99,6 +107,11 @@ private:
|
||||
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);
|
||||
@@ -118,14 +131,18 @@ private:
|
||||
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;
|
||||
@@ -141,10 +158,28 @@ private:
|
||||
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)
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
ProductionWorker::ProductionWorker(QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_service(nullptr)
|
||||
, m_stopRequested(false)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -13,8 +14,13 @@ void ProductionWorker::ensureService()
|
||||
}
|
||||
|
||||
m_service = new ProductionService(this);
|
||||
m_service->setExternalStopFlag(&m_stopRequested);
|
||||
connect(m_service, &ProductionService::stateChanged,
|
||||
this, &ProductionWorker::stateChanged);
|
||||
connect(m_service, &ProductionService::deviceStatusChanged,
|
||||
this, &ProductionWorker::deviceStatusChanged);
|
||||
connect(m_service, &ProductionService::deviceCommunicationError,
|
||||
this, &ProductionWorker::deviceCommunicationError);
|
||||
connect(m_service, &ProductionService::currentWorkpieceChanged,
|
||||
this, &ProductionWorker::currentWorkpieceChanged);
|
||||
connect(m_service, &ProductionService::currentWorkpieceSnapshotChanged,
|
||||
@@ -37,6 +43,11 @@ void ProductionWorker::ensureService()
|
||||
this, &ProductionWorker::recoveryCompleted);
|
||||
}
|
||||
|
||||
void ProductionWorker::requestStopImmediate()
|
||||
{
|
||||
m_stopRequested.store(true);
|
||||
}
|
||||
|
||||
void ProductionWorker::reportFailure(bool success, const QString &error)
|
||||
{
|
||||
if (!success && !error.isEmpty()) {
|
||||
@@ -79,6 +90,13 @@ void ProductionWorker::requestReset()
|
||||
reportFailure(m_service->reset(&error), error);
|
||||
}
|
||||
|
||||
void ProductionWorker::requestClearAlarm()
|
||||
{
|
||||
ensureService();
|
||||
QString error;
|
||||
reportFailure(m_service->clearAlarm(&error), error);
|
||||
}
|
||||
|
||||
void ProductionWorker::requestRecover()
|
||||
{
|
||||
ensureService();
|
||||
@@ -106,3 +124,16 @@ void ProductionWorker::requestRestore()
|
||||
QString error;
|
||||
reportFailure(m_service->restoreActiveRun(&error), error);
|
||||
}
|
||||
|
||||
void ProductionWorker::requestShutdown()
|
||||
{
|
||||
ensureService();
|
||||
m_service->shutdown(
|
||||
[this](bool success, const QString &error) {
|
||||
if (!success) {
|
||||
reportFailure(false, error);
|
||||
return;
|
||||
}
|
||||
emit shutdownReady();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "productionservice.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <atomic>
|
||||
|
||||
class ProductionWorker : public QObject
|
||||
{
|
||||
@@ -11,6 +12,7 @@ class ProductionWorker : public QObject
|
||||
|
||||
public:
|
||||
explicit ProductionWorker(QObject *parent = nullptr);
|
||||
void requestStopImmediate();
|
||||
|
||||
public slots:
|
||||
void requestStart(int orderId);
|
||||
@@ -18,13 +20,17 @@ public slots:
|
||||
void requestResume();
|
||||
void requestStop();
|
||||
void requestReset();
|
||||
void requestClearAlarm();
|
||||
void requestRecover();
|
||||
void requestRetry();
|
||||
void requestMarkNg();
|
||||
void requestRestore();
|
||||
void requestShutdown();
|
||||
|
||||
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);
|
||||
@@ -35,12 +41,14 @@ signals:
|
||||
void timeoutWarning(const QString &state, int elapsedMs, int timeoutMs);
|
||||
void timeoutRecoveryRequired(const QString &state, const QString &message);
|
||||
void recoveryCompleted(const QString &mode);
|
||||
void shutdownReady();
|
||||
|
||||
private:
|
||||
void ensureService();
|
||||
void reportFailure(bool success, const QString &error);
|
||||
|
||||
ProductionService *m_service;
|
||||
std::atomic_bool m_stopRequested;
|
||||
};
|
||||
|
||||
#endif // PRODUCTIONWORKER_H
|
||||
|
||||
Reference in New Issue
Block a user