673 lines
25 KiB
C++
673 lines
25 KiB
C++
#include "dashboardpage.h"
|
|
#include "ui_dashboardpage.h"
|
|
|
|
#include "../services/orderservice.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>
|
|
|
|
DashboardPage::DashboardPage(QWidget *parent)
|
|
: QWidget(parent)
|
|
, ui(new Ui::DashboardPage)
|
|
, orderCombo(new QComboBox(this))
|
|
, rackBox(nullptr)
|
|
, deviceStatusLabel(new QLabel("PLC: - | Robot: - | Safety: -", this))
|
|
, recoverButton(new QPushButton("Recover", this))
|
|
, retryButton(new QPushButton("Retry", this))
|
|
, markNgButton(new QPushButton("Mark NG", this))
|
|
, stopButton(new QPushButton("Stop", this))
|
|
, processBox(new QGroupBox("Process timing", 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);
|
|
|
|
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("Current process:", currentProcessLabel);
|
|
processSummaryLayout->addRow("Elapsed:", processElapsedLabel);
|
|
processSummaryLayout->addRow("Total:", processTotalElapsedLabel);
|
|
|
|
QVBoxLayout *processLayout = new QVBoxLayout(processBox);
|
|
processLayout->addLayout(processSummaryLayout);
|
|
processTable->setColumnCount(4);
|
|
processTable->setHorizontalHeaderLabels(
|
|
QStringList() << "Process" << "Status" << "Attempt" << "Duration");
|
|
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);
|
|
ui->deviceLayout->addWidget(UiHelpers::makeStatusCell("检测机\n待机", "#607d8b"), 0, 0, 2, 2);
|
|
ui->deviceLayout->addWidget(UiHelpers::makeStatusCell("总控电脑", "#43a047"), 0, 3);
|
|
ui->deviceLayout->addWidget(UiHelpers::makeStatusCell("安全门1\n关闭", "#2e7d32"), 0, 4);
|
|
QLabel *robotCell = UiHelpers::makeStatusCell("机器人\n空闲", "#f9a825");
|
|
robotCell->setFixedSize(82, 58);
|
|
ui->deviceLayout->addWidget(robotCell, 2, 3, 2, 2, Qt::AlignCenter);
|
|
ui->deviceLayout->addWidget(UiHelpers::makeStatusCell("机器人\n控制柜", "#546e7a"), 3, 0);
|
|
ui->deviceLayout->addWidget(UiHelpers::makeStatusCell("总控柜", "#546e7a"), 4, 0);
|
|
ui->deviceLayout->addWidget(UiHelpers::makeStatusCell("安全门2\n关闭", "#2e7d32"), 2, 5);
|
|
ui->deviceLayout->addWidget(UiHelpers::makeStatusCell("安全门3\n关闭", "#2e7d32"), 5, 0);
|
|
|
|
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%2/49").arg(i + 1).arg(49 - i * 4),
|
|
i == 0 ? "#00897b" : "#78909c");
|
|
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(&DeviceService::instance(), &DeviceService::statusChanged,
|
|
this, &DashboardPage::onDeviceStatusChanged);
|
|
|
|
productionThread->start();
|
|
QMetaObject::invokeMethod(productionWorker, "requestRestore",
|
|
Qt::QueuedConnection);
|
|
|
|
loadOrderOptions();
|
|
DeviceService::instance().startPolling();
|
|
processRefreshTimer->setInterval(1000);
|
|
connect(processRefreshTimer, &QTimer::timeout,
|
|
this, &DashboardPage::refreshProcessRecords);
|
|
processRefreshTimer->start();
|
|
refreshProcessRecords();
|
|
}
|
|
|
|
DashboardPage::~DashboardPage()
|
|
{
|
|
if (productionThread->isRunning()) {
|
|
productionThread->quit();
|
|
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()
|
|
{
|
|
int selectedOrderId = orderCombo->currentData().toInt();
|
|
if (selectedOrderId <= 0) {
|
|
loadOrderOptions();
|
|
selectedOrderId = orderCombo->currentData().toInt();
|
|
}
|
|
if (selectedOrderId <= 0) {
|
|
QMessageBox::information(this, "Start production",
|
|
"No prepared order is available");
|
|
return;
|
|
}
|
|
|
|
if (productionState == ProductionService::Paused) {
|
|
QMetaObject::invokeMethod(productionWorker, "requestResume",
|
|
Qt::QueuedConnection);
|
|
} else {
|
|
QMetaObject::invokeMethod(
|
|
productionWorker, "requestStart", Qt::QueuedConnection,
|
|
Q_ARG(int, selectedOrderId));
|
|
}
|
|
}
|
|
|
|
void DashboardPage::pauseProduction()
|
|
{
|
|
QMetaObject::invokeMethod(productionWorker, "requestPause",
|
|
Qt::QueuedConnection);
|
|
}
|
|
|
|
void DashboardPage::stopProduction()
|
|
{
|
|
QMetaObject::invokeMethod(productionWorker, "requestStop",
|
|
Qt::QueuedConnection);
|
|
}
|
|
|
|
void DashboardPage::resetProduction()
|
|
{
|
|
QMetaObject::invokeMethod(productionWorker, "requestReset",
|
|
Qt::QueuedConnection);
|
|
}
|
|
|
|
void DashboardPage::clearAlarm()
|
|
{
|
|
QString error;
|
|
if (!DeviceService::instance().clearAlarm(&error)) {
|
|
QMessageBox::warning(this, "Clear alarm failed", error);
|
|
emit logRequested("Alarm log", "Dashboard", "Clear alarm",
|
|
error, "Failed");
|
|
return;
|
|
}
|
|
if (!ProductionRepository().clearActiveAlarms(
|
|
QString(), "OPERATOR", &error)) {
|
|
QMessageBox::warning(this, "Clear alarm failed", error);
|
|
emit logRequested("Alarm log", "Dashboard", "Clear alarm",
|
|
error, "Failed");
|
|
return;
|
|
}
|
|
emit logRequested("Alarm log", "Dashboard", "Clear alarm",
|
|
"PLC alarm clear command completed", "Success");
|
|
}
|
|
|
|
void DashboardPage::onOrderChanged(int)
|
|
{
|
|
updateOrderSummary();
|
|
}
|
|
|
|
void DashboardPage::onProductionStateChanged(ProductionService::State state,
|
|
const QString &name)
|
|
{
|
|
productionState = state;
|
|
ui->actionLabel->setText("Production state: " + name);
|
|
updateRecoveryButtons();
|
|
emit systemStateChanged(name);
|
|
emit currentStepChanged(name);
|
|
refreshProcessRecords();
|
|
updateProductionStats();
|
|
}
|
|
|
|
void DashboardPage::onCurrentWorkpieceChanged(int workpieceId,
|
|
const QString &barcode)
|
|
{
|
|
if (workpieceId <= 0) {
|
|
ui->workpieceLabel->setText("Current workpiece: -");
|
|
ui->trayLabel->setText("Current tray: -");
|
|
currentProcessElapsedMs = 0;
|
|
} else {
|
|
ui->workpieceLabel->setText(
|
|
QString("Current workpiece: %1").arg(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("Current tray: %1 / slot: %2").arg(trayNo).arg(slotNo)
|
|
: "Current tray: -");
|
|
}
|
|
|
|
void DashboardPage::onProductionError(const QString &error)
|
|
{
|
|
ui->actionLabel->setText("Production error: " + error);
|
|
emit systemStateChanged("ERROR");
|
|
emit logRequested("Alarm log", "Production", "Production error",
|
|
error, "Failed");
|
|
}
|
|
|
|
void DashboardPage::onProductionFinished(int orderId)
|
|
{
|
|
emit logRequested("Production log", "Production", "Order completed",
|
|
QString("Order %1 completed").arg(orderId), "Success");
|
|
loadOrderOptions();
|
|
updateProductionStats();
|
|
}
|
|
|
|
void DashboardPage::onProcessElapsedChanged(const QString &processStep,
|
|
int elapsedMs)
|
|
{
|
|
currentProcessElapsedMs = elapsedMs;
|
|
currentProcessLabel->setText(processStep);
|
|
processElapsedLabel->setText(formatDuration(elapsedMs));
|
|
updateCurrentProcessDuration(elapsedMs);
|
|
}
|
|
|
|
void DashboardPage::onTimeoutRecoveryRequired(const QString &state,
|
|
const QString &message)
|
|
{
|
|
ui->actionLabel->setText("Manual recovery required: " + message);
|
|
emit logRequested("Operation log", "Production", "Manual recovery",
|
|
state + ": " + message, "Pending");
|
|
}
|
|
|
|
void DashboardPage::onRecoveryCompleted(const QString &mode)
|
|
{
|
|
emit logRequested("Production log", "Production", "Recovery completed",
|
|
mode, "Success");
|
|
}
|
|
|
|
void DashboardPage::recoverProduction()
|
|
{
|
|
if (productionState == ProductionService::ManualConfirm
|
|
&& QMessageBox::question(
|
|
this, "Continue production",
|
|
"Continue only after confirming the equipment and current PLC step are safe?",
|
|
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, "Mark workpiece NG",
|
|
"Mark the current workpiece as NG and continue with the next workpiece?",
|
|
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;
|
|
recoverButton->setVisible(recoveryRequired);
|
|
retryButton->setVisible(recoveryRequired);
|
|
markNgButton->setVisible(recoveryRequired
|
|
&& productionWorkpiece.id > 0);
|
|
stopButton->setVisible(running);
|
|
recoverButton->setText(state == ProductionService::ManualConfirm
|
|
? "Continue" : "Recover");
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
processTable->setItem(row, 0,
|
|
new QTableWidgetItem(record.processStep));
|
|
processTable->setItem(row, 1,
|
|
new QTableWidgetItem(record.processState));
|
|
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(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->text() != workpiece.processStep) {
|
|
continue;
|
|
}
|
|
QTableWidgetItem *statusItem = processTable->item(row, 1);
|
|
if (!statusItem || statusItem->text() != "RUNNING") {
|
|
continue;
|
|
}
|
|
processTable->setItem(row, 3,
|
|
new QTableWidgetItem(formatDuration(elapsedMs)));
|
|
break;
|
|
}
|
|
}
|
|
|
|
void DashboardPage::onDeviceStatusChanged(const DeviceStatus &status)
|
|
{
|
|
deviceStatusLabel->setText(
|
|
QString("PLC: %1 | Robot: %2 | Safety door: %3 | E-stop: %4")
|
|
.arg(status.plcConnected ? "ONLINE" : "OFFLINE")
|
|
.arg(status.robotReady ? "READY" : "NOT_READY")
|
|
.arg(status.safetyDoorClosed ? "CLOSED" : "OPEN")
|
|
.arg(status.emergencyStop ? "ACTIVE" : "CLEAR"));
|
|
}
|
|
|
|
void DashboardPage::loadOrderOptions()
|
|
{
|
|
QString error;
|
|
const QList<Order> orders = OrderService::instance().listOrders(&error);
|
|
if (!error.isEmpty()) {
|
|
orderCombo->clear();
|
|
orderCombo->addItem("No order data", 0);
|
|
emit logRequested("Operation log", "Dashboard", "Load orders",
|
|
error, "Failed");
|
|
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("No prepared order", 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("Current order: -");
|
|
ui->trayLabel->setText("Current tray: -");
|
|
ui->workpieceLabel->setText("Current workpiece: -");
|
|
updateProductionStats();
|
|
return;
|
|
}
|
|
|
|
Order order;
|
|
QString error;
|
|
if (!OrderService::instance().findOrder(selectedOrderId, &order, &error)) {
|
|
ui->orderLabel->setText("Current order: -");
|
|
return;
|
|
}
|
|
ui->orderLabel->setText("Current order: " + 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));
|
|
}
|