feat(vision): 状态栏增加相机状态

This commit is contained in:
lidg
2026-10-29 11:57:47 +08:00
parent a7af930a0a
commit 31454722a0
9 changed files with 92 additions and 26 deletions
+54 -12
View File
@@ -51,6 +51,7 @@ VisionService::VisionService(const VisionConfig &config, QObject *parent)
m_initializeTimer->setSingleShot(true);
connect(m_connectTimer, &QTimer::timeout, this, &VisionService::connectAttempt);
connect(m_socket, &QLocalSocket::connected, this, [this]() {
m_startIfUnavailable = false;
m_connectTimer->stop();
m_buffer.clear();
m_session.clear();
@@ -60,12 +61,29 @@ VisionService::VisionService(const VisionConfig &config, QObject *parent)
});
connect(m_socket, &QLocalSocket::readyRead, this, &VisionService::readMessages);
connect(m_socket, &QLocalSocket::disconnected, this, [this]() {
if (!m_connecting)
if (m_connecting) {
// The previous controller may still be disconnecting. Retry hello
// within the original deadline without launching another process.
m_pollTimer->stop();
m_commands.clear();
m_helloId.clear();
m_buffer.clear();
m_connectTimer->start();
} else {
failConnection("视觉进程连接已断开");
}
});
// Qt 5.14 exposes error(...), not the newer errorOccurred socket signal.
connect(m_socket, QOverload<QLocalSocket::LocalSocketError>::of(&QLocalSocket::error),
this, [this](QLocalSocket::LocalSocketError) {
this, [this](QLocalSocket::LocalSocketError error) {
if (m_connecting && m_startIfUnavailable) {
m_startIfUnavailable = false;
if (error == QLocalSocket::ServerNotFoundError || error == QLocalSocket::ConnectionRefusedError)
launchProcess();
else
failConnection(m_socket->errorString());
return;
}
if (!m_connecting)
failConnection(m_socket->errorString());
});
@@ -100,8 +118,12 @@ VisionService::VisionService(const VisionConfig &config, QObject *parent)
failConnection(m_process->errorString());
});
connect(m_process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, [this](int code, QProcess::ExitStatus) {
this, [this](int code, QProcess::ExitStatus status) {
emit logMessage(QString("视觉子进程已退出,退出码 %1").arg(code));
// A simultaneous launch can lose the instance lock. Keep waiting for
// the winning process rather than aborting its pending IPC connection.
if (m_connecting && code == 0 && status == QProcess::NormalExit)
return;
if (m_serverProcessId == QString::number(m_process->processId()) || m_connecting)
failConnection("视觉子进程已退出");
});
@@ -175,6 +197,7 @@ void VisionService::disconnectService()
return;
}
if (!m_request.isEmpty()) cancel(m_request.value("request_id").toString());
m_startIfUnavailable = false;
m_connecting = false;
m_connectTimer->stop();
m_pollTimer->stop();
@@ -185,12 +208,19 @@ void VisionService::disconnectService()
void VisionService::startService()
{
if (m_process->state() != QProcess::NotRunning || m_connecting
|| m_socket->state() == QLocalSocket::ConnectedState)
if (m_connecting || m_socket->state() == QLocalSocket::ConnectedState)
return;
m_startIfUnavailable = m_process->state() == QProcess::NotRunning;
connectService();
}
void VisionService::launchProcess()
{
if (!m_connecting || m_shuttingDown)
return;
const QFileInfo executable(m_config.executablePath);
if (!executable.isFile()) {
emit logMessage("请在 config.ini 配置有效的 VisionGuide executablePath");
failConnection("请在 config.ini 配置有效的 VisionGuide executablePath");
return;
}
QProcessEnvironment environment = QProcessEnvironment::systemEnvironment();
@@ -209,9 +239,10 @@ void VisionService::startService()
environment.insert("PATH", cleanPath.join(';'));
m_process->setProcessEnvironment(environment);
m_process->setWorkingDirectory(executable.absolutePath());
++m_processGeneration;
m_connectElapsed.restart();
m_process->start(executable.absoluteFilePath(),
QStringList() << "--ipc-name" << m_config.ipcName << "--background");
connectService();
}
void VisionService::connectAttempt()
@@ -254,6 +285,10 @@ bool VisionService::sendMessage(const QJsonObject &message)
void VisionService::readMessages()
{
if (m_shuttingDown) {
if (m_socket->isOpen()) m_socket->readAll();
return;
}
m_buffer.append(m_socket->readAll());
while (m_buffer.size() >= 4) {
const quint32 size = qFromBigEndian<quint32>(reinterpret_cast<const uchar *>(m_buffer.constData()));
@@ -472,19 +507,25 @@ void VisionService::shutdown()
if (m_shuttingDown) return;
m_shuttingDown = true;
if (!m_request.isEmpty()) cancel(m_request.value("request_id").toString());
if (m_process->state() != QProcess::NotRunning
&& m_serverProcessId == QString::number(m_process->processId()) && !m_session.isEmpty()) {
const bool ownsServer = m_process->state() != QProcess::NotRunning
&& m_serverProcessId == QString::number(m_process->processId()) && !m_session.isEmpty();
if (ownsServer) {
sendCommand("shutdown");
m_socket->flush();
QTimer::singleShot(3000, m_process, [this]() {
if (m_process->state() != QProcess::NotRunning) m_process->kill();
const quint64 generation = m_processGeneration;
QTimer::singleShot(3000, m_process, [this, generation]() {
if (generation == m_processGeneration && m_process->state() != QProcess::NotRunning)
m_process->kill();
});
}
m_startIfUnavailable = false;
m_connecting = false;
m_connectTimer->stop();
m_pollTimer->stop();
m_initializeTimer->stop();
m_socket->disconnectFromServer();
// Keep the owned server's pipe open until it has read shutdown and exited.
// Closing immediately can discard unread data on Windows named pipes.
if (!ownsServer) m_socket->disconnectFromServer();
m_ready = false;
m_session.clear();
publishAvailability();
@@ -515,6 +556,7 @@ void VisionService::finishMeasurement(const QJsonObject &result)
void VisionService::failConnection(const QString &message)
{
const bool hadConnection = m_connecting || !m_session.isEmpty() || !m_request.isEmpty();
m_startIfUnavailable = false;
m_connecting = false;
m_connectTimer->stop();
m_pollTimer->stop();