feat: 视觉引导页面优化
- 点击各个按钮后,能在界面显示操作后结果 - 对排版进行优化 - 下面的位姿拍照删除
This commit is contained in:
+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)
|
||||
|
||||
Reference in New Issue
Block a user