feat: 视觉引导页面优化
- 点击各个按钮后,能在界面显示操作后结果 - 对排版进行优化 - 下面的位姿拍照删除
This commit is contained in:
+128
-82
@@ -1,95 +1,122 @@
|
||||
#include "visionpage.h"
|
||||
#include "../services/visionservice.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QDoubleSpinBox>
|
||||
#include <QGridLayout>
|
||||
#include <QGroupBox>
|
||||
#include <QHeaderView>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QLabel>
|
||||
#include <QPushButton>
|
||||
#include <QSpinBox>
|
||||
#include <QTableWidget>
|
||||
#include <QUuid>
|
||||
#include <QVBoxLayout>
|
||||
|
||||
VisionPage::VisionPage(VisionService *service, QWidget *parent)
|
||||
: QWidget(parent), m_service(service), m_productionActive(false)
|
||||
: QWidget(parent), m_service(service), m_operationPending(false)
|
||||
{
|
||||
QVBoxLayout *layout = new QVBoxLayout(this);
|
||||
layout->setContentsMargins(18, 18, 18, 18);
|
||||
QHBoxLayout *buttons = new QHBoxLayout;
|
||||
QPushButton *start = new QPushButton("启动视觉程序", this);
|
||||
QPushButton *connectButton = new QPushButton("连接已有视觉程序", this);
|
||||
m_initialize = new QPushButton("初始化左右相机", this);
|
||||
QPushButton *show = new QPushButton("打开视觉调试窗口", this);
|
||||
QPushButton *disconnectButton = new QPushButton("断开并释放调试控制", this);
|
||||
buttons->addWidget(start);
|
||||
buttons->addWidget(connectButton);
|
||||
buttons->addWidget(m_initialize);
|
||||
buttons->addWidget(show);
|
||||
buttons->addWidget(disconnectButton);
|
||||
buttons->addStretch();
|
||||
layout->addLayout(buttons);
|
||||
m_status = new QLabel("视觉进程:未连接", this);
|
||||
m_initialization = new QLabel("相机初始化:未执行", this);
|
||||
layout->setSpacing(12);
|
||||
setStyleSheet(
|
||||
"QGroupBox#visionControlBox QPushButton { min-width: 160px; }"
|
||||
"QGroupBox#visionControlBox QPushButton:pressed {"
|
||||
" background: #cfd8dc; border-color: #607d8b; padding-top: 7px; padding-bottom: 3px; }"
|
||||
"QGroupBox#visionControlBox QPushButton:disabled {"
|
||||
" color: #90a4ae; background: #eceff1; border-color: #cfd8dc; }");
|
||||
|
||||
QGroupBox *control = new QGroupBox("视觉程序控制", this);
|
||||
control->setObjectName("visionControlBox");
|
||||
QGridLayout *controlLayout = new QGridLayout(control);
|
||||
controlLayout->setHorizontalSpacing(10);
|
||||
controlLayout->setVerticalSpacing(8);
|
||||
m_start = new QPushButton("启动视觉程序", control);
|
||||
m_connect = new QPushButton("连接已有视觉程序", control);
|
||||
m_initialize = new QPushButton("初始化左右相机", control);
|
||||
m_show = new QPushButton("打开视觉调试窗口", control);
|
||||
m_disconnect = new QPushButton("断开并释放调试控制", control);
|
||||
controlLayout->addWidget(m_start, 0, 0);
|
||||
controlLayout->addWidget(m_connect, 0, 1);
|
||||
controlLayout->addWidget(m_initialize, 0, 2);
|
||||
controlLayout->addWidget(m_show, 1, 0);
|
||||
controlLayout->addWidget(m_disconnect, 1, 1);
|
||||
controlLayout->setColumnStretch(0, 1);
|
||||
controlLayout->setColumnStretch(1, 1);
|
||||
controlLayout->setColumnStretch(2, 1);
|
||||
m_operation = new QLabel("最近操作:等待操作", control);
|
||||
m_operation->setObjectName("visionOperationFeedback");
|
||||
m_operation->setMinimumHeight(38);
|
||||
m_operation->setWordWrap(true);
|
||||
controlLayout->addWidget(m_operation, 2, 0, 1, 3);
|
||||
showOperation("info", "等待操作");
|
||||
layout->addWidget(control);
|
||||
|
||||
QGroupBox *status = new QGroupBox("运行状态", this);
|
||||
QVBoxLayout *statusLayout = new QVBoxLayout(status);
|
||||
statusLayout->setSpacing(7);
|
||||
m_status = new QLabel("视觉进程:未连接", status);
|
||||
m_initialization = new QLabel("相机初始化:未执行", status);
|
||||
m_initialization->setWordWrap(true);
|
||||
m_cycle = new QLabel("自动流程:未开始", this);
|
||||
QLabel *parameters = new QLabel(
|
||||
"左相机 · 高精细化 · 测量截止 5 秒 · MinScore 0.70 · Greediness 0.80 · 中心选择 · 模型原点 · 抓取偏移 4.40 mm",
|
||||
status);
|
||||
parameters->setWordWrap(true);
|
||||
m_cycle = new QLabel("自动流程:未开始", status);
|
||||
m_cycle->setWordWrap(true);
|
||||
m_result = new QLabel("本次结果:无", this);
|
||||
m_result = new QLabel("本次结果:无", status);
|
||||
m_result->setWordWrap(true);
|
||||
layout->addWidget(m_status);
|
||||
layout->addWidget(m_initialization);
|
||||
layout->addWidget(new QLabel("左相机 · 高精细化 · 测量截止 5 秒 · MinScore 0.70 · Greediness 0.80 · 中心选择 · 模型原点 · 抓取偏移 4.40 mm", this));
|
||||
layout->addWidget(m_cycle);
|
||||
layout->addWidget(m_result);
|
||||
m_poses = new QTableWidget(3, 6, this);
|
||||
statusLayout->addWidget(m_status);
|
||||
statusLayout->addWidget(m_initialization);
|
||||
statusLayout->addWidget(parameters);
|
||||
statusLayout->addWidget(m_cycle);
|
||||
statusLayout->addWidget(m_result);
|
||||
layout->addWidget(status);
|
||||
|
||||
QGroupBox *poses = new QGroupBox("位姿结果", this);
|
||||
QVBoxLayout *poseLayout = new QVBoxLayout(poses);
|
||||
m_poses = new QTableWidget(3, 6, poses);
|
||||
m_poses->setHorizontalHeaderLabels(QStringList() << "X" << "Y" << "Z" << "A" << "B" << "C");
|
||||
m_poses->setVerticalHeaderLabels(QStringList() << "R40 拍照位姿" << "相机模型原点" << "R60 抓取目标");
|
||||
m_poses->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||||
m_poses->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
|
||||
m_poses->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
|
||||
m_poses->setMinimumHeight(190);
|
||||
for (int row = 0; row < 3; ++row)
|
||||
for (int col = 0; col < 6; ++col)
|
||||
m_poses->setItem(row, col, new QTableWidgetItem("—"));
|
||||
layout->addWidget(m_poses);
|
||||
layout->addWidget(new QLabel("机器人坐标单位:mm / °。相机姿态沿用 VisionGuide 原始类型;显示抓取目标不代表 PLC 已写入或机器人已接收。", this));
|
||||
poseLayout->addWidget(m_poses);
|
||||
QLabel *units = new QLabel(
|
||||
"机器人坐标单位:mm / °。相机姿态沿用 VisionGuide 原始类型;显示抓取目标不代表 PLC 已写入或机器人已接收。",
|
||||
poses);
|
||||
units->setWordWrap(true);
|
||||
poseLayout->addWidget(units);
|
||||
layout->addWidget(poses, 1);
|
||||
|
||||
QGroupBox *diagnostic = new QGroupBox("手动诊断(只测量,不写 R60)", this);
|
||||
QGridLayout *form = new QGridLayout(diagnostic);
|
||||
form->addWidget(new QLabel("视觉料盘", diagnostic), 0, 0);
|
||||
m_tray = new QSpinBox(diagnostic);
|
||||
m_tray->setRange(1, 99);
|
||||
form->addWidget(m_tray, 0, 1);
|
||||
const QStringList axes = QStringList() << "X" << "Y" << "Z" << "A" << "B" << "C";
|
||||
for (int i = 0; i < 6; ++i) {
|
||||
form->addWidget(new QLabel(axes.at(i), diagnostic), 1, i);
|
||||
m_diagnosticPose[i] = new QDoubleSpinBox(diagnostic);
|
||||
m_diagnosticPose[i]->setRange(-1000000, 1000000);
|
||||
m_diagnosticPose[i]->setDecimals(6);
|
||||
form->addWidget(m_diagnosticPose[i], 2, i);
|
||||
}
|
||||
m_diagnose = new QPushButton("使用上述拍照位姿测量", diagnostic);
|
||||
form->addWidget(m_diagnose, 3, 0, 1, 3);
|
||||
layout->addWidget(diagnostic);
|
||||
layout->addStretch();
|
||||
connect(start, &QPushButton::clicked, service, &VisionService::startService);
|
||||
connect(connectButton, &QPushButton::clicked, service, &VisionService::connectService);
|
||||
connect(m_initialize, &QPushButton::clicked, service, &VisionService::initialize);
|
||||
connect(show, &QPushButton::clicked, service, &VisionService::showWindow);
|
||||
connect(disconnectButton, &QPushButton::clicked, service, &VisionService::disconnectService);
|
||||
connect(m_diagnose, &QPushButton::clicked, this, [this]() {
|
||||
QJsonArray pose;
|
||||
for (QDoubleSpinBox *axis : m_diagnosticPose) pose.append(axis->value());
|
||||
const QString id = QUuid::createUuid().toString(QUuid::WithoutBraces);
|
||||
QJsonObject request{{"request_id", id}, {"robot_pose", pose},
|
||||
{"context", QJsonObject{{"workpiece_id", "diagnostic"}, {"task_id", "diagnostic"},
|
||||
{"tray_no", m_tray->value()}, {"generation", id}}},
|
||||
{"pose_source", "diagnostic"}, {"pose_read_at", QDateTime::currentDateTimeUtc().toString(Qt::ISODateWithMs)},
|
||||
{"parameters", VisionService::standardParameters()}, {"convention", VisionService::standardConvention()},
|
||||
{"require_robot_pose", true}};
|
||||
m_service->diagnose(request);
|
||||
connect(m_start, &QPushButton::clicked, this, [this]() {
|
||||
beginOperation("start", "正在启动或连接视觉程序…");
|
||||
m_service->startService();
|
||||
});
|
||||
connect(m_connect, &QPushButton::clicked, this, [this]() {
|
||||
beginOperation("connect", "正在连接已有视觉程序…");
|
||||
m_service->connectService();
|
||||
});
|
||||
connect(m_initialize, &QPushButton::clicked, this, [this]() {
|
||||
beginOperation("initialize", "正在初始化左右相机…");
|
||||
m_service->initialize();
|
||||
});
|
||||
connect(m_show, &QPushButton::clicked, this, [this]() {
|
||||
beginOperation("show_window", "正在请求显示视觉调试窗口…");
|
||||
m_service->showWindow();
|
||||
});
|
||||
connect(m_disconnect, &QPushButton::clicked, this, [this]() {
|
||||
beginOperation("disconnect", "正在断开视觉控制…");
|
||||
m_service->disconnectService();
|
||||
});
|
||||
connect(service, &VisionService::operationFeedback, this,
|
||||
[this](const QString &operation, const QString &state, const QString &message) {
|
||||
if (!m_operationPending || operation != m_pendingOperation)
|
||||
return;
|
||||
m_operationPending = false;
|
||||
showOperation(state, message);
|
||||
refreshControls();
|
||||
});
|
||||
connect(service, &VisionService::availabilityChanged, this, [this](bool ready, bool busy, const QString &session) {
|
||||
m_status->setText(session.isEmpty() ? "视觉进程:未连接"
|
||||
@@ -115,14 +142,11 @@ VisionPage::VisionPage(VisionService *service, QWidget *parent)
|
||||
});
|
||||
connect(service, &VisionService::requestStarted, this, [this](const QJsonObject &request) {
|
||||
invalidateResult();
|
||||
m_poses->verticalHeaderItem(0)->setText(request.value("pose_source").toString() == "diagnostic"
|
||||
? "诊断拍照位姿" : "R40 拍照位姿");
|
||||
showPose(0, request.value("robot_pose"));
|
||||
const QJsonObject context = request.value("context").toObject();
|
||||
m_cycle->setText(QString("请求 %1;工件 %2;视觉料盘 %3;等待测量结果%4")
|
||||
m_cycle->setText(QString("请求 %1;工件 %2;视觉料盘 %3;等待测量结果")
|
||||
.arg(request.value("request_id").toString(), context.value("workpiece_id").toString())
|
||||
.arg(context.value("tray_no").toInt())
|
||||
.arg(request.value("pose_source").toString() == "diagnostic" ? "(诊断)" : ""));
|
||||
.arg(context.value("tray_no").toInt()));
|
||||
});
|
||||
connect(service, &VisionService::measurementFinished, this, [this](const QJsonObject &result) {
|
||||
if (result.value("status").toString() != "ok") {
|
||||
@@ -135,12 +159,38 @@ VisionPage::VisionPage(VisionService *service, QWidget *parent)
|
||||
m_result->setText(QString("视觉计算成功;匹配分数 %1;测量 %2 ms;抓取计算 %3 ms;等待生产流程确认提交状态")
|
||||
.arg(result.value("score").toDouble(), 0, 'f', 3)
|
||||
.arg(result.value("measure_elapsed_ms").toInt()).arg(result.value("grasp_elapsed_ms").toInt()));
|
||||
if (result.value("context").toObject().value("task_id").toString() == "diagnostic")
|
||||
m_result->setText(m_result->text().replace("等待生产流程确认提交状态", "诊断结果,未写入 PLC"));
|
||||
});
|
||||
refreshControls();
|
||||
}
|
||||
|
||||
void VisionPage::beginOperation(const QString &operation, const QString &message)
|
||||
{
|
||||
m_pendingOperation = operation;
|
||||
m_operationPending = true;
|
||||
showOperation("pending", message);
|
||||
refreshControls();
|
||||
}
|
||||
|
||||
void VisionPage::showOperation(const QString &state, const QString &message)
|
||||
{
|
||||
QString color = "#546e7a";
|
||||
QString background = "#eceff1";
|
||||
if (state == "pending") {
|
||||
color = "#1565c0";
|
||||
background = "#e3f2fd";
|
||||
} else if (state == "success") {
|
||||
color = "#2e7d32";
|
||||
background = "#e8f5e9";
|
||||
} else if (state == "error") {
|
||||
color = "#c62828";
|
||||
background = "#ffebee";
|
||||
}
|
||||
m_operation->setText("最近操作:" + message);
|
||||
m_operation->setStyleSheet(QString(
|
||||
"QLabel { color: %1; background: %2; border: 1px solid %1; border-radius: 4px; padding: 7px 10px; }")
|
||||
.arg(color, background));
|
||||
}
|
||||
|
||||
void VisionPage::showPose(int row, const QJsonValue &pose)
|
||||
{
|
||||
const QJsonArray values = pose.toArray();
|
||||
@@ -163,16 +213,12 @@ void VisionPage::updateCycle(const QJsonObject &cycle)
|
||||
if (cycle.value("invalidated").toBool()) invalidateResult();
|
||||
}
|
||||
|
||||
void VisionPage::setProductionActive(bool active)
|
||||
{
|
||||
m_productionActive = active;
|
||||
refreshControls();
|
||||
}
|
||||
|
||||
void VisionPage::refreshControls()
|
||||
{
|
||||
m_initialize->setEnabled(m_service->canInitialize());
|
||||
m_diagnose->setEnabled(!m_productionActive && m_service->isReady() && !m_service->isBusy());
|
||||
m_tray->setEnabled(!m_productionActive);
|
||||
for (QDoubleSpinBox *axis : m_diagnosticPose) axis->setEnabled(!m_productionActive);
|
||||
m_start->setEnabled(!m_operationPending || m_pendingOperation != "start");
|
||||
m_connect->setEnabled(!m_operationPending || m_pendingOperation != "connect");
|
||||
m_initialize->setEnabled(m_service->canInitialize()
|
||||
&& (!m_operationPending || m_pendingOperation != "initialize"));
|
||||
m_show->setEnabled(!m_operationPending || m_pendingOperation != "show_window");
|
||||
m_disconnect->setEnabled(!m_operationPending || m_pendingOperation != "disconnect");
|
||||
}
|
||||
|
||||
+9
-7
@@ -7,8 +7,6 @@
|
||||
class QLabel;
|
||||
class QPushButton;
|
||||
class QTableWidget;
|
||||
class QDoubleSpinBox;
|
||||
class QSpinBox;
|
||||
class VisionService;
|
||||
|
||||
class VisionPage : public QWidget
|
||||
@@ -19,9 +17,10 @@ public:
|
||||
|
||||
public slots:
|
||||
void updateCycle(const QJsonObject &cycle);
|
||||
void setProductionActive(bool active);
|
||||
|
||||
private:
|
||||
void beginOperation(const QString &operation, const QString &message);
|
||||
void showOperation(const QString &state, const QString &message);
|
||||
void showPose(int row, const QJsonValue &pose);
|
||||
void invalidateResult();
|
||||
void refreshControls();
|
||||
@@ -31,12 +30,15 @@ private:
|
||||
QLabel *m_initialization;
|
||||
QLabel *m_cycle;
|
||||
QLabel *m_result;
|
||||
QLabel *m_operation;
|
||||
QTableWidget *m_poses;
|
||||
QPushButton *m_start;
|
||||
QPushButton *m_connect;
|
||||
QPushButton *m_initialize;
|
||||
QPushButton *m_diagnose;
|
||||
QSpinBox *m_tray;
|
||||
QDoubleSpinBox *m_diagnosticPose[6];
|
||||
bool m_productionActive;
|
||||
QPushButton *m_show;
|
||||
QPushButton *m_disconnect;
|
||||
QString m_pendingOperation;
|
||||
bool m_operationPending;
|
||||
};
|
||||
|
||||
#endif // VISIONPAGE_H
|
||||
|
||||
+101
-12
@@ -61,6 +61,10 @@ VisionService::VisionService(const VisionConfig &config, QObject *parent)
|
||||
});
|
||||
connect(m_socket, &QLocalSocket::readyRead, this, &VisionService::readMessages);
|
||||
connect(m_socket, &QLocalSocket::disconnected, this, [this]() {
|
||||
if (m_disconnectRequested) {
|
||||
finishDisconnect();
|
||||
return;
|
||||
}
|
||||
if (m_connecting) {
|
||||
// The previous controller may still be disconnecting. Retry hello
|
||||
// within the original deadline without launching another process.
|
||||
@@ -181,9 +185,22 @@ QString VisionService::sessionId() const { return m_session; }
|
||||
|
||||
void VisionService::connectService()
|
||||
{
|
||||
if (m_connecting || m_socket->state() == QLocalSocket::ConnectedState)
|
||||
beginConnection("connect", false);
|
||||
}
|
||||
|
||||
void VisionService::beginConnection(const QString &operation, bool startIfUnavailable)
|
||||
{
|
||||
if (!m_session.isEmpty()) {
|
||||
emit operationFeedback(operation, "success", "视觉程序已连接");
|
||||
return;
|
||||
}
|
||||
if (m_connecting || m_socket->state() != QLocalSocket::UnconnectedState) {
|
||||
emit operationFeedback(operation, "info", "视觉程序正在连接,请稍候");
|
||||
return;
|
||||
}
|
||||
m_shuttingDown = false;
|
||||
m_connectionOperation = operation;
|
||||
m_startIfUnavailable = startIfUnavailable;
|
||||
m_connecting = true;
|
||||
m_connectElapsed.start();
|
||||
m_connectTimer->start();
|
||||
@@ -193,9 +210,18 @@ void VisionService::connectService()
|
||||
void VisionService::disconnectService()
|
||||
{
|
||||
if (m_productionActive && !m_maintenanceAllowed) {
|
||||
emit logMessage("请先停止生产,再断开视觉控制连接");
|
||||
const QString message = "请先停止生产,再断开视觉控制连接";
|
||||
emit logMessage(message);
|
||||
emit operationFeedback("disconnect", "error", message);
|
||||
return;
|
||||
}
|
||||
if (!m_connecting && m_session.isEmpty()
|
||||
&& m_socket->state() == QLocalSocket::UnconnectedState) {
|
||||
emit operationFeedback("disconnect", "info", "视觉控制未连接");
|
||||
return;
|
||||
}
|
||||
m_disconnectRequested = true;
|
||||
m_disconnectCanceledConnection = m_connecting && m_session.isEmpty();
|
||||
if (!m_request.isEmpty()) cancel(m_request.value("request_id").toString());
|
||||
m_startIfUnavailable = false;
|
||||
m_connecting = false;
|
||||
@@ -203,15 +229,23 @@ void VisionService::disconnectService()
|
||||
m_pollTimer->stop();
|
||||
m_socket->disconnectFromServer();
|
||||
if (m_socket->state() == QLocalSocket::UnconnectedState)
|
||||
failConnection("视觉控制连接已断开,可在视觉窗口手动调试");
|
||||
finishDisconnect();
|
||||
}
|
||||
|
||||
void VisionService::startService()
|
||||
{
|
||||
if (m_connecting || m_socket->state() == QLocalSocket::ConnectedState)
|
||||
return;
|
||||
m_startIfUnavailable = m_process->state() == QProcess::NotRunning;
|
||||
connectService();
|
||||
beginConnection("start", m_process->state() == QProcess::NotRunning);
|
||||
}
|
||||
|
||||
void VisionService::finishDisconnect()
|
||||
{
|
||||
const bool canceledConnection = m_disconnectCanceledConnection;
|
||||
m_disconnectRequested = false;
|
||||
m_disconnectCanceledConnection = false;
|
||||
failConnection(canceledConnection ? "视觉连接已取消"
|
||||
: "视觉控制连接已断开,可在视觉窗口手动调试");
|
||||
emit operationFeedback("disconnect", "success",
|
||||
canceledConnection ? "视觉连接已取消" : "视觉控制已断开");
|
||||
}
|
||||
|
||||
void VisionService::launchProcess()
|
||||
@@ -336,6 +370,10 @@ void VisionService::handleMessage(const QJsonObject &message)
|
||||
m_lastReply.restart();
|
||||
updateAvailability(message);
|
||||
emit logMessage("视觉通信已连接,等待初始化/就绪");
|
||||
const QString operation = m_connectionOperation;
|
||||
m_connectionOperation.clear();
|
||||
if (!operation.isEmpty())
|
||||
emit operationFeedback(operation, "success", "视觉程序已连接");
|
||||
return;
|
||||
}
|
||||
// The server may publish its initial state immediately after accepting the socket.
|
||||
@@ -380,9 +418,21 @@ void VisionService::handleMessage(const QJsonObject &message)
|
||||
if (type == "initialize" && id == m_initializeId && status != "accepted") {
|
||||
m_initializeTimer->stop();
|
||||
m_initializeId.clear();
|
||||
emit logMessage(status == "ok" ? "视觉初始化完成" : "视觉初始化失败:" + message.value("message").toString());
|
||||
const QString detail = message.value("message").toString();
|
||||
const QString text = status == "ok" ? "视觉初始化完成"
|
||||
: "视觉初始化失败:" + (detail.isEmpty() ? "视觉程序未返回失败原因" : detail);
|
||||
emit logMessage(text);
|
||||
emit operationFeedback("initialize", status == "ok" ? "success" : "error", text);
|
||||
publishAvailability();
|
||||
}
|
||||
if (type == "show_window" && id == m_showWindowId && status != "accepted") {
|
||||
m_showWindowId.clear();
|
||||
const QString detail = message.value("message").toString();
|
||||
emit operationFeedback("show_window", status == "ok" ? "success" : "error",
|
||||
status == "ok" ? "显示窗口命令已受理"
|
||||
: "显示窗口失败:" + (detail.isEmpty()
|
||||
? "视觉程序未返回失败原因" : detail));
|
||||
}
|
||||
if (status == "error")
|
||||
emit logMessage(message.value("error_code").toString() + ":" + message.value("message").toString());
|
||||
}
|
||||
@@ -390,12 +440,18 @@ void VisionService::handleMessage(const QJsonObject &message)
|
||||
void VisionService::initialize()
|
||||
{
|
||||
if (!canInitialize()) {
|
||||
emit logMessage("初始化需要已连接、资源空闲且生产已停止");
|
||||
const QString message = "初始化需要已连接、资源空闲且生产已停止";
|
||||
emit logMessage(message);
|
||||
emit operationFeedback("initialize", "error", message);
|
||||
return;
|
||||
}
|
||||
m_ready = false;
|
||||
m_initializeId = sendCommand("initialize");
|
||||
if (!m_initializeId.isEmpty()) m_initializeTimer->start(m_config.initializeTimeoutMs);
|
||||
if (m_initializeId.isEmpty()) {
|
||||
emit operationFeedback("initialize", "error", "发送视觉初始化命令失败");
|
||||
} else {
|
||||
m_initializeTimer->start(m_config.initializeTimeoutMs);
|
||||
}
|
||||
publishAvailability();
|
||||
}
|
||||
|
||||
@@ -499,7 +555,27 @@ void VisionService::setProductionActive(bool active, bool maintenanceAllowed)
|
||||
|
||||
void VisionService::showWindow()
|
||||
{
|
||||
if (!m_session.isEmpty()) sendCommand("show_window");
|
||||
if (m_session.isEmpty()) {
|
||||
emit operationFeedback("show_window", "error", "视觉程序未连接");
|
||||
return;
|
||||
}
|
||||
if (!m_showWindowId.isEmpty()) {
|
||||
emit operationFeedback("show_window", "info", "显示窗口命令正在等待响应");
|
||||
return;
|
||||
}
|
||||
m_showWindowId = sendCommand("show_window");
|
||||
if (m_showWindowId.isEmpty()) {
|
||||
emit operationFeedback("show_window", "error", "发送显示窗口命令失败");
|
||||
return;
|
||||
}
|
||||
const QString requestId = m_showWindowId;
|
||||
QTimer::singleShot(qMax(1, m_config.requestTimeoutMs), this, [this, requestId]() {
|
||||
if (m_showWindowId != requestId)
|
||||
return;
|
||||
m_commands.remove(requestId);
|
||||
m_showWindowId.clear();
|
||||
emit operationFeedback("show_window", "error", "等待显示窗口命令响应超时");
|
||||
});
|
||||
}
|
||||
|
||||
void VisionService::shutdown()
|
||||
@@ -556,6 +632,9 @@ void VisionService::finishMeasurement(const QJsonObject &result)
|
||||
void VisionService::failConnection(const QString &message)
|
||||
{
|
||||
const bool hadConnection = m_connecting || !m_session.isEmpty() || !m_request.isEmpty();
|
||||
const QString connectionOperation = m_connectionOperation;
|
||||
const bool initializationPending = !m_initializeId.isEmpty();
|
||||
const bool showWindowPending = !m_showWindowId.isEmpty();
|
||||
m_startIfUnavailable = false;
|
||||
m_connecting = false;
|
||||
m_connectTimer->stop();
|
||||
@@ -564,6 +643,8 @@ void VisionService::failConnection(const QString &message)
|
||||
m_ready = false;
|
||||
m_busy = false;
|
||||
m_initializeId.clear();
|
||||
m_connectionOperation.clear();
|
||||
m_showWindowId.clear();
|
||||
m_commands.clear();
|
||||
m_session.clear();
|
||||
m_serverProcessId.clear();
|
||||
@@ -571,7 +652,15 @@ void VisionService::failConnection(const QString &message)
|
||||
failMeasurement("DISCONNECTED", message);
|
||||
m_socket->abort();
|
||||
publishAvailability();
|
||||
if (hadConnection && !m_shuttingDown) emit logMessage(message);
|
||||
if (hadConnection && !m_shuttingDown) {
|
||||
emit logMessage(message);
|
||||
if (!connectionOperation.isEmpty())
|
||||
emit operationFeedback(connectionOperation, "error", message);
|
||||
if (initializationPending)
|
||||
emit operationFeedback("initialize", "error", message);
|
||||
if (showWindowPending)
|
||||
emit operationFeedback("show_window", "error", message);
|
||||
}
|
||||
}
|
||||
|
||||
void VisionService::updateAvailability(const QJsonObject &message)
|
||||
|
||||
@@ -42,9 +42,13 @@ signals:
|
||||
void requestStarted(const QJsonObject &request);
|
||||
void statusChanged(const QJsonObject &status);
|
||||
void availabilityChanged(bool ready, bool busy, const QString &session);
|
||||
void operationFeedback(const QString &operation, const QString &state,
|
||||
const QString &message);
|
||||
void logMessage(const QString &message);
|
||||
|
||||
private:
|
||||
void beginConnection(const QString &operation, bool startIfUnavailable);
|
||||
void finishDisconnect();
|
||||
void launchProcess();
|
||||
void connectAttempt();
|
||||
void readMessages();
|
||||
@@ -73,6 +77,8 @@ private:
|
||||
QString m_serverProcessId;
|
||||
QString m_helloId;
|
||||
QString m_initializeId;
|
||||
QString m_connectionOperation;
|
||||
QString m_showWindowId;
|
||||
QHash<QString, QString> m_commands;
|
||||
QSet<QString> m_cancelledRequests;
|
||||
QSet<QString> m_completedRequests;
|
||||
@@ -85,6 +91,8 @@ private:
|
||||
bool m_diagnostic;
|
||||
bool m_connecting;
|
||||
bool m_startIfUnavailable = false;
|
||||
bool m_disconnectRequested = false;
|
||||
bool m_disconnectCanceledConnection = false;
|
||||
quint64 m_processGeneration = 0;
|
||||
bool m_shuttingDown;
|
||||
};
|
||||
|
||||
@@ -139,7 +139,6 @@ void Widget::setupMainArea(QVBoxLayout *rootLayout)
|
||||
visionService = new VisionService(ConfigManager::instance().visionConfig(), this);
|
||||
visionPage = new VisionPage(visionService, this);
|
||||
connect(dashboardPage, &DashboardPage::visionCycleChanged, visionPage, &VisionPage::updateCycle);
|
||||
connect(dashboardPage, &DashboardPage::visionProductionActiveChanged, visionPage, &VisionPage::setProductionActive);
|
||||
dashboardPage->attachVisionService(visionService);
|
||||
|
||||
pageStack = new QStackedWidget;
|
||||
|
||||
Reference in New Issue
Block a user