3175 lines
147 KiB
C++
3175 lines
147 KiB
C++
|
|
#include "robotclient.h"
|
|||
|
|
#include "videodecoder.h"
|
|||
|
|
|
|||
|
|
#include <QDateTime>
|
|||
|
|
#include <QElapsedTimer>
|
|||
|
|
#include <QJsonArray>
|
|||
|
|
#include <QJsonDocument>
|
|||
|
|
#include <QJsonObject>
|
|||
|
|
#include <QJsonParseError>
|
|||
|
|
#include <QMutexLocker>
|
|||
|
|
#include <QPainter>
|
|||
|
|
#include <QTimer>
|
|||
|
|
#include <QUuid>
|
|||
|
|
#include <QtMath>
|
|||
|
|
|
|||
|
|
#include <chrono>
|
|||
|
|
#include <algorithm>
|
|||
|
|
#include <atomic>
|
|||
|
|
#include <condition_variable>
|
|||
|
|
#include <cstring>
|
|||
|
|
#include <deque>
|
|||
|
|
#include <functional>
|
|||
|
|
#include <limits>
|
|||
|
|
#include <memory>
|
|||
|
|
#include <mutex>
|
|||
|
|
#include <thread>
|
|||
|
|
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
#include <grpcpp/grpcpp.h>
|
|||
|
|
#include <grpcpp/generic/generic_stub.h>
|
|||
|
|
#include <google/protobuf/dynamic_message.h>
|
|||
|
|
#include <google/protobuf/util/json_util.h>
|
|||
|
|
#include "cmvr/api/agv_command.pb.h"
|
|||
|
|
#include "cmvr/api/agv_service.grpc.pb.h"
|
|||
|
|
#include "cmvr/api/arm_command.pb.h"
|
|||
|
|
#include "cmvr/api/arm_service.grpc.pb.h"
|
|||
|
|
#include "cmvr/api/camera_command.pb.h"
|
|||
|
|
#include "cmvr/api/camera_service.grpc.pb.h"
|
|||
|
|
#include "cmvr/api/configuration_service.grpc.pb.h"
|
|||
|
|
#include "cmvr/api/microphone_command.pb.h"
|
|||
|
|
#include "cmvr/api/microphone_service.grpc.pb.h"
|
|||
|
|
#include "cmvr/api/speaker_command.pb.h"
|
|||
|
|
#include "cmvr/api/speaker_service.grpc.pb.h"
|
|||
|
|
#include "cmvr/api/system_command.pb.h"
|
|||
|
|
#include "cmvr/api/system_service.grpc.pb.h"
|
|||
|
|
#endif
|
|||
|
|
|
|||
|
|
namespace {
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
constexpr int kGrpcMaxMessageBytes = 64 * 1024 * 1024;
|
|||
|
|
constexpr int kArmMotionTimeoutMs = 120000;
|
|||
|
|
|
|||
|
|
std::shared_ptr<grpc::Channel> createGrpcChannel(const std::string &endpoint)
|
|||
|
|
{
|
|||
|
|
grpc::ChannelArguments arguments;
|
|||
|
|
arguments.SetMaxReceiveMessageSize(kGrpcMaxMessageBytes);
|
|||
|
|
arguments.SetMaxSendMessageSize(kGrpcMaxMessageBytes);
|
|||
|
|
return grpc::CreateCustomChannel(endpoint, grpc::InsecureChannelCredentials(), arguments);
|
|||
|
|
}
|
|||
|
|
#endif
|
|||
|
|
|
|||
|
|
QStringList armJointNamesForDevice(const QString &deviceId)
|
|||
|
|
{
|
|||
|
|
const QString prefix = deviceId.contains(QStringLiteral("left"), Qt::CaseInsensitive)
|
|||
|
|
? QStringLiteral("L_") : QStringLiteral("R_");
|
|||
|
|
return {
|
|||
|
|
prefix + QStringLiteral("SHOULDER_P"), prefix + QStringLiteral("SHOULDER_R"),
|
|||
|
|
prefix + QStringLiteral("SHOULDER_Y"), prefix + QStringLiteral("ELBOW_R"),
|
|||
|
|
prefix + QStringLiteral("WRIST_P"), prefix + QStringLiteral("WRIST_Y"),
|
|||
|
|
prefix + QStringLiteral("WRIST_R")
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
VideoFrame videoFrameFromImage(const QImage &image)
|
|||
|
|
{
|
|||
|
|
const QImage rgba = image.convertToFormat(QImage::Format_RGBA8888);
|
|||
|
|
VideoFrame frame;
|
|||
|
|
frame.width = rgba.width();
|
|||
|
|
frame.height = rgba.height();
|
|||
|
|
frame.format = VideoPixelFormat::RGBA;
|
|||
|
|
frame.stride0 = frame.width * 4;
|
|||
|
|
frame.plane0.resize(frame.width * frame.height * 4);
|
|||
|
|
for (int row = 0; row < frame.height; ++row) {
|
|||
|
|
std::memcpy(frame.plane0.data() + row * frame.width * 4,
|
|||
|
|
rgba.constScanLine(row), static_cast<size_t>(frame.width * 4));
|
|||
|
|
}
|
|||
|
|
return frame;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
VideoFrame videoFrameFromDepth(const cmvr::api::FrameData &frame)
|
|||
|
|
{
|
|||
|
|
if (frame.width() <= 0 || frame.height() <= 0 ||
|
|||
|
|
frame.type() != cmvr::api::FrameData::U16C1 ||
|
|||
|
|
frame.data().size() < frame.width() * frame.height() * 2) {
|
|||
|
|
return {};
|
|||
|
|
}
|
|||
|
|
QImage gray(frame.width(), frame.height(), QImage::Format_Grayscale8);
|
|||
|
|
const auto *source = reinterpret_cast<const uchar *>(frame.data().data());
|
|||
|
|
for (int y = 0; y < frame.height(); ++y) {
|
|||
|
|
uchar *destination = gray.scanLine(y);
|
|||
|
|
for (int x = 0; x < frame.width(); ++x) {
|
|||
|
|
quint16 millimeters = 0;
|
|||
|
|
std::memcpy(&millimeters,
|
|||
|
|
source + (y * frame.width() + x) * sizeof(quint16),
|
|||
|
|
sizeof(quint16));
|
|||
|
|
const int clamped = std::min<int>(millimeters, 5000);
|
|||
|
|
destination[x] = static_cast<uchar>(255 - (clamped * 255 / 5000));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return videoFrameFromImage(gray);
|
|||
|
|
}
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
class BackendWorker : public QObject
|
|||
|
|
{
|
|||
|
|
public:
|
|||
|
|
struct CameraStreamSession {
|
|||
|
|
QString cameraId;
|
|||
|
|
QString codec;
|
|||
|
|
QString mode = QStringLiteral("rgb");
|
|||
|
|
std::atomic<bool> stop{false};
|
|||
|
|
std::atomic<bool> running{false};
|
|||
|
|
std::thread thread;
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
grpc::ClientContext *context = nullptr;
|
|||
|
|
std::mutex contextMutex;
|
|||
|
|
#endif
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
struct ArmMotionSession {
|
|||
|
|
std::atomic<bool> stop{false};
|
|||
|
|
std::atomic<bool> running{false};
|
|||
|
|
std::thread thread;
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
grpc::ClientContext *context = nullptr;
|
|||
|
|
std::mutex contextMutex;
|
|||
|
|
#endif
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
explicit BackendWorker(QObject *parent = nullptr)
|
|||
|
|
: QObject(parent)
|
|||
|
|
{
|
|||
|
|
m_pollTimer = new QTimer(this);
|
|||
|
|
m_pollTimer->setInterval(100);
|
|||
|
|
connect(m_pollTimer, &QTimer::timeout, this, &BackendWorker::poll);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
~BackendWorker() override
|
|||
|
|
{
|
|||
|
|
stopArmMotionInternal();
|
|||
|
|
stopCameraStreamInternal();
|
|||
|
|
stopSpeakerStreamInternal(true);
|
|||
|
|
stopMicrophoneStreamInternal();
|
|||
|
|
stopAgvMapStreamInternal();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public:
|
|||
|
|
void connectToServer(const QString &host, int port, bool demoMode)
|
|||
|
|
{
|
|||
|
|
m_host = host.trimmed();
|
|||
|
|
m_port = port;
|
|||
|
|
m_demoMode = demoMode;
|
|||
|
|
m_tick = 0;
|
|||
|
|
|
|||
|
|
if (m_host.isEmpty()) {
|
|||
|
|
emit connectionChanged(false, 0, QStringLiteral("主机地址不能为空"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (m_demoMode) {
|
|||
|
|
m_connected = true;
|
|||
|
|
m_pollTimer->start();
|
|||
|
|
emit connectionChanged(true, 8, QStringLiteral("演示数据源"));
|
|||
|
|
emit logMessage(QStringLiteral("INFO"),
|
|||
|
|
QStringLiteral("已启动演示模式,界面数据由本地模拟器生成"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
const std::string endpoint = QStringLiteral("%1:%2").arg(m_host).arg(m_port).toStdString();
|
|||
|
|
m_channel = createGrpcChannel(endpoint);
|
|||
|
|
QElapsedTimer elapsed;
|
|||
|
|
elapsed.start();
|
|||
|
|
const auto deadline = std::chrono::system_clock::now() + std::chrono::milliseconds(1200);
|
|||
|
|
if (!m_channel->WaitForConnected(deadline)) {
|
|||
|
|
m_connected = false;
|
|||
|
|
emit connectionChanged(false, 0, QStringLiteral("连接超时,请检查地址和服务状态"));
|
|||
|
|
emit logMessage(QStringLiteral("ERROR"), QStringLiteral("gRPC 通道连接超时:%1").arg(QString::fromStdString(endpoint)));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
m_arm = cmvr::api::ArmService::NewStub(m_channel);
|
|||
|
|
m_camera = cmvr::api::CameraService::NewStub(m_channel);
|
|||
|
|
m_system = cmvr::api::SystemService::NewStub(m_channel);
|
|||
|
|
m_agv = cmvr::api::AgvService::NewStub(m_channel);
|
|||
|
|
m_microphone = cmvr::api::MicPhoneService::NewStub(m_channel);
|
|||
|
|
m_generic = std::make_unique<grpc::GenericStub>(m_channel);
|
|||
|
|
m_connected = true;
|
|||
|
|
m_pollTimer->start();
|
|||
|
|
emit connectionChanged(true, static_cast<int>(elapsed.elapsed()), QString::fromStdString(endpoint));
|
|||
|
|
emit logMessage(QStringLiteral("INFO"), QStringLiteral("gRPC 通道已连接:%1").arg(QString::fromStdString(endpoint)));
|
|||
|
|
#else
|
|||
|
|
m_connected = false;
|
|||
|
|
emit connectionChanged(false, 0, QStringLiteral("当前程序未编译 gRPC 支持"));
|
|||
|
|
emit logMessage(QStringLiteral("ERROR"),
|
|||
|
|
QStringLiteral("请使用 CMake 和 gRPC C++ 依赖重新构建,或启用演示模式"));
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void disconnectFromServer()
|
|||
|
|
{
|
|||
|
|
m_pollTimer->stop();
|
|||
|
|
stopArmMotionInternal();
|
|||
|
|
stopCameraStreamInternal();
|
|||
|
|
stopSpeakerStreamInternal(true);
|
|||
|
|
stopMicrophoneStreamInternal();
|
|||
|
|
stopAgvMapStreamInternal();
|
|||
|
|
if (m_cameraRecording) {
|
|||
|
|
m_cameraRecording = false;
|
|||
|
|
emit cameraRecordingStateChanged(false, QStringLiteral("连接已断开,录像状态未知"));
|
|||
|
|
}
|
|||
|
|
m_connected = false;
|
|||
|
|
m_armPollingEnabled = false;
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
m_arm.reset();
|
|||
|
|
m_camera.reset();
|
|||
|
|
m_system.reset();
|
|||
|
|
m_agv.reset();
|
|||
|
|
m_microphone.reset();
|
|||
|
|
m_generic.reset();
|
|||
|
|
m_channel.reset();
|
|||
|
|
#endif
|
|||
|
|
emit connectionChanged(false, 0, QStringLiteral("已断开"));
|
|||
|
|
emit logMessage(QStringLiteral("INFO"), QStringLiteral("连接已断开"));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void connectConfigurationServer(const QString &host, int port)
|
|||
|
|
{
|
|||
|
|
const QString trimmedHost = host.trimmed();
|
|||
|
|
if (trimmedHost.isEmpty()) {
|
|||
|
|
emit configurationConnectionChanged(false, QStringLiteral("主机地址不能为空"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
const std::string endpoint = QStringLiteral("%1:%2").arg(trimmedHost).arg(port).toStdString();
|
|||
|
|
m_configChannel = createGrpcChannel(endpoint);
|
|||
|
|
const auto deadline = std::chrono::system_clock::now() + std::chrono::milliseconds(1500);
|
|||
|
|
if (!m_configChannel->WaitForConnected(deadline)) {
|
|||
|
|
m_configuration.reset();
|
|||
|
|
m_configChannel.reset();
|
|||
|
|
m_configConnected = false;
|
|||
|
|
emit configurationConnectionChanged(
|
|||
|
|
false, QStringLiteral("配置服务连接超时:%1").arg(QString::fromStdString(endpoint)));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
m_configuration = cmvr::api::ConfigurationService::NewStub(m_configChannel);
|
|||
|
|
m_configConnected = true;
|
|||
|
|
emit configurationConnectionChanged(
|
|||
|
|
true, QStringLiteral("已连接配置服务:%1").arg(QString::fromStdString(endpoint)));
|
|||
|
|
#else
|
|||
|
|
Q_UNUSED(port)
|
|||
|
|
emit configurationConnectionChanged(false, QStringLiteral("当前程序未编译 gRPC 支持"));
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void disconnectConfigurationServer()
|
|||
|
|
{
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
m_configuration.reset();
|
|||
|
|
m_configChannel.reset();
|
|||
|
|
#endif
|
|||
|
|
m_configConnected = false;
|
|||
|
|
emit configurationConnectionChanged(false, QStringLiteral("配置服务已断开"));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void listConfigurationFiles()
|
|||
|
|
{
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
if (!ensureConfigurationConnected(QStringLiteral("读取配置列表")))
|
|||
|
|
return;
|
|||
|
|
cmvr::api::ListConfigFilesCommand::Request request;
|
|||
|
|
cmvr::api::ListConfigFilesCommand::Feedback response;
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
setDeadline(context, 3000);
|
|||
|
|
const grpc::Status status = m_configuration->ListConfigFiles(&context, request, &response);
|
|||
|
|
const bool success = status.ok() && response.header().success();
|
|||
|
|
QVariantList files;
|
|||
|
|
if (success) {
|
|||
|
|
for (const auto &file : response.files()) {
|
|||
|
|
QVariantMap item;
|
|||
|
|
item.insert(QStringLiteral("relativePath"), QString::fromStdString(file.relative_path()));
|
|||
|
|
item.insert(QStringLiteral("sizeBytes"), QVariant::fromValue<qulonglong>(file.size_bytes()));
|
|||
|
|
item.insert(QStringLiteral("modifiedUnixMs"), QVariant::fromValue<qlonglong>(file.modified_unix_ms()));
|
|||
|
|
item.insert(QStringLiteral("messageType"), QString::fromStdString(file.message_type()));
|
|||
|
|
files.append(item);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
emit configurationFilesReceived(
|
|||
|
|
QString::fromStdString(response.config_root()), files, success,
|
|||
|
|
configurationError(status, response.header().error_message()));
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void readConfigurationFile(const QString &relativePath)
|
|||
|
|
{
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
if (!ensureConfigurationConnected(QStringLiteral("读取配置文件")))
|
|||
|
|
return;
|
|||
|
|
cmvr::api::ReadConfigFileCommand::Request request;
|
|||
|
|
request.set_relative_path(relativePath.toStdString());
|
|||
|
|
cmvr::api::ReadConfigFileCommand::Feedback response;
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
setDeadline(context, 3000);
|
|||
|
|
const grpc::Status status = m_configuration->ReadConfigFile(&context, request, &response);
|
|||
|
|
const bool success = status.ok() && response.header().success();
|
|||
|
|
emit configurationFileReceived(
|
|||
|
|
relativePath,
|
|||
|
|
success ? QString::fromStdString(response.content()) : QString(),
|
|||
|
|
success ? QString::fromStdString(response.revision()) : QString(),
|
|||
|
|
success ? QString::fromStdString(response.file().message_type()) : QString(),
|
|||
|
|
success, configurationError(status, response.header().error_message()));
|
|||
|
|
#else
|
|||
|
|
Q_UNUSED(relativePath)
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void validateConfigurationFile(const QString &relativePath, const QString &content)
|
|||
|
|
{
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
if (!ensureConfigurationConnected(QStringLiteral("校验配置文件")))
|
|||
|
|
return;
|
|||
|
|
cmvr::api::ValidateConfigFileCommand::Request request;
|
|||
|
|
request.set_relative_path(relativePath.toStdString());
|
|||
|
|
request.set_content(content.toStdString());
|
|||
|
|
cmvr::api::ValidateConfigFileCommand::Feedback response;
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
setDeadline(context, 3000);
|
|||
|
|
const grpc::Status status = m_configuration->ValidateConfigFile(&context, request, &response);
|
|||
|
|
const bool success = status.ok() && response.header().success();
|
|||
|
|
QString detail = configurationError(status, response.header().error_message());
|
|||
|
|
if (success)
|
|||
|
|
detail = QStringLiteral("配置有效:%1").arg(QString::fromStdString(response.message_type()));
|
|||
|
|
emit configurationValidationFinished(relativePath, success, detail);
|
|||
|
|
#else
|
|||
|
|
Q_UNUSED(relativePath)
|
|||
|
|
Q_UNUSED(content)
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void saveConfigurationFile(const QString &relativePath, const QString &content,
|
|||
|
|
const QString &expectedRevision)
|
|||
|
|
{
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
if (!ensureConfigurationConnected(QStringLiteral("保存配置文件")))
|
|||
|
|
return;
|
|||
|
|
cmvr::api::SaveConfigFileCommand::Request request;
|
|||
|
|
request.set_relative_path(relativePath.toStdString());
|
|||
|
|
request.set_content(content.toStdString());
|
|||
|
|
request.set_expected_revision(expectedRevision.toStdString());
|
|||
|
|
cmvr::api::SaveConfigFileCommand::Feedback response;
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
setDeadline(context, 5000);
|
|||
|
|
const grpc::Status status = m_configuration->SaveConfigFile(&context, request, &response);
|
|||
|
|
const bool success = status.ok() && response.header().success();
|
|||
|
|
emit configurationSaveFinished(
|
|||
|
|
relativePath, success ? QString::fromStdString(response.revision()) : QString(),
|
|||
|
|
success, configurationError(status, response.header().error_message()));
|
|||
|
|
#else
|
|||
|
|
Q_UNUSED(relativePath)
|
|||
|
|
Q_UNUSED(content)
|
|||
|
|
Q_UNUSED(expectedRevision)
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void setArmTelemetry(const QString &deviceId, bool enabled)
|
|||
|
|
{
|
|||
|
|
if (enabled && !ensureConnected(QStringLiteral("Arm Telemetry")))
|
|||
|
|
return;
|
|||
|
|
if (enabled && !ensureDeviceId(QStringLiteral("Arm Telemetry"), deviceId))
|
|||
|
|
return;
|
|||
|
|
const QString nextDeviceId = enabled ? deviceId.trimmed() : QString();
|
|||
|
|
if (m_armDeviceId != nextDeviceId)
|
|||
|
|
m_demoTargets.clear();
|
|||
|
|
m_armDeviceId = nextDeviceId;
|
|||
|
|
m_armPollingEnabled = enabled;
|
|||
|
|
m_consecutivePollErrors = 0;
|
|||
|
|
emit commandFinished(QStringLiteral("Arm Telemetry"), true,
|
|||
|
|
enabled ? QStringLiteral("机械臂遥测已启动")
|
|||
|
|
: QStringLiteral("机械臂遥测已停止"));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void moveJ(const QString &deviceId, const QVariantList &positions,
|
|||
|
|
double velocity, double acceleration)
|
|||
|
|
{
|
|||
|
|
if (!ensureConnected(QStringLiteral("MoveJ")))
|
|||
|
|
return;
|
|||
|
|
if (!ensureDeviceId(QStringLiteral("MoveJ"), deviceId))
|
|||
|
|
return;
|
|||
|
|
if (positions.isEmpty()) {
|
|||
|
|
emit commandFinished(QStringLiteral("MoveJ"), false, QStringLiteral("没有可发送的目标关节"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
if (m_demoMode) {
|
|||
|
|
m_demoTargets.clear();
|
|||
|
|
for (const QVariant &value : positions)
|
|||
|
|
m_demoTargets.append(value.toDouble());
|
|||
|
|
emit commandFinished(QStringLiteral("MoveJ"), true,
|
|||
|
|
QStringLiteral("已发送 %1 个目标,v=%2,a=%3")
|
|||
|
|
.arg(positions.size()).arg(velocity, 0, 'f', 2).arg(acceleration, 0, 'f', 2));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
cmvr::api::MoveJ::Request request;
|
|||
|
|
fillHeader(request.mutable_header(), deviceId, kArmMotionTimeoutMs);
|
|||
|
|
for (const QVariant &value : positions)
|
|||
|
|
request.mutable_target()->add_position(value.toDouble());
|
|||
|
|
request.mutable_options()->set_velocity(velocity);
|
|||
|
|
request.mutable_options()->set_acceleration(acceleration);
|
|||
|
|
startArmMotion<cmvr::api::MoveJ::Request, cmvr::api::MoveJ::Response>(
|
|||
|
|
QStringLiteral("MoveJ"), std::move(request),
|
|||
|
|
[](cmvr::api::ArmService::Stub &stub, grpc::ClientContext *context,
|
|||
|
|
const cmvr::api::MoveJ::Request &motionRequest,
|
|||
|
|
cmvr::api::MoveJ::Response *response) {
|
|||
|
|
return stub.moveJ(context, motionRequest, response);
|
|||
|
|
});
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void moveJPose(const QString &deviceId, const QVariantMap &pose,
|
|||
|
|
const QString &baseLink, const QString &eeLink,
|
|||
|
|
double velocity, double acceleration)
|
|||
|
|
{
|
|||
|
|
if (!ensureConnected(QStringLiteral("MoveL")))
|
|||
|
|
return;
|
|||
|
|
if (!ensureDeviceId(QStringLiteral("MoveL"), deviceId))
|
|||
|
|
return;
|
|||
|
|
if (m_demoMode) {
|
|||
|
|
emit commandFinished(QStringLiteral("MoveL"), true,
|
|||
|
|
QStringLiteral("已发送末端位姿 MoveL,%1 → %2,v=%3,a=%4")
|
|||
|
|
.arg(baseLink, eeLink)
|
|||
|
|
.arg(velocity, 0, 'f', 2)
|
|||
|
|
.arg(acceleration, 0, 'f', 2));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
Q_UNUSED(baseLink)
|
|||
|
|
Q_UNUSED(eeLink)
|
|||
|
|
cmvr::api::MoveL::Request request;
|
|||
|
|
fillHeader(request.mutable_header(), deviceId, kArmMotionTimeoutMs);
|
|||
|
|
auto *target = request.mutable_target();
|
|||
|
|
target->set_x(pose.value(QStringLiteral("x")).toDouble());
|
|||
|
|
target->set_y(pose.value(QStringLiteral("y")).toDouble());
|
|||
|
|
target->set_z(pose.value(QStringLiteral("z")).toDouble());
|
|||
|
|
target->set_rx(pose.value(QStringLiteral("rx")).toDouble());
|
|||
|
|
target->set_ry(pose.value(QStringLiteral("ry")).toDouble());
|
|||
|
|
target->set_rz(pose.value(QStringLiteral("rz")).toDouble());
|
|||
|
|
request.mutable_options()->set_velocity(velocity);
|
|||
|
|
request.mutable_options()->set_acceleration(acceleration);
|
|||
|
|
request.set_frame(cmvr::api::ARM_FRAME_BASE);
|
|||
|
|
startArmMotion<cmvr::api::MoveL::Request, cmvr::api::MoveL::Response>(
|
|||
|
|
QStringLiteral("MoveL"), std::move(request),
|
|||
|
|
[](cmvr::api::ArmService::Stub &stub, grpc::ClientContext *context,
|
|||
|
|
const cmvr::api::MoveL::Request &motionRequest,
|
|||
|
|
cmvr::api::MoveL::Response *response) {
|
|||
|
|
return stub.moveL(context, motionRequest, response);
|
|||
|
|
});
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void torque(const QString &deviceId, bool enabled)
|
|||
|
|
{
|
|||
|
|
const QString command = enabled ? QStringLiteral("Torque On") : QStringLiteral("Torque Off");
|
|||
|
|
if (!ensureConnected(command))
|
|||
|
|
return;
|
|||
|
|
if (!ensureDeviceId(command, deviceId))
|
|||
|
|
return;
|
|||
|
|
if (m_demoMode) {
|
|||
|
|
emit commandFinished(command, true, enabled ? QStringLiteral("关节已使能") : QStringLiteral("关节已去使能"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
cmvr::api::CommandHeader::Request request;
|
|||
|
|
fillHeader(&request, deviceId);
|
|||
|
|
cmvr::api::CommandHeader::Feedback response;
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
setDeadline(context, 2000);
|
|||
|
|
const grpc::Status status = enabled
|
|||
|
|
? m_arm->torqueOn(&context, request, &response)
|
|||
|
|
: m_arm->torqueOff(&context, request, &response);
|
|||
|
|
finish(command, status, response.success(), QString::fromStdString(response.error_message()));
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void armStop(const QString &deviceId)
|
|||
|
|
{
|
|||
|
|
if (!ensureConnected(QStringLiteral("Stop Motion")))
|
|||
|
|
return;
|
|||
|
|
if (!ensureDeviceId(QStringLiteral("Stop Motion"), deviceId))
|
|||
|
|
return;
|
|||
|
|
if (m_demoMode) {
|
|||
|
|
m_demoTargets.clear();
|
|||
|
|
emit commandFinished(QStringLiteral("Stop Motion"), true, QStringLiteral("运动已停止"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
cmvr::api::CommandHeader::Request request;
|
|||
|
|
fillHeader(&request, deviceId);
|
|||
|
|
cmvr::api::CommandHeader::Feedback response;
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
setDeadline(context, 1500);
|
|||
|
|
const grpc::Status status = m_arm->stopMotion(&context, request, &response);
|
|||
|
|
finish(QStringLiteral("Stop Motion"), status, response.success(), QString::fromStdString(response.error_message()));
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void armClearFault(const QString &deviceId)
|
|||
|
|
{
|
|||
|
|
if (!ensureConnected(QStringLiteral("Clear Fault")))
|
|||
|
|
return;
|
|||
|
|
if (!ensureDeviceId(QStringLiteral("Clear Fault"), deviceId))
|
|||
|
|
return;
|
|||
|
|
if (m_demoMode) {
|
|||
|
|
emit commandFinished(QStringLiteral("Clear Fault"), true, QStringLiteral("故障状态已清除"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
cmvr::api::CommandHeader::Request request;
|
|||
|
|
fillHeader(&request, deviceId);
|
|||
|
|
cmvr::api::CommandHeader::Feedback response;
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
setDeadline(context, 2000);
|
|||
|
|
const grpc::Status status = m_arm->clearFault(&context, request, &response);
|
|||
|
|
finish(QStringLiteral("Clear Fault"), status, response.success(), QString::fromStdString(response.error_message()));
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void armCalibrateZero(const QString &deviceId, const QString &jointName)
|
|||
|
|
{
|
|||
|
|
const QString command = QStringLiteral("Calibrate Zero Q");
|
|||
|
|
if (!ensureConnected(command))
|
|||
|
|
return;
|
|||
|
|
if (!ensureDeviceId(command, deviceId))
|
|||
|
|
return;
|
|||
|
|
if (jointName.trimmed().isEmpty()) {
|
|||
|
|
emit commandFinished(command, false, QStringLiteral("关节名称不能为空"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
if (m_demoMode) {
|
|||
|
|
emit commandFinished(command, true,
|
|||
|
|
QStringLiteral("已将 %1 的当前位置设置为零位(演示)")
|
|||
|
|
.arg(jointName));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
cmvr::api::CalibrateZeroQ::Request request;
|
|||
|
|
fillHeader(request.mutable_header(), deviceId);
|
|||
|
|
request.set_joint_name(jointName.toStdString());
|
|||
|
|
cmvr::api::CalibrateZeroQ::Response response;
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
setDeadline(context, 2500);
|
|||
|
|
const grpc::Status status = m_arm->calibrateZeroQ(&context, request, &response);
|
|||
|
|
finish(command, status, response.header().success(),
|
|||
|
|
QString::fromStdString(response.header().error_message()));
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void snapshot(const QString &cameraId)
|
|||
|
|
{
|
|||
|
|
if (!ensureConnected(QStringLiteral("Snapshot")))
|
|||
|
|
return;
|
|||
|
|
if (!ensureDeviceId(QStringLiteral("Snapshot"), cameraId))
|
|||
|
|
return;
|
|||
|
|
if (m_demoMode) {
|
|||
|
|
QImage image(1280, 720, QImage::Format_RGB32);
|
|||
|
|
image.fill(QColor("#20252b"));
|
|||
|
|
QPainter painter(&image);
|
|||
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|||
|
|
painter.setPen(QPen(QColor("#3b424b"), 1));
|
|||
|
|
for (int x = 0; x < image.width(); x += 64)
|
|||
|
|
painter.drawLine(x, 0, x, image.height());
|
|||
|
|
for (int y = 0; y < image.height(); y += 64)
|
|||
|
|
painter.drawLine(0, y, image.width(), y);
|
|||
|
|
painter.setPen(QColor("#e9edf2"));
|
|||
|
|
painter.setFont(QFont("Microsoft YaHei UI", 28, QFont::DemiBold));
|
|||
|
|
painter.drawText(image.rect().adjusted(0, -40, 0, 0), Qt::AlignCenter, QStringLiteral("相机预览"));
|
|||
|
|
painter.setPen(QColor("#9ca4ad"));
|
|||
|
|
painter.setFont(QFont("Microsoft YaHei UI", 14));
|
|||
|
|
painter.drawText(image.rect().adjusted(0, 48, 0, 0), Qt::AlignCenter,
|
|||
|
|
QStringLiteral("%1 · DEMO · %2")
|
|||
|
|
.arg(cameraId, QDateTime::currentDateTime().toString("HH:mm:ss")));
|
|||
|
|
emit imageReceived(image, QStringLiteral("1280 x 720 · 演示帧"));
|
|||
|
|
emit commandFinished(QStringLiteral("Snapshot"), true, QStringLiteral("已获取演示图像"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
cmvr::api::GetRGBImageCommand::Request request;
|
|||
|
|
fillHeader(request.mutable_header(), cameraId);
|
|||
|
|
cmvr::api::GetRGBImageCommand::Feedback response;
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
setDeadline(context, 3000);
|
|||
|
|
const grpc::Status status = m_camera->GetRGBImage(&context, request, &response);
|
|||
|
|
if (!status.ok() || !response.header().success()) {
|
|||
|
|
finish(QStringLiteral("Snapshot"), status, response.header().success(),
|
|||
|
|
QString::fromStdString(response.header().error_message()));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
const auto &frame = response.color_frame();
|
|||
|
|
const QByteArray bytes(frame.data().data(), static_cast<int>(frame.data().size()));
|
|||
|
|
QImage image;
|
|||
|
|
if (frame.type() == cmvr::api::FrameData::U8C3 &&
|
|||
|
|
frame.width() > 0 && frame.height() > 0 && bytes.size() == frame.width() * frame.height() * 3) {
|
|||
|
|
image = QImage(reinterpret_cast<const uchar *>(bytes.constData()), frame.width(), frame.height(),
|
|||
|
|
frame.width() * 3, QImage::Format_RGB888).rgbSwapped();
|
|||
|
|
} else if (frame.type() == cmvr::api::FrameData::U8C1 &&
|
|||
|
|
frame.width() > 0 && frame.height() > 0 && bytes.size() == frame.width() * frame.height()) {
|
|||
|
|
image = QImage(reinterpret_cast<const uchar *>(bytes.constData()), frame.width(), frame.height(),
|
|||
|
|
frame.width(), QImage::Format_Grayscale8).copy();
|
|||
|
|
} else {
|
|||
|
|
image = QImage::fromData(bytes);
|
|||
|
|
}
|
|||
|
|
if (image.isNull()) {
|
|||
|
|
emit commandFinished(QStringLiteral("Snapshot"), false,
|
|||
|
|
QStringLiteral("暂不支持图像编码:%1").arg(QString::fromStdString(frame.codec())));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
emit imageReceived(image, QStringLiteral("%1 x %2 · %3")
|
|||
|
|
.arg(image.width()).arg(image.height()).arg(QString::fromStdString(frame.codec())));
|
|||
|
|
emit commandFinished(QStringLiteral("Snapshot"), true, QStringLiteral("图像获取成功"));
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void startCameraStream(const QString &cameraId, const QString &codec,
|
|||
|
|
const QString &mode)
|
|||
|
|
{
|
|||
|
|
if (!ensureConnected(QStringLiteral("Camera Stream")) ||
|
|||
|
|
!ensureDeviceId(QStringLiteral("Camera Stream"), cameraId))
|
|||
|
|
return;
|
|||
|
|
|
|||
|
|
const QString normalizedCameraId = cameraId.trimmed();
|
|||
|
|
std::shared_ptr<CameraStreamSession> existing;
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(m_cameraSessionsMutex);
|
|||
|
|
const auto it = m_cameraSessions.find(normalizedCameraId);
|
|||
|
|
if (it != m_cameraSessions.end())
|
|||
|
|
existing = it.value();
|
|||
|
|
}
|
|||
|
|
if (existing && existing->running.load()) {
|
|||
|
|
emit commandFinished(QStringLiteral("Camera Stream"), false,
|
|||
|
|
QStringLiteral("摄像头 %1 的视频预览已经在运行")
|
|||
|
|
.arg(normalizedCameraId));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
if (existing)
|
|||
|
|
stopCameraStreamInternal(normalizedCameraId);
|
|||
|
|
|
|||
|
|
auto session = std::make_shared<CameraStreamSession>();
|
|||
|
|
session->cameraId = normalizedCameraId;
|
|||
|
|
session->codec = codec.trimmed().toLower();
|
|||
|
|
session->mode = mode.trimmed().toLower();
|
|||
|
|
if (session->mode != QStringLiteral("depth") &&
|
|||
|
|
session->mode != QStringLiteral("rgbd"))
|
|||
|
|
session->mode = QStringLiteral("rgb");
|
|||
|
|
if (session->codec.isEmpty())
|
|||
|
|
session->codec = m_defaultCameraCodec;
|
|||
|
|
session->running.store(true);
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(m_cameraSessionsMutex);
|
|||
|
|
m_cameraSessions[normalizedCameraId] = session;
|
|||
|
|
}
|
|||
|
|
emit cameraStreamStateChangedForCamera(normalizedCameraId, true,
|
|||
|
|
QStringLiteral("正在建立视频流..."));
|
|||
|
|
|
|||
|
|
if (m_demoMode) {
|
|||
|
|
session->thread = std::thread([this, session]() {
|
|||
|
|
int frameNumber = 0;
|
|||
|
|
QElapsedTimer elapsed;
|
|||
|
|
elapsed.start();
|
|||
|
|
while (!session->stop.load()) {
|
|||
|
|
QImage image(1280, 720, QImage::Format_RGB32);
|
|||
|
|
image.fill(QColor("#20252b"));
|
|||
|
|
QPainter painter(&image);
|
|||
|
|
painter.setPen(QPen(QColor("#3b424b"), 1));
|
|||
|
|
const int offset = frameNumber % 64;
|
|||
|
|
for (int x = offset; x < image.width(); x += 64)
|
|||
|
|
painter.drawLine(x, 0, x, image.height());
|
|||
|
|
for (int y = offset; y < image.height(); y += 64)
|
|||
|
|
painter.drawLine(0, y, image.width(), y);
|
|||
|
|
painter.setPen(QColor("#e9edf2"));
|
|||
|
|
painter.setFont(QFont("Microsoft YaHei UI", 28, QFont::DemiBold));
|
|||
|
|
painter.drawText(image.rect().adjusted(0, -40, 0, 0), Qt::AlignCenter,
|
|||
|
|
QStringLiteral("摄像头实时预览"));
|
|||
|
|
painter.setPen(QColor("#9ca4ad"));
|
|||
|
|
painter.setFont(QFont("Microsoft YaHei UI", 14));
|
|||
|
|
painter.drawText(image.rect().adjusted(0, 48, 0, 0), Qt::AlignCenter,
|
|||
|
|
QStringLiteral("%1 | DEMO | frame %2")
|
|||
|
|
.arg(session->cameraId).arg(frameNumber));
|
|||
|
|
const QString decodeCodec = session->codec == QStringLiteral("h265")
|
|||
|
|
? QStringLiteral("H.265") : QStringLiteral("H.264");
|
|||
|
|
if (videoFrameReceivedForCamera)
|
|||
|
|
videoFrameReceivedForCamera(
|
|||
|
|
session->cameraId, videoFrameFromImage(image),
|
|||
|
|
QStringLiteral("1280 x 720 | DEMO / %1 | 30.0 FPS | #%2")
|
|||
|
|
.arg(decodeCodec).arg(frameNumber));
|
|||
|
|
++frameNumber;
|
|||
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(33));
|
|||
|
|
}
|
|||
|
|
session->running.store(false);
|
|||
|
|
if (cameraStreamStateChangedForCamera)
|
|||
|
|
cameraStreamStateChangedForCamera(
|
|||
|
|
session->cameraId, false,
|
|||
|
|
QStringLiteral("视频预览已停止,%1 帧,%2 秒")
|
|||
|
|
.arg(frameNumber)
|
|||
|
|
.arg(elapsed.elapsed() / 1000.0, 0, 'f', 1));
|
|||
|
|
});
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
const std::shared_ptr<grpc::Channel> channel = m_channel;
|
|||
|
|
session->thread = std::thread([this, channel, session]() {
|
|||
|
|
if (session->mode == QStringLiteral("depth"))
|
|||
|
|
runDepthCameraStream(channel, session, false);
|
|||
|
|
else if (session->mode == QStringLiteral("rgbd"))
|
|||
|
|
runDepthCameraStream(channel, session, true);
|
|||
|
|
else
|
|||
|
|
runCameraStream(channel, session);
|
|||
|
|
});
|
|||
|
|
#else
|
|||
|
|
session->running.store(false);
|
|||
|
|
emit cameraStreamStateChangedForCamera(session->cameraId, false,
|
|||
|
|
QStringLiteral("当前程序未编译 gRPC 支持"));
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void stopCameraStream(const QString &cameraId)
|
|||
|
|
{
|
|||
|
|
stopCameraStreamInternal(cameraId.trimmed());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void startCameraRecording(const QString &cameraId, const QString &videoPath)
|
|||
|
|
{
|
|||
|
|
const QString command = QStringLiteral("Camera Start Recording");
|
|||
|
|
if (!ensureConnected(command) || !ensureDeviceId(command, cameraId))
|
|||
|
|
return;
|
|||
|
|
const QString path = videoPath.trimmed();
|
|||
|
|
if (path.isEmpty()) {
|
|||
|
|
emit commandFinished(command, false, QStringLiteral("服务端录像路径不能为空"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
if (m_cameraRecording) {
|
|||
|
|
emit commandFinished(command, false, QStringLiteral("相机已经在录像"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
if (m_demoMode) {
|
|||
|
|
m_cameraRecording = true;
|
|||
|
|
emit cameraRecordingStateChanged(true,
|
|||
|
|
QStringLiteral("演示录像中:%1").arg(path));
|
|||
|
|
emit commandFinished(command, true, QStringLiteral("录像已开始(演示)"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
cmvr::api::StartCameraRecordingCommand::Request request;
|
|||
|
|
fillHeader(request.mutable_header(), cameraId);
|
|||
|
|
request.set_video_path(path.toStdString());
|
|||
|
|
cmvr::api::StartCameraRecordingCommand::Feedback response;
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
setDeadline(context, 3000);
|
|||
|
|
const grpc::Status status = m_camera->StartRecording(&context, request, &response);
|
|||
|
|
const bool success = status.ok() && response.header().success();
|
|||
|
|
const QString error = status.ok()
|
|||
|
|
? QString::fromStdString(response.header().error_message())
|
|||
|
|
: QString::fromStdString(status.error_message());
|
|||
|
|
if (success) {
|
|||
|
|
m_cameraRecording = true;
|
|||
|
|
emit cameraRecordingStateChanged(true, QStringLiteral("录像中:%1").arg(path));
|
|||
|
|
}
|
|||
|
|
finish(command, status, response.header().success(), error);
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void stopCameraRecording(const QString &cameraId)
|
|||
|
|
{
|
|||
|
|
const QString command = QStringLiteral("Camera Stop Recording");
|
|||
|
|
if (!ensureConnected(command) || !ensureDeviceId(command, cameraId))
|
|||
|
|
return;
|
|||
|
|
if (!m_cameraRecording) {
|
|||
|
|
emit commandFinished(command, false, QStringLiteral("当前没有正在进行的录像"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
if (m_demoMode) {
|
|||
|
|
m_cameraRecording = false;
|
|||
|
|
emit cameraRecordingStateChanged(false, QStringLiteral("录像已停止"));
|
|||
|
|
emit commandFinished(command, true, QStringLiteral("录像已停止(演示)"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
cmvr::api::StopCameraRecordingCommand::Request request;
|
|||
|
|
fillHeader(request.mutable_header(), cameraId);
|
|||
|
|
cmvr::api::StopCameraRecordingCommand::Feedback response;
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
setDeadline(context, 3000);
|
|||
|
|
const grpc::Status status = m_camera->StopRecording(&context, request, &response);
|
|||
|
|
const bool success = status.ok() && response.header().success();
|
|||
|
|
const QString error = status.ok()
|
|||
|
|
? QString::fromStdString(response.header().error_message())
|
|||
|
|
: QString::fromStdString(status.error_message());
|
|||
|
|
if (success) {
|
|||
|
|
m_cameraRecording = false;
|
|||
|
|
emit cameraRecordingStateChanged(false, QStringLiteral("录像已停止"));
|
|||
|
|
}
|
|||
|
|
finish(command, status, response.header().success(), error);
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void setCameraStreamCodec(const QString &codec)
|
|||
|
|
{
|
|||
|
|
const QString normalized = codec.trimmed().toLower();
|
|||
|
|
const bool hevc = normalized == QStringLiteral("h265") ||
|
|||
|
|
normalized == QStringLiteral("hevc") ||
|
|||
|
|
normalized == QStringLiteral("h.265");
|
|||
|
|
m_defaultCameraCodec = hevc ? QStringLiteral("h265") : QStringLiteral("h264");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void startSpeakerAudioStream(const QString &deviceId)
|
|||
|
|
{
|
|||
|
|
if (!ensureConnected(QStringLiteral("Speaker Stream")) ||
|
|||
|
|
!ensureDeviceId(QStringLiteral("Speaker Stream"), deviceId))
|
|||
|
|
return;
|
|||
|
|
if (m_speakerStreamRunning.load()) {
|
|||
|
|
emit commandFinished(QStringLiteral("Speaker Stream"), false,
|
|||
|
|
QStringLiteral("扬声器音频流已经在运行"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
if (m_speakerStreamThread.joinable())
|
|||
|
|
m_speakerStreamThread.join();
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(m_speakerQueueMutex);
|
|||
|
|
m_speakerQueue.clear();
|
|||
|
|
m_speakerQueueBytes = 0;
|
|||
|
|
}
|
|||
|
|
m_speakerStreamStop.store(false);
|
|||
|
|
m_speakerStreamRunning.store(true);
|
|||
|
|
m_speakerFrames.store(0);
|
|||
|
|
m_speakerBytes.store(0);
|
|||
|
|
emit audioStreamStateChanged(QStringLiteral("SpeakerService"), true,
|
|||
|
|
QStringLiteral("正在建立扬声器客户端音频流..."));
|
|||
|
|
|
|||
|
|
if (m_demoMode) {
|
|||
|
|
emit audioStreamStateChanged(QStringLiteral("SpeakerService"), true,
|
|||
|
|
QStringLiteral("演示模式:音频帧将在本地计数,不发送到设备"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
const std::shared_ptr<grpc::Channel> channel = m_channel;
|
|||
|
|
m_speakerStreamThread = std::thread([this, channel, deviceId]() {
|
|||
|
|
runSpeakerAudioStream(channel, deviceId);
|
|||
|
|
});
|
|||
|
|
#else
|
|||
|
|
m_speakerStreamRunning.store(false);
|
|||
|
|
emit audioStreamStateChanged(QStringLiteral("SpeakerService"), false,
|
|||
|
|
QStringLiteral("当前程序未编译 gRPC 支持"));
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void enqueueSpeakerAudioFrame(const QByteArray &data, int sampleRate, int channels,
|
|||
|
|
qint64 pts, int samples)
|
|||
|
|
{
|
|||
|
|
if (!m_speakerStreamRunning.load() || data.isEmpty() ||
|
|||
|
|
sampleRate <= 0 || channels <= 0 || samples <= 0)
|
|||
|
|
return;
|
|||
|
|
if (m_demoMode) {
|
|||
|
|
const quint64 frames = ++m_speakerFrames;
|
|||
|
|
const quint64 bytes = m_speakerBytes.fetch_add(data.size()) + data.size();
|
|||
|
|
emit audioStreamStatistics(QStringLiteral("SpeakerService"), frames, bytes,
|
|||
|
|
pts / 1000 + samples * 1000LL / sampleRate);
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
SpeakerAudioFrame frame;
|
|||
|
|
frame.data = data;
|
|||
|
|
frame.sampleRate = sampleRate;
|
|||
|
|
frame.channels = channels;
|
|||
|
|
frame.pts = pts;
|
|||
|
|
frame.samples = samples;
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(m_speakerQueueMutex);
|
|||
|
|
static const int kMaximumQueueBytes = 2 * 1024 * 1024;
|
|||
|
|
while (!m_speakerQueue.empty() &&
|
|||
|
|
m_speakerQueueBytes + data.size() > kMaximumQueueBytes) {
|
|||
|
|
m_speakerQueueBytes -= m_speakerQueue.front().data.size();
|
|||
|
|
m_speakerQueue.pop_front();
|
|||
|
|
}
|
|||
|
|
m_speakerQueue.push_back(std::move(frame));
|
|||
|
|
m_speakerQueueBytes += data.size();
|
|||
|
|
}
|
|||
|
|
m_speakerQueueCondition.notify_one();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void stopSpeakerAudioStream()
|
|||
|
|
{
|
|||
|
|
stopSpeakerStreamInternal();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void startMicrophoneAudioStream(const QString &deviceId)
|
|||
|
|
{
|
|||
|
|
if (!ensureConnected(QStringLiteral("MicPhone Stream")) ||
|
|||
|
|
!ensureDeviceId(QStringLiteral("MicPhone Stream"), deviceId))
|
|||
|
|
return;
|
|||
|
|
if (m_microphoneStreamRunning.load()) {
|
|||
|
|
emit commandFinished(QStringLiteral("MicPhone Stream"), false,
|
|||
|
|
QStringLiteral("麦克风音频流已经在运行"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
if (m_microphoneStreamThread.joinable())
|
|||
|
|
m_microphoneStreamThread.join();
|
|||
|
|
m_microphoneStreamStop.store(false);
|
|||
|
|
m_microphoneStreamRunning.store(true);
|
|||
|
|
m_microphoneFrames.store(0);
|
|||
|
|
m_microphoneBytes.store(0);
|
|||
|
|
emit audioStreamStateChanged(QStringLiteral("MicPhoneService"), true,
|
|||
|
|
QStringLiteral("正在建立麦克风服务端音频流..."));
|
|||
|
|
|
|||
|
|
if (m_demoMode) {
|
|||
|
|
m_microphoneStreamThread = std::thread([this]() {
|
|||
|
|
const int sampleRate = 48000;
|
|||
|
|
const int samples = sampleRate / 50;
|
|||
|
|
const double pi = 3.14159265358979323846;
|
|||
|
|
qint64 sampleIndex = 0;
|
|||
|
|
while (!m_microphoneStreamStop.load()) {
|
|||
|
|
QByteArray pcm(samples * 2, Qt::Uninitialized);
|
|||
|
|
qint16 *output = reinterpret_cast<qint16 *>(pcm.data());
|
|||
|
|
for (int i = 0; i < samples; ++i) {
|
|||
|
|
output[i] = static_cast<qint16>(
|
|||
|
|
1800.0 * qSin((sampleIndex + i) * 2.0 * pi * 440.0 / sampleRate));
|
|||
|
|
}
|
|||
|
|
const qint64 pts = sampleIndex * 1000000LL / sampleRate;
|
|||
|
|
sampleIndex += samples;
|
|||
|
|
const quint64 frames = ++m_microphoneFrames;
|
|||
|
|
const quint64 bytes = m_microphoneBytes.fetch_add(pcm.size()) + pcm.size();
|
|||
|
|
emit audioFrameReceived(pcm, sampleRate, 1, 0,
|
|||
|
|
QStringLiteral("pcm_s16le"), pts, samples);
|
|||
|
|
emit audioStreamStatistics(QStringLiteral("MicPhoneService"), frames, bytes,
|
|||
|
|
sampleIndex * 1000LL / sampleRate);
|
|||
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(20));
|
|||
|
|
}
|
|||
|
|
m_microphoneStreamRunning.store(false);
|
|||
|
|
emit audioStreamStateChanged(QStringLiteral("MicPhoneService"), false,
|
|||
|
|
QStringLiteral("麦克风音频拉流已停止"));
|
|||
|
|
});
|
|||
|
|
emit audioStreamStateChanged(QStringLiteral("MicPhoneService"), true,
|
|||
|
|
QStringLiteral("演示模式:正在播放本地测试音"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
const std::shared_ptr<grpc::Channel> channel = m_channel;
|
|||
|
|
m_microphoneStreamThread = std::thread([this, channel, deviceId]() {
|
|||
|
|
runMicrophoneAudioStream(channel, deviceId);
|
|||
|
|
});
|
|||
|
|
#else
|
|||
|
|
m_microphoneStreamRunning.store(false);
|
|||
|
|
emit audioStreamStateChanged(QStringLiteral("MicPhoneService"), false,
|
|||
|
|
QStringLiteral("当前程序未编译 gRPC 支持"));
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void stopMicrophoneAudioStream()
|
|||
|
|
{
|
|||
|
|
stopMicrophoneStreamInternal();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void systemRefresh()
|
|||
|
|
{
|
|||
|
|
if (!ensureConnected(QStringLiteral("System Refresh")))
|
|||
|
|
return;
|
|||
|
|
if (m_demoMode) {
|
|||
|
|
QVariantMap info;
|
|||
|
|
info.insert(QStringLiteral("name"), QStringLiteral("CMVR Demo Controller"));
|
|||
|
|
info.insert(QStringLiteral("version"), QStringLiteral("demo-1.0"));
|
|||
|
|
info.insert(QStringLiteral("os"), QStringLiteral("Windows / simulator"));
|
|||
|
|
info.insert(QStringLiteral("architecture"), QStringLiteral("x86_64"));
|
|||
|
|
emit systemInfoReceived(info);
|
|||
|
|
emit systemStatusReceived(34.0, 48.0, 62.0, 6);
|
|||
|
|
emit commandFinished(QStringLiteral("System Refresh"), true, QStringLiteral("系统信息已刷新"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
cmvr::api::GetSystemInfoCommand::Request request;
|
|||
|
|
cmvr::api::GetSystemInfoCommand::Feedback response;
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
setDeadline(context, 1200);
|
|||
|
|
const grpc::Status status = m_system->GetSystemInfo(&context, request, &response);
|
|||
|
|
if (status.ok() && response.header().success()) {
|
|||
|
|
QVariantMap info;
|
|||
|
|
info.insert(QStringLiteral("name"), QString::fromStdString(response.system_name()));
|
|||
|
|
info.insert(QStringLiteral("version"), QString::fromStdString(response.version()));
|
|||
|
|
info.insert(QStringLiteral("description"), QString::fromStdString(response.description()));
|
|||
|
|
info.insert(QStringLiteral("os"), QString::fromStdString(response.os()));
|
|||
|
|
info.insert(QStringLiteral("kernel"), QString::fromStdString(response.kernel_version()));
|
|||
|
|
info.insert(QStringLiteral("architecture"), QString::fromStdString(response.architecture()));
|
|||
|
|
emit systemInfoReceived(info);
|
|||
|
|
}
|
|||
|
|
finish(QStringLiteral("System Refresh"), status, response.header().success(),
|
|||
|
|
QString::fromStdString(response.header().error_message()));
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void systemStopAll(const QString &deviceId)
|
|||
|
|
{
|
|||
|
|
if (!ensureConnected(QStringLiteral("System StopAll")) ||
|
|||
|
|
!ensureDeviceId(QStringLiteral("System StopAll"), deviceId))
|
|||
|
|
return;
|
|||
|
|
if (m_demoMode) {
|
|||
|
|
emit commandFinished(QStringLiteral("System StopAll"), true, QStringLiteral("全局停止命令已执行"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
cmvr::api::StopAllCommand::Request request;
|
|||
|
|
fillHeader(request.mutable_header(), deviceId);
|
|||
|
|
cmvr::api::StopAllCommand::Feedback response;
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
setDeadline(context, 1800);
|
|||
|
|
const grpc::Status status = m_system->StopAll(&context, request, &response);
|
|||
|
|
finish(QStringLiteral("System StopAll"), status, response.header().success(),
|
|||
|
|
QString::fromStdString(response.header().error_message()));
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void agvRefresh(const QString &deviceId)
|
|||
|
|
{
|
|||
|
|
if (!ensureConnected(QStringLiteral("AGV Refresh")) ||
|
|||
|
|
!ensureDeviceId(QStringLiteral("AGV Refresh"), deviceId))
|
|||
|
|
return;
|
|||
|
|
if (m_demoMode) {
|
|||
|
|
QVariantMap state;
|
|||
|
|
state.insert(QStringLiteral("connected"), true);
|
|||
|
|
state.insert(QStringLiteral("timestamp"), QDateTime::currentMSecsSinceEpoch() / 1000.0);
|
|||
|
|
state.insert(QStringLiteral("mode"), 1);
|
|||
|
|
state.insert(QStringLiteral("localized"), true);
|
|||
|
|
state.insert(QStringLiteral("moving"), false);
|
|||
|
|
state.insert(QStringLiteral("fault"), false);
|
|||
|
|
state.insert(QStringLiteral("emergency"), false);
|
|||
|
|
state.insert(QStringLiteral("map"), QStringLiteral("demo_map"));
|
|||
|
|
state.insert(QStringLiteral("station"), QStringLiteral("HOME"));
|
|||
|
|
state.insert(QStringLiteral("battery"), 0.82);
|
|||
|
|
state.insert(QStringLiteral("voltage"), 49.2);
|
|||
|
|
state.insert(QStringLiteral("current"), -2.9);
|
|||
|
|
state.insert(QStringLiteral("temperature"), 28.0);
|
|||
|
|
state.insert(QStringLiteral("x"), 1.24);
|
|||
|
|
state.insert(QStringLiteral("y"), -0.36);
|
|||
|
|
state.insert(QStringLiteral("theta"), 0.12);
|
|||
|
|
QVariantMap navigation;
|
|||
|
|
navigation.insert(QStringLiteral("state"), 0);
|
|||
|
|
navigation.insert(QStringLiteral("type"), 0);
|
|||
|
|
navigation.insert(QStringLiteral("progress"), 0.0);
|
|||
|
|
navigation.insert(QStringLiteral("message"), QStringLiteral("空闲"));
|
|||
|
|
emit agvStateReceived(state, navigation);
|
|||
|
|
emit commandFinished(QStringLiteral("AGV Refresh"), true, QStringLiteral("AGV 状态已刷新"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
cmvr::api::AgvRuntimeStateCommand::Request stateRequest;
|
|||
|
|
fillHeader(stateRequest.mutable_header(), deviceId);
|
|||
|
|
cmvr::api::AgvRuntimeStateCommand::Feedback stateResponse;
|
|||
|
|
grpc::ClientContext stateContext;
|
|||
|
|
setDeadline(stateContext, 1000);
|
|||
|
|
const grpc::Status stateStatus = m_agv->getRuntimeState(&stateContext, stateRequest, &stateResponse);
|
|||
|
|
|
|||
|
|
cmvr::api::AgvNavigationStatusCommand::Request navRequest;
|
|||
|
|
fillHeader(navRequest.mutable_header(), deviceId);
|
|||
|
|
cmvr::api::AgvNavigationStatusCommand::Feedback navResponse;
|
|||
|
|
grpc::ClientContext navContext;
|
|||
|
|
setDeadline(navContext, 1000);
|
|||
|
|
const grpc::Status navStatus = m_agv->getNavigationStatus(&navContext, navRequest, &navResponse);
|
|||
|
|
|
|||
|
|
if (stateStatus.ok() && stateResponse.header().success()) {
|
|||
|
|
const auto &source = stateResponse.state();
|
|||
|
|
QVariantMap state;
|
|||
|
|
state.insert(QStringLiteral("connected"), source.connected());
|
|||
|
|
state.insert(QStringLiteral("timestamp"), source.timestamp());
|
|||
|
|
state.insert(QStringLiteral("mode"), source.mode());
|
|||
|
|
state.insert(QStringLiteral("localized"), source.localized());
|
|||
|
|
state.insert(QStringLiteral("moving"), source.moving());
|
|||
|
|
state.insert(QStringLiteral("fault"), source.fault());
|
|||
|
|
state.insert(QStringLiteral("emergency"), source.emergency_stopped());
|
|||
|
|
state.insert(QStringLiteral("map"), QString::fromStdString(source.current_map()));
|
|||
|
|
state.insert(QStringLiteral("station"), QString::fromStdString(source.current_station()));
|
|||
|
|
state.insert(QStringLiteral("battery"), source.battery().percentage());
|
|||
|
|
state.insert(QStringLiteral("voltage"), source.battery().voltage());
|
|||
|
|
state.insert(QStringLiteral("current"), source.battery().current());
|
|||
|
|
state.insert(QStringLiteral("temperature"), source.battery().temperature());
|
|||
|
|
state.insert(QStringLiteral("x"), source.pose().x());
|
|||
|
|
state.insert(QStringLiteral("y"), source.pose().y());
|
|||
|
|
state.insert(QStringLiteral("theta"), source.pose().theta());
|
|||
|
|
state.insert(QStringLiteral("vx"), source.velocity().vx());
|
|||
|
|
state.insert(QStringLiteral("vy"), source.velocity().vy());
|
|||
|
|
state.insert(QStringLiteral("wz"), source.velocity().wz());
|
|||
|
|
state.insert(QStringLiteral("error"), QString::fromStdString(source.last_error()));
|
|||
|
|
QVariantMap navigation;
|
|||
|
|
if (navStatus.ok() && navResponse.header().success()) {
|
|||
|
|
navigation.insert(QStringLiteral("state"), navResponse.status().state());
|
|||
|
|
navigation.insert(QStringLiteral("type"), navResponse.status().type());
|
|||
|
|
navigation.insert(QStringLiteral("progress"), navResponse.status().progress());
|
|||
|
|
navigation.insert(QStringLiteral("message"), QString::fromStdString(navResponse.status().message()));
|
|||
|
|
}
|
|||
|
|
emit agvStateReceived(state, navigation);
|
|||
|
|
}
|
|||
|
|
finish(QStringLiteral("AGV Refresh"), stateStatus, stateResponse.header().success(),
|
|||
|
|
QString::fromStdString(stateResponse.header().error_message()));
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void agvNavigatePose(const QString &deviceId, double x, double y, double theta, double maxSpeed)
|
|||
|
|
{
|
|||
|
|
if (!ensureConnected(QStringLiteral("AGV NavigateToPose")) ||
|
|||
|
|
!ensureDeviceId(QStringLiteral("AGV NavigateToPose"), deviceId))
|
|||
|
|
return;
|
|||
|
|
if (m_demoMode) {
|
|||
|
|
emit commandFinished(QStringLiteral("AGV NavigateToPose"), true,
|
|||
|
|
QStringLiteral("目标位姿 (%1, %2, %3) 已下发").arg(x).arg(y).arg(theta));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
cmvr::api::AgvNavigateToPoseCommand::Request request;
|
|||
|
|
fillHeader(request.mutable_header(), deviceId);
|
|||
|
|
request.mutable_pose()->set_x(x);
|
|||
|
|
request.mutable_pose()->set_y(y);
|
|||
|
|
request.mutable_pose()->set_theta(theta);
|
|||
|
|
request.mutable_options()->set_max_speed(maxSpeed);
|
|||
|
|
request.mutable_options()->set_asynchronous(true);
|
|||
|
|
cmvr::api::AgvNavigateToPoseCommand::Feedback response;
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
setDeadline(context, 2500);
|
|||
|
|
const grpc::Status status = m_agv->navigateToPose(&context, request, &response);
|
|||
|
|
finish(QStringLiteral("AGV NavigateToPose"), status, response.header().success(),
|
|||
|
|
QString::fromStdString(response.header().error_message()));
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void agvNavigateStation(const QString &deviceId, const QString &stationId, double maxSpeed)
|
|||
|
|
{
|
|||
|
|
if (!ensureConnected(QStringLiteral("AGV NavigateToStation")) ||
|
|||
|
|
!ensureDeviceId(QStringLiteral("AGV NavigateToStation"), deviceId))
|
|||
|
|
return;
|
|||
|
|
if (stationId.trimmed().isEmpty()) {
|
|||
|
|
emit commandFinished(QStringLiteral("AGV NavigateToStation"), false, QStringLiteral("站点 ID 不能为空"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
if (m_demoMode) {
|
|||
|
|
emit commandFinished(QStringLiteral("AGV NavigateToStation"), true,
|
|||
|
|
QStringLiteral("目标站点 %1 已下发").arg(stationId));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
cmvr::api::AgvNavigateToStationCommand::Request request;
|
|||
|
|
fillHeader(request.mutable_header(), deviceId);
|
|||
|
|
request.set_station_id(stationId.toStdString());
|
|||
|
|
request.mutable_options()->set_max_speed(maxSpeed);
|
|||
|
|
request.mutable_options()->set_asynchronous(true);
|
|||
|
|
cmvr::api::AgvNavigateToStationCommand::Feedback response;
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
setDeadline(context, 2500);
|
|||
|
|
const grpc::Status status = m_agv->navigateToStation(&context, request, &response);
|
|||
|
|
finish(QStringLiteral("AGV NavigateToStation"), status, response.header().success(),
|
|||
|
|
QString::fromStdString(response.header().error_message()));
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void agvFollowPath(const QString &deviceId, const QVariantList &path, double maxSpeed)
|
|||
|
|
{
|
|||
|
|
const QString command = QStringLiteral("AGV FollowPath");
|
|||
|
|
if (!ensureConnected(command) || !ensureDeviceId(command, deviceId))
|
|||
|
|
return;
|
|||
|
|
if (path.isEmpty()) {
|
|||
|
|
emit commandFinished(command, false, QStringLiteral("路径至少需要包含一个有效路径段"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
if (m_demoMode) {
|
|||
|
|
emit commandFinished(command, true,
|
|||
|
|
QStringLiteral("包含 %1 个路径段的导航任务已下发").arg(path.size()));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
cmvr::api::AgvFollowPathCommand::Request request;
|
|||
|
|
fillHeader(request.mutable_header(), deviceId);
|
|||
|
|
for (const QVariant &entry : path) {
|
|||
|
|
const QVariantMap segmentValue = entry.toMap();
|
|||
|
|
const QString source = segmentValue.value(QStringLiteral("sourceStation")).toString().trimmed();
|
|||
|
|
const QString target = segmentValue.value(QStringLiteral("targetStation")).toString().trimmed();
|
|||
|
|
if (source.isEmpty() || target.isEmpty()) {
|
|||
|
|
emit commandFinished(command, false, QStringLiteral("路径段的起点和终点不能为空"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
cmvr::msgs::AgvPathSegment *segment = request.add_path();
|
|||
|
|
segment->set_source_station(source.toStdString());
|
|||
|
|
segment->set_target_station(target.toStdString());
|
|||
|
|
}
|
|||
|
|
request.mutable_options()->set_max_speed(maxSpeed);
|
|||
|
|
request.mutable_options()->set_asynchronous(true);
|
|||
|
|
cmvr::api::AgvFollowPathCommand::Feedback response;
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
setDeadline(context, 2500);
|
|||
|
|
const grpc::Status status = m_agv->followPath(&context, request, &response);
|
|||
|
|
finish(command, status, response.header().success(),
|
|||
|
|
QString::fromStdString(response.header().error_message()));
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void agvVelocity(const QString &deviceId, double vx, double vy, double wz)
|
|||
|
|
{
|
|||
|
|
if (!ensureConnected(QStringLiteral("AGV SetVelocity")) ||
|
|||
|
|
!ensureDeviceId(QStringLiteral("AGV SetVelocity"), deviceId))
|
|||
|
|
return;
|
|||
|
|
if (m_demoMode) {
|
|||
|
|
emit commandFinished(QStringLiteral("AGV SetVelocity"), true, QStringLiteral("底盘速度已下发"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
cmvr::api::AgvSetVelocityCommand::Request request;
|
|||
|
|
fillHeader(request.mutable_header(), deviceId);
|
|||
|
|
request.mutable_velocity()->set_vx(vx);
|
|||
|
|
request.mutable_velocity()->set_vy(vy);
|
|||
|
|
request.mutable_velocity()->set_wz(wz);
|
|||
|
|
cmvr::api::AgvSetVelocityCommand::Feedback response;
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
setDeadline(context, 1200);
|
|||
|
|
const grpc::Status status = m_agv->setVelocity(&context, request, &response);
|
|||
|
|
finish(QStringLiteral("AGV SetVelocity"), status, response.header().success(),
|
|||
|
|
QString::fromStdString(response.header().error_message()));
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void agvAction(const QString &deviceId, const QString &action)
|
|||
|
|
{
|
|||
|
|
const QString command = QStringLiteral("AGV %1").arg(action);
|
|||
|
|
if (!ensureConnected(command) || !ensureDeviceId(command, deviceId))
|
|||
|
|
return;
|
|||
|
|
if (m_demoMode) {
|
|||
|
|
emit commandFinished(command, true, QStringLiteral("AGV 操作已执行"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
cmvr::api::CommandHeader::Request request;
|
|||
|
|
fillHeader(&request, deviceId);
|
|||
|
|
cmvr::api::CommandHeader::Feedback response;
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
setDeadline(context, 1500);
|
|||
|
|
grpc::Status status;
|
|||
|
|
if (action == QStringLiteral("EmergencyStop"))
|
|||
|
|
status = m_agv->emergencyStop(&context, request, &response);
|
|||
|
|
else if (action == QStringLiteral("ClearFault"))
|
|||
|
|
status = m_agv->clearFault(&context, request, &response);
|
|||
|
|
else if (action == QStringLiteral("Pause"))
|
|||
|
|
status = m_agv->pauseNavigation(&context, request, &response);
|
|||
|
|
else if (action == QStringLiteral("Resume"))
|
|||
|
|
status = m_agv->resumeNavigation(&context, request, &response);
|
|||
|
|
else if (action == QStringLiteral("Cancel"))
|
|||
|
|
status = m_agv->cancelNavigation(&context, request, &response);
|
|||
|
|
else if (action == QStringLiteral("StopVelocity"))
|
|||
|
|
status = m_agv->stopVelocityControl(&context, request, &response);
|
|||
|
|
else if (action == QStringLiteral("StopMapping"))
|
|||
|
|
status = m_agv->stopMapping(&context, request, &response);
|
|||
|
|
else {
|
|||
|
|
emit commandFinished(command, false, QStringLiteral("未知 AGV 操作"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
finish(command, status, response.success(), QString::fromStdString(response.error_message()));
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void agvInventory(const QString &deviceId)
|
|||
|
|
{
|
|||
|
|
if (!ensureConnected(QStringLiteral("AGV Inventory")) ||
|
|||
|
|
!ensureDeviceId(QStringLiteral("AGV Inventory"), deviceId))
|
|||
|
|
return;
|
|||
|
|
if (m_demoMode) {
|
|||
|
|
QVariantList stations;
|
|||
|
|
QVariantMap home;
|
|||
|
|
home.insert(QStringLiteral("id"), QStringLiteral("HOME"));
|
|||
|
|
home.insert(QStringLiteral("type"), QStringLiteral("charge"));
|
|||
|
|
home.insert(QStringLiteral("x"), 0.0);
|
|||
|
|
home.insert(QStringLiteral("y"), 0.0);
|
|||
|
|
stations.append(home);
|
|||
|
|
const QStringList maps = {QStringLiteral("demo_map"), QStringLiteral("warehouse")};
|
|||
|
|
emit agvInventoryReceived(maps, stations);
|
|||
|
|
emit commandFinished(QStringLiteral("AGV Inventory"), true, QStringLiteral("地图与站点已刷新"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
cmvr::api::AgvListMapsCommand::Request mapsRequest;
|
|||
|
|
fillHeader(mapsRequest.mutable_header(), deviceId);
|
|||
|
|
cmvr::api::AgvListMapsCommand::Feedback mapsResponse;
|
|||
|
|
grpc::ClientContext mapsContext;
|
|||
|
|
setDeadline(mapsContext, 1200);
|
|||
|
|
const grpc::Status mapsStatus = m_agv->listMaps(&mapsContext, mapsRequest, &mapsResponse);
|
|||
|
|
|
|||
|
|
cmvr::api::AgvListStationsCommand::Request stationsRequest;
|
|||
|
|
fillHeader(stationsRequest.mutable_header(), deviceId);
|
|||
|
|
cmvr::api::AgvListStationsCommand::Feedback stationsResponse;
|
|||
|
|
grpc::ClientContext stationsContext;
|
|||
|
|
setDeadline(stationsContext, 1200);
|
|||
|
|
const grpc::Status stationsStatus = m_agv->listStations(&stationsContext, stationsRequest, &stationsResponse);
|
|||
|
|
|
|||
|
|
if (mapsStatus.ok() && mapsResponse.header().success()) {
|
|||
|
|
QStringList maps;
|
|||
|
|
for (const auto &mapName : mapsResponse.maps())
|
|||
|
|
maps.append(QString::fromStdString(mapName));
|
|||
|
|
QVariantList stations;
|
|||
|
|
if (stationsStatus.ok() && stationsResponse.header().success()) {
|
|||
|
|
for (const auto &source : stationsResponse.stations()) {
|
|||
|
|
QVariantMap station;
|
|||
|
|
station.insert(QStringLiteral("id"), QString::fromStdString(source.id()));
|
|||
|
|
station.insert(QStringLiteral("type"), QString::fromStdString(source.type()));
|
|||
|
|
station.insert(QStringLiteral("description"), QString::fromStdString(source.description()));
|
|||
|
|
station.insert(QStringLiteral("x"), source.pose().x());
|
|||
|
|
station.insert(QStringLiteral("y"), source.pose().y());
|
|||
|
|
station.insert(QStringLiteral("theta"), source.pose().theta());
|
|||
|
|
stations.append(station);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
emit agvInventoryReceived(maps, stations);
|
|||
|
|
}
|
|||
|
|
finish(QStringLiteral("AGV Inventory"), mapsStatus, mapsResponse.header().success(),
|
|||
|
|
QString::fromStdString(mapsResponse.header().error_message()));
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void agvSwitchMap(const QString &deviceId, const QString &mapName)
|
|||
|
|
{
|
|||
|
|
if (!ensureConnected(QStringLiteral("AGV SwitchMap")) ||
|
|||
|
|
!ensureDeviceId(QStringLiteral("AGV SwitchMap"), deviceId))
|
|||
|
|
return;
|
|||
|
|
if (mapName.trimmed().isEmpty()) {
|
|||
|
|
emit commandFinished(QStringLiteral("AGV SwitchMap"), false, QStringLiteral("地图名称不能为空"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
if (m_demoMode) {
|
|||
|
|
emit commandFinished(QStringLiteral("AGV SwitchMap"), true,
|
|||
|
|
QStringLiteral("已切换到地图 %1").arg(mapName));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
cmvr::api::AgvMapCommand::Request request;
|
|||
|
|
fillHeader(request.mutable_header(), deviceId);
|
|||
|
|
request.set_map_name(mapName.toStdString());
|
|||
|
|
cmvr::api::AgvMapCommand::Feedback response;
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
setDeadline(context, 2500);
|
|||
|
|
const grpc::Status status = m_agv->switchMap(&context, request, &response);
|
|||
|
|
finish(QStringLiteral("AGV SwitchMap"), status, response.header().success(),
|
|||
|
|
QString::fromStdString(response.header().error_message()));
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void startAgvMapStream(const QString &deviceId, const QString &mapName,
|
|||
|
|
int dimension, bool snapshot, bool incremental)
|
|||
|
|
{
|
|||
|
|
if (!ensureConnected(QStringLiteral("AGV StreamMap")) ||
|
|||
|
|
!ensureDeviceId(QStringLiteral("AGV StreamMap"), deviceId))
|
|||
|
|
return;
|
|||
|
|
stopAgvMapStreamInternal();
|
|||
|
|
m_agvMapStreamStop.store(false);
|
|||
|
|
m_agvMapStreamRunning.store(true);
|
|||
|
|
emit agvMapStreamStateChanged(true, QStringLiteral("正在等待地图数据"));
|
|||
|
|
|
|||
|
|
if (m_demoMode) {
|
|||
|
|
QImage image(1280, 720, QImage::Format_RGB32);
|
|||
|
|
image.fill(QColor(QStringLiteral("#eef2f6")));
|
|||
|
|
QPainter painter(&image);
|
|||
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|||
|
|
painter.setPen(QPen(QColor(QStringLiteral("#d3dde6")), 1));
|
|||
|
|
for (int x = 60; x < image.width(); x += 80)
|
|||
|
|
painter.drawLine(x, 30, x, image.height() - 30);
|
|||
|
|
for (int y = 40; y < image.height(); y += 60)
|
|||
|
|
painter.drawLine(30, y, image.width() - 30, y);
|
|||
|
|
const QVector<QPointF> stations = {
|
|||
|
|
QPointF(180, 160), QPointF(460, 220), QPointF(760, 150),
|
|||
|
|
QPointF(1050, 260), QPointF(850, 500), QPointF(400, 540)
|
|||
|
|
};
|
|||
|
|
for (int i = 1; i < stations.size(); ++i) {
|
|||
|
|
painter.setPen(QPen(QColor(QStringLiteral("#9db7ce")), 4));
|
|||
|
|
painter.drawLine(stations.at(i - 1), stations.at(i));
|
|||
|
|
}
|
|||
|
|
for (int i = 0; i < stations.size(); ++i) {
|
|||
|
|
painter.setPen(Qt::NoPen);
|
|||
|
|
painter.setBrush(QColor(QStringLiteral("#218c74")));
|
|||
|
|
painter.drawEllipse(stations.at(i), 11, 11);
|
|||
|
|
painter.setPen(QColor(QStringLiteral("#26323d")));
|
|||
|
|
painter.drawText(stations.at(i) + QPointF(15, -10),
|
|||
|
|
QStringLiteral("STATION_%1").arg(i + 1));
|
|||
|
|
}
|
|||
|
|
painter.setBrush(QColor(QStringLiteral("#237ddd")));
|
|||
|
|
painter.setPen(Qt::NoPen);
|
|||
|
|
QPolygonF robot;
|
|||
|
|
robot << QPointF(290, 320) << QPointF(260, 338)
|
|||
|
|
<< QPointF(260, 302);
|
|||
|
|
painter.drawPolygon(robot);
|
|||
|
|
painter.end();
|
|||
|
|
emit agvMapFrameReceived(image, QStringLiteral("演示 2D 地图 · 6 个站点"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
m_agvMapStreamThread = std::thread(
|
|||
|
|
[this, deviceId, mapName, dimension, snapshot, incremental]() {
|
|||
|
|
cmvr::api::AgvMapStreamCommand::Request request;
|
|||
|
|
fillHeader(request.mutable_header(), deviceId);
|
|||
|
|
request.set_map_name(mapName.toStdString());
|
|||
|
|
request.set_dimension(static_cast<cmvr::msgs::AgvMapDimension>(dimension));
|
|||
|
|
request.set_snapshot(snapshot);
|
|||
|
|
request.set_incremental(incremental);
|
|||
|
|
request.set_max_chunk_bytes(1024 * 1024);
|
|||
|
|
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(m_agvMapStreamContextMutex);
|
|||
|
|
m_agvMapStreamContext = &context;
|
|||
|
|
}
|
|||
|
|
std::unique_ptr<grpc::ClientReader<cmvr::api::AgvMapStreamCommand::Feedback>>
|
|||
|
|
reader = m_agv->streamMap(&context, request);
|
|||
|
|
cmvr::api::AgvMapStreamCommand::Feedback response;
|
|||
|
|
while (!m_agvMapStreamStop.load() && reader->Read(&response)) {
|
|||
|
|
if (!response.header().success())
|
|||
|
|
continue;
|
|||
|
|
const auto &update = response.update();
|
|||
|
|
QImage image;
|
|||
|
|
QString detail;
|
|||
|
|
if (update.has_map_2d()) {
|
|||
|
|
const auto &map = update.map_2d();
|
|||
|
|
const int sourceWidth = static_cast<int>(map.width());
|
|||
|
|
const int sourceHeight = static_cast<int>(map.height());
|
|||
|
|
if (sourceWidth <= 0 || sourceHeight <= 0)
|
|||
|
|
continue;
|
|||
|
|
const double scale = qMin(
|
|||
|
|
1.0, qMin(1600.0 / sourceWidth, 1000.0 / sourceHeight));
|
|||
|
|
const int width = qMax(1, qRound(sourceWidth * scale));
|
|||
|
|
const int height = qMax(1, qRound(sourceHeight * scale));
|
|||
|
|
image = QImage(width, height, QImage::Format_RGB32);
|
|||
|
|
for (int y = 0; y < height; ++y) {
|
|||
|
|
QRgb *line = reinterpret_cast<QRgb *>(
|
|||
|
|
image.scanLine(height - 1 - y));
|
|||
|
|
const int sourceY = qMin(sourceHeight - 1,
|
|||
|
|
qFloor(y / scale));
|
|||
|
|
for (int x = 0; x < width; ++x) {
|
|||
|
|
const int sourceX = qMin(sourceWidth - 1,
|
|||
|
|
qFloor(x / scale));
|
|||
|
|
const int index = sourceY * sourceWidth + sourceX;
|
|||
|
|
const int value = index < map.data_size()
|
|||
|
|
? map.data(index) : -1;
|
|||
|
|
if (value < 0)
|
|||
|
|
line[x] = qRgb(205, 212, 219);
|
|||
|
|
else if (value == 0)
|
|||
|
|
line[x] = qRgb(248, 250, 252);
|
|||
|
|
else {
|
|||
|
|
const int shade = qBound(35, 235 - value * 2, 210);
|
|||
|
|
line[x] = qRgb(shade, shade, shade);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
QPainter painter(&image);
|
|||
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|||
|
|
const double resolution = map.resolution() > 0.0
|
|||
|
|
? map.resolution() : 1.0;
|
|||
|
|
for (const auto &object : map.objects()) {
|
|||
|
|
QPolygonF geometry;
|
|||
|
|
for (const auto &point : object.points()) {
|
|||
|
|
geometry.append(QPointF(
|
|||
|
|
(point.x() - map.origin().x()) / resolution * scale,
|
|||
|
|
height - (point.y() - map.origin().y()) /
|
|||
|
|
resolution * scale));
|
|||
|
|
}
|
|||
|
|
painter.setPen(QPen(QColor(QStringLiteral("#218c74")), 2));
|
|||
|
|
painter.setBrush(QColor(33, 140, 116, 70));
|
|||
|
|
if (geometry.size() == 1)
|
|||
|
|
painter.drawEllipse(geometry.first(), 6, 6);
|
|||
|
|
else if (geometry.size() > 1)
|
|||
|
|
painter.drawPolyline(geometry);
|
|||
|
|
if (!geometry.isEmpty()) {
|
|||
|
|
painter.setPen(QColor(QStringLiteral("#145da8")));
|
|||
|
|
painter.drawText(geometry.first() + QPointF(8, -6),
|
|||
|
|
QString::fromStdString(object.id()));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
painter.end();
|
|||
|
|
detail = QStringLiteral("%1 · 2D · %2×%3 · seq %4")
|
|||
|
|
.arg(QString::fromStdString(update.map_id()))
|
|||
|
|
.arg(sourceWidth).arg(sourceHeight)
|
|||
|
|
.arg(static_cast<qulonglong>(update.sequence()));
|
|||
|
|
} else if (update.has_map_3d()) {
|
|||
|
|
const auto &map = update.map_3d();
|
|||
|
|
image = QImage(1280, 720, QImage::Format_RGB32);
|
|||
|
|
image.fill(QColor(QStringLiteral("#eef2f6")));
|
|||
|
|
if (map.points_size() > 0) {
|
|||
|
|
double minX = map.points(0).x(), maxX = minX;
|
|||
|
|
double minY = map.points(0).y(), maxY = minY;
|
|||
|
|
const int step = qMax(1, map.points_size() / 80000);
|
|||
|
|
for (int i = 0; i < map.points_size(); i += step) {
|
|||
|
|
minX = qMin(minX, map.points(i).x());
|
|||
|
|
maxX = qMax(maxX, map.points(i).x());
|
|||
|
|
minY = qMin(minY, map.points(i).y());
|
|||
|
|
maxY = qMax(maxY, map.points(i).y());
|
|||
|
|
}
|
|||
|
|
if (qFuzzyCompare(minX, maxX)) { minX -= 1.0; maxX += 1.0; }
|
|||
|
|
if (qFuzzyCompare(minY, maxY)) { minY -= 1.0; maxY += 1.0; }
|
|||
|
|
QPainter painter(&image);
|
|||
|
|
painter.setPen(QColor(QStringLiteral("#456b87")));
|
|||
|
|
for (int i = 0; i < map.points_size(); i += step) {
|
|||
|
|
const auto &point = map.points(i);
|
|||
|
|
painter.drawPoint(QPointF(
|
|||
|
|
30.0 + (point.x() - minX) / (maxX - minX) * 1220.0,
|
|||
|
|
690.0 - (point.y() - minY) / (maxY - minY) * 660.0));
|
|||
|
|
}
|
|||
|
|
painter.end();
|
|||
|
|
}
|
|||
|
|
detail = QStringLiteral("%1 · 3D · %2 points · seq %3")
|
|||
|
|
.arg(QString::fromStdString(update.map_id()))
|
|||
|
|
.arg(map.points_size())
|
|||
|
|
.arg(static_cast<qulonglong>(update.sequence()));
|
|||
|
|
}
|
|||
|
|
if (!image.isNull())
|
|||
|
|
emit agvMapFrameReceived(image, detail);
|
|||
|
|
}
|
|||
|
|
const grpc::Status status = reader->Finish();
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(m_agvMapStreamContextMutex);
|
|||
|
|
m_agvMapStreamContext = nullptr;
|
|||
|
|
}
|
|||
|
|
m_agvMapStreamRunning.store(false);
|
|||
|
|
const QString detail = m_agvMapStreamStop.load()
|
|||
|
|
? QStringLiteral("地图流已停止")
|
|||
|
|
: status.ok() ? QStringLiteral("地图流已结束")
|
|||
|
|
: QString::fromStdString(status.error_message());
|
|||
|
|
emit agvMapStreamStateChanged(false, detail);
|
|||
|
|
});
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void stopAgvMapStream()
|
|||
|
|
{
|
|||
|
|
stopAgvMapStreamInternal();
|
|||
|
|
emit agvMapStreamStateChanged(false, QStringLiteral("地图流已停止"));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void invokeRpc(const QString &serviceName, const QString &methodName,
|
|||
|
|
const QString &requestJson, const QString &deviceId)
|
|||
|
|
{
|
|||
|
|
const QString command = QStringLiteral("%1.%2").arg(serviceName, methodName);
|
|||
|
|
if (!ensureConnected(command)) {
|
|||
|
|
emit rpcFinished(serviceName, methodName, false,
|
|||
|
|
QStringLiteral("{\"error\":\"设备尚未连接\"}"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
if (m_demoMode) {
|
|||
|
|
QJsonObject result;
|
|||
|
|
result.insert(QStringLiteral("demo"), true);
|
|||
|
|
result.insert(QStringLiteral("service"), serviceName);
|
|||
|
|
result.insert(QStringLiteral("method"), methodName);
|
|||
|
|
result.insert(QStringLiteral("deviceId"), deviceId);
|
|||
|
|
result.insert(QStringLiteral("message"), QStringLiteral("演示模式调用成功"));
|
|||
|
|
emit rpcFinished(serviceName, methodName, true,
|
|||
|
|
QString::fromUtf8(QJsonDocument(result).toJson(QJsonDocument::Indented)));
|
|||
|
|
emit commandFinished(command, true, QStringLiteral("演示模式 RPC 调用完成"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
const google::protobuf::ServiceDescriptor *service =
|
|||
|
|
google::protobuf::DescriptorPool::generated_pool()->FindServiceByName(
|
|||
|
|
serviceName.toStdString());
|
|||
|
|
const google::protobuf::MethodDescriptor *method =
|
|||
|
|
service ? service->FindMethodByName(methodName.toStdString()) : nullptr;
|
|||
|
|
if (!method || !m_generic) {
|
|||
|
|
const QString error = QStringLiteral("找不到 RPC 描述符:%1").arg(command);
|
|||
|
|
emit rpcFinished(serviceName, methodName, false,
|
|||
|
|
QStringLiteral("{\"error\":\"%1\"}").arg(error));
|
|||
|
|
emit commandFinished(command, false, error);
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
google::protobuf::DynamicMessageFactory factory;
|
|||
|
|
const google::protobuf::Message *requestPrototype =
|
|||
|
|
factory.GetPrototype(method->input_type());
|
|||
|
|
const google::protobuf::Message *responsePrototype =
|
|||
|
|
factory.GetPrototype(method->output_type());
|
|||
|
|
if (!requestPrototype || !responsePrototype) {
|
|||
|
|
const QString error = QStringLiteral("无法创建 protobuf 动态消息");
|
|||
|
|
emit rpcFinished(serviceName, methodName, false,
|
|||
|
|
QStringLiteral("{\"error\":\"%1\"}").arg(error));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QStringList requestDocuments;
|
|||
|
|
QJsonParseError parseError;
|
|||
|
|
const QJsonDocument jsonDocument = QJsonDocument::fromJson(requestJson.toUtf8(), &parseError);
|
|||
|
|
if (parseError.error != QJsonParseError::NoError) {
|
|||
|
|
const QString error = QStringLiteral("请求 JSON 无效:%1").arg(parseError.errorString());
|
|||
|
|
emit rpcFinished(serviceName, methodName, false,
|
|||
|
|
QStringLiteral("{\"error\":\"%1\"}").arg(error));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
if (jsonDocument.isArray()) {
|
|||
|
|
for (const QJsonValue &value : jsonDocument.array())
|
|||
|
|
requestDocuments.append(QString::fromUtf8(
|
|||
|
|
QJsonDocument(value.toObject()).toJson(QJsonDocument::Compact)));
|
|||
|
|
} else {
|
|||
|
|
requestDocuments.append(QString::fromUtf8(
|
|||
|
|
QJsonDocument(jsonDocument.object()).toJson(QJsonDocument::Compact)));
|
|||
|
|
}
|
|||
|
|
if (requestDocuments.isEmpty())
|
|||
|
|
requestDocuments.append(QStringLiteral("{}"));
|
|||
|
|
|
|||
|
|
std::vector<std::unique_ptr<google::protobuf::Message>> requests;
|
|||
|
|
std::vector<grpc::ByteBuffer> requestBuffers;
|
|||
|
|
google::protobuf::util::JsonParseOptions jsonOptions;
|
|||
|
|
jsonOptions.ignore_unknown_fields = false;
|
|||
|
|
for (const QString &document : requestDocuments) {
|
|||
|
|
std::unique_ptr<google::protobuf::Message> request(requestPrototype->New());
|
|||
|
|
const auto jsonStatus = google::protobuf::util::JsonStringToMessage(
|
|||
|
|
document.toStdString(), request.get(), jsonOptions);
|
|||
|
|
if (!jsonStatus.ok()) {
|
|||
|
|
const QString error = QStringLiteral("protobuf JSON 转换失败:%1")
|
|||
|
|
.arg(QString::fromStdString(jsonStatus.ToString()));
|
|||
|
|
emit rpcFinished(serviceName, methodName, false,
|
|||
|
|
QStringLiteral("{\"error\":\"%1\"}").arg(error));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
setDynamicDeviceId(request.get(), deviceId);
|
|||
|
|
std::string serialized;
|
|||
|
|
request->SerializeToString(&serialized);
|
|||
|
|
grpc::Slice slice(serialized);
|
|||
|
|
requestBuffers.emplace_back(&slice, 1);
|
|||
|
|
requests.emplace_back(std::move(request));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const std::string path = QStringLiteral("/%1/%2").arg(serviceName, methodName).toStdString();
|
|||
|
|
grpc::Status grpcStatus;
|
|||
|
|
QJsonArray streamResponses;
|
|||
|
|
QString unaryResponse;
|
|||
|
|
bool transportOk = false;
|
|||
|
|
if (!method->client_streaming() && !method->server_streaming()) {
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
setDeadline(context, 4000);
|
|||
|
|
grpc::CompletionQueue queue;
|
|||
|
|
grpc::ByteBuffer responseBuffer;
|
|||
|
|
std::unique_ptr<grpc::GenericClientAsyncResponseReader> call =
|
|||
|
|
m_generic->PrepareUnaryCall(&context, path, requestBuffers.front(), &queue);
|
|||
|
|
call->StartCall();
|
|||
|
|
call->Finish(&responseBuffer, &grpcStatus, reinterpret_cast<void *>(1));
|
|||
|
|
void *tag = nullptr;
|
|||
|
|
bool eventOk = false;
|
|||
|
|
transportOk = queue.Next(&tag, &eventOk) && eventOk &&
|
|||
|
|
tag == reinterpret_cast<void *>(1) && grpcStatus.ok();
|
|||
|
|
if (transportOk)
|
|||
|
|
unaryResponse = byteBufferToJson(responseBuffer, responsePrototype, factory);
|
|||
|
|
queue.Shutdown();
|
|||
|
|
} else {
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
setDeadline(context, 5000);
|
|||
|
|
grpc::CompletionQueue queue;
|
|||
|
|
std::unique_ptr<grpc::GenericClientAsyncReaderWriter> call =
|
|||
|
|
m_generic->PrepareCall(&context, path, &queue);
|
|||
|
|
void *tag = nullptr;
|
|||
|
|
bool eventOk = false;
|
|||
|
|
call->StartCall(reinterpret_cast<void *>(1));
|
|||
|
|
bool started = queue.Next(&tag, &eventOk) && eventOk;
|
|||
|
|
for (int index = 0; started && index < static_cast<int>(requestBuffers.size()); ++index) {
|
|||
|
|
call->Write(requestBuffers.at(index), reinterpret_cast<void *>(2));
|
|||
|
|
started = queue.Next(&tag, &eventOk) && eventOk;
|
|||
|
|
}
|
|||
|
|
if (started) {
|
|||
|
|
call->WritesDone(reinterpret_cast<void *>(3));
|
|||
|
|
queue.Next(&tag, &eventOk);
|
|||
|
|
}
|
|||
|
|
while (started && streamResponses.size() < 100) {
|
|||
|
|
grpc::ByteBuffer responseBuffer;
|
|||
|
|
call->Read(&responseBuffer, reinterpret_cast<void *>(4));
|
|||
|
|
if (!queue.Next(&tag, &eventOk) || !eventOk)
|
|||
|
|
break;
|
|||
|
|
const QString response = byteBufferToJson(responseBuffer, responsePrototype, factory);
|
|||
|
|
QJsonParseError responseError;
|
|||
|
|
const QJsonDocument parsed = QJsonDocument::fromJson(response.toUtf8(), &responseError);
|
|||
|
|
streamResponses.append(responseError.error == QJsonParseError::NoError
|
|||
|
|
? QJsonValue(parsed.object()) : QJsonValue(response));
|
|||
|
|
}
|
|||
|
|
if (streamResponses.size() >= 100)
|
|||
|
|
context.TryCancel();
|
|||
|
|
call->Finish(&grpcStatus, reinterpret_cast<void *>(5));
|
|||
|
|
queue.Next(&tag, &eventOk);
|
|||
|
|
transportOk = grpcStatus.ok() ||
|
|||
|
|
(grpcStatus.error_code() == grpc::StatusCode::DEADLINE_EXCEEDED &&
|
|||
|
|
!streamResponses.isEmpty());
|
|||
|
|
queue.Shutdown();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QString result;
|
|||
|
|
if (transportOk) {
|
|||
|
|
result = method->client_streaming() || method->server_streaming()
|
|||
|
|
? QString::fromUtf8(QJsonDocument(streamResponses).toJson(QJsonDocument::Indented))
|
|||
|
|
: unaryResponse;
|
|||
|
|
} else {
|
|||
|
|
QJsonObject error;
|
|||
|
|
error.insert(QStringLiteral("grpcCode"), grpcStatus.error_code());
|
|||
|
|
error.insert(QStringLiteral("error"), QString::fromStdString(grpcStatus.error_message()));
|
|||
|
|
result = QString::fromUtf8(QJsonDocument(error).toJson(QJsonDocument::Indented));
|
|||
|
|
}
|
|||
|
|
emit rpcFinished(serviceName, methodName, transportOk, result);
|
|||
|
|
emit commandFinished(command, transportOk,
|
|||
|
|
transportOk ? QStringLiteral("RPC 调用完成")
|
|||
|
|
: QString::fromStdString(grpcStatus.error_message()));
|
|||
|
|
#else
|
|||
|
|
Q_UNUSED(requestJson)
|
|||
|
|
Q_UNUSED(deviceId)
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
std::function<void(bool, int, const QString &)> connectionChanged;
|
|||
|
|
std::function<void(const QVariantList &, const QVariantMap &)> telemetryReceived;
|
|||
|
|
std::function<void(double, double, double, int)> systemStatusReceived;
|
|||
|
|
std::function<void(const QVariantMap &)> systemInfoReceived;
|
|||
|
|
std::function<void(const QImage &, const QString &)> imageReceived;
|
|||
|
|
std::function<void(const QString &, const VideoFrame &, const QString &)>
|
|||
|
|
videoFrameReceivedForCamera;
|
|||
|
|
std::function<void(const QString &, bool, const QString &)>
|
|||
|
|
cameraStreamStateChangedForCamera;
|
|||
|
|
std::function<void(bool, const QString &)> cameraRecordingStateChanged;
|
|||
|
|
std::function<void(const QVariantMap &, const QVariantMap &)> agvStateReceived;
|
|||
|
|
std::function<void(const QStringList &, const QVariantList &)> agvInventoryReceived;
|
|||
|
|
std::function<void(const QImage &, const QString &)> agvMapFrameReceived;
|
|||
|
|
std::function<void(bool, const QString &)> agvMapStreamStateChanged;
|
|||
|
|
std::function<void(const QString &, const QString &, bool, const QString &)> rpcFinished;
|
|||
|
|
std::function<void(const QString &, bool, const QString &)> audioStreamStateChanged;
|
|||
|
|
std::function<void(const QString &, quint64, quint64, qint64)> audioStreamStatistics;
|
|||
|
|
std::function<void(const QByteArray &, int, int, int, const QString &, qint64, int)>
|
|||
|
|
audioFrameReceived;
|
|||
|
|
std::function<void(bool, const QString &)> configurationConnectionChanged;
|
|||
|
|
std::function<void(const QString &, const QVariantList &, bool, const QString &)>
|
|||
|
|
configurationFilesReceived;
|
|||
|
|
std::function<void(const QString &, const QString &, const QString &, const QString &,
|
|||
|
|
bool, const QString &)> configurationFileReceived;
|
|||
|
|
std::function<void(const QString &, bool, const QString &)>
|
|||
|
|
configurationValidationFinished;
|
|||
|
|
std::function<void(const QString &, const QString &, bool, const QString &)>
|
|||
|
|
configurationSaveFinished;
|
|||
|
|
std::function<void(const QString &, bool, const QString &)> commandFinished;
|
|||
|
|
std::function<void(const QString &, const QString &)> logMessage;
|
|||
|
|
|
|||
|
|
private:
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
bool ensureConfigurationConnected(const QString &operation)
|
|||
|
|
{
|
|||
|
|
if (m_configConnected && m_configuration)
|
|||
|
|
return true;
|
|||
|
|
emit configurationConnectionChanged(
|
|||
|
|
false, QStringLiteral("%1失败:配置服务未连接").arg(operation));
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
static QString configurationError(const grpc::Status &status,
|
|||
|
|
const std::string &applicationError)
|
|||
|
|
{
|
|||
|
|
if (!status.ok())
|
|||
|
|
return QString::fromStdString(status.error_message());
|
|||
|
|
return QString::fromStdString(applicationError);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
std::shared_ptr<ArmMotionSession> prepareArmMotionSession(const QString &command)
|
|||
|
|
{
|
|||
|
|
std::shared_ptr<ArmMotionSession> previous;
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(m_armMotionSessionMutex);
|
|||
|
|
previous = m_armMotionSession;
|
|||
|
|
if (previous && previous->running.load()) {
|
|||
|
|
emit commandFinished(command, false,
|
|||
|
|
QStringLiteral("已有机械臂运动命令正在执行"));
|
|||
|
|
return {};
|
|||
|
|
}
|
|||
|
|
m_armMotionSession.reset();
|
|||
|
|
}
|
|||
|
|
if (previous && previous->thread.joinable() &&
|
|||
|
|
previous->thread.get_id() != std::this_thread::get_id()) {
|
|||
|
|
previous->thread.join();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
auto session = std::make_shared<ArmMotionSession>();
|
|||
|
|
session->running.store(true);
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(m_armMotionSessionMutex);
|
|||
|
|
m_armMotionSession = session;
|
|||
|
|
}
|
|||
|
|
return session;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
template <typename Request, typename Response, typename Call>
|
|||
|
|
void startArmMotion(const QString &command, Request request, Call call)
|
|||
|
|
{
|
|||
|
|
const auto session = prepareArmMotionSession(command);
|
|||
|
|
if (!session)
|
|||
|
|
return;
|
|||
|
|
const auto channel = m_channel;
|
|||
|
|
session->thread = std::thread(
|
|||
|
|
[this, channel, session, command, request = std::move(request), call]() mutable {
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
setDeadline(context, kArmMotionTimeoutMs);
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(session->contextMutex);
|
|||
|
|
session->context = &context;
|
|||
|
|
if (session->stop.load())
|
|||
|
|
context.TryCancel();
|
|||
|
|
}
|
|||
|
|
try {
|
|||
|
|
auto stub = cmvr::api::ArmService::NewStub(channel);
|
|||
|
|
Response response;
|
|||
|
|
const grpc::Status status = call(*stub, &context, request, &response);
|
|||
|
|
finish(command, status, response.header().success(),
|
|||
|
|
QString::fromStdString(response.header().error_message()));
|
|||
|
|
} catch (const std::exception &error) {
|
|||
|
|
emit commandFinished(command, false,
|
|||
|
|
QStringLiteral("机械臂运动调用异常:%1")
|
|||
|
|
.arg(QString::fromLocal8Bit(error.what())));
|
|||
|
|
} catch (...) {
|
|||
|
|
emit commandFinished(command, false,
|
|||
|
|
QStringLiteral("机械臂运动调用发生未知异常"));
|
|||
|
|
}
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(session->contextMutex);
|
|||
|
|
session->context = nullptr;
|
|||
|
|
}
|
|||
|
|
session->running.store(false);
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
static void setDynamicDeviceId(google::protobuf::Message *request,
|
|||
|
|
const QString &deviceId)
|
|||
|
|
{
|
|||
|
|
if (!request || deviceId.trimmed().isEmpty())
|
|||
|
|
return;
|
|||
|
|
const google::protobuf::FieldDescriptor *headerField =
|
|||
|
|
request->GetDescriptor()->FindFieldByName("header");
|
|||
|
|
if (!headerField || headerField->cpp_type() !=
|
|||
|
|
google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE)
|
|||
|
|
return;
|
|||
|
|
google::protobuf::Message *header =
|
|||
|
|
request->GetReflection()->MutableMessage(request, headerField);
|
|||
|
|
const google::protobuf::FieldDescriptor *deviceField =
|
|||
|
|
header->GetDescriptor()->FindFieldByName("device_id");
|
|||
|
|
if (deviceField)
|
|||
|
|
header->GetReflection()->SetString(header, deviceField, deviceId.toStdString());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
static QString byteBufferToJson(const grpc::ByteBuffer &buffer,
|
|||
|
|
const google::protobuf::Message *prototype,
|
|||
|
|
google::protobuf::DynamicMessageFactory &factory)
|
|||
|
|
{
|
|||
|
|
Q_UNUSED(factory)
|
|||
|
|
std::vector<grpc::Slice> slices;
|
|||
|
|
buffer.Dump(&slices);
|
|||
|
|
std::string serialized;
|
|||
|
|
for (const grpc::Slice &slice : slices)
|
|||
|
|
serialized.append(reinterpret_cast<const char *>(slice.begin()), slice.size());
|
|||
|
|
std::unique_ptr<google::protobuf::Message> response(prototype->New());
|
|||
|
|
if (!response->ParseFromString(serialized))
|
|||
|
|
return QStringLiteral("{\"error\":\"无法解析 protobuf 反馈\"}");
|
|||
|
|
std::string json;
|
|||
|
|
google::protobuf::util::JsonPrintOptions options;
|
|||
|
|
options.add_whitespace = true;
|
|||
|
|
options.always_print_primitive_fields = true;
|
|||
|
|
const auto status = google::protobuf::util::MessageToJsonString(*response, &json, options);
|
|||
|
|
return status.ok() ? QString::fromStdString(json)
|
|||
|
|
: QStringLiteral("{\"error\":\"%1\"}")
|
|||
|
|
.arg(QString::fromStdString(status.ToString()));
|
|||
|
|
}
|
|||
|
|
#endif
|
|||
|
|
|
|||
|
|
void stopArmMotionInternal()
|
|||
|
|
{
|
|||
|
|
std::shared_ptr<ArmMotionSession> session;
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(m_armMotionSessionMutex);
|
|||
|
|
session = m_armMotionSession;
|
|||
|
|
}
|
|||
|
|
if (!session)
|
|||
|
|
return;
|
|||
|
|
|
|||
|
|
session->stop.store(true);
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(session->contextMutex);
|
|||
|
|
if (session->context)
|
|||
|
|
session->context->TryCancel();
|
|||
|
|
}
|
|||
|
|
#endif
|
|||
|
|
if (session->thread.joinable() &&
|
|||
|
|
session->thread.get_id() != std::this_thread::get_id()) {
|
|||
|
|
session->thread.join();
|
|||
|
|
}
|
|||
|
|
session->running.store(false);
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(m_armMotionSessionMutex);
|
|||
|
|
if (m_armMotionSession == session)
|
|||
|
|
m_armMotionSession.reset();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void stopAgvMapStreamInternal()
|
|||
|
|
{
|
|||
|
|
m_agvMapStreamStop.store(true);
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(m_agvMapStreamContextMutex);
|
|||
|
|
if (m_agvMapStreamContext)
|
|||
|
|
m_agvMapStreamContext->TryCancel();
|
|||
|
|
}
|
|||
|
|
#endif
|
|||
|
|
if (m_agvMapStreamThread.joinable() &&
|
|||
|
|
m_agvMapStreamThread.get_id() != std::this_thread::get_id())
|
|||
|
|
m_agvMapStreamThread.join();
|
|||
|
|
m_agvMapStreamRunning.store(false);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void stopCameraStreamInternal(const QString &cameraId = QString())
|
|||
|
|
{
|
|||
|
|
QVector<std::shared_ptr<CameraStreamSession>> sessions;
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(m_cameraSessionsMutex);
|
|||
|
|
if (cameraId.isEmpty()) {
|
|||
|
|
sessions.reserve(m_cameraSessions.size());
|
|||
|
|
for (auto it = m_cameraSessions.cbegin();
|
|||
|
|
it != m_cameraSessions.cend(); ++it) {
|
|||
|
|
sessions.append(it.value());
|
|||
|
|
}
|
|||
|
|
} else {
|
|||
|
|
const auto it = m_cameraSessions.find(cameraId);
|
|||
|
|
if (it != m_cameraSessions.end())
|
|||
|
|
sessions.append(it.value());
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
for (const auto &session : sessions) {
|
|||
|
|
session->stop.store(true);
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
std::lock_guard<std::mutex> lock(session->contextMutex);
|
|||
|
|
if (session->context)
|
|||
|
|
session->context->TryCancel();
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
for (const auto &session : sessions) {
|
|||
|
|
if (session->thread.joinable() &&
|
|||
|
|
session->thread.get_id() != std::this_thread::get_id()) {
|
|||
|
|
session->thread.join();
|
|||
|
|
}
|
|||
|
|
session->running.store(false);
|
|||
|
|
}
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(m_cameraSessionsMutex);
|
|||
|
|
for (const auto &session : sessions) {
|
|||
|
|
const auto it = m_cameraSessions.find(session->cameraId);
|
|||
|
|
if (it != m_cameraSessions.end() && it.value() == session)
|
|||
|
|
m_cameraSessions.erase(it);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void stopSpeakerStreamInternal(bool cancel = false)
|
|||
|
|
{
|
|||
|
|
const bool wasRunning = m_speakerStreamRunning.load();
|
|||
|
|
m_speakerStreamStop.store(true);
|
|||
|
|
m_speakerQueueCondition.notify_all();
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(m_speakerContextMutex);
|
|||
|
|
if (cancel && m_speakerStreamContext)
|
|||
|
|
m_speakerStreamContext->TryCancel();
|
|||
|
|
}
|
|||
|
|
#endif
|
|||
|
|
const bool hadThread = m_speakerStreamThread.joinable();
|
|||
|
|
if (hadThread && m_speakerStreamThread.get_id() != std::this_thread::get_id())
|
|||
|
|
m_speakerStreamThread.join();
|
|||
|
|
m_speakerStreamRunning.store(false);
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(m_speakerQueueMutex);
|
|||
|
|
m_speakerQueue.clear();
|
|||
|
|
m_speakerQueueBytes = 0;
|
|||
|
|
}
|
|||
|
|
if (wasRunning && !hadThread)
|
|||
|
|
emit audioStreamStateChanged(QStringLiteral("SpeakerService"), false,
|
|||
|
|
QStringLiteral("扬声器音频流已停止"));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void stopMicrophoneStreamInternal()
|
|||
|
|
{
|
|||
|
|
const bool wasRunning = m_microphoneStreamRunning.load();
|
|||
|
|
m_microphoneStreamStop.store(true);
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(m_microphoneContextMutex);
|
|||
|
|
if (m_microphoneStreamContext)
|
|||
|
|
m_microphoneStreamContext->TryCancel();
|
|||
|
|
}
|
|||
|
|
#endif
|
|||
|
|
const bool hadThread = m_microphoneStreamThread.joinable();
|
|||
|
|
if (hadThread && m_microphoneStreamThread.get_id() != std::this_thread::get_id())
|
|||
|
|
m_microphoneStreamThread.join();
|
|||
|
|
m_microphoneStreamRunning.store(false);
|
|||
|
|
if (wasRunning && !hadThread)
|
|||
|
|
emit audioStreamStateChanged(QStringLiteral("MicPhoneService"), false,
|
|||
|
|
QStringLiteral("麦克风音频拉流已停止"));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void poll()
|
|||
|
|
{
|
|||
|
|
if (!m_connected)
|
|||
|
|
return;
|
|||
|
|
if (m_demoMode) {
|
|||
|
|
pollDemo();
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
pollGrpc();
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private:
|
|||
|
|
bool ensureConnected(const QString &command)
|
|||
|
|
{
|
|||
|
|
if (m_connected)
|
|||
|
|
return true;
|
|||
|
|
emit commandFinished(command, false, QStringLiteral("设备尚未连接"));
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool ensureDeviceId(const QString &command, const QString &deviceId)
|
|||
|
|
{
|
|||
|
|
if (!deviceId.trimmed().isEmpty())
|
|||
|
|
return true;
|
|||
|
|
emit commandFinished(command, false, QStringLiteral("请在对应服务页填写设备 ID"));
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void pollDemo()
|
|||
|
|
{
|
|||
|
|
++m_tick;
|
|||
|
|
if (m_armPollingEnabled) {
|
|||
|
|
QVariantList joints;
|
|||
|
|
const QStringList jointNames = armJointNamesForDevice(m_armDeviceId);
|
|||
|
|
for (int i = 0; i < jointNames.size(); ++i) {
|
|||
|
|
const double phase = m_tick * 0.035 + i * 0.46;
|
|||
|
|
double position = 0.36 * qSin(phase);
|
|||
|
|
if (i < m_demoTargets.size())
|
|||
|
|
position = m_demoTargets.at(i) + 0.025 * qSin(phase * 1.7);
|
|||
|
|
QVariantMap joint;
|
|||
|
|
joint.insert(QStringLiteral("name"), jointNames.at(i));
|
|||
|
|
joint.insert(QStringLiteral("position"), position);
|
|||
|
|
joint.insert(QStringLiteral("velocity"), 0.22 * qCos(phase));
|
|||
|
|
joint.insert(QStringLiteral("effort"), 0.4 + 0.3 * qAbs(qSin(phase * 0.7)));
|
|||
|
|
joints.append(joint);
|
|||
|
|
}
|
|||
|
|
QVariantMap pose;
|
|||
|
|
pose.insert(QStringLiteral("x"), 0.421 + 0.004 * qSin(m_tick * 0.02));
|
|||
|
|
pose.insert(QStringLiteral("y"), -0.182 + 0.003 * qCos(m_tick * 0.025));
|
|||
|
|
pose.insert(QStringLiteral("z"), 0.736 + 0.002 * qSin(m_tick * 0.017));
|
|||
|
|
pose.insert(QStringLiteral("rx"), 1.571);
|
|||
|
|
pose.insert(QStringLiteral("ry"), 0.014);
|
|||
|
|
pose.insert(QStringLiteral("rz"), -0.021);
|
|||
|
|
emit telemetryReceived(joints, pose);
|
|||
|
|
}
|
|||
|
|
if (m_tick % 10 == 0) {
|
|||
|
|
emit systemStatusReceived(31.0 + 8.0 * qSin(m_tick * 0.04),
|
|||
|
|
47.0 + 3.0 * qCos(m_tick * 0.025), 62.0, 6);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
static void fillHeader(cmvr::api::CommandHeader::Request *header,
|
|||
|
|
const QString &deviceId, int validForMs = 5000)
|
|||
|
|
{
|
|||
|
|
header->set_device_id(deviceId.toStdString());
|
|||
|
|
const qint64 now = QDateTime::currentMSecsSinceEpoch();
|
|||
|
|
header->mutable_timestamp()->set_seconds(now / 1000);
|
|||
|
|
header->mutable_timestamp()->set_nanos(static_cast<int>((now % 1000) * 1000000));
|
|||
|
|
header->set_command_id(
|
|||
|
|
QUuid::createUuid().toString(QUuid::WithoutBraces).toStdString());
|
|||
|
|
header->set_valid_for_ms(static_cast<quint32>(qMax(1, validForMs)));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
static void setDeadline(grpc::ClientContext &context, int milliseconds)
|
|||
|
|
{
|
|||
|
|
context.set_deadline(std::chrono::system_clock::now() + std::chrono::milliseconds(milliseconds));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void runSpeakerAudioStream(const std::shared_ptr<grpc::Channel> &channel,
|
|||
|
|
const QString &deviceId)
|
|||
|
|
{
|
|||
|
|
std::unique_ptr<cmvr::api::SpeakerService::Stub> speaker =
|
|||
|
|
cmvr::api::SpeakerService::NewStub(channel);
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(m_speakerContextMutex);
|
|||
|
|
m_speakerStreamContext = &context;
|
|||
|
|
}
|
|||
|
|
if (m_speakerStreamStop.load())
|
|||
|
|
context.TryCancel();
|
|||
|
|
|
|||
|
|
cmvr::api::StreamSpeakerAudioCommand::Feedback feedback;
|
|||
|
|
std::unique_ptr<grpc::ClientWriter<cmvr::api::StreamSpeakerAudioCommand::Request>> writer =
|
|||
|
|
speaker->StreamAudio(&context, &feedback);
|
|||
|
|
bool writeOk = static_cast<bool>(writer);
|
|||
|
|
while (writeOk) {
|
|||
|
|
SpeakerAudioFrame frame;
|
|||
|
|
{
|
|||
|
|
std::unique_lock<std::mutex> lock(m_speakerQueueMutex);
|
|||
|
|
m_speakerQueueCondition.wait(lock, [this]() {
|
|||
|
|
return m_speakerStreamStop.load() || !m_speakerQueue.empty();
|
|||
|
|
});
|
|||
|
|
if (m_speakerQueue.empty()) {
|
|||
|
|
if (m_speakerStreamStop.load())
|
|||
|
|
break;
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
frame = std::move(m_speakerQueue.front());
|
|||
|
|
m_speakerQueue.pop_front();
|
|||
|
|
m_speakerQueueBytes -= frame.data.size();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
cmvr::api::StreamSpeakerAudioCommand::Request request;
|
|||
|
|
fillHeader(request.mutable_header(), deviceId);
|
|||
|
|
cmvr::api::AudioData *audio = request.mutable_audio();
|
|||
|
|
audio->set_data(frame.data.constData(), frame.data.size());
|
|||
|
|
audio->set_sample_rate(frame.sampleRate);
|
|||
|
|
audio->set_channels(frame.channels);
|
|||
|
|
audio->set_format(cmvr::api::AudioData::PCM);
|
|||
|
|
audio->set_codec("pcm_s16le");
|
|||
|
|
audio->set_pts(frame.pts);
|
|||
|
|
audio->set_nb_samples(frame.samples);
|
|||
|
|
writeOk = writer->Write(request);
|
|||
|
|
if (writeOk) {
|
|||
|
|
const quint64 frames = ++m_speakerFrames;
|
|||
|
|
const quint64 bytes = m_speakerBytes.fetch_add(frame.data.size()) + frame.data.size();
|
|||
|
|
emit audioStreamStatistics(QStringLiteral("SpeakerService"), frames, bytes,
|
|||
|
|
frame.pts / 1000 + frame.samples * 1000LL / frame.sampleRate);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (writer)
|
|||
|
|
writer->WritesDone();
|
|||
|
|
const grpc::Status status = writer ? writer->Finish()
|
|||
|
|
: grpc::Status(grpc::StatusCode::UNKNOWN,
|
|||
|
|
"failed to create client writer");
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(m_speakerContextMutex);
|
|||
|
|
m_speakerStreamContext = nullptr;
|
|||
|
|
}
|
|||
|
|
const bool stopped = m_speakerStreamStop.load();
|
|||
|
|
const bool success = !stopped && status.ok() && feedback.header().success();
|
|||
|
|
QString detail;
|
|||
|
|
if (stopped)
|
|||
|
|
detail = QStringLiteral("扬声器音频流已停止");
|
|||
|
|
else if (!status.ok())
|
|||
|
|
detail = QStringLiteral("扬声器音频流失败:%1")
|
|||
|
|
.arg(QString::fromStdString(status.error_message()));
|
|||
|
|
else if (!feedback.header().success())
|
|||
|
|
detail = QStringLiteral("扬声器拒绝音频流:%1")
|
|||
|
|
.arg(QString::fromStdString(feedback.header().error_message()));
|
|||
|
|
else
|
|||
|
|
detail = QStringLiteral("扬声器音频流发送完成");
|
|||
|
|
m_speakerStreamRunning.store(false);
|
|||
|
|
emit audioStreamStateChanged(QStringLiteral("SpeakerService"), false, detail);
|
|||
|
|
if (!stopped)
|
|||
|
|
emit commandFinished(QStringLiteral("Speaker Stream"), success, detail);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void runMicrophoneAudioStream(const std::shared_ptr<grpc::Channel> &channel,
|
|||
|
|
const QString &deviceId)
|
|||
|
|
{
|
|||
|
|
std::unique_ptr<cmvr::api::MicPhoneService::Stub> microphone =
|
|||
|
|
cmvr::api::MicPhoneService::NewStub(channel);
|
|||
|
|
cmvr::api::StreamMicAudioCommand::Request request;
|
|||
|
|
fillHeader(request.mutable_header(), deviceId);
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(m_microphoneContextMutex);
|
|||
|
|
m_microphoneStreamContext = &context;
|
|||
|
|
}
|
|||
|
|
if (m_microphoneStreamStop.load())
|
|||
|
|
context.TryCancel();
|
|||
|
|
|
|||
|
|
std::unique_ptr<grpc::ClientReader<cmvr::api::StreamMicAudioCommand::Feedback>> reader =
|
|||
|
|
microphone->StreamAudio(&context, request);
|
|||
|
|
cmvr::api::StreamMicAudioCommand::Feedback feedback;
|
|||
|
|
while (reader && !m_microphoneStreamStop.load() && reader->Read(&feedback)) {
|
|||
|
|
if (!feedback.header().success()) {
|
|||
|
|
emit logMessage(QStringLiteral("WARN"),
|
|||
|
|
QStringLiteral("麦克风音频帧反馈失败:%1")
|
|||
|
|
.arg(QString::fromStdString(feedback.header().error_message())));
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
const cmvr::api::AudioData &audio = feedback.audio();
|
|||
|
|
const QByteArray data(audio.data().data(), static_cast<int>(audio.data().size()));
|
|||
|
|
const quint64 frames = ++m_microphoneFrames;
|
|||
|
|
const quint64 bytes = m_microphoneBytes.fetch_add(data.size()) + data.size();
|
|||
|
|
emit audioFrameReceived(data, audio.sample_rate(), audio.channels(),
|
|||
|
|
static_cast<int>(audio.format()),
|
|||
|
|
QString::fromStdString(audio.codec()), audio.pts(),
|
|||
|
|
audio.nb_samples());
|
|||
|
|
const qint64 durationMs = audio.sample_rate() > 0
|
|||
|
|
? audio.pts() / 1000 + audio.nb_samples() * 1000LL / audio.sample_rate()
|
|||
|
|
: 0;
|
|||
|
|
emit audioStreamStatistics(QStringLiteral("MicPhoneService"), frames, bytes,
|
|||
|
|
durationMs);
|
|||
|
|
}
|
|||
|
|
const grpc::Status status = reader ? reader->Finish()
|
|||
|
|
: grpc::Status(grpc::StatusCode::UNKNOWN,
|
|||
|
|
"failed to create client reader");
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(m_microphoneContextMutex);
|
|||
|
|
m_microphoneStreamContext = nullptr;
|
|||
|
|
}
|
|||
|
|
const bool stopped = m_microphoneStreamStop.load();
|
|||
|
|
QString detail;
|
|||
|
|
if (stopped)
|
|||
|
|
detail = QStringLiteral("麦克风音频拉流已停止");
|
|||
|
|
else if (!status.ok())
|
|||
|
|
detail = QStringLiteral("麦克风音频拉流失败:%1")
|
|||
|
|
.arg(QString::fromStdString(status.error_message()));
|
|||
|
|
else
|
|||
|
|
detail = QStringLiteral("麦克风音频拉流已结束");
|
|||
|
|
m_microphoneStreamRunning.store(false);
|
|||
|
|
emit audioStreamStateChanged(QStringLiteral("MicPhoneService"), false, detail);
|
|||
|
|
if (!stopped)
|
|||
|
|
emit commandFinished(QStringLiteral("MicPhone Stream"), status.ok(), detail);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void runDepthCameraStream(const std::shared_ptr<grpc::Channel> &channel,
|
|||
|
|
const std::shared_ptr<CameraStreamSession> &session,
|
|||
|
|
bool rgbd)
|
|||
|
|
{
|
|||
|
|
const QString cameraId = session->cameraId;
|
|||
|
|
auto camera = cmvr::api::CameraService::NewStub(channel);
|
|||
|
|
cmvr::api::StartCameraCommand::Request startRequest;
|
|||
|
|
fillHeader(startRequest.mutable_header(), cameraId);
|
|||
|
|
cmvr::api::StartCameraCommand::Feedback startResponse;
|
|||
|
|
grpc::ClientContext startContext;
|
|||
|
|
setDeadline(startContext, 3000);
|
|||
|
|
const grpc::Status startStatus = camera->StartCamera(&startContext, startRequest, &startResponse);
|
|||
|
|
if (!startStatus.ok() || !startResponse.header().success()) {
|
|||
|
|
const QString error = !startStatus.ok()
|
|||
|
|
? QString::fromStdString(startStatus.error_message())
|
|||
|
|
: QString::fromStdString(startResponse.header().error_message());
|
|||
|
|
session->running.store(false);
|
|||
|
|
emit cameraStreamStateChangedForCamera(cameraId, false,
|
|||
|
|
QStringLiteral("鍚姩鐩告満澶辫触锛?1").arg(error));
|
|||
|
|
emit commandFinished(QStringLiteral("Camera Stream"), false, error);
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(session->contextMutex);
|
|||
|
|
session->context = &context;
|
|||
|
|
}
|
|||
|
|
if (session->stop.load())
|
|||
|
|
context.TryCancel();
|
|||
|
|
|
|||
|
|
QString terminalError;
|
|||
|
|
if (rgbd) {
|
|||
|
|
auto stream = camera->GetRGBDImagesStream(&context);
|
|||
|
|
cmvr::api::GetRGBDImagesStreamCommand::Request request;
|
|||
|
|
fillHeader(request.mutable_header(), cameraId);
|
|||
|
|
request.set_eof(false);
|
|||
|
|
if (!stream || !stream->Write(request)) {
|
|||
|
|
terminalError = QStringLiteral("鍙戦€佽棰戞祦璁㈤槄璇锋眰澶辫触");
|
|||
|
|
} else {
|
|||
|
|
cmvr::api::GetRGBDImagesStreamCommand::Feedback response;
|
|||
|
|
while (!session->stop.load() && stream->Read(&response)) {
|
|||
|
|
if (!response.header().success()) {
|
|||
|
|
terminalError = QString::fromStdString(response.header().error_message());
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
const auto &depth = response.depth_frame();
|
|||
|
|
VideoFrame frame = videoFrameFromDepth(depth);
|
|||
|
|
if (!frame.isValid())
|
|||
|
|
continue;
|
|||
|
|
if (videoFrameReceivedForCamera)
|
|||
|
|
videoFrameReceivedForCamera(cameraId, frame,
|
|||
|
|
QStringLiteral("%1 x %2 | DEPTH | #%3")
|
|||
|
|
.arg(depth.width()).arg(depth.height()).arg(response.seq_no()));
|
|||
|
|
}
|
|||
|
|
stream->WritesDone();
|
|||
|
|
stream->Finish();
|
|||
|
|
}
|
|||
|
|
} else {
|
|||
|
|
auto stream = camera->GetDepthImageStream(&context);
|
|||
|
|
cmvr::api::GetDepthImageStreamCommand::Request request;
|
|||
|
|
fillHeader(request.mutable_header(), cameraId);
|
|||
|
|
request.set_eof(false);
|
|||
|
|
if (!stream || !stream->Write(request)) {
|
|||
|
|
terminalError = QStringLiteral("鍙戦€佽棰戞祦璁㈤槄璇锋眰澶辫触");
|
|||
|
|
} else {
|
|||
|
|
cmvr::api::GetDepthImageStreamCommand::Feedback response;
|
|||
|
|
while (!session->stop.load() && stream->Read(&response)) {
|
|||
|
|
if (!response.header().success()) {
|
|||
|
|
terminalError = QString::fromStdString(response.header().error_message());
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
const auto &depth = response.depth_frame();
|
|||
|
|
VideoFrame frame = videoFrameFromDepth(depth);
|
|||
|
|
if (!frame.isValid())
|
|||
|
|
continue;
|
|||
|
|
if (videoFrameReceivedForCamera)
|
|||
|
|
videoFrameReceivedForCamera(cameraId, frame,
|
|||
|
|
QStringLiteral("%1 x %2 | DEPTH | #%3")
|
|||
|
|
.arg(depth.width()).arg(depth.height()).arg(response.seq_no()));
|
|||
|
|
}
|
|||
|
|
stream->WritesDone();
|
|||
|
|
stream->Finish();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if (session->stop.load())
|
|||
|
|
terminalError = QStringLiteral("瑙嗛棰勮宸插仠姝?");
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(session->contextMutex);
|
|||
|
|
if (session->context == &context)
|
|||
|
|
session->context = nullptr;
|
|||
|
|
}
|
|||
|
|
session->running.store(false);
|
|||
|
|
emit cameraStreamStateChangedForCamera(
|
|||
|
|
cameraId, false,
|
|||
|
|
terminalError.isEmpty() ? QStringLiteral("Depth stream ended") : terminalError);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void runCameraStream(const std::shared_ptr<grpc::Channel> &channel,
|
|||
|
|
const std::shared_ptr<CameraStreamSession> &session)
|
|||
|
|
{
|
|||
|
|
const QString cameraId = session->cameraId;
|
|||
|
|
std::unique_ptr<cmvr::api::CameraService::Stub> camera =
|
|||
|
|
cmvr::api::CameraService::NewStub(channel);
|
|||
|
|
|
|||
|
|
cmvr::api::StartCameraCommand::Request startRequest;
|
|||
|
|
fillHeader(startRequest.mutable_header(), cameraId);
|
|||
|
|
cmvr::api::StartCameraCommand::Feedback startResponse;
|
|||
|
|
grpc::ClientContext startContext;
|
|||
|
|
setDeadline(startContext, 3000);
|
|||
|
|
const grpc::Status startStatus = camera->StartCamera(&startContext, startRequest, &startResponse);
|
|||
|
|
if (!startStatus.ok() || !startResponse.header().success()) {
|
|||
|
|
const QString error = !startStatus.ok()
|
|||
|
|
? QString::fromStdString(startStatus.error_message())
|
|||
|
|
: QString::fromStdString(startResponse.header().error_message());
|
|||
|
|
session->running.store(false);
|
|||
|
|
emit cameraStreamStateChangedForCamera(
|
|||
|
|
cameraId, false, QStringLiteral("启动相机失败:%1").arg(error));
|
|||
|
|
emit commandFinished(QStringLiteral("Camera Stream"), false, error);
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
if (session->stop.load()) {
|
|||
|
|
session->running.store(false);
|
|||
|
|
emit cameraStreamStateChangedForCamera(
|
|||
|
|
cameraId, false, QStringLiteral("视频预览已停止"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(session->contextMutex);
|
|||
|
|
session->context = &context;
|
|||
|
|
}
|
|||
|
|
if (session->stop.load())
|
|||
|
|
context.TryCancel();
|
|||
|
|
std::unique_ptr<grpc::ClientReaderWriter<
|
|||
|
|
cmvr::api::GetRGBImageStreamCommand::Request,
|
|||
|
|
cmvr::api::GetRGBImageStreamCommand::Feedback>> stream =
|
|||
|
|
camera->GetRGBImageStream(&context);
|
|||
|
|
|
|||
|
|
cmvr::api::GetRGBImageStreamCommand::Request request;
|
|||
|
|
fillHeader(request.mutable_header(), cameraId);
|
|||
|
|
request.set_eof(false);
|
|||
|
|
if (!stream || !stream->Write(request)) {
|
|||
|
|
context.TryCancel();
|
|||
|
|
if (stream)
|
|||
|
|
stream->Finish();
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(session->contextMutex);
|
|||
|
|
session->context = nullptr;
|
|||
|
|
}
|
|||
|
|
session->running.store(false);
|
|||
|
|
emit cameraStreamStateChangedForCamera(
|
|||
|
|
cameraId, false, QStringLiteral("发送视频流订阅请求失败"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
if (session->stop.load())
|
|||
|
|
context.TryCancel();
|
|||
|
|
|
|||
|
|
emit logMessage(QStringLiteral("INFO"),
|
|||
|
|
QStringLiteral("CameraService.GetRGBImageStream 已启动:%1,解码格式=%2")
|
|||
|
|
.arg(cameraId, session->codec == QStringLiteral("h265")
|
|||
|
|
? QStringLiteral("H.265")
|
|||
|
|
: QStringLiteral("H.264")));
|
|||
|
|
struct CompressedVideoFrame {
|
|||
|
|
QByteArray data;
|
|||
|
|
QString codec;
|
|||
|
|
qint64 pts = 0;
|
|||
|
|
qint64 dts = 0;
|
|||
|
|
int width = 0;
|
|||
|
|
int height = 0;
|
|||
|
|
int seqNo = 0;
|
|||
|
|
quint32 sourceFps = 0;
|
|||
|
|
quint64 generation = 0;
|
|||
|
|
bool keyFrame = false;
|
|||
|
|
};
|
|||
|
|
// Frame-threaded HEVC needs several packets before its first output.
|
|||
|
|
// Keep enough startup headroom so the low-latency recovery logic does
|
|||
|
|
// not repeatedly reset a decoder that is still warming up.
|
|||
|
|
constexpr std::size_t kMaxCompressedFrames = 45;
|
|||
|
|
std::deque<CompressedVideoFrame> compressedFrames;
|
|||
|
|
std::mutex compressedFramesMutex;
|
|||
|
|
std::condition_variable compressedFramesReady;
|
|||
|
|
bool readerFinished = false;
|
|||
|
|
quint64 queueGeneration = 0;
|
|||
|
|
std::atomic<quint64> decodedFrames{0};
|
|||
|
|
std::atomic<quint64> displayedFrames{0};
|
|||
|
|
std::atomic<quint64> receivedFrames{0};
|
|||
|
|
std::atomic<quint64> receivedBytes{0};
|
|||
|
|
std::atomic<quint64> droppedFrames{0};
|
|||
|
|
std::atomic<quint64> sourceSequence{0};
|
|||
|
|
std::atomic<quint64> sourceSequenceGaps{0};
|
|||
|
|
std::atomic<std::size_t> compressedQueueDepth{0};
|
|||
|
|
QElapsedTimer totalTimer;
|
|||
|
|
totalTimer.start();
|
|||
|
|
QString terminalError;
|
|||
|
|
|
|||
|
|
std::thread decoderThread([&] {
|
|||
|
|
VideoDecoder decoder;
|
|||
|
|
QElapsedTimer rateTimer;
|
|||
|
|
rateTimer.start();
|
|||
|
|
quint64 rateFrames = 0;
|
|||
|
|
quint64 previousReceivedFrames = 0;
|
|||
|
|
quint64 previousDecodedFrames = 0;
|
|||
|
|
quint64 previousRateBytes = 0;
|
|||
|
|
quint64 previousSourceSequence = 0;
|
|||
|
|
qint64 decodeNanoseconds = 0;
|
|||
|
|
quint64 timedDecodePackets = 0;
|
|||
|
|
double receiveFps = 0.0;
|
|||
|
|
double sourceSequenceFps = 0.0;
|
|||
|
|
double displayFps = 0.0;
|
|||
|
|
double decodeFps = 0.0;
|
|||
|
|
double decodeMilliseconds = 0.0;
|
|||
|
|
double bitrateKbps = 0.0;
|
|||
|
|
int decodeErrors = 0;
|
|||
|
|
QString activeCodec;
|
|||
|
|
quint64 activeGeneration = std::numeric_limits<quint64>::max();
|
|||
|
|
while (!session->stop.load()) {
|
|||
|
|
CompressedVideoFrame packet;
|
|||
|
|
{
|
|||
|
|
std::unique_lock<std::mutex> lock(compressedFramesMutex);
|
|||
|
|
compressedFramesReady.wait(lock, [&] {
|
|||
|
|
return session->stop.load() || readerFinished ||
|
|||
|
|
!compressedFrames.empty();
|
|||
|
|
});
|
|||
|
|
if ((session->stop.load() || readerFinished) &&
|
|||
|
|
compressedFrames.empty())
|
|||
|
|
break;
|
|||
|
|
packet = std::move(compressedFrames.front());
|
|||
|
|
compressedFrames.pop_front();
|
|||
|
|
compressedQueueDepth.store(compressedFrames.size());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const QString codec = packet.codec.isEmpty()
|
|||
|
|
? session->codec : packet.codec;
|
|||
|
|
if (codec != activeCodec ||
|
|||
|
|
packet.generation != activeGeneration) {
|
|||
|
|
decoder.reset();
|
|||
|
|
activeCodec = codec;
|
|||
|
|
activeGeneration = packet.generation;
|
|||
|
|
decodeErrors = 0;
|
|||
|
|
emit logMessage(QStringLiteral("INFO"),
|
|||
|
|
QStringLiteral("视频解码器已切换为 %1,等待参数集和关键帧")
|
|||
|
|
.arg(codec.toUpper()));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QVector<VideoFrame> frames;
|
|||
|
|
int decodedCount = 0;
|
|||
|
|
QElapsedTimer decodeTimer;
|
|||
|
|
decodeTimer.start();
|
|||
|
|
if (!decoder.decode(packet.data, codec, packet.pts, packet.dts,
|
|||
|
|
&frames, true, &decodedCount)) {
|
|||
|
|
++decodeErrors;
|
|||
|
|
if (decodeErrors == 1 || decodeErrors % 60 == 0) {
|
|||
|
|
emit logMessage(QStringLiteral("WARN"),
|
|||
|
|
QStringLiteral("视频解码失败(%1 次):%2")
|
|||
|
|
.arg(decodeErrors).arg(decoder.lastError()));
|
|||
|
|
}
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
decodeNanoseconds += decodeTimer.nsecsElapsed();
|
|||
|
|
++timedDecodePackets;
|
|||
|
|
decodedFrames.fetch_add(static_cast<quint64>(decodedCount));
|
|||
|
|
|
|||
|
|
if (!frames.isEmpty()) {
|
|||
|
|
if (displayedFrames.load() == 0) {
|
|||
|
|
emit logMessage(QStringLiteral("INFO"),
|
|||
|
|
QStringLiteral("视频预览解码后端:%1")
|
|||
|
|
.arg(decoder.backendName()));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
for (const VideoFrame &frame : frames) {
|
|||
|
|
displayedFrames.fetch_add(1);
|
|||
|
|
++rateFrames;
|
|||
|
|
if (rateTimer.elapsed() >= 1000) {
|
|||
|
|
const double seconds = rateTimer.elapsed() / 1000.0;
|
|||
|
|
const quint64 currentBytes = receivedBytes.load();
|
|||
|
|
const quint64 currentReceivedFrames = receivedFrames.load();
|
|||
|
|
const quint64 currentSourceSequence = sourceSequence.load();
|
|||
|
|
receiveFps = (currentReceivedFrames - previousReceivedFrames) / seconds;
|
|||
|
|
sourceSequenceFps = previousSourceSequence > 0 &&
|
|||
|
|
currentSourceSequence >= previousSourceSequence
|
|||
|
|
? (currentSourceSequence - previousSourceSequence) / seconds : 0.0;
|
|||
|
|
displayFps = rateFrames / seconds;
|
|||
|
|
const quint64 currentDecodedFrames = decodedFrames.load();
|
|||
|
|
decodeFps = (currentDecodedFrames - previousDecodedFrames) / seconds;
|
|||
|
|
bitrateKbps = (currentBytes - previousRateBytes) * 8.0 /
|
|||
|
|
seconds / 1000.0;
|
|||
|
|
decodeMilliseconds = timedDecodePackets > 0
|
|||
|
|
? decodeNanoseconds / 1000000.0 / timedDecodePackets : 0.0;
|
|||
|
|
rateFrames = 0;
|
|||
|
|
previousReceivedFrames = currentReceivedFrames;
|
|||
|
|
previousSourceSequence = currentSourceSequence;
|
|||
|
|
previousDecodedFrames = currentDecodedFrames;
|
|||
|
|
previousRateBytes = currentBytes;
|
|||
|
|
decodeNanoseconds = 0;
|
|||
|
|
timedDecodePackets = 0;
|
|||
|
|
rateTimer.restart();
|
|||
|
|
}
|
|||
|
|
const QString sourceRate = packet.sourceFps > 0
|
|||
|
|
? QStringLiteral("/%1").arg(packet.sourceFps) : QString();
|
|||
|
|
const QString dropDetail = droppedFrames.load() > 0
|
|||
|
|
? QStringLiteral(" | 低延迟丢帧 %1").arg(droppedFrames.load())
|
|||
|
|
: QString();
|
|||
|
|
const QString detail = QStringLiteral("%1 x %2 | %3 | FPS 源/收/解/显 %4/%5/%6/%7%8 | %9 %10 ms | Q%11 缺%12 | %13 kbps | #%14%15")
|
|||
|
|
.arg(packet.width).arg(packet.height).arg(codec.toUpper())
|
|||
|
|
.arg(sourceSequenceFps, 0, 'f', 1).arg(receiveFps, 0, 'f', 1)
|
|||
|
|
.arg(decodeFps, 0, 'f', 1).arg(displayFps, 0, 'f', 1)
|
|||
|
|
.arg(sourceRate).arg(decoder.backendName())
|
|||
|
|
.arg(decodeMilliseconds, 0, 'f', 1).arg(compressedQueueDepth.load())
|
|||
|
|
.arg(sourceSequenceGaps.load()).arg(bitrateKbps, 0, 'f', 0)
|
|||
|
|
.arg(packet.seqNo).arg(dropDetail);
|
|||
|
|
if (videoFrameReceivedForCamera)
|
|||
|
|
videoFrameReceivedForCamera(cameraId, frame, detail);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
cmvr::api::GetRGBImageStreamCommand::Feedback response;
|
|||
|
|
while (!session->stop.load() && stream->Read(&response)) {
|
|||
|
|
if (!response.header().success()) {
|
|||
|
|
terminalError = QString::fromStdString(response.header().error_message());
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
const auto &frame = response.color_frame();
|
|||
|
|
if (frame.data().empty())
|
|||
|
|
continue;
|
|||
|
|
|
|||
|
|
receivedBytes.fetch_add(static_cast<quint64>(frame.data().size()));
|
|||
|
|
receivedFrames.fetch_add(1);
|
|||
|
|
const quint64 currentSourceSequence = frame.source_sequence();
|
|||
|
|
const quint64 previousSequence = sourceSequence.exchange(currentSourceSequence);
|
|||
|
|
if (previousSequence > 0 && currentSourceSequence > previousSequence + 1)
|
|||
|
|
sourceSequenceGaps.fetch_add(currentSourceSequence - previousSequence - 1);
|
|||
|
|
CompressedVideoFrame packet;
|
|||
|
|
packet.data = QByteArray(frame.data().data(), static_cast<int>(frame.data().size()));
|
|||
|
|
packet.codec = QString::fromStdString(frame.codec()).trimmed().toLower();
|
|||
|
|
packet.pts = frame.pts();
|
|||
|
|
packet.dts = frame.dts();
|
|||
|
|
packet.width = frame.width();
|
|||
|
|
packet.height = frame.height();
|
|||
|
|
packet.seqNo = response.seq_no();
|
|||
|
|
packet.sourceFps = frame.source_fps();
|
|||
|
|
packet.keyFrame = frame.is_key_frame();
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(compressedFramesMutex);
|
|||
|
|
// Resynchronize only at a keyframe already in hand. Clearing a
|
|||
|
|
// full queue at an arbitrary P-frame makes the decoder wait for
|
|||
|
|
// a future keyframe and can cause a reset loop during startup.
|
|||
|
|
if (compressedFrames.size() >= kMaxCompressedFrames) {
|
|||
|
|
if (packet.keyFrame) {
|
|||
|
|
droppedFrames.fetch_add(
|
|||
|
|
static_cast<quint64>(compressedFrames.size()));
|
|||
|
|
compressedFrames.clear();
|
|||
|
|
compressedQueueDepth.store(0);
|
|||
|
|
++queueGeneration;
|
|||
|
|
} else {
|
|||
|
|
droppedFrames.fetch_add(1);
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
packet.generation = queueGeneration;
|
|||
|
|
compressedFrames.push_back(std::move(packet));
|
|||
|
|
compressedQueueDepth.store(compressedFrames.size());
|
|||
|
|
}
|
|||
|
|
compressedFramesReady.notify_one();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(compressedFramesMutex);
|
|||
|
|
readerFinished = true;
|
|||
|
|
}
|
|||
|
|
compressedFramesReady.notify_all();
|
|||
|
|
|
|||
|
|
stream->WritesDone();
|
|||
|
|
const grpc::Status status = stream->Finish();
|
|||
|
|
if (decoderThread.joinable())
|
|||
|
|
decoderThread.join();
|
|||
|
|
{
|
|||
|
|
std::lock_guard<std::mutex> lock(session->contextMutex);
|
|||
|
|
if (session->context == &context)
|
|||
|
|
session->context = nullptr;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const bool stoppedByUser = session->stop.load();
|
|||
|
|
if (terminalError.isEmpty() && !status.ok() && !stoppedByUser)
|
|||
|
|
terminalError = QString::fromStdString(status.error_message());
|
|||
|
|
session->running.store(false);
|
|||
|
|
const QString summary = terminalError.isEmpty()
|
|||
|
|
? QStringLiteral("视频预览已停止,解码 %1 帧,显示 %2 帧,%3 MB,%4 秒")
|
|||
|
|
.arg(decodedFrames.load()).arg(displayedFrames.load())
|
|||
|
|
.arg(receivedBytes.load() / 1048576.0, 0, 'f', 1)
|
|||
|
|
.arg(totalTimer.elapsed() / 1000.0, 0, 'f', 1)
|
|||
|
|
: QStringLiteral("视频流失败:%1").arg(terminalError);
|
|||
|
|
emit cameraStreamStateChangedForCamera(cameraId, false, summary);
|
|||
|
|
emit logMessage(terminalError.isEmpty() ? QStringLiteral("INFO") : QStringLiteral("ERROR"), summary);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void finish(const QString &command, const grpc::Status &status, bool responseSuccess,
|
|||
|
|
const QString &responseError)
|
|||
|
|
{
|
|||
|
|
const bool success = status.ok() && responseSuccess;
|
|||
|
|
QString detail;
|
|||
|
|
if (!status.ok()) {
|
|||
|
|
const QString message = QString::fromStdString(status.error_message());
|
|||
|
|
detail = QStringLiteral("gRPC code %1:%2")
|
|||
|
|
.arg(static_cast<int>(status.error_code()))
|
|||
|
|
.arg(message.isEmpty() ? QStringLiteral("服务端未返回错误详情")
|
|||
|
|
: message);
|
|||
|
|
}
|
|||
|
|
else if (!responseSuccess)
|
|||
|
|
detail = responseError.isEmpty() ? QStringLiteral("设备拒绝了命令") : responseError;
|
|||
|
|
else
|
|||
|
|
detail = QStringLiteral("命令执行成功");
|
|||
|
|
emit commandFinished(command, success, detail);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void pollGrpc()
|
|||
|
|
{
|
|||
|
|
++m_tick;
|
|||
|
|
if (m_armPollingEnabled && !m_armDeviceId.isEmpty()) {
|
|||
|
|
cmvr::api::JointRequest request;
|
|||
|
|
fillHeader(request.mutable_header(), m_armDeviceId);
|
|||
|
|
cmvr::api::JointResponse response;
|
|||
|
|
grpc::ClientContext context;
|
|||
|
|
setDeadline(context, 350);
|
|||
|
|
const grpc::Status status = m_arm->getJointState(&context, request, &response);
|
|||
|
|
if (status.ok() && response.header().success()) {
|
|||
|
|
m_consecutivePollErrors = 0;
|
|||
|
|
QVariantList joints;
|
|||
|
|
const auto &state = response.state();
|
|||
|
|
for (int i = 0; i < state.name_size(); ++i) {
|
|||
|
|
QVariantMap joint;
|
|||
|
|
joint.insert(QStringLiteral("name"), QString::fromStdString(state.name(i)));
|
|||
|
|
joint.insert(QStringLiteral("position"), i < state.position_size() ? state.position(i) : 0.0);
|
|||
|
|
joint.insert(QStringLiteral("velocity"), i < state.velocity_size() ? state.velocity(i) : 0.0);
|
|||
|
|
joint.insert(QStringLiteral("effort"), i < state.effort_size() ? state.effort(i) : 0.0);
|
|||
|
|
joints.append(joint);
|
|||
|
|
}
|
|||
|
|
emit telemetryReceived(joints, QVariantMap());
|
|||
|
|
} else {
|
|||
|
|
++m_consecutivePollErrors;
|
|||
|
|
if (m_consecutivePollErrors == 10)
|
|||
|
|
emit logMessage(QStringLiteral("WARN"), QStringLiteral("ArmService 连续 10 次遥测请求失败"));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (m_tick % 5 == 0) {
|
|||
|
|
cmvr::api::GetPose::Request poseRequest;
|
|||
|
|
fillHeader(poseRequest.mutable_header(), m_armDeviceId);
|
|||
|
|
poseRequest.set_base_link("PELVIS_S");
|
|||
|
|
poseRequest.set_ee_link(
|
|||
|
|
m_armDeviceId.contains(QStringLiteral("left"), Qt::CaseInsensitive)
|
|||
|
|
? "L_FINGER_TIP" : "R_FINGER_TIP");
|
|||
|
|
cmvr::api::GetPose::Response poseResponse;
|
|||
|
|
grpc::ClientContext poseContext;
|
|||
|
|
setDeadline(poseContext, 400);
|
|||
|
|
const grpc::Status poseStatus = m_arm->getPose(&poseContext, poseRequest, &poseResponse);
|
|||
|
|
if (poseStatus.ok() && poseResponse.header().success()) {
|
|||
|
|
const auto &source = poseResponse.pose();
|
|||
|
|
QVariantMap pose;
|
|||
|
|
pose.insert(QStringLiteral("x"), source.x());
|
|||
|
|
pose.insert(QStringLiteral("y"), source.y());
|
|||
|
|
pose.insert(QStringLiteral("z"), source.z());
|
|||
|
|
pose.insert(QStringLiteral("rx"), source.rx());
|
|||
|
|
pose.insert(QStringLiteral("ry"), source.ry());
|
|||
|
|
pose.insert(QStringLiteral("rz"), source.rz());
|
|||
|
|
emit telemetryReceived(QVariantList(), pose);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (m_tick % 10 != 0)
|
|||
|
|
return;
|
|||
|
|
cmvr::api::GetSystemStatusCommand::Request statusRequest;
|
|||
|
|
cmvr::api::GetSystemStatusCommand::Feedback statusResponse;
|
|||
|
|
grpc::ClientContext statusContext;
|
|||
|
|
setDeadline(statusContext, 500);
|
|||
|
|
const grpc::Status rpcStatus = m_system->GetSystemStatus(&statusContext, statusRequest, &statusResponse);
|
|||
|
|
if (rpcStatus.ok() && statusResponse.header().success()) {
|
|||
|
|
const double memory = statusResponse.mem_total_mb() > 0
|
|||
|
|
? statusResponse.mem_used_mb() * 100.0 / statusResponse.mem_total_mb() : 0.0;
|
|||
|
|
const double disk = statusResponse.disk_total_gb() > 0
|
|||
|
|
? statusResponse.disk_used_gb() * 100.0 / statusResponse.disk_total_gb() : 0.0;
|
|||
|
|
emit systemStatusReceived(statusResponse.cpu_usage(), memory, disk,
|
|||
|
|
statusResponse.device_list_size());
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
std::shared_ptr<grpc::Channel> m_channel;
|
|||
|
|
std::shared_ptr<grpc::Channel> m_configChannel;
|
|||
|
|
std::unique_ptr<cmvr::api::ArmService::Stub> m_arm;
|
|||
|
|
std::unique_ptr<cmvr::api::CameraService::Stub> m_camera;
|
|||
|
|
std::unique_ptr<cmvr::api::SystemService::Stub> m_system;
|
|||
|
|
std::unique_ptr<cmvr::api::AgvService::Stub> m_agv;
|
|||
|
|
std::unique_ptr<cmvr::api::MicPhoneService::Stub> m_microphone;
|
|||
|
|
std::unique_ptr<grpc::GenericStub> m_generic;
|
|||
|
|
std::unique_ptr<cmvr::api::ConfigurationService::Stub> m_configuration;
|
|||
|
|
grpc::ClientContext *m_speakerStreamContext = nullptr;
|
|||
|
|
std::mutex m_speakerContextMutex;
|
|||
|
|
grpc::ClientContext *m_microphoneStreamContext = nullptr;
|
|||
|
|
std::mutex m_microphoneContextMutex;
|
|||
|
|
grpc::ClientContext *m_agvMapStreamContext = nullptr;
|
|||
|
|
std::mutex m_agvMapStreamContextMutex;
|
|||
|
|
#endif
|
|||
|
|
|
|||
|
|
struct SpeakerAudioFrame {
|
|||
|
|
QByteArray data;
|
|||
|
|
int sampleRate = 0;
|
|||
|
|
int channels = 0;
|
|||
|
|
qint64 pts = 0;
|
|||
|
|
int samples = 0;
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
QTimer *m_pollTimer = nullptr;
|
|||
|
|
QString m_host;
|
|||
|
|
QString m_armDeviceId;
|
|||
|
|
int m_port = 50052;
|
|||
|
|
int m_tick = 0;
|
|||
|
|
int m_consecutivePollErrors = 0;
|
|||
|
|
bool m_connected = false;
|
|||
|
|
bool m_configConnected = false;
|
|||
|
|
bool m_demoMode = true;
|
|||
|
|
bool m_armPollingEnabled = false;
|
|||
|
|
std::shared_ptr<ArmMotionSession> m_armMotionSession;
|
|||
|
|
std::mutex m_armMotionSessionMutex;
|
|||
|
|
QHash<QString, std::shared_ptr<CameraStreamSession>> m_cameraSessions;
|
|||
|
|
std::mutex m_cameraSessionsMutex;
|
|||
|
|
QString m_defaultCameraCodec = QStringLiteral("h264");
|
|||
|
|
bool m_cameraRecording = false;
|
|||
|
|
std::atomic<bool> m_speakerStreamRunning{false};
|
|||
|
|
std::atomic<bool> m_speakerStreamStop{false};
|
|||
|
|
std::atomic<quint64> m_speakerFrames{0};
|
|||
|
|
std::atomic<quint64> m_speakerBytes{0};
|
|||
|
|
std::thread m_speakerStreamThread;
|
|||
|
|
std::deque<SpeakerAudioFrame> m_speakerQueue;
|
|||
|
|
int m_speakerQueueBytes = 0;
|
|||
|
|
std::mutex m_speakerQueueMutex;
|
|||
|
|
std::condition_variable m_speakerQueueCondition;
|
|||
|
|
std::atomic<bool> m_microphoneStreamRunning{false};
|
|||
|
|
std::atomic<bool> m_microphoneStreamStop{false};
|
|||
|
|
std::atomic<quint64> m_microphoneFrames{0};
|
|||
|
|
std::atomic<quint64> m_microphoneBytes{0};
|
|||
|
|
std::thread m_microphoneStreamThread;
|
|||
|
|
std::atomic<bool> m_agvMapStreamRunning{false};
|
|||
|
|
std::atomic<bool> m_agvMapStreamStop{false};
|
|||
|
|
std::thread m_agvMapStreamThread;
|
|||
|
|
QVector<double> m_demoTargets;
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
RobotClient::RobotClient(QObject *parent)
|
|||
|
|
: QObject(parent), m_worker(new BackendWorker)
|
|||
|
|
{
|
|||
|
|
qRegisterMetaType<QImage>("QImage");
|
|||
|
|
qRegisterMetaType<VideoFrame>("VideoFrame");
|
|||
|
|
|
|||
|
|
m_worker->connectionChanged = [this](bool connected, int latencyMs, const QString &detail) {
|
|||
|
|
QMetaObject::invokeMethod(this, [this, connected, latencyMs, detail]() {
|
|||
|
|
emit connectionChanged(connected, latencyMs, detail);
|
|||
|
|
}, Qt::QueuedConnection);
|
|||
|
|
};
|
|||
|
|
m_worker->telemetryReceived = [this](const QVariantList &joints, const QVariantMap &pose) {
|
|||
|
|
QMetaObject::invokeMethod(this, [this, joints, pose]() {
|
|||
|
|
emit telemetryReceived(joints, pose);
|
|||
|
|
}, Qt::QueuedConnection);
|
|||
|
|
};
|
|||
|
|
m_worker->systemStatusReceived = [this](double cpu, double memory, double disk, int devices) {
|
|||
|
|
QMetaObject::invokeMethod(this, [this, cpu, memory, disk, devices]() {
|
|||
|
|
emit systemStatusReceived(cpu, memory, disk, devices);
|
|||
|
|
}, Qt::QueuedConnection);
|
|||
|
|
};
|
|||
|
|
m_worker->systemInfoReceived = [this](const QVariantMap &info) {
|
|||
|
|
QMetaObject::invokeMethod(this, [this, info]() {
|
|||
|
|
emit systemInfoReceived(info);
|
|||
|
|
}, Qt::QueuedConnection);
|
|||
|
|
};
|
|||
|
|
m_worker->imageReceived = [this](const QImage &image, const QString &detail) {
|
|||
|
|
QMetaObject::invokeMethod(this, [this, image, detail]() {
|
|||
|
|
emit imageReceived(image, detail);
|
|||
|
|
}, Qt::QueuedConnection);
|
|||
|
|
};
|
|||
|
|
const auto queueVideoFrame = [this](const QString &cameraId,
|
|||
|
|
const VideoFrame &frame,
|
|||
|
|
const QString &detail) {
|
|||
|
|
{
|
|||
|
|
QMutexLocker locker(&m_videoFrameMutex);
|
|||
|
|
PendingVideoFrame &pending = m_pendingVideoFrames[cameraId];
|
|||
|
|
pending.frame = frame;
|
|||
|
|
pending.detail = detail;
|
|||
|
|
if (m_videoFrameDeliveryScheduled.contains(cameraId))
|
|||
|
|
return;
|
|||
|
|
m_videoFrameDeliveryScheduled.insert(cameraId);
|
|||
|
|
}
|
|||
|
|
QMetaObject::invokeMethod(this, [this, cameraId]() {
|
|||
|
|
PendingVideoFrame pending;
|
|||
|
|
{
|
|||
|
|
QMutexLocker locker(&m_videoFrameMutex);
|
|||
|
|
pending = m_pendingVideoFrames.take(cameraId);
|
|||
|
|
m_videoFrameDeliveryScheduled.remove(cameraId);
|
|||
|
|
}
|
|||
|
|
emit videoFrameReceived(cameraId, pending.frame, pending.detail);
|
|||
|
|
}, Qt::QueuedConnection);
|
|||
|
|
};
|
|||
|
|
m_worker->videoFrameReceivedForCamera = queueVideoFrame;
|
|||
|
|
const auto queueCameraState = [this](const QString &cameraId, bool running,
|
|||
|
|
const QString &detail) {
|
|||
|
|
QMetaObject::invokeMethod(this, [this, cameraId, running, detail]() {
|
|||
|
|
emit cameraStreamStateChanged(cameraId, running, detail);
|
|||
|
|
}, Qt::QueuedConnection);
|
|||
|
|
};
|
|||
|
|
m_worker->cameraStreamStateChangedForCamera = queueCameraState;
|
|||
|
|
m_worker->cameraRecordingStateChanged = [this](bool recording, const QString &detail) {
|
|||
|
|
QMetaObject::invokeMethod(this, [this, recording, detail]() {
|
|||
|
|
emit cameraRecordingStateChanged(recording, detail);
|
|||
|
|
}, Qt::QueuedConnection);
|
|||
|
|
};
|
|||
|
|
m_worker->agvStateReceived = [this](const QVariantMap &state, const QVariantMap &navigation) {
|
|||
|
|
QMetaObject::invokeMethod(this, [this, state, navigation]() {
|
|||
|
|
emit agvStateReceived(state, navigation);
|
|||
|
|
}, Qt::QueuedConnection);
|
|||
|
|
};
|
|||
|
|
m_worker->agvInventoryReceived = [this](const QStringList &maps, const QVariantList &stations) {
|
|||
|
|
QMetaObject::invokeMethod(this, [this, maps, stations]() {
|
|||
|
|
emit agvInventoryReceived(maps, stations);
|
|||
|
|
}, Qt::QueuedConnection);
|
|||
|
|
};
|
|||
|
|
m_worker->agvMapFrameReceived = [this](const QImage &image, const QString &detail) {
|
|||
|
|
QMetaObject::invokeMethod(this, [this, image, detail]() {
|
|||
|
|
emit agvMapFrameReceived(image, detail);
|
|||
|
|
}, Qt::QueuedConnection);
|
|||
|
|
};
|
|||
|
|
m_worker->agvMapStreamStateChanged = [this](bool running, const QString &detail) {
|
|||
|
|
QMetaObject::invokeMethod(this, [this, running, detail]() {
|
|||
|
|
emit agvMapStreamStateChanged(running, detail);
|
|||
|
|
}, Qt::QueuedConnection);
|
|||
|
|
};
|
|||
|
|
m_worker->rpcFinished = [this](const QString &serviceName, const QString &methodName,
|
|||
|
|
bool success, const QString &responseJson) {
|
|||
|
|
QMetaObject::invokeMethod(this, [this, serviceName, methodName, success, responseJson]() {
|
|||
|
|
emit rpcFinished(serviceName, methodName, success, responseJson);
|
|||
|
|
}, Qt::QueuedConnection);
|
|||
|
|
};
|
|||
|
|
m_worker->audioStreamStateChanged = [this](const QString &serviceName, bool running,
|
|||
|
|
const QString &detail) {
|
|||
|
|
QMetaObject::invokeMethod(this, [this, serviceName, running, detail]() {
|
|||
|
|
emit audioStreamStateChanged(serviceName, running, detail);
|
|||
|
|
}, Qt::QueuedConnection);
|
|||
|
|
};
|
|||
|
|
m_worker->audioStreamStatistics = [this](const QString &serviceName, quint64 frames,
|
|||
|
|
quint64 bytes, qint64 durationMs) {
|
|||
|
|
QMetaObject::invokeMethod(this, [this, serviceName, frames, bytes, durationMs]() {
|
|||
|
|
emit audioStreamStatistics(serviceName, frames, bytes, durationMs);
|
|||
|
|
}, Qt::QueuedConnection);
|
|||
|
|
};
|
|||
|
|
m_worker->audioFrameReceived = [this](const QByteArray &data, int sampleRate, int channels,
|
|||
|
|
int format, const QString &codec,
|
|||
|
|
qint64 pts, int samples) {
|
|||
|
|
QMetaObject::invokeMethod(this,
|
|||
|
|
[this, data, sampleRate, channels, format, codec, pts, samples]() {
|
|||
|
|
emit audioFrameReceived(data, sampleRate, channels, format, codec, pts, samples);
|
|||
|
|
}, Qt::QueuedConnection);
|
|||
|
|
};
|
|||
|
|
m_worker->configurationConnectionChanged = [this](bool connected, const QString &detail) {
|
|||
|
|
QMetaObject::invokeMethod(this, [this, connected, detail]() {
|
|||
|
|
emit configurationConnectionChanged(connected, detail);
|
|||
|
|
}, Qt::QueuedConnection);
|
|||
|
|
};
|
|||
|
|
m_worker->configurationFilesReceived =
|
|||
|
|
[this](const QString &root, const QVariantList &files, bool success,
|
|||
|
|
const QString &detail) {
|
|||
|
|
QMetaObject::invokeMethod(this, [this, root, files, success, detail]() {
|
|||
|
|
emit configurationFilesReceived(root, files, success, detail);
|
|||
|
|
}, Qt::QueuedConnection);
|
|||
|
|
};
|
|||
|
|
m_worker->configurationFileReceived =
|
|||
|
|
[this](const QString &path, const QString &content, const QString &revision,
|
|||
|
|
const QString &messageType, bool success, const QString &detail) {
|
|||
|
|
QMetaObject::invokeMethod(
|
|||
|
|
this, [this, path, content, revision, messageType, success, detail]() {
|
|||
|
|
emit configurationFileReceived(path, content, revision, messageType,
|
|||
|
|
success, detail);
|
|||
|
|
}, Qt::QueuedConnection);
|
|||
|
|
};
|
|||
|
|
m_worker->configurationValidationFinished =
|
|||
|
|
[this](const QString &path, bool success, const QString &detail) {
|
|||
|
|
QMetaObject::invokeMethod(this, [this, path, success, detail]() {
|
|||
|
|
emit configurationValidationFinished(path, success, detail);
|
|||
|
|
}, Qt::QueuedConnection);
|
|||
|
|
};
|
|||
|
|
m_worker->configurationSaveFinished =
|
|||
|
|
[this](const QString &path, const QString &revision, bool success,
|
|||
|
|
const QString &detail) {
|
|||
|
|
QMetaObject::invokeMethod(this, [this, path, revision, success, detail]() {
|
|||
|
|
emit configurationSaveFinished(path, revision, success, detail);
|
|||
|
|
}, Qt::QueuedConnection);
|
|||
|
|
};
|
|||
|
|
m_worker->commandFinished = [this](const QString &command, bool success, const QString &detail) {
|
|||
|
|
QMetaObject::invokeMethod(this, [this, command, success, detail]() {
|
|||
|
|
emit commandFinished(command, success, detail);
|
|||
|
|
}, Qt::QueuedConnection);
|
|||
|
|
};
|
|||
|
|
m_worker->logMessage = [this](const QString &level, const QString &message) {
|
|||
|
|
QMetaObject::invokeMethod(this, [this, level, message]() {
|
|||
|
|
emit logMessage(level, message);
|
|||
|
|
}, Qt::QueuedConnection);
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
m_worker->moveToThread(&m_thread);
|
|||
|
|
|
|||
|
|
connect(this, &RobotClient::connectRequested, m_worker, &BackendWorker::connectToServer);
|
|||
|
|
connect(this, &RobotClient::disconnectRequested, m_worker, &BackendWorker::disconnectFromServer);
|
|||
|
|
connect(this, &RobotClient::armTelemetryRequested, m_worker, &BackendWorker::setArmTelemetry);
|
|||
|
|
connect(this, &RobotClient::moveJRequested, m_worker, &BackendWorker::moveJ);
|
|||
|
|
connect(this, &RobotClient::moveJPoseRequested, m_worker, &BackendWorker::moveJPose);
|
|||
|
|
connect(this, &RobotClient::torqueRequested, m_worker, &BackendWorker::torque);
|
|||
|
|
connect(this, &RobotClient::armStopRequested, m_worker, &BackendWorker::armStop);
|
|||
|
|
connect(this, &RobotClient::armClearFaultRequested, m_worker, &BackendWorker::armClearFault);
|
|||
|
|
connect(this, &RobotClient::armCalibrateZeroRequested,
|
|||
|
|
m_worker, &BackendWorker::armCalibrateZero);
|
|||
|
|
connect(this, &RobotClient::snapshotRequested, m_worker, &BackendWorker::snapshot);
|
|||
|
|
connect(this, &RobotClient::cameraStreamStartRequested, m_worker, &BackendWorker::startCameraStream);
|
|||
|
|
connect(this, &RobotClient::cameraStreamCodecRequested,
|
|||
|
|
m_worker, &BackendWorker::setCameraStreamCodec);
|
|||
|
|
connect(this, &RobotClient::cameraStreamStopRequested, m_worker, &BackendWorker::stopCameraStream);
|
|||
|
|
connect(this, &RobotClient::cameraRecordingStartRequested,
|
|||
|
|
m_worker, &BackendWorker::startCameraRecording);
|
|||
|
|
connect(this, &RobotClient::cameraRecordingStopRequested,
|
|||
|
|
m_worker, &BackendWorker::stopCameraRecording);
|
|||
|
|
connect(this, &RobotClient::systemRefreshRequested, m_worker, &BackendWorker::systemRefresh);
|
|||
|
|
connect(this, &RobotClient::systemStopAllRequested, m_worker, &BackendWorker::systemStopAll);
|
|||
|
|
connect(this, &RobotClient::agvRefreshRequested, m_worker, &BackendWorker::agvRefresh);
|
|||
|
|
connect(this, &RobotClient::agvNavigatePoseRequested, m_worker, &BackendWorker::agvNavigatePose);
|
|||
|
|
connect(this, &RobotClient::agvNavigateStationRequested, m_worker, &BackendWorker::agvNavigateStation);
|
|||
|
|
connect(this, &RobotClient::agvFollowPathRequested, m_worker, &BackendWorker::agvFollowPath);
|
|||
|
|
connect(this, &RobotClient::agvVelocityRequested, m_worker, &BackendWorker::agvVelocity);
|
|||
|
|
connect(this, &RobotClient::agvActionRequested, m_worker, &BackendWorker::agvAction);
|
|||
|
|
connect(this, &RobotClient::agvInventoryRequested, m_worker, &BackendWorker::agvInventory);
|
|||
|
|
connect(this, &RobotClient::agvSwitchMapRequested, m_worker, &BackendWorker::agvSwitchMap);
|
|||
|
|
connect(this, &RobotClient::agvMapStreamStartRequested,
|
|||
|
|
m_worker, &BackendWorker::startAgvMapStream);
|
|||
|
|
connect(this, &RobotClient::agvMapStreamStopRequested,
|
|||
|
|
m_worker, &BackendWorker::stopAgvMapStream);
|
|||
|
|
connect(this, &RobotClient::rpcRequested, m_worker, &BackendWorker::invokeRpc);
|
|||
|
|
connect(this, &RobotClient::speakerAudioStreamStartRequested,
|
|||
|
|
m_worker, &BackendWorker::startSpeakerAudioStream);
|
|||
|
|
connect(this, &RobotClient::speakerAudioFrameRequested,
|
|||
|
|
m_worker, &BackendWorker::enqueueSpeakerAudioFrame);
|
|||
|
|
connect(this, &RobotClient::speakerAudioStreamStopRequested,
|
|||
|
|
m_worker, &BackendWorker::stopSpeakerAudioStream);
|
|||
|
|
connect(this, &RobotClient::microphoneAudioStreamStartRequested,
|
|||
|
|
m_worker, &BackendWorker::startMicrophoneAudioStream);
|
|||
|
|
connect(this, &RobotClient::microphoneAudioStreamStopRequested,
|
|||
|
|
m_worker, &BackendWorker::stopMicrophoneAudioStream);
|
|||
|
|
connect(this, &RobotClient::configurationConnectRequested,
|
|||
|
|
m_worker, &BackendWorker::connectConfigurationServer);
|
|||
|
|
connect(this, &RobotClient::configurationDisconnectRequested,
|
|||
|
|
m_worker, &BackendWorker::disconnectConfigurationServer);
|
|||
|
|
connect(this, &RobotClient::configurationFilesRequested,
|
|||
|
|
m_worker, &BackendWorker::listConfigurationFiles);
|
|||
|
|
connect(this, &RobotClient::configurationFileReadRequested,
|
|||
|
|
m_worker, &BackendWorker::readConfigurationFile);
|
|||
|
|
connect(this, &RobotClient::configurationFileValidateRequested,
|
|||
|
|
m_worker, &BackendWorker::validateConfigurationFile);
|
|||
|
|
connect(this, &RobotClient::configurationFileSaveRequested,
|
|||
|
|
m_worker, &BackendWorker::saveConfigurationFile);
|
|||
|
|
|
|||
|
|
connect(&m_thread, &QThread::finished, m_worker, &QObject::deleteLater);
|
|||
|
|
m_thread.start();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
RobotClient::~RobotClient()
|
|||
|
|
{
|
|||
|
|
if (m_thread.isRunning()) {
|
|||
|
|
QMetaObject::invokeMethod(m_worker, [this]() {
|
|||
|
|
m_worker->disconnectConfigurationServer();
|
|||
|
|
m_worker->disconnectFromServer();
|
|||
|
|
}, Qt::BlockingQueuedConnection);
|
|||
|
|
}
|
|||
|
|
m_thread.quit();
|
|||
|
|
m_thread.wait(2500);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool RobotClient::grpcAvailable() const
|
|||
|
|
{
|
|||
|
|
#ifdef CMVR_WITH_GRPC
|
|||
|
|
return true;
|
|||
|
|
#else
|
|||
|
|
return false;
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void RobotClient::connectToServer(const QString &host, int port, bool demoMode)
|
|||
|
|
{
|
|||
|
|
emit connectRequested(host, port, demoMode);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void RobotClient::disconnectFromServer() { emit disconnectRequested(); }
|
|||
|
|
void RobotClient::setArmTelemetry(const QString &deviceId, bool enabled)
|
|||
|
|
{
|
|||
|
|
emit armTelemetryRequested(deviceId, enabled);
|
|||
|
|
}
|
|||
|
|
void RobotClient::requestMoveJ(const QString &deviceId, const QVariantList &positions,
|
|||
|
|
double velocity, double acceleration)
|
|||
|
|
{
|
|||
|
|
emit moveJRequested(deviceId, positions, velocity, acceleration);
|
|||
|
|
}
|
|||
|
|
void RobotClient::requestMoveJPose(const QString &deviceId, const QVariantMap &pose,
|
|||
|
|
const QString &baseLink, const QString &eeLink,
|
|||
|
|
double velocity, double acceleration)
|
|||
|
|
{
|
|||
|
|
emit moveJPoseRequested(deviceId, pose, baseLink, eeLink, velocity, acceleration);
|
|||
|
|
}
|
|||
|
|
void RobotClient::requestTorque(const QString &deviceId, bool enabled) { emit torqueRequested(deviceId, enabled); }
|
|||
|
|
void RobotClient::requestArmStop(const QString &deviceId) { emit armStopRequested(deviceId); }
|
|||
|
|
void RobotClient::requestArmClearFault(const QString &deviceId) { emit armClearFaultRequested(deviceId); }
|
|||
|
|
void RobotClient::requestArmCalibrateZero(const QString &deviceId, const QString &jointName)
|
|||
|
|
{
|
|||
|
|
emit armCalibrateZeroRequested(deviceId, jointName);
|
|||
|
|
}
|
|||
|
|
void RobotClient::requestSnapshot(const QString &cameraId) { emit snapshotRequested(cameraId); }
|
|||
|
|
void RobotClient::startCameraStream(const QString &cameraId, const QString &codec,
|
|||
|
|
const QString &mode)
|
|||
|
|
{
|
|||
|
|
emit cameraStreamStartRequested(cameraId, codec, mode);
|
|||
|
|
}
|
|||
|
|
void RobotClient::setCameraStreamCodec(const QString &codec)
|
|||
|
|
{
|
|||
|
|
emit cameraStreamCodecRequested(codec);
|
|||
|
|
}
|
|||
|
|
void RobotClient::stopCameraStream(const QString &cameraId)
|
|||
|
|
{
|
|||
|
|
emit cameraStreamStopRequested(cameraId);
|
|||
|
|
}
|
|||
|
|
void RobotClient::startCameraRecording(const QString &cameraId, const QString &videoPath)
|
|||
|
|
{
|
|||
|
|
emit cameraRecordingStartRequested(cameraId, videoPath);
|
|||
|
|
}
|
|||
|
|
void RobotClient::stopCameraRecording(const QString &cameraId)
|
|||
|
|
{
|
|||
|
|
emit cameraRecordingStopRequested(cameraId);
|
|||
|
|
}
|
|||
|
|
void RobotClient::requestSystemRefresh() { emit systemRefreshRequested(); }
|
|||
|
|
void RobotClient::requestSystemStopAll(const QString &deviceId) { emit systemStopAllRequested(deviceId); }
|
|||
|
|
void RobotClient::requestAgvRefresh(const QString &deviceId) { emit agvRefreshRequested(deviceId); }
|
|||
|
|
void RobotClient::requestAgvNavigatePose(const QString &deviceId, double x, double y,
|
|||
|
|
double theta, double maxSpeed)
|
|||
|
|
{
|
|||
|
|
emit agvNavigatePoseRequested(deviceId, x, y, theta, maxSpeed);
|
|||
|
|
}
|
|||
|
|
void RobotClient::requestAgvNavigateStation(const QString &deviceId, const QString &stationId,
|
|||
|
|
double maxSpeed)
|
|||
|
|
{
|
|||
|
|
emit agvNavigateStationRequested(deviceId, stationId, maxSpeed);
|
|||
|
|
}
|
|||
|
|
void RobotClient::requestAgvFollowPath(const QString &deviceId, const QVariantList &path,
|
|||
|
|
double maxSpeed)
|
|||
|
|
{
|
|||
|
|
emit agvFollowPathRequested(deviceId, path, maxSpeed);
|
|||
|
|
}
|
|||
|
|
void RobotClient::requestAgvVelocity(const QString &deviceId, double vx, double vy, double wz)
|
|||
|
|
{
|
|||
|
|
emit agvVelocityRequested(deviceId, vx, vy, wz);
|
|||
|
|
}
|
|||
|
|
void RobotClient::requestAgvAction(const QString &deviceId, const QString &action)
|
|||
|
|
{
|
|||
|
|
emit agvActionRequested(deviceId, action);
|
|||
|
|
}
|
|||
|
|
void RobotClient::requestAgvInventory(const QString &deviceId) { emit agvInventoryRequested(deviceId); }
|
|||
|
|
void RobotClient::requestAgvSwitchMap(const QString &deviceId, const QString &mapName)
|
|||
|
|
{
|
|||
|
|
emit agvSwitchMapRequested(deviceId, mapName);
|
|||
|
|
}
|
|||
|
|
void RobotClient::startAgvMapStream(const QString &deviceId, const QString &mapName,
|
|||
|
|
int dimension, bool snapshot, bool incremental)
|
|||
|
|
{
|
|||
|
|
emit agvMapStreamStartRequested(deviceId, mapName, dimension, snapshot, incremental);
|
|||
|
|
}
|
|||
|
|
void RobotClient::stopAgvMapStream()
|
|||
|
|
{
|
|||
|
|
emit agvMapStreamStopRequested();
|
|||
|
|
}
|
|||
|
|
void RobotClient::requestRpc(const QString &serviceName, const QString &methodName,
|
|||
|
|
const QString &requestJson, const QString &deviceId)
|
|||
|
|
{
|
|||
|
|
emit rpcRequested(serviceName, methodName, requestJson, deviceId);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void RobotClient::startSpeakerAudioStream(const QString &deviceId)
|
|||
|
|
{
|
|||
|
|
emit speakerAudioStreamStartRequested(deviceId);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void RobotClient::sendSpeakerAudioFrame(const QByteArray &data, int sampleRate, int channels,
|
|||
|
|
qint64 pts, int samples)
|
|||
|
|
{
|
|||
|
|
emit speakerAudioFrameRequested(data, sampleRate, channels, pts, samples);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void RobotClient::stopSpeakerAudioStream()
|
|||
|
|
{
|
|||
|
|
emit speakerAudioStreamStopRequested();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void RobotClient::startMicrophoneAudioStream(const QString &deviceId)
|
|||
|
|
{
|
|||
|
|
emit microphoneAudioStreamStartRequested(deviceId);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void RobotClient::stopMicrophoneAudioStream()
|
|||
|
|
{
|
|||
|
|
emit microphoneAudioStreamStopRequested();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void RobotClient::connectConfigurationServer(const QString &host, int port)
|
|||
|
|
{
|
|||
|
|
emit configurationConnectRequested(host, port);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void RobotClient::disconnectConfigurationServer()
|
|||
|
|
{
|
|||
|
|
emit configurationDisconnectRequested();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void RobotClient::requestConfigurationFiles()
|
|||
|
|
{
|
|||
|
|
emit configurationFilesRequested();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void RobotClient::readConfigurationFile(const QString &relativePath)
|
|||
|
|
{
|
|||
|
|
emit configurationFileReadRequested(relativePath);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void RobotClient::validateConfigurationFile(const QString &relativePath,
|
|||
|
|
const QString &content)
|
|||
|
|
{
|
|||
|
|
emit configurationFileValidateRequested(relativePath, content);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void RobotClient::saveConfigurationFile(const QString &relativePath, const QString &content,
|
|||
|
|
const QString &expectedRevision)
|
|||
|
|
{
|
|||
|
|
emit configurationFileSaveRequested(relativePath, content, expectedRevision);
|
|||
|
|
}
|