866 lines
33 KiB
C++
866 lines
33 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 <QAbstractItemView>
|
||
#include <QComboBox>
|
||
#include <QDateTime>
|
||
#include <QFormLayout>
|
||
#include <QGridLayout>
|
||
#include <QGroupBox>
|
||
#include <QHeaderView>
|
||
#include <QLabel>
|
||
#include <QMessageBox>
|
||
#include <QSignalBlocker>
|
||
#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 == "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 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;
|
||
}
|
||
|
||
void updateStatusCell(QLabel *cell, const QString &text, const QString &color)
|
||
{
|
||
if (!cell) {
|
||
return;
|
||
}
|
||
cell->setText(text);
|
||
cell->setStyleSheet(QString("QLabel { background: %1; border: 1px solid #ffffff; "
|
||
"border-radius: 3px; color: white; font-size: 11px; }")
|
||
.arg(color));
|
||
}
|
||
|
||
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)
|
||
, orderCombo(new QComboBox(this))
|
||
, rackBox(nullptr)
|
||
, deviceStatusLabel(new QLabel("PLC:未采集 | 机器人:未采集 | 安全回路:未采集", this))
|
||
, inspectionMachineCell(nullptr)
|
||
, robotCell(nullptr)
|
||
, robotControlCabinetCell(nullptr)
|
||
, mainControlCabinetCell(nullptr)
|
||
, safetyDoor1Cell(nullptr)
|
||
, safetyDoor2Cell(nullptr)
|
||
, safetyDoor3Cell(nullptr)
|
||
, alarmCell(nullptr)
|
||
, recoverButton(new QPushButton("恢复", this))
|
||
, retryButton(new QPushButton("重试", this))
|
||
, 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)
|
||
, productionTrayNo(0)
|
||
, productionSlotNo(0)
|
||
{
|
||
ui->setupUi(this);
|
||
|
||
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);
|
||
recoverButton->setObjectName("recoveryButton");
|
||
recoverButton->setVisible(false);
|
||
ui->buttonLayout->insertWidget(3, recoverButton);
|
||
retryButton->setObjectName("recoveryRetryButton");
|
||
retryButton->setVisible(false);
|
||
ui->buttonLayout->insertWidget(4, retryButton);
|
||
markNgButton->setObjectName("recoveryNgButton");
|
||
markNgButton->setVisible(false);
|
||
ui->buttonLayout->insertWidget(5, markNgButton);
|
||
stopButton->setObjectName("stopButton");
|
||
stopButton->setVisible(false);
|
||
ui->buttonLayout->insertWidget(6, stopButton);
|
||
orderCombo->setObjectName("orderSelector");
|
||
orderCombo->setMinimumWidth(220);
|
||
ui->taskLayout->insertWidget(0, orderCombo);
|
||
deviceStatusLabel->setStyleSheet("color: #546e7a;");
|
||
ui->taskLayout->insertWidget(1, 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() << "工序" << "状态" << "次数" << "耗时");
|
||
processTable->horizontalHeader()->setStretchLastSection(true);
|
||
processTable->horizontalHeader()->setSectionResizeMode(
|
||
0, QHeaderView::Stretch);
|
||
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() << "时间" << "类型" << "模块" << "内容" << "状态");
|
||
ui->recentLogTable->horizontalHeader()->setStretchLastSection(true);
|
||
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->resetButton, &QPushButton::clicked,
|
||
this, &DashboardPage::resetProduction);
|
||
connect(ui->clearAlarmButton, &QPushButton::clicked,
|
||
this, &DashboardPage::clearAlarm);
|
||
connect(recoverButton, &QPushButton::clicked,
|
||
this, &DashboardPage::recoverProduction);
|
||
connect(retryButton, &QPushButton::clicked,
|
||
this, &DashboardPage::retryRecovery);
|
||
connect(markNgButton, &QPushButton::clicked,
|
||
this, &DashboardPage::markWorkpieceNg);
|
||
connect(orderCombo, QOverload<int>::of(&QComboBox::currentIndexChanged),
|
||
this, &DashboardPage::onOrderChanged);
|
||
|
||
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::deviceStatusChanged,
|
||
this, &DashboardPage::onDeviceStatusChanged);
|
||
connect(productionWorker, &ProductionWorker::deviceCommunicationError,
|
||
this, [this](const QString &error) {
|
||
emit deviceCommunicationError(error);
|
||
});
|
||
|
||
productionThread->start();
|
||
QMetaObject::invokeMethod(productionWorker, "requestRestore",
|
||
Qt::QueuedConnection);
|
||
|
||
loadOrderOptions();
|
||
processRefreshTimer->setInterval(1000);
|
||
connect(processRefreshTimer, &QTimer::timeout,
|
||
this, &DashboardPage::refreshProcessRecords);
|
||
processRefreshTimer->start();
|
||
refreshProcessRecords();
|
||
updateRackSummary();
|
||
}
|
||
|
||
DashboardPage::~DashboardPage()
|
||
{
|
||
if (productionThread->isRunning()) {
|
||
productionWorker->requestStopImmediate();
|
||
QMetaObject::invokeMethod(productionWorker, "requestShutdown",
|
||
Qt::QueuedConnection);
|
||
productionThread->wait();
|
||
}
|
||
delete ui;
|
||
}
|
||
|
||
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) {
|
||
ui->recentLogTable->setItem(0, col, new QTableWidgetItem(values.at(col)));
|
||
}
|
||
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 (productionState != ProductionService::Idle
|
||
&& productionState != ProductionService::Completed) {
|
||
return;
|
||
}
|
||
|
||
int selectedOrderId = orderCombo->currentData().toInt();
|
||
if (selectedOrderId <= 0) {
|
||
loadOrderOptions();
|
||
selectedOrderId = orderCombo->currentData().toInt();
|
||
}
|
||
if (selectedOrderId <= 0) {
|
||
QMessageBox::information(this, "启动生产",
|
||
"没有可生产的订单,请先生成工件。");
|
||
return;
|
||
}
|
||
|
||
ui->startButton->setEnabled(false);
|
||
if (!QMetaObject::invokeMethod(
|
||
productionWorker, "requestStart", Qt::QueuedConnection,
|
||
Q_ARG(int, selectedOrderId))) {
|
||
updateRecoveryButtons();
|
||
QMessageBox::warning(this, "启动生产",
|
||
"提交生产启动请求失败。");
|
||
}
|
||
}
|
||
|
||
void DashboardPage::pauseProduction()
|
||
{
|
||
QMetaObject::invokeMethod(productionWorker, "requestPause",
|
||
Qt::QueuedConnection);
|
||
}
|
||
|
||
void DashboardPage::stopProduction()
|
||
{
|
||
productionWorker->requestStopImmediate();
|
||
QMetaObject::invokeMethod(productionWorker, "requestStop",
|
||
Qt::QueuedConnection);
|
||
}
|
||
|
||
void DashboardPage::resetProduction()
|
||
{
|
||
QMetaObject::invokeMethod(productionWorker, "requestReset",
|
||
Qt::QueuedConnection);
|
||
}
|
||
|
||
void DashboardPage::clearAlarm()
|
||
{
|
||
QMetaObject::invokeMethod(productionWorker, "requestClearAlarm",
|
||
Qt::QueuedConnection);
|
||
emit logRequested("报警日志", "控制面板", "清除报警",
|
||
"已请求 PLC 清除报警", "待处理");
|
||
}
|
||
|
||
void DashboardPage::onOrderChanged(int)
|
||
{
|
||
updateOrderSummary();
|
||
}
|
||
|
||
void DashboardPage::onProductionStateChanged(ProductionService::State state,
|
||
const QString &name)
|
||
{
|
||
productionState = state;
|
||
const QString stateText = displayState(name);
|
||
ui->actionLabel->setText("生产状态:" + stateText);
|
||
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), "成功");
|
||
loadOrderOptions();
|
||
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 (productionState == ProductionService::ManualConfirm
|
||
&& QMessageBox::question(
|
||
this, "继续生产",
|
||
"请确认设备和 PLC 当前步骤安全后再继续生产。",
|
||
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes)
|
||
!= QMessageBox::Yes) {
|
||
return;
|
||
}
|
||
|
||
QMetaObject::invokeMethod(productionWorker, "requestRecover",
|
||
Qt::QueuedConnection);
|
||
}
|
||
|
||
void DashboardPage::retryRecovery()
|
||
{
|
||
QMetaObject::invokeMethod(productionWorker, "requestRetry",
|
||
Qt::QueuedConnection);
|
||
}
|
||
|
||
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;
|
||
const bool running = state != ProductionService::Idle
|
||
&& state != ProductionService::Completed;
|
||
const bool terminating = state == ProductionService::Terminating;
|
||
const bool canStart = state == ProductionService::Idle
|
||
|| state == ProductionService::Completed;
|
||
ui->startButton->setEnabled(canStart);
|
||
recoverButton->setVisible(recoveryRequired);
|
||
retryButton->setVisible(recoveryRequired);
|
||
markNgButton->setVisible(recoveryRequired
|
||
&& productionWorkpiece.id > 0);
|
||
stopButton->setVisible(running);
|
||
stopButton->setEnabled(!terminating);
|
||
stopButton->setText(terminating ? "终止中..." : "终止");
|
||
recoverButton->setText(state == ProductionService::ManualConfirm
|
||
? "继续" : "恢复");
|
||
}
|
||
|
||
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);
|
||
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);
|
||
}
|
||
}
|
||
|
||
void DashboardPage::loadOrderOptions()
|
||
{
|
||
QString error;
|
||
const QList<Order> orders = OrderService::instance().listOrders(&error);
|
||
if (!error.isEmpty()) {
|
||
orderCombo->clear();
|
||
orderCombo->addItem("无订单数据", 0);
|
||
emit logRequested("操作日志", "控制面板", "加载订单",
|
||
error, "失败");
|
||
return;
|
||
}
|
||
|
||
const int previousOrderId = orderCombo->currentData().toInt();
|
||
QSignalBlocker blocker(orderCombo);
|
||
orderCombo->clear();
|
||
for (const Order &order : orders) {
|
||
if (order.status != "NOT_START") {
|
||
continue;
|
||
}
|
||
|
||
QString workpieceError;
|
||
const QList<Workpiece> workpieces =
|
||
WorkpieceService::instance().listByOrder(order.id, &workpieceError);
|
||
if (!workpieceError.isEmpty() || workpieces.isEmpty()) {
|
||
continue;
|
||
}
|
||
orderCombo->addItem(order.no, order.id);
|
||
}
|
||
|
||
if (orderCombo->count() == 0) {
|
||
orderCombo->addItem("暂无可生产订单", 0);
|
||
} else {
|
||
const int previousIndex = orderCombo->findData(previousOrderId);
|
||
orderCombo->setCurrentIndex(previousIndex >= 0 ? previousIndex : 0);
|
||
}
|
||
updateOrderSummary();
|
||
}
|
||
|
||
void DashboardPage::updateOrderSummary()
|
||
{
|
||
const int selectedOrderId = orderCombo->currentData().toInt();
|
||
if (selectedOrderId <= 0) {
|
||
ui->orderLabel->setText("当前订单:-");
|
||
ui->trayLabel->setText("当前托盘:-");
|
||
ui->workpieceLabel->setText("当前工件:-");
|
||
emit currentOrderChanged("-");
|
||
updateProductionStats();
|
||
return;
|
||
}
|
||
|
||
Order order;
|
||
QString error;
|
||
if (!OrderService::instance().findOrder(selectedOrderId, &order, &error)) {
|
||
ui->orderLabel->setText("当前订单:-");
|
||
emit currentOrderChanged("-");
|
||
return;
|
||
}
|
||
ui->orderLabel->setText("当前订单:" + order.no);
|
||
emit currentOrderChanged(order.no);
|
||
updateProductionStats();
|
||
}
|
||
|
||
void DashboardPage::updateProductionStats()
|
||
{
|
||
const int selectedOrderId = orderCombo->currentData().toInt();
|
||
if (selectedOrderId <= 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(selectedOrderId, &order, &error)) {
|
||
return;
|
||
}
|
||
const QList<Workpiece> workpieces =
|
||
WorkpieceService::instance().listByOrder(selectedOrderId, &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));
|
||
}
|