1171 lines
50 KiB
C++
1171 lines
50 KiB
C++
#include "dashboardpage.h"
|
||
#include "ui_dashboardpage.h"
|
||
|
||
#include "../services/orderservice.h"
|
||
#include "../services/trayservice.h"
|
||
#include "../services/workpieceservice.h"
|
||
#include "../repositories/productionrepository.h"
|
||
#include "uihelpers.h"
|
||
#include "../services/visionservice.h"
|
||
#include <QJsonDocument>
|
||
#include <QJsonArray>
|
||
|
||
#include <QAbstractItemView>
|
||
#include <QDateTime>
|
||
#include <QCheckBox>
|
||
#include <QComboBox>
|
||
#include <QDialog>
|
||
#include <QDialogButtonBox>
|
||
#include <QFormLayout>
|
||
#include <QGridLayout>
|
||
#include <QGroupBox>
|
||
#include <QLabel>
|
||
#include <QLineEdit>
|
||
#include <QMessageBox>
|
||
#include <QSet>
|
||
#include <QtGlobal>
|
||
#include <QPushButton>
|
||
#include <QMetaObject>
|
||
#include <QResizeEvent>
|
||
#include <QSizePolicy>
|
||
#include <QTableWidgetItem>
|
||
#include <QTimer>
|
||
#include <QThread>
|
||
#include <QVBoxLayout>
|
||
|
||
namespace {
|
||
|
||
QString displayState(const QString &state)
|
||
{
|
||
if (state == "IDLE") return "待机";
|
||
if (state == "PREPARING") return "准备中";
|
||
if (state == "WAITING_DEVICE_READY") return "等待设备就绪";
|
||
if (state == "PHOTO_MOVING") return "移动到拍照位置";
|
||
if (state == "VISION_PROCESSING") return "相机视觉处理";
|
||
if (state == "RACK_PICKING") return "料架取料";
|
||
if (state == "FRONT_LOADING") return "正面上料";
|
||
if (state == "LOADING") return "上料";
|
||
if (state == "FRONT_CHECKING") return "正面检测";
|
||
if (state == "FRONT_UNLOADING") return "正面下料";
|
||
if (state == "FLIPPING") return "翻面";
|
||
if (state == "BACK_LOADING") return "反面上料";
|
||
if (state == "BACK_CHECKING") return "反面检测";
|
||
if (state == "BACK_UNLOADING") return "反面下料";
|
||
if (state == "RACK_PLACING") return "料架放料";
|
||
if (state == "JUDGING") return "结果判定";
|
||
if (state == "UNLOADING") return "下料";
|
||
if (state == "PAUSED") return "已暂停";
|
||
if (state == "ERROR") return "故障";
|
||
if (state == "COMPLETED") return "已完成";
|
||
if (state == "TIMEOUT_WARNING") return "工序耗时提示";
|
||
if (state == "TIMEOUT_PAUSED") return "等待人工恢复";
|
||
if (state == "RECOVERY_CHECKING") return "恢复检查";
|
||
if (state == "MANUAL_CONFIRM") return "等待人工确认";
|
||
if (state == "RETRYING") return "重试中";
|
||
if (state == "TERMINATING") return "终止中";
|
||
return state.isEmpty() ? "未采集" : state;
|
||
}
|
||
|
||
QString displayProcessStep(const QString &step)
|
||
{
|
||
return displayState(step);
|
||
}
|
||
|
||
QString displayRecoveryAction(const QString &action)
|
||
{
|
||
if (action == "PHOTO_MOVE") return "移动到拍照位置";
|
||
if (action == "VISION") return "相机视觉处理";
|
||
if (action == "RACK_PICK") return "料架取料";
|
||
if (action == "FRONT_LOAD") return "正面上料";
|
||
if (action == "INSPECT") return "检测";
|
||
if (action == "FRONT_UNLOAD") return "正面下料";
|
||
if (action == "FLIP") return "翻面";
|
||
if (action == "BACK_LOAD") return "反面上料";
|
||
if (action == "BACK_UNLOAD") return "反面下料";
|
||
if (action == "RACK_PLACE") return "料架放料";
|
||
return action.isEmpty() ? "未知" : action;
|
||
}
|
||
|
||
QString displayActionPhase(const QString &phase)
|
||
{
|
||
if (phase == "NOT_SENT") return "命令尚未发送";
|
||
if (phase == "SENDING") return "命令发送结果待核对";
|
||
if (phase == "COMMAND_CONFIRMED") return "命令写入已确认";
|
||
if (phase == "FEEDBACK_RECEIVED") return "已收到完成反馈";
|
||
if (phase == "SIGNALS_CLEARED") return "完成握手已清零";
|
||
if (phase == "STEP_COMMITTED") return "下一工序已保存";
|
||
if (phase == "RESULT_WRITING") return "坐标写入结果待核对";
|
||
if (phase == "RESULT_CONFIRMED") return "坐标写入已确认";
|
||
if (phase == "MODE_WRITING") return "取料模式写入结果待核对";
|
||
if (phase == "MODE_CONFIRMED") return "取料模式写入已确认";
|
||
return phase.isEmpty() ? "未知" : phase;
|
||
}
|
||
|
||
QString displayProcessStatus(const QString &status)
|
||
{
|
||
if (status == "RUNNING") return "执行中";
|
||
if (status == "COMPLETED") return "已完成";
|
||
if (status == "FAILED") return "失败";
|
||
if (status == "PAUSED") return "已暂停";
|
||
if (status == "CANCELED") return "已取消";
|
||
return status.isEmpty() ? "未采集" : status;
|
||
}
|
||
|
||
QString displayOrderStatus(const QString &status)
|
||
{
|
||
if (status == "NOT_START") return "待启动";
|
||
if (status == "RUNNING") return "加工中";
|
||
if (status == "PAUSED") return "已暂停";
|
||
if (status == "FINISHED" || status == "COMPLETED") return "已完成";
|
||
if (status == "CANCELED") return "已取消";
|
||
return status.isEmpty() ? "未采集" : status;
|
||
}
|
||
|
||
void updateStatusCell(QLabel *cell, const QString &text, const QString &color,
|
||
bool highlighted = false)
|
||
{
|
||
if (!cell) {
|
||
return;
|
||
}
|
||
cell->setText(text);
|
||
cell->setStyleSheet(QString("QLabel { background: %1; border: %2; "
|
||
"border-radius: 3px; color: white; font-size: 11px; }")
|
||
.arg(color)
|
||
.arg(highlighted ? "3px solid #ffb300" : "1px solid #ffffff"));
|
||
}
|
||
|
||
QString boolStatus(bool valid, bool value, const QString &yes, const QString &no)
|
||
{
|
||
return !valid ? "未采集" : value ? yes : no;
|
||
}
|
||
|
||
}
|
||
|
||
DashboardPage::DashboardPage(QWidget *parent)
|
||
: QWidget(parent)
|
||
, ui(new Ui::DashboardPage)
|
||
, rackBox(nullptr)
|
||
, deviceStatusLabel(new QLabel("PLC:未采集 | 机器人:未采集 | 安全回路:未采集", this))
|
||
, inspectionMachineCell(nullptr)
|
||
, robotCell(nullptr)
|
||
, robotControlCabinetCell(nullptr)
|
||
, mainControlCabinetCell(nullptr)
|
||
, safetyDoor1Cell(nullptr)
|
||
, safetyDoor2Cell(nullptr)
|
||
, safetyDoor3Cell(nullptr)
|
||
, alarmCell(nullptr)
|
||
, markNgButton(new QPushButton("标记 NG", this))
|
||
, stopButton(new QPushButton("终止", this))
|
||
, processBox(new QGroupBox("工序耗时", this))
|
||
, currentProcessLabel(new QLabel("-", processBox))
|
||
, processElapsedLabel(new QLabel("00:00:00.000", processBox))
|
||
, processTotalElapsedLabel(new QLabel("00:00:00.000", processBox))
|
||
, processTable(new QTableWidget(processBox))
|
||
, processRefreshTimer(new QTimer(this))
|
||
, currentProcessElapsedMs(0)
|
||
, productionThread(new QThread(this))
|
||
, productionWorker(new ProductionWorker)
|
||
, productionState(ProductionService::Idle)
|
||
, currentOrderId(0)
|
||
, currentOrderStartable(false)
|
||
, currentOrderResumable(false)
|
||
, productionTrayNo(0)
|
||
, productionSlotNo(0)
|
||
{
|
||
ui->setupUi(this);
|
||
qRegisterMetaType<QJsonObject>("QJsonObject");
|
||
|
||
productionWorker->moveToThread(productionThread);
|
||
connect(productionThread, &QThread::finished,
|
||
productionWorker, &QObject::deleteLater);
|
||
connect(productionWorker, &ProductionWorker::shutdownReady,
|
||
productionThread, &QThread::quit, Qt::DirectConnection);
|
||
|
||
const QList<QLabel *> values = QList<QLabel *>()
|
||
<< ui->planValue << ui->pendingValue << ui->frontValue
|
||
<< ui->backValue << ui->okValue << ui->ngValue;
|
||
for (QLabel *label : values) {
|
||
label->setStyleSheet("font-size: 26px; font-weight: bold; color: #00897b;");
|
||
}
|
||
|
||
ui->startButton->setObjectName("primaryButton");
|
||
ui->nextButton->setVisible(false);
|
||
ui->recoveryButton->setEnabled(false);
|
||
markNgButton->setObjectName("recoveryNgButton");
|
||
markNgButton->setVisible(false);
|
||
ui->buttonLayout->insertWidget(ui->buttonLayout->indexOf(ui->recoveryButton),
|
||
markNgButton);
|
||
stopButton->setObjectName("stopButton");
|
||
stopButton->setVisible(true);
|
||
ui->buttonLayout->insertWidget(6, stopButton);
|
||
deviceStatusLabel->setStyleSheet("color: #546e7a;");
|
||
ui->taskLayout->insertWidget(0, deviceStatusLabel);
|
||
|
||
QFormLayout *processSummaryLayout = new QFormLayout;
|
||
processSummaryLayout->addRow("当前工序:", currentProcessLabel);
|
||
processSummaryLayout->addRow("当前耗时:", processElapsedLabel);
|
||
processSummaryLayout->addRow("累计耗时:", processTotalElapsedLabel);
|
||
|
||
QVBoxLayout *processLayout = new QVBoxLayout(processBox);
|
||
processLayout->addLayout(processSummaryLayout);
|
||
processTable->setColumnCount(4);
|
||
processTable->setHorizontalHeaderLabels(
|
||
QStringList() << "工序" << "状态" << "次数" << "耗时");
|
||
UiHelpers::configureTableColumns(
|
||
processTable, QList<int>() << 180 << 120 << 70 << 100);
|
||
processTable->verticalHeader()->setVisible(false);
|
||
processTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||
processTable->setAlternatingRowColors(true);
|
||
processTable->setMinimumHeight(150);
|
||
processLayout->addWidget(processTable);
|
||
ui->taskLayout->insertWidget(6, processBox);
|
||
|
||
ui->recentLogTable->setColumnCount(5);
|
||
ui->recentLogTable->setHorizontalHeaderLabels(QStringList() << "时间" << "类型" << "模块" << "内容" << "状态");
|
||
UiHelpers::configureTableColumns(
|
||
ui->recentLogTable, QList<int>() << 80 << 100 << 100 << 300 << 80,
|
||
3, 60);
|
||
ui->recentLogTable->verticalHeader()->setVisible(false);
|
||
ui->recentLogTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||
ui->recentLogTable->setAlternatingRowColors(true);
|
||
|
||
ui->deviceLayout->setSpacing(10);
|
||
ui->deviceLayout->setColumnStretch(0, 1);
|
||
ui->deviceLayout->setColumnStretch(1, 1);
|
||
ui->deviceLayout->setColumnStretch(2, 2);
|
||
ui->deviceLayout->setColumnStretch(3, 2);
|
||
ui->deviceLayout->setColumnStretch(4, 2);
|
||
ui->deviceLayout->setColumnStretch(5, 1);
|
||
ui->deviceLayout->setRowStretch(0, 1);
|
||
ui->deviceLayout->setRowStretch(1, 1);
|
||
ui->deviceLayout->setRowStretch(2, 1);
|
||
ui->deviceLayout->setRowStretch(3, 1);
|
||
ui->deviceLayout->setRowStretch(4, 1);
|
||
ui->deviceLayout->setRowStretch(5, 1);
|
||
inspectionMachineCell = UiHelpers::makeStatusCell("检测机\n未采集", "#607d8b");
|
||
ui->deviceLayout->addWidget(inspectionMachineCell, 0, 0, 2, 2);
|
||
ui->deviceLayout->addWidget(UiHelpers::makeStatusCell("总控电脑", "#43a047"), 0, 3);
|
||
safetyDoor1Cell = UiHelpers::makeStatusCell("安全门 1\n未采集", "#607d8b");
|
||
ui->deviceLayout->addWidget(safetyDoor1Cell, 0, 4);
|
||
robotCell = UiHelpers::makeStatusCell("机器人\n未采集", "#607d8b");
|
||
robotCell->setFixedSize(82, 58);
|
||
ui->deviceLayout->addWidget(robotCell, 2, 3, 2, 2, Qt::AlignCenter);
|
||
robotControlCabinetCell = UiHelpers::makeStatusCell("机器人控制柜\n未采集", "#607d8b");
|
||
ui->deviceLayout->addWidget(robotControlCabinetCell, 3, 0);
|
||
mainControlCabinetCell = UiHelpers::makeStatusCell("总控柜\n未采集", "#607d8b");
|
||
ui->deviceLayout->addWidget(mainControlCabinetCell, 4, 0);
|
||
safetyDoor2Cell = UiHelpers::makeStatusCell("安全门 2\n未采集", "#607d8b");
|
||
ui->deviceLayout->addWidget(safetyDoor2Cell, 2, 5);
|
||
safetyDoor3Cell = UiHelpers::makeStatusCell("安全门 3\n未采集", "#607d8b");
|
||
ui->deviceLayout->addWidget(safetyDoor3Cell, 5, 0);
|
||
alarmCell = UiHelpers::makeStatusCell("PLC 报警\n未采集", "#607d8b");
|
||
ui->deviceLayout->addWidget(alarmCell, 5, 5);
|
||
|
||
rackBox = new QGroupBox("料架 2x3");
|
||
rackBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
|
||
QGridLayout *rackLayout = new QGridLayout(rackBox);
|
||
rackLayout->setContentsMargins(8, 18, 8, 8);
|
||
rackLayout->setSpacing(4);
|
||
for (int i = 0; i < 6; ++i) {
|
||
QLabel *trayCell = UiHelpers::makeSquareStatusCell(
|
||
QString("托盘 %1\n未采集").arg(i + 1), "#607d8b");
|
||
rackTrayCells.append(trayCell);
|
||
rackLayout->addWidget(trayCell, i / 3, i % 3, Qt::AlignCenter);
|
||
}
|
||
ui->deviceLayout->addWidget(rackBox, 4, 2, 2, 3);
|
||
updateRackSize();
|
||
|
||
connect(ui->startButton, &QPushButton::clicked,
|
||
this, &DashboardPage::startProduction);
|
||
connect(ui->pauseButton, &QPushButton::clicked,
|
||
this, &DashboardPage::pauseProduction);
|
||
connect(stopButton, &QPushButton::clicked,
|
||
this, &DashboardPage::stopProduction);
|
||
connect(ui->recoveryButton, &QPushButton::clicked,
|
||
this, &DashboardPage::recoverProduction);
|
||
connect(ui->clearAlarmButton, &QPushButton::clicked,
|
||
this, &DashboardPage::clearAlarm);
|
||
connect(markNgButton, &QPushButton::clicked,
|
||
this, &DashboardPage::markWorkpieceNg);
|
||
connect(productionWorker, &ProductionWorker::stateChanged,
|
||
this, &DashboardPage::onProductionStateChanged);
|
||
connect(productionWorker, &ProductionWorker::currentWorkpieceChanged,
|
||
this, &DashboardPage::onCurrentWorkpieceChanged);
|
||
connect(productionWorker, &ProductionWorker::currentWorkpieceSnapshotChanged,
|
||
this, &DashboardPage::onCurrentWorkpieceSnapshotChanged);
|
||
connect(productionWorker, &ProductionWorker::currentPositionChanged,
|
||
this, &DashboardPage::onCurrentPositionChanged);
|
||
connect(productionWorker, &ProductionWorker::productionError,
|
||
this, &DashboardPage::onProductionError);
|
||
connect(productionWorker, &ProductionWorker::productionFinished,
|
||
this, &DashboardPage::onProductionFinished);
|
||
connect(productionWorker, &ProductionWorker::processElapsedChanged,
|
||
this, &DashboardPage::onProcessElapsedChanged);
|
||
connect(productionWorker, &ProductionWorker::timeoutRecoveryRequired,
|
||
this, &DashboardPage::onTimeoutRecoveryRequired);
|
||
connect(productionWorker, &ProductionWorker::recoveryCompleted,
|
||
this, &DashboardPage::onRecoveryCompleted);
|
||
connect(productionWorker, &ProductionWorker::recoveryDecisionRequired,
|
||
this, &DashboardPage::onRecoveryDecisionRequired);
|
||
connect(productionWorker, &ProductionWorker::recoveryBusyChanged,
|
||
this, &DashboardPage::onRecoveryBusyChanged);
|
||
connect(this, &DashboardPage::recoveryDecisionSubmitted,
|
||
productionWorker, &ProductionWorker::requestRecoveryDecision,
|
||
Qt::QueuedConnection);
|
||
connect(productionWorker, &ProductionWorker::deviceStatusChanged,
|
||
this, &DashboardPage::onDeviceStatusChanged);
|
||
connect(productionWorker, &ProductionWorker::deviceCommunicationError,
|
||
this, [this](const QString &error) {
|
||
emit deviceCommunicationError(error);
|
||
});
|
||
|
||
productionThread->start();
|
||
QMetaObject::invokeMethod(productionWorker, "requestRestore",
|
||
Qt::QueuedConnection);
|
||
|
||
loadCurrentOrder();
|
||
processRefreshTimer->setInterval(1000);
|
||
connect(processRefreshTimer, &QTimer::timeout, this, [this]() {
|
||
refreshProcessRecords();
|
||
loadCurrentOrder();
|
||
});
|
||
processRefreshTimer->start();
|
||
refreshProcessRecords();
|
||
updateRackSummary();
|
||
}
|
||
|
||
DashboardPage::~DashboardPage()
|
||
{
|
||
recoveryBusy = false;
|
||
if (recoveryDialog) recoveryDialog->reject();
|
||
if (productionThread->isRunning()) {
|
||
productionWorker->requestStopImmediate();
|
||
QMetaObject::invokeMethod(productionWorker, "requestShutdown",
|
||
Qt::QueuedConnection);
|
||
productionThread->wait();
|
||
}
|
||
delete ui;
|
||
}
|
||
|
||
void DashboardPage::attachVisionService(VisionService *service)
|
||
{
|
||
connect(productionWorker, &ProductionWorker::visionMeasureRequested,
|
||
service, &VisionService::measure, Qt::QueuedConnection);
|
||
connect(productionWorker, &ProductionWorker::visionCancelRequested,
|
||
service, &VisionService::cancel, Qt::QueuedConnection);
|
||
connect(productionWorker, &ProductionWorker::visionProductionActiveChanged,
|
||
service, &VisionService::setProductionActive, Qt::QueuedConnection);
|
||
connect(productionWorker, &ProductionWorker::visionProductionActiveChanged,
|
||
this, &DashboardPage::visionProductionActiveChanged, Qt::QueuedConnection);
|
||
connect(service, &VisionService::measurementFinished,
|
||
productionWorker, &ProductionWorker::receiveVisionResult, Qt::QueuedConnection);
|
||
connect(service, &VisionService::availabilityChanged,
|
||
productionWorker, &ProductionWorker::updateVisionAvailability, Qt::QueuedConnection);
|
||
connect(productionWorker, &ProductionWorker::visionCycleChanged, this,
|
||
[this](const QJsonObject &cycle) {
|
||
visionRecoveryRequired = cycle.value("recovery_required").toBool();
|
||
updateRecoveryButtons();
|
||
emit visionCycleChanged(cycle);
|
||
emit logRequested("生产日志", "视觉引导", "自动取料",
|
||
QString::fromUtf8(QJsonDocument(cycle).toJson(QJsonDocument::Compact)),
|
||
visionRecoveryRequired ? "待恢复" : "记录");
|
||
}, Qt::QueuedConnection);
|
||
QMetaObject::invokeMethod(productionWorker, "updateVisionAvailability", Qt::QueuedConnection,
|
||
Q_ARG(bool, service->isReady()), Q_ARG(bool, service->isBusy()), Q_ARG(QString, service->sessionId()));
|
||
QMetaObject::invokeMethod(productionWorker, "requestVisionSnapshot", Qt::QueuedConnection);
|
||
}
|
||
|
||
void DashboardPage::addLog(const QString &type, const QString &module, const QString &content,
|
||
const QString &status)
|
||
{
|
||
ui->recentLogTable->insertRow(0);
|
||
const QStringList values = QStringList()
|
||
<< QDateTime::currentDateTime().toString("HH:mm:ss")
|
||
<< type << module << content << status;
|
||
for (int col = 0; col < values.size(); ++col) {
|
||
QTableWidgetItem *item = new QTableWidgetItem(values.at(col));
|
||
if (col == 3) item->setTextAlignment(Qt::AlignCenter);
|
||
ui->recentLogTable->setItem(0, col, item);
|
||
}
|
||
while (ui->recentLogTable->rowCount() > 8) {
|
||
ui->recentLogTable->removeRow(ui->recentLogTable->rowCount() - 1);
|
||
}
|
||
}
|
||
|
||
void DashboardPage::resizeEvent(QResizeEvent *event)
|
||
{
|
||
QWidget::resizeEvent(event);
|
||
updateRackSize();
|
||
}
|
||
|
||
void DashboardPage::updateRackSize()
|
||
{
|
||
if (!rackBox || rackTrayCells.isEmpty()) {
|
||
return;
|
||
}
|
||
|
||
const int spacing = 4;
|
||
const int leftMargin = 8;
|
||
const int rightMargin = 8;
|
||
const int topMargin = 18;
|
||
const int bottomMargin = 8;
|
||
const int maxRackHeight = qMax(150, ui->layoutGroup->height() / 3);
|
||
const int maxCellByHeight = (maxRackHeight - topMargin - bottomMargin - spacing) / 2;
|
||
const int maxCellByWidth = (ui->layoutGroup->width() - leftMargin - rightMargin - spacing * 2) / 3;
|
||
const int cellSide = qMax(52, qMin(maxCellByHeight, maxCellByWidth));
|
||
const int rackWidth = leftMargin + rightMargin + cellSide * 3 + spacing * 2;
|
||
const int rackHeight = topMargin + bottomMargin + cellSide * 2 + spacing;
|
||
|
||
for (QLabel *cell : rackTrayCells) {
|
||
cell->setFixedSize(cellSide, cellSide);
|
||
}
|
||
rackBox->setFixedSize(rackWidth, rackHeight);
|
||
}
|
||
|
||
void DashboardPage::startProduction()
|
||
{
|
||
if (recoveryBusy || visionRecoveryRequired) return;
|
||
if (productionState == ProductionService::Paused) {
|
||
if (!currentOrderResumable) return;
|
||
if (!confirmControlOperation("继续订单", "安全检查通过后,从保存的动作边界继续此暂停订单。")) return;
|
||
ui->startButton->setEnabled(false);
|
||
QMetaObject::invokeMethod(productionWorker, "requestResume",
|
||
Qt::QueuedConnection);
|
||
return;
|
||
}
|
||
if (productionState != ProductionService::Idle
|
||
&& productionState != ProductionService::Completed) {
|
||
return;
|
||
}
|
||
|
||
loadCurrentOrder();
|
||
if (!currentOrderStartable || currentOrderId <= 0) {
|
||
QMessageBox::information(this, "启动生产",
|
||
"没有可生产的订单,请先生成工件。");
|
||
return;
|
||
}
|
||
|
||
if (!confirmControlOperation("启动订单", "设备检查通过后,启动此订单并执行下述首件工件。")) return;
|
||
ui->startButton->setEnabled(false);
|
||
if (!QMetaObject::invokeMethod(
|
||
productionWorker, "requestStart", Qt::QueuedConnection,
|
||
Q_ARG(int, currentOrderId))) {
|
||
updateRecoveryButtons();
|
||
QMessageBox::warning(this, "启动生产",
|
||
"提交生产启动请求失败。");
|
||
}
|
||
}
|
||
|
||
void DashboardPage::pauseProduction()
|
||
{
|
||
if (!ui->pauseButton->isEnabled()
|
||
|| !confirmControlOperation("暂停订单", "机器人当前独立动作完成及握手结束后暂停;未提交的视觉请求作废。")) return;
|
||
QMetaObject::invokeMethod(productionWorker, "requestPause",
|
||
Qt::QueuedConnection);
|
||
}
|
||
|
||
void DashboardPage::stopProduction()
|
||
{
|
||
if (!stopButton->isEnabled()
|
||
|| !confirmControlOperation("终止订单", "将当前订单标记为已完成,保留实际加工数量及记录,剩余工件不再执行。此操作不是机器人急停。")) return;
|
||
if (recoveryDialog) recoveryDialog->reject();
|
||
QMetaObject::invokeMethod(productionWorker, "requestStopOrder",
|
||
Qt::QueuedConnection, Q_ARG(int, currentOrderId));
|
||
}
|
||
|
||
void DashboardPage::clearAlarm()
|
||
{
|
||
if (!ui->clearAlarmButton->isEnabled()
|
||
|| !confirmControlOperation("清除报警", "向 PLC 写清报警命令;成功后清除 ssp 活动报警记录,不恢复生产。")) return;
|
||
QMetaObject::invokeMethod(productionWorker, "requestClearAlarm",
|
||
Qt::QueuedConnection);
|
||
emit logRequested("报警日志", "控制面板", "清除报警",
|
||
"已请求 PLC 清除报警", "待处理");
|
||
}
|
||
|
||
QString DashboardPage::controlOperationContext(QString *identity) const
|
||
{
|
||
QString error;
|
||
Order order;
|
||
ProductionRunSnapshot run;
|
||
if (currentOrderId > 0
|
||
&& !OrderService::instance().findOrder(currentOrderId, &order, &error)) return QString();
|
||
if (!ProductionRepository().findActiveRun(&run, &error)) return QString();
|
||
if (run.id > 0 && run.orderId != currentOrderId) return QString();
|
||
|
||
Workpiece piece;
|
||
if (run.currentWorkpieceId > 0) {
|
||
if (!WorkpieceService::instance().findWorkpiece(run.currentWorkpieceId, &piece, &error)
|
||
|| piece.orderId != currentOrderId) return QString();
|
||
} else if (order.id > 0) {
|
||
const QList<Workpiece> pieces = WorkpieceService::instance().listByOrder(order.id, &error);
|
||
if (!error.isEmpty()) return QString();
|
||
for (const Workpiece &candidate : pieces) {
|
||
if (candidate.state == "PENDING") { piece = candidate; break; }
|
||
}
|
||
}
|
||
int trayNo = 0;
|
||
if (piece.trayId > 0) {
|
||
const QList<Tray> trays = TrayService::instance().listTrays(&error);
|
||
if (!error.isEmpty()) return QString();
|
||
for (int i = 0; i < trays.size(); ++i) {
|
||
if (trays.at(i).id == piece.trayId) { trayNo = i + 1; break; }
|
||
}
|
||
}
|
||
QString step = piece.processStep;
|
||
if (run.currentWorkpieceId <= 0 && piece.id > 0) step = "WAITING_DEVICE_READY";
|
||
const QString position = piece.id > 0
|
||
? QString("工件:%1\n料盘:%2,格位:%3(第 %4 行,第 %5 列)\n工序:%6")
|
||
.arg(piece.barcode).arg(trayNo).arg(piece.trayPos)
|
||
.arg((piece.trayPos - 1) / 7 + 1).arg((piece.trayPos - 1) % 7 + 1)
|
||
.arg(displayProcessStep(step))
|
||
: QString("工件:暂无当前或待加工工件");
|
||
QString text = order.id > 0
|
||
? QString("订单:%1\n订单状态:%2(%3暂停订单)\n%4")
|
||
.arg(order.no, displayOrderStatus(order.status),
|
||
order.status == "PAUSED" ? "是" : "不是", position)
|
||
: QString("当前没有未完成订单");
|
||
if (run.id > 0) {
|
||
text += QString("\n生产状态:%1\n动作记录:%2 / %3\n记录的下一工序:%4")
|
||
.arg(displayState(run.productionState), displayRecoveryAction(run.action.type),
|
||
displayActionPhase(run.action.phase), displayProcessStep(run.action.nextStep));
|
||
} else if (piece.id > 0) {
|
||
text += "\n开始步骤:等待设备就绪 → 移动到拍照位置";
|
||
}
|
||
// Exclude elapsed time: only changes affecting the confirmed operation invalidate it.
|
||
const QJsonObject snapshot{
|
||
{"order", order.id}, {"status", order.status}, {"run", run.id},
|
||
{"state", int(productionState)}, {"run_state", run.productionState},
|
||
{"piece", piece.id}, {"tray", trayNo}, {"slot", piece.trayPos}, {"step", step},
|
||
{"token", run.action.token}, {"attempt", run.action.attempt},
|
||
{"version", run.action.version}, {"phase", run.action.phase},
|
||
{"next", run.action.nextStep}, {"busy", recoveryBusy},
|
||
{"vision_recovery", visionRecoveryRequired}};
|
||
if (identity) *identity = QString::fromUtf8(QJsonDocument(snapshot).toJson(QJsonDocument::Compact));
|
||
return text;
|
||
}
|
||
|
||
bool DashboardPage::confirmControlOperation(const QString &title, const QString &effect)
|
||
{
|
||
if (controlConfirmationOpen) return false;
|
||
QString identity;
|
||
const QString context = controlOperationContext(&identity);
|
||
if (context.isEmpty()) {
|
||
QMessageBox::warning(this, title, "无法读取一致的订单和动作记录,请刷新状态后再操作。");
|
||
return false;
|
||
}
|
||
controlConfirmationOpen = true;
|
||
QMessageBox question(QMessageBox::Question, title,
|
||
context + "\n\n" + effect + "\n\n是否执行本步操作?",
|
||
QMessageBox::Yes | QMessageBox::No, this);
|
||
question.setObjectName("controlConfirmation");
|
||
question.setTextFormat(Qt::PlainText);
|
||
question.button(QMessageBox::Yes)->setText("执行");
|
||
question.button(QMessageBox::No)->setText("取消");
|
||
question.setDefaultButton(QMessageBox::No);
|
||
question.setEscapeButton(QMessageBox::No);
|
||
const int answer = question.exec();
|
||
controlConfirmationOpen = false;
|
||
if (answer != QMessageBox::Yes) return false;
|
||
QString currentIdentity;
|
||
if (controlOperationContext(¤tIdentity).isEmpty() || currentIdentity != identity) {
|
||
QMessageBox::information(this, title, "订单、工件或动作状态已经变化,本次操作未执行,请重新确认。");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
void DashboardPage::onProductionStateChanged(ProductionService::State state,
|
||
const QString &name)
|
||
{
|
||
productionState = state;
|
||
if (state == ProductionService::Terminating && recoveryDialog)
|
||
recoveryDialog->reject();
|
||
const QString stateText = displayState(name);
|
||
ui->actionLabel->setText("生产状态:" + stateText);
|
||
loadCurrentOrder();
|
||
updateRecoveryButtons();
|
||
emit systemStateChanged(stateText);
|
||
emit currentStepChanged(stateText);
|
||
refreshProcessRecords();
|
||
updateProductionStats();
|
||
}
|
||
|
||
void DashboardPage::onCurrentWorkpieceChanged(int workpieceId,
|
||
const QString &barcode)
|
||
{
|
||
if (workpieceId <= 0) {
|
||
ui->workpieceLabel->setText("当前工件:-");
|
||
ui->trayLabel->setText("当前托盘:-");
|
||
currentProcessElapsedMs = 0;
|
||
emit currentWorkpieceChanged("-");
|
||
} else {
|
||
ui->workpieceLabel->setText(
|
||
QString("当前工件:%1").arg(barcode));
|
||
emit currentWorkpieceChanged(barcode);
|
||
}
|
||
refreshProcessRecords();
|
||
updateRecoveryButtons();
|
||
updateProductionStats();
|
||
}
|
||
|
||
void DashboardPage::onCurrentWorkpieceSnapshotChanged(
|
||
const Workpiece &workpiece)
|
||
{
|
||
productionWorkpiece = workpiece;
|
||
refreshProcessRecords();
|
||
updateProductionStats();
|
||
}
|
||
|
||
void DashboardPage::onCurrentPositionChanged(int trayNo, int slotNo)
|
||
{
|
||
productionTrayNo = trayNo;
|
||
productionSlotNo = slotNo;
|
||
ui->trayLabel->setText(trayNo > 0
|
||
? QString("当前托盘:%1,位置:%2").arg(trayNo).arg(slotNo)
|
||
: "当前托盘:-");
|
||
}
|
||
|
||
void DashboardPage::onProductionError(const QString &error)
|
||
{
|
||
ui->actionLabel->setText("生产故障:" + error);
|
||
updateRecoveryButtons();
|
||
emit systemStateChanged("故障");
|
||
emit logRequested("报警日志", "生产流程", "生产故障",
|
||
error, "失败");
|
||
}
|
||
|
||
void DashboardPage::onProductionFinished(int orderId)
|
||
{
|
||
emit logRequested("生产日志", "生产流程", "订单完成",
|
||
QString("订单 %1 已完成").arg(orderId), "成功");
|
||
loadCurrentOrder();
|
||
updateProductionStats();
|
||
updateRackSummary();
|
||
}
|
||
|
||
void DashboardPage::onProcessElapsedChanged(const QString &processStep,
|
||
int elapsedMs)
|
||
{
|
||
currentProcessElapsedMs = elapsedMs;
|
||
currentProcessLabel->setText(displayProcessStep(processStep));
|
||
processElapsedLabel->setText(formatDuration(elapsedMs));
|
||
updateCurrentProcessDuration(elapsedMs);
|
||
}
|
||
|
||
void DashboardPage::onTimeoutRecoveryRequired(const QString &state,
|
||
const QString &message)
|
||
{
|
||
ui->actionLabel->setText("需要人工恢复:" + message);
|
||
emit logRequested("操作日志", "生产流程", "人工恢复",
|
||
displayState(state) + ":" + message, "待处理");
|
||
}
|
||
|
||
void DashboardPage::onRecoveryCompleted(const QString &mode)
|
||
{
|
||
emit logRequested("生产日志", "生产流程", "恢复完成",
|
||
displayState(mode), "成功");
|
||
}
|
||
|
||
void DashboardPage::recoverProduction()
|
||
{
|
||
if (recoveryBusy || !ui->recoveryButton->isEnabled()) return;
|
||
if (!confirmControlOperation("恢复/重试", "先读取设备及动作记录,检查能否安全恢复;状态不明确时再由人工选择。本次确认不保证立即继续生产。")) return;
|
||
onRecoveryBusyChanged(true);
|
||
ui->actionLabel->setText("正在检查设备状态和动作记录...");
|
||
if (!QMetaObject::invokeMethod(productionWorker, "requestRecover",
|
||
Qt::QueuedConnection)) {
|
||
onRecoveryBusyChanged(false);
|
||
onProductionError("提交恢复检查请求失败");
|
||
}
|
||
}
|
||
|
||
void DashboardPage::onRecoveryBusyChanged(bool busy)
|
||
{
|
||
recoveryBusy = busy;
|
||
if (!busy && recoveryDialog) recoveryDialog->reject();
|
||
updateRecoveryButtons();
|
||
}
|
||
|
||
void DashboardPage::onRecoveryDecisionRequired(const QJsonObject &context)
|
||
{
|
||
if (!recoveryBusy || recoveryDialog) return;
|
||
|
||
QDialog *dialog = new QDialog(this);
|
||
recoveryDialog = dialog;
|
||
dialog->setObjectName("recoveryDecisionDialog");
|
||
dialog->setWindowTitle("恢复/重试 · 人工核对");
|
||
dialog->setAttribute(Qt::WA_DeleteOnClose);
|
||
dialog->setWindowModality(Qt::WindowModal);
|
||
dialog->setMinimumWidth(520);
|
||
QVBoxLayout *layout = new QVBoxLayout(dialog);
|
||
const auto signalText = [](const QJsonValue &value) {
|
||
return value.isBool() ? (value.toBool() ? QString("1") : QString("0"))
|
||
: QString("未知");
|
||
};
|
||
QLabel *details = new QLabel(
|
||
QString("订单:%1 工件:%2\n工序:%3\n动作:%4 检查点:%5\n"
|
||
"命令:%6 反馈:%7\n原因:%8")
|
||
.arg(context.value("order_id").toInt())
|
||
.arg(context.value("workpiece_id").toInt())
|
||
.arg(displayProcessStep(context.value("process_step").toString()))
|
||
.arg(displayRecoveryAction(context.value("action_type").toString()))
|
||
.arg(displayActionPhase(context.value("action_phase").toString()))
|
||
.arg(signalText(context.value("command")))
|
||
.arg(signalText(context.value("feedback")))
|
||
.arg(context.value("reason").toString()), dialog);
|
||
details->setTextFormat(Qt::PlainText);
|
||
details->setWordWrap(true);
|
||
details->setTextInteractionFlags(Qt::TextSelectableByMouse);
|
||
layout->addWidget(details);
|
||
|
||
QFormLayout *form = new QFormLayout;
|
||
QComboBox *choice = new QComboBox(dialog);
|
||
choice->setObjectName("recoveryDecisionChoice");
|
||
choice->addItem("请选择处理方式", QString());
|
||
const QJsonArray options = context.value("options").toArray();
|
||
for (const QJsonValue &option : options) {
|
||
const QString code = option.toString();
|
||
QString label;
|
||
if (code == "ACTION_COMPLETED") label = "确认动作已完成";
|
||
else if (code == "ACTION_NOT_EXECUTED") label = "确认动作未执行,重新执行";
|
||
else if (code == "RETRY_VISION") label = "重新视觉识别";
|
||
else if (code == "NORMAL_PICK") label = "改为普通取料";
|
||
else if (code == "UNLOCK_VISION") label = "仅解除视觉遗留锁(不恢复订单)";
|
||
if (!label.isEmpty()) choice->addItem(label, code);
|
||
}
|
||
QLineEdit *operatorName = new QLineEdit(dialog);
|
||
operatorName->setObjectName("recoveryOperatorName");
|
||
operatorName->setMaxLength(64);
|
||
operatorName->setPlaceholderText("填写现场核对人的姓名或工号");
|
||
QLineEdit *note = new QLineEdit(dialog);
|
||
note->setObjectName("recoveryDecisionNote");
|
||
note->setMaxLength(512);
|
||
note->setPlaceholderText("记录机器人位置、工件状态等核对情况");
|
||
form->addRow("处理方式:", choice);
|
||
form->addRow("操作人:", operatorName);
|
||
form->addRow("核对备注:", note);
|
||
layout->addLayout(form);
|
||
QCheckBox *confirmed = new QCheckBox("请先选择处理方式,再完成现场核对", dialog);
|
||
confirmed->setObjectName("recoveryFieldConfirmed");
|
||
confirmed->setEnabled(false);
|
||
layout->addWidget(confirmed);
|
||
QDialogButtonBox *buttons = new QDialogButtonBox(
|
||
QDialogButtonBox::Ok | QDialogButtonBox::Cancel, dialog);
|
||
QPushButton *submit = buttons->button(QDialogButtonBox::Ok);
|
||
submit->setText("提交核对结果");
|
||
submit->setEnabled(false);
|
||
submit->setAutoDefault(false);
|
||
QPushButton *cancel = buttons->button(QDialogButtonBox::Cancel);
|
||
cancel->setText("暂不处理");
|
||
cancel->setDefault(true);
|
||
layout->addWidget(buttons);
|
||
|
||
const auto updateSubmit = [choice, operatorName, confirmed, submit]() {
|
||
submit->setEnabled(!choice->currentData().toString().isEmpty()
|
||
&& !operatorName->text().trimmed().isEmpty()
|
||
&& confirmed->isChecked());
|
||
};
|
||
connect(choice, QOverload<int>::of(&QComboBox::currentIndexChanged), dialog,
|
||
[choice, confirmed, updateSubmit](int) {
|
||
const QString code = choice->currentData().toString();
|
||
confirmed->setChecked(false);
|
||
confirmed->setEnabled(!code.isEmpty());
|
||
if (code == "ACTION_COMPLETED")
|
||
confirmed->setText("已确认机器人停止、动作已到终点、工件位置正确");
|
||
else if (code == "ACTION_NOT_EXECUTED")
|
||
confirmed->setText("已确认机器人停止且在原起点,尚未取放或加工");
|
||
else if (code == "UNLOCK_VISION")
|
||
confirmed->setText("已核对机器人停止、取料状态明确,原订单不再继续");
|
||
else if (!code.isEmpty())
|
||
confirmed->setText("已确认机器人停止在拍照位,RackPick 尚未开始");
|
||
else
|
||
confirmed->setText("请先选择处理方式,再完成现场核对");
|
||
updateSubmit();
|
||
});
|
||
connect(operatorName, &QLineEdit::textChanged, dialog, updateSubmit);
|
||
connect(confirmed, &QCheckBox::toggled, dialog, updateSubmit);
|
||
connect(buttons, &QDialogButtonBox::accepted, dialog, &QDialog::accept);
|
||
connect(buttons, &QDialogButtonBox::rejected, dialog, &QDialog::reject);
|
||
connect(dialog, &QDialog::finished, this,
|
||
[this, context, choice, operatorName, note, confirmed](int result) {
|
||
recoveryDialog = nullptr;
|
||
if (!recoveryBusy) return;
|
||
QJsonObject decision = context;
|
||
decision.insert("decision", result == QDialog::Accepted
|
||
? choice->currentData().toString() : QString("CANCEL"));
|
||
decision.insert("operator_name", operatorName->text().trimmed());
|
||
decision.insert("note", note->text().trimmed());
|
||
decision.insert("field_confirmed", confirmed->isChecked());
|
||
emit recoveryDecisionSubmitted(decision);
|
||
});
|
||
cancel->setFocus();
|
||
dialog->open();
|
||
}
|
||
|
||
void DashboardPage::markWorkpieceNg()
|
||
{
|
||
if (QMessageBox::question(
|
||
this, "标记工件 NG",
|
||
"确认将当前工件标记为 NG,并继续处理下一个工件吗?",
|
||
QMessageBox::Yes | QMessageBox::No, QMessageBox::No)
|
||
!= QMessageBox::Yes) {
|
||
return;
|
||
}
|
||
|
||
QMetaObject::invokeMethod(productionWorker, "requestMarkNg",
|
||
Qt::QueuedConnection);
|
||
}
|
||
|
||
void DashboardPage::updateRecoveryButtons()
|
||
{
|
||
const ProductionService::State state = productionState;
|
||
const bool recoveryRequired = state == ProductionService::TimeoutPaused
|
||
|| state == ProductionService::ManualConfirm
|
||
|| state == ProductionService::Error || visionRecoveryRequired;
|
||
const bool running = state != ProductionService::Idle
|
||
&& state != ProductionService::Completed;
|
||
const bool terminating = state == ProductionService::Terminating;
|
||
const bool canStart = (((state == ProductionService::Idle
|
||
|| state == ProductionService::Completed)
|
||
&& currentOrderStartable)
|
||
|| (state == ProductionService::Paused
|
||
&& currentOrderResumable))
|
||
&& !visionRecoveryRequired && !recoveryBusy;
|
||
ui->startButton->setEnabled(canStart);
|
||
ui->startButton->setText(state == ProductionService::Paused
|
||
? "继续" : "启动");
|
||
ui->recoveryButton->setVisible(true);
|
||
ui->recoveryButton->setEnabled(recoveryRequired && !terminating && !recoveryBusy);
|
||
ui->recoveryButton->setText(recoveryBusy ? "恢复检查中..." : "恢复/重试");
|
||
ui->pauseButton->setEnabled(running && !recoveryRequired
|
||
&& state != ProductionService::Paused
|
||
&& !recoveryBusy && !terminating);
|
||
ui->clearAlarmButton->setEnabled(!recoveryBusy && !terminating);
|
||
markNgButton->setVisible(recoveryRequired
|
||
&& productionWorkpiece.id > 0 && !visionRecoveryRequired);
|
||
markNgButton->setEnabled(!recoveryBusy && !terminating);
|
||
stopButton->setVisible(true);
|
||
stopButton->setEnabled(currentOrderId > 0 && !terminating);
|
||
stopButton->setText(terminating ? "终止中..." : "终止");
|
||
}
|
||
|
||
QString DashboardPage::formatDuration(int elapsedMs)
|
||
{
|
||
const int totalSeconds = qMax(0, elapsedMs) / 1000;
|
||
const int hours = totalSeconds / 3600;
|
||
const int minutes = (totalSeconds % 3600) / 60;
|
||
const int seconds = totalSeconds % 60;
|
||
const int milliseconds = qMax(0, elapsedMs) % 1000;
|
||
return QString("%1:%2:%3.%4")
|
||
.arg(hours, 2, 10, QLatin1Char('0'))
|
||
.arg(minutes, 2, 10, QLatin1Char('0'))
|
||
.arg(seconds, 2, 10, QLatin1Char('0'))
|
||
.arg(milliseconds, 3, 10, QLatin1Char('0'));
|
||
}
|
||
|
||
void DashboardPage::refreshProcessRecords()
|
||
{
|
||
const Workpiece workpiece = productionWorkpiece;
|
||
if (workpiece.id <= 0) {
|
||
currentProcessLabel->setText("-");
|
||
processElapsedLabel->setText(formatDuration(0));
|
||
processTotalElapsedLabel->setText(formatDuration(0));
|
||
processTable->setRowCount(0);
|
||
return;
|
||
}
|
||
|
||
QString error;
|
||
const QList<WorkpieceProcessRecord> records =
|
||
ProductionRepository().listProcessRecords(workpiece.id, &error);
|
||
if (!error.isEmpty()) {
|
||
return;
|
||
}
|
||
|
||
const QString currentStep = workpiece.processStep;
|
||
const qint64 nowMs = QDateTime::currentDateTime().toMSecsSinceEpoch();
|
||
int totalElapsedMs = 0;
|
||
processTable->setRowCount(records.size());
|
||
for (int row = 0; row < records.size(); ++row) {
|
||
const WorkpieceProcessRecord &record = records.at(row);
|
||
int durationMs = record.durationMs;
|
||
if (durationMs < 0 && record.startedAt.isValid()) {
|
||
if (record.processState == "RUNNING"
|
||
&& record.processStep == currentStep
|
||
&& currentProcessElapsedMs > 0) {
|
||
durationMs = currentProcessElapsedMs;
|
||
} else {
|
||
const qint64 elapsedMs = qMax<qint64>(
|
||
0, nowMs - record.startedAt.toMSecsSinceEpoch());
|
||
durationMs = static_cast<int>(
|
||
qMin<qint64>(elapsedMs, 2147483647));
|
||
}
|
||
}
|
||
if (durationMs >= 0) {
|
||
totalElapsedMs += durationMs;
|
||
}
|
||
|
||
QTableWidgetItem *stepItem = new QTableWidgetItem(
|
||
displayProcessStep(record.processStep));
|
||
stepItem->setData(Qt::UserRole, record.processStep);
|
||
processTable->setItem(row, 0, stepItem);
|
||
QTableWidgetItem *statusItem = new QTableWidgetItem(
|
||
displayProcessStatus(record.processState));
|
||
statusItem->setData(Qt::UserRole, record.processState);
|
||
processTable->setItem(row, 1, statusItem);
|
||
processTable->setItem(row, 2,
|
||
new QTableWidgetItem(
|
||
QString::number(record.attemptNo)));
|
||
processTable->setItem(row, 3,
|
||
new QTableWidgetItem(
|
||
durationMs >= 0
|
||
? formatDuration(durationMs) : "-"));
|
||
}
|
||
|
||
if (!currentStep.isEmpty()) {
|
||
currentProcessLabel->setText(displayProcessStep(currentStep));
|
||
}
|
||
processTotalElapsedLabel->setText(formatDuration(totalElapsedMs));
|
||
}
|
||
|
||
void DashboardPage::updateCurrentProcessDuration(int elapsedMs)
|
||
{
|
||
const Workpiece workpiece = productionWorkpiece;
|
||
if (workpiece.id <= 0) {
|
||
return;
|
||
}
|
||
|
||
for (int row = 0; row < processTable->rowCount(); ++row) {
|
||
QTableWidgetItem *stepItem = processTable->item(row, 0);
|
||
if (!stepItem || stepItem->data(Qt::UserRole).toString()
|
||
!= workpiece.processStep) {
|
||
continue;
|
||
}
|
||
QTableWidgetItem *statusItem = processTable->item(row, 1);
|
||
if (!statusItem || statusItem->data(Qt::UserRole).toString()
|
||
!= "RUNNING") {
|
||
continue;
|
||
}
|
||
processTable->setItem(row, 3,
|
||
new QTableWidgetItem(formatDuration(elapsedMs)));
|
||
break;
|
||
}
|
||
}
|
||
|
||
void DashboardPage::onDeviceStatusChanged(const DeviceStatus &status)
|
||
{
|
||
emit deviceStatusChanged(status);
|
||
const QString plcText = boolStatus(status.statusValid,
|
||
status.plcReady, "就绪", "未就绪");
|
||
const QString robotText = boolStatus(status.statusValid,
|
||
status.robotReady, "就绪", "未就绪");
|
||
const QString safetyText = !status.statusValid ? "未采集"
|
||
: status.safetyDoorClosed ? "正常" : "异常";
|
||
deviceStatusLabel->setText(
|
||
QString("PLC:%1 | 机器人:%2 | 安全回路:%3 | 急停:%4")
|
||
.arg(!status.statusValid ? "未采集"
|
||
: status.plcConnected ? "在线" : "离线")
|
||
.arg(robotText)
|
||
.arg(safetyText)
|
||
.arg(boolStatus(status.statusValid, status.emergencyStop,
|
||
"已触发", "正常")));
|
||
|
||
updateStatusCell(inspectionMachineCell,
|
||
QString("检测机\n%1").arg(boolStatus(
|
||
status.statusValid, status.inspectionMachineReady,
|
||
"就绪", "未就绪")),
|
||
!status.statusValid ? "#607d8b"
|
||
: status.inspectionMachineReady ? "#2e7d32" : "#d32f2f");
|
||
updateStatusCell(robotCell,
|
||
QString("机器人\n%1").arg(robotText),
|
||
!status.statusValid ? "#607d8b"
|
||
: status.robotReady ? "#2e7d32" : "#d32f2f");
|
||
updateStatusCell(robotControlCabinetCell,
|
||
QString("机器人控制柜\n%1").arg(boolStatus(
|
||
status.statusValid, status.robotControlCabinetReady,
|
||
"正常", "未就绪")),
|
||
!status.statusValid ? "#607d8b"
|
||
: status.robotControlCabinetReady ? "#2e7d32" : "#d32f2f");
|
||
updateStatusCell(mainControlCabinetCell,
|
||
QString("总控柜\n%1").arg(boolStatus(
|
||
status.statusValid, status.mainControlCabinetReady,
|
||
"正常", "未就绪")),
|
||
!status.statusValid ? "#607d8b"
|
||
: status.mainControlCabinetReady ? "#2e7d32" : "#d32f2f");
|
||
updateStatusCell(safetyDoor1Cell,
|
||
QString("安全门 1\n%1").arg(boolStatus(
|
||
status.statusValid, status.safetyDoor1Closed,
|
||
"关闭", "打开")),
|
||
!status.statusValid ? "#607d8b"
|
||
: status.safetyDoor1Closed ? "#2e7d32" : "#d32f2f");
|
||
updateStatusCell(safetyDoor2Cell,
|
||
QString("安全门 2\n%1").arg(boolStatus(
|
||
status.statusValid, status.safetyDoor2Closed,
|
||
"关闭", "打开")),
|
||
!status.statusValid ? "#607d8b"
|
||
: status.safetyDoor2Closed ? "#2e7d32" : "#d32f2f");
|
||
updateStatusCell(safetyDoor3Cell,
|
||
QString("安全门 3\n%1").arg(boolStatus(
|
||
status.statusValid, status.safetyDoor3Closed,
|
||
"关闭", "打开")),
|
||
!status.statusValid ? "#607d8b"
|
||
: status.safetyDoor3Closed ? "#2e7d32" : "#d32f2f");
|
||
updateStatusCell(alarmCell,
|
||
QString("PLC 报警\n%1").arg(boolStatus(
|
||
status.statusValid, status.activeAlarm,
|
||
"有报警", "无报警")),
|
||
!status.statusValid ? "#607d8b"
|
||
: status.activeAlarm ? "#d32f2f" : "#2e7d32");
|
||
updateRackSummary();
|
||
}
|
||
|
||
void DashboardPage::updateRackSummary()
|
||
{
|
||
QString error;
|
||
const QList<Tray> trays = TrayService::instance().listTrays(&error);
|
||
QSet<int> highlightedTrayIds;
|
||
if (error.isEmpty() && currentOrderId > 0) {
|
||
QString workpieceError;
|
||
const QList<Workpiece> workpieces =
|
||
WorkpieceService::instance().listByOrder(currentOrderId,
|
||
&workpieceError);
|
||
if (workpieceError.isEmpty()) {
|
||
for (const Workpiece &workpiece : workpieces) {
|
||
if (workpiece.trayId > 0) {
|
||
highlightedTrayIds.insert(workpiece.trayId);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
for (int index = 0; index < rackTrayCells.size(); ++index) {
|
||
QLabel *cell = rackTrayCells.at(index);
|
||
if (!error.isEmpty() || index >= trays.size()) {
|
||
updateStatusCell(cell,
|
||
QString("托盘 %1\n未采集").arg(index + 1),
|
||
"#607d8b");
|
||
continue;
|
||
}
|
||
|
||
const Tray &tray = trays.at(index);
|
||
const QString status = tray.present ? tray.status : "离位";
|
||
const QString statusText = status == "OK" ? "已完成"
|
||
: status == "NG" ? "有 NG"
|
||
: status == "PROCESSING" ? "加工中"
|
||
: status == "PENDING" ? "待检测"
|
||
: tray.present ? "在位" : "离位";
|
||
const QString color = !tray.present ? "#90a4ae"
|
||
: status == "NG" ? "#d32f2f"
|
||
: status == "PROCESSING" ? "#1e88e5"
|
||
: status == "OK" ? "#2e7d32" : "#78909c";
|
||
updateStatusCell(cell,
|
||
QString("托盘 %1\n%2:%3/49")
|
||
.arg(index + 1)
|
||
.arg(statusText)
|
||
.arg(tray.loadedCount),
|
||
color, highlightedTrayIds.contains(tray.id));
|
||
}
|
||
}
|
||
|
||
void DashboardPage::loadCurrentOrder()
|
||
{
|
||
QString error;
|
||
Order order;
|
||
ProductionRunSnapshot run;
|
||
const bool loaded = ProductionRepository().findActiveRun(&run, &error)
|
||
&& (run.id > 0
|
||
? OrderService::instance().findOrder(run.orderId, &order, &error)
|
||
: OrderService::instance().findUnfinishedOrder(&order, &error));
|
||
if (!loaded) {
|
||
currentOrderId = 0;
|
||
currentOrderStartable = false;
|
||
currentOrderResumable = false;
|
||
ui->orderLabel->setText("当前订单:读取失败");
|
||
updateProductionStats();
|
||
updateRackSummary();
|
||
emit logRequested("操作日志", "控制面板", "加载订单",
|
||
error, "失败");
|
||
return;
|
||
}
|
||
|
||
currentOrderId = order.id;
|
||
currentOrderStartable = false;
|
||
currentOrderResumable = false;
|
||
if (order.id <= 0) {
|
||
ui->orderLabel->setText("当前订单:暂无未完成订单");
|
||
emit currentOrderChanged("-");
|
||
updateProductionStats();
|
||
updateRackSummary();
|
||
updateRecoveryButtons();
|
||
return;
|
||
}
|
||
|
||
QString workpieceError;
|
||
const QList<Workpiece> workpieces =
|
||
WorkpieceService::instance().listByOrder(order.id, &workpieceError);
|
||
if (workpieceError.isEmpty()) {
|
||
currentOrderStartable = order.status == "NOT_START"
|
||
&& !workpieces.isEmpty();
|
||
currentOrderResumable = order.status == "PAUSED"
|
||
&& !workpieces.isEmpty();
|
||
}
|
||
|
||
ui->orderLabel->setText(QString("当前订单:%1(%2)")
|
||
.arg(order.no)
|
||
.arg(displayOrderStatus(order.status)));
|
||
emit currentOrderChanged(order.no);
|
||
updateProductionStats();
|
||
updateRackSummary();
|
||
updateRecoveryButtons();
|
||
}
|
||
|
||
void DashboardPage::updateProductionStats()
|
||
{
|
||
if (currentOrderId <= 0) {
|
||
ui->planValue->setText("0");
|
||
ui->pendingValue->setText("0");
|
||
ui->frontValue->setText("0");
|
||
ui->backValue->setText("0");
|
||
ui->okValue->setText("0");
|
||
ui->ngValue->setText("0");
|
||
return;
|
||
}
|
||
|
||
Order order;
|
||
QString error;
|
||
if (!OrderService::instance().findOrder(currentOrderId, &order, &error)) {
|
||
return;
|
||
}
|
||
const QList<Workpiece> workpieces =
|
||
WorkpieceService::instance().listByOrder(currentOrderId, &error);
|
||
if (!error.isEmpty()) {
|
||
return;
|
||
}
|
||
|
||
int pending = 0;
|
||
int frontChecking = 0;
|
||
int backChecking = 0;
|
||
int ok = 0;
|
||
int ng = 0;
|
||
for (const Workpiece &workpiece : workpieces) {
|
||
if (workpiece.state == "PENDING") {
|
||
++pending;
|
||
} else if (workpiece.processStep == "FRONT_CHECKING") {
|
||
++frontChecking;
|
||
} else if (workpiece.processStep == "BACK_CHECKING") {
|
||
++backChecking;
|
||
} else if (workpiece.state == "OK") {
|
||
++ok;
|
||
} else if (workpiece.state == "NG") {
|
||
++ng;
|
||
}
|
||
}
|
||
|
||
ui->planValue->setText(QString::number(order.planNum));
|
||
ui->pendingValue->setText(QString::number(pending));
|
||
ui->frontValue->setText(QString::number(frontChecking));
|
||
ui->backValue->setText(QString::number(backChecking));
|
||
ui->okValue->setText(QString::number(ok));
|
||
ui->ngValue->setText(QString::number(ng));
|
||
}
|