2026-08-18 09:40:43 +08:00
|
|
|
#include "devicepage.h"
|
|
|
|
|
#include "ui_devicepage.h"
|
2026-11-05 14:14:29 +08:00
|
|
|
#include "uihelpers.h"
|
2026-11-09 19:30:28 +08:00
|
|
|
#include "../config/configmanager.h"
|
|
|
|
|
#include "../services/visionservice.h"
|
2026-08-18 09:40:43 +08:00
|
|
|
|
|
|
|
|
#include <QAbstractItemView>
|
|
|
|
|
#include <QDateTime>
|
|
|
|
|
#include <QDebug>
|
2026-08-24 17:28:59 +08:00
|
|
|
#include <QLabel>
|
2026-08-18 09:40:43 +08:00
|
|
|
#include <QTableWidget>
|
|
|
|
|
#include <QTableWidgetItem>
|
|
|
|
|
|
|
|
|
|
DevicePage::DevicePage(QWidget *parent)
|
|
|
|
|
: QWidget(parent)
|
|
|
|
|
, ui(new Ui::DevicePage)
|
2026-08-24 17:28:59 +08:00
|
|
|
, communicationErrorLabel(new QLabel(this))
|
2026-08-18 09:40:43 +08:00
|
|
|
{
|
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
2026-08-24 17:28:59 +08:00
|
|
|
communicationErrorLabel->setText("通信状态:未采集");
|
|
|
|
|
communicationErrorLabel->setStyleSheet("color: #607d8b; padding: 4px 0;");
|
|
|
|
|
ui->mainLayout->insertWidget(0, communicationErrorLabel);
|
2026-08-18 09:40:43 +08:00
|
|
|
setupTable();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DevicePage::setupTable()
|
|
|
|
|
{
|
|
|
|
|
QTableWidget *table = ui->deviceTable;
|
|
|
|
|
table->setColumnCount(5);
|
|
|
|
|
table->setHorizontalHeaderLabels(QStringList()
|
|
|
|
|
<< "设备/信号" << "连接状态" << "运行状态" << "当前动作" << "最后更新时间");
|
2026-11-05 14:14:29 +08:00
|
|
|
table->horizontalHeaderItem(0)->setTextAlignment(Qt::AlignCenter);
|
|
|
|
|
const int dateTimeWidth = UiHelpers::textColumnWidth(
|
|
|
|
|
table, "2026-10-30 23:59:59");
|
|
|
|
|
UiHelpers::configureTableColumns(
|
|
|
|
|
table, QList<int>() << 110 << 100 << 110 << 210 << dateTimeWidth);
|
2026-08-18 09:40:43 +08:00
|
|
|
table->verticalHeader()->setVisible(false);
|
|
|
|
|
table->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
|
|
|
|
table->setAlternatingRowColors(true);
|
|
|
|
|
|
|
|
|
|
const QStringList names = QStringList()
|
2026-11-09 19:30:28 +08:00
|
|
|
<< "PLC" << "上下料机器人" << "安全门 1" << "安全门 2"
|
|
|
|
|
<< "安全门 3" << "急停" << "PLC 活动报警"
|
|
|
|
|
<< "视觉引导系统" << "检测机";
|
2026-08-18 09:40:43 +08:00
|
|
|
table->setRowCount(names.size());
|
|
|
|
|
for (int row = 0; row < names.size(); ++row) {
|
2026-11-05 14:14:29 +08:00
|
|
|
QTableWidgetItem *item = new QTableWidgetItem(names.at(row));
|
|
|
|
|
item->setTextAlignment(Qt::AlignCenter);
|
|
|
|
|
table->setItem(row, 0, item);
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
2026-11-09 19:30:28 +08:00
|
|
|
|
|
|
|
|
const QString now = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
setRow(8, QStringList() << "检测机" << "未知" << "未知" << "未知" << now);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DevicePage::attachVisionService(VisionService *service)
|
|
|
|
|
{
|
|
|
|
|
if (!service) return;
|
|
|
|
|
connect(service, &VisionService::availabilityChanged, this,
|
|
|
|
|
[this](bool ready, bool, const QString &session) {
|
|
|
|
|
updateVisionStatus(ready, session);
|
|
|
|
|
});
|
|
|
|
|
updateVisionStatus(service->isReady(), service->sessionId());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DevicePage::updateVisionStatus(bool ready, const QString &session)
|
|
|
|
|
{
|
|
|
|
|
const bool enabled = ConfigManager::instance().visionConfig().enabled;
|
|
|
|
|
const QString now = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
if (!enabled) {
|
|
|
|
|
setRow(7, QStringList() << "视觉引导系统" << "未启用" << "未启用"
|
|
|
|
|
<< "未启用" << now);
|
|
|
|
|
} else if (session.isEmpty()) {
|
|
|
|
|
setRow(7, QStringList() << "视觉引导系统" << "离线" << "离线"
|
|
|
|
|
<< "离线" << now);
|
|
|
|
|
} else {
|
|
|
|
|
setRow(7, QStringList() << "视觉引导系统" << "在线"
|
|
|
|
|
<< (ready ? "就绪" : "在线")
|
|
|
|
|
<< "等待视觉任务" << now);
|
|
|
|
|
}
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DevicePage::setRow(int row, const QStringList &values)
|
|
|
|
|
{
|
|
|
|
|
QTableWidget *table = ui->deviceTable;
|
|
|
|
|
for (int column = 0; column < values.size(); ++column) {
|
|
|
|
|
QTableWidgetItem *item = table->item(row, column);
|
|
|
|
|
if (!item) {
|
|
|
|
|
item = new QTableWidgetItem;
|
|
|
|
|
table->setItem(row, column, item);
|
|
|
|
|
}
|
|
|
|
|
item->setText(values.at(column));
|
2026-11-05 14:14:29 +08:00
|
|
|
if (column == 0) {
|
|
|
|
|
item->setTextAlignment(Qt::AlignCenter);
|
|
|
|
|
}
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DevicePage::updateStatus(const DeviceStatus &status)
|
|
|
|
|
{
|
2026-11-09 17:25:14 +08:00
|
|
|
const bool offline = !status.statusValid || !status.plcConnected;
|
|
|
|
|
communicationErrorLabel->setText(!offline
|
2026-08-24 17:28:59 +08:00
|
|
|
? "通信状态:PLC 在线"
|
2026-11-09 17:25:14 +08:00
|
|
|
: "通信状态:PLC 离线");
|
|
|
|
|
communicationErrorLabel->setStyleSheet(!offline
|
2026-08-24 17:28:59 +08:00
|
|
|
? "color: #2e7d32; padding: 4px 0;"
|
|
|
|
|
: "color: #d32f2f; padding: 4px 0;");
|
|
|
|
|
|
2026-11-09 17:25:14 +08:00
|
|
|
const QString connection = offline ? "离线" : "在线";
|
|
|
|
|
const QString robotState = offline ? "离线"
|
|
|
|
|
: status.emergencyStop ? "急停"
|
|
|
|
|
: status.robotAlarm ? "报警"
|
|
|
|
|
: status.robotRunning ? "运行中"
|
|
|
|
|
: status.robotIdle ? "空闲" : "未就绪";
|
2026-11-09 19:30:28 +08:00
|
|
|
const QString safetyDoor1State = offline ? "离线"
|
|
|
|
|
: status.safetyDoor1Closed ? "关闭" : "打开";
|
|
|
|
|
const QString safetyDoor2State = offline ? "离线"
|
|
|
|
|
: status.safetyDoor2Closed ? "关闭" : "打开";
|
|
|
|
|
const QString safetyDoor3State = offline ? "离线"
|
|
|
|
|
: status.safetyDoor3Closed ? "关闭" : "打开";
|
|
|
|
|
const QString emergencyState = offline ? "离线"
|
|
|
|
|
: status.emergencyStop ? "已触发" : "未触发";
|
|
|
|
|
const QString alarmState = offline ? "离线"
|
|
|
|
|
: status.activeAlarm ? "有报警" : "无报警";
|
2026-08-18 09:40:43 +08:00
|
|
|
const QString now = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
|
2026-11-09 19:30:28 +08:00
|
|
|
setRow(0, QStringList() << "PLC" << connection
|
|
|
|
|
<< (offline ? "离线" : "就绪")
|
|
|
|
|
<< (offline ? "离线" : "就绪") << now);
|
2026-08-18 09:40:43 +08:00
|
|
|
setRow(1, QStringList() << "上下料机器人" << connection << robotState
|
2026-11-09 19:30:28 +08:00
|
|
|
<< (offline ? "离线"
|
2026-11-09 17:25:14 +08:00
|
|
|
: status.emergencyStop ? "急停已触发"
|
|
|
|
|
: status.robotAlarm ? "请处理机器人报警"
|
|
|
|
|
: status.robotRunning ? "执行中"
|
|
|
|
|
: status.robotIdle ? "等待动作" : "等待就绪") << now);
|
2026-11-09 19:30:28 +08:00
|
|
|
setRow(2, QStringList() << "安全门 1" << connection << safetyDoor1State
|
|
|
|
|
<< (offline ? "离线"
|
2026-08-24 17:28:59 +08:00
|
|
|
: status.safetyDoor1Closed ? "互锁正常" : "请关闭安全门") << now);
|
2026-11-09 19:30:28 +08:00
|
|
|
setRow(3, QStringList() << "安全门 2" << connection << safetyDoor2State
|
|
|
|
|
<< (offline ? "离线"
|
2026-08-24 17:28:59 +08:00
|
|
|
: status.safetyDoor2Closed ? "互锁正常" : "请关闭安全门") << now);
|
2026-11-09 19:30:28 +08:00
|
|
|
setRow(4, QStringList() << "安全门 3" << connection << safetyDoor3State
|
|
|
|
|
<< (offline ? "离线"
|
2026-08-24 17:28:59 +08:00
|
|
|
: status.safetyDoor3Closed ? "互锁正常" : "请关闭安全门") << now);
|
2026-11-09 19:30:28 +08:00
|
|
|
setRow(5, QStringList() << "急停" << connection << emergencyState
|
|
|
|
|
<< (offline ? "离线"
|
2026-08-24 17:28:59 +08:00
|
|
|
: status.emergencyStop ? "安全回路断开" : "安全回路正常") << now);
|
2026-11-09 19:30:28 +08:00
|
|
|
setRow(6, QStringList() << "PLC 活动报警" << connection << alarmState
|
|
|
|
|
<< (offline ? "离线"
|
2026-08-24 17:28:59 +08:00
|
|
|
: status.activeAlarm ? "请处理 PLC 报警" : "无活动报警") << now);
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DevicePage::handleCommunicationError(const QString &error)
|
|
|
|
|
{
|
2026-11-09 19:30:28 +08:00
|
|
|
updateStatus(DeviceStatus());
|
2026-08-24 17:28:59 +08:00
|
|
|
communicationErrorLabel->setText("通信异常:" + error);
|
|
|
|
|
communicationErrorLabel->setStyleSheet("color: #d32f2f; padding: 4px 0;");
|
2026-08-18 09:40:43 +08:00
|
|
|
qWarning() << "Device communication error:" << error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DevicePage::~DevicePage()
|
|
|
|
|
{
|
|
|
|
|
delete ui;
|
|
|
|
|
}
|