2026-04-06 23:46:03 +08:00
|
|
|
|
#include "MeasureClient.h"
|
|
|
|
|
|
|
|
|
|
|
|
MeasureClient::MeasureClient(QObject *parent) : QObject(parent)
|
|
|
|
|
|
{
|
2026-04-07 19:15:58 +08:00
|
|
|
|
_host = "127.0.0.1";
|
|
|
|
|
|
_port = 9999;
|
2026-04-06 23:46:03 +08:00
|
|
|
|
|
|
|
|
|
|
// 创建工作线程
|
|
|
|
|
|
_workerThread = new QThread(this);
|
|
|
|
|
|
this->moveToThread(_workerThread);
|
|
|
|
|
|
_workerThread->start();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MeasureClient::~MeasureClient()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_workerThread->isRunning()) {
|
|
|
|
|
|
_workerThread->quit();
|
|
|
|
|
|
_workerThread->wait();
|
|
|
|
|
|
}
|
|
|
|
|
|
delete _workerThread;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MeasureClient::setServerAddress(const QString &host, quint16 port)
|
|
|
|
|
|
{
|
|
|
|
|
|
QMutexLocker locker(&_mutex);
|
|
|
|
|
|
_host = host;
|
|
|
|
|
|
_port = port;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MeasureClient::startMeasure(const QString &deviceGuid, const QVariantMap &config)
|
|
|
|
|
|
{
|
|
|
|
|
|
QMetaObject::invokeMethod(this, "processCommand", Qt::QueuedConnection,
|
|
|
|
|
|
Q_ARG(QString, "START"),
|
|
|
|
|
|
Q_ARG(QString, deviceGuid),
|
|
|
|
|
|
Q_ARG(QVariantMap, config));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MeasureClient::stopMeasure(const QString &deviceGuid)
|
|
|
|
|
|
{
|
|
|
|
|
|
QMetaObject::invokeMethod(this, "processCommand", Qt::QueuedConnection,
|
|
|
|
|
|
Q_ARG(QString, "STOP"),
|
|
|
|
|
|
Q_ARG(QString, deviceGuid),
|
|
|
|
|
|
Q_ARG(QVariantMap, QVariantMap()));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MeasureClient::setMeasureConfigParams(const QString &deviceGuid, const QVariantMap &config)
|
|
|
|
|
|
{
|
|
|
|
|
|
QMetaObject::invokeMethod(this, "processCommand", Qt::QueuedConnection,
|
|
|
|
|
|
Q_ARG(QString, "SET"),
|
|
|
|
|
|
Q_ARG(QString, deviceGuid),
|
|
|
|
|
|
Q_ARG(QVariantMap, config));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MeasureClient::clearData(const QString &deviceGuid)
|
|
|
|
|
|
{
|
|
|
|
|
|
QMetaObject::invokeMethod(this, "processCommand", Qt::QueuedConnection,
|
|
|
|
|
|
Q_ARG(QString, "CLEAR"),
|
|
|
|
|
|
Q_ARG(QString, deviceGuid),
|
|
|
|
|
|
Q_ARG(QVariantMap, QVariantMap()));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MeasureClient::getDeviceList()
|
|
|
|
|
|
{
|
|
|
|
|
|
QMetaObject::invokeMethod(this, "processCommand", Qt::QueuedConnection,
|
|
|
|
|
|
Q_ARG(QString, "DEVICE"),
|
|
|
|
|
|
Q_ARG(QString, ""),
|
|
|
|
|
|
Q_ARG(QVariantMap, QVariantMap()));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MeasureClient::processCommand(const QString &command, const QString &device_guid, const QVariantMap &config)
|
|
|
|
|
|
{
|
|
|
|
|
|
QString data;
|
|
|
|
|
|
if (!config.isEmpty()) {
|
|
|
|
|
|
QJsonDocument json_doc = QJsonDocument::fromVariant(config);
|
|
|
|
|
|
data = json_doc.toJson(QJsonDocument::Compact);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QVariantMap response = sendCommand(command, device_guid, data);
|
|
|
|
|
|
|
|
|
|
|
|
bool success = response["success"].toBool();
|
|
|
|
|
|
QString message = response["message"].toString();
|
|
|
|
|
|
|
|
|
|
|
|
if (command == "START") {
|
2026-04-07 19:16:08 +08:00
|
|
|
|
QString gvf_filename;
|
|
|
|
|
|
if (success) {
|
|
|
|
|
|
gvf_filename = response["devices"].toString();
|
|
|
|
|
|
emit startMeasureResult(success, gvf_filename);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
emit startMeasureResult(success, message);
|
|
|
|
|
|
}
|
2026-04-06 23:46:03 +08:00
|
|
|
|
} else if (command == "STOP") {
|
|
|
|
|
|
emit stopMeasureResult(success, message);
|
|
|
|
|
|
} else if (command == "SET") {
|
|
|
|
|
|
emit setMeasureConfigParamsResult(success, message);
|
|
|
|
|
|
} else if (command == "CLEAR") {
|
|
|
|
|
|
emit clearDataResult(success, message);
|
|
|
|
|
|
} else if (command == "DEVICE") {
|
|
|
|
|
|
QStringList devices;
|
|
|
|
|
|
if (success) {
|
|
|
|
|
|
devices = response["devices"].toStringList();
|
|
|
|
|
|
}
|
|
|
|
|
|
emit getDeviceListResult(success, devices);
|
2026-04-07 19:15:58 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
emit errorOccurred(message);
|
2026-04-06 23:46:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QVariantMap MeasureClient::sendCommand(const QString &command, const QString &device_guid, const QString &data)
|
|
|
|
|
|
{
|
|
|
|
|
|
QVariantMap result;
|
|
|
|
|
|
result["success"] = false;
|
|
|
|
|
|
|
|
|
|
|
|
QMutexLocker locker(&_mutex);
|
|
|
|
|
|
QString host = _host;
|
|
|
|
|
|
quint16 port = _port;
|
|
|
|
|
|
locker.unlock();
|
|
|
|
|
|
|
|
|
|
|
|
// 创建临时socket(短连接)
|
|
|
|
|
|
QTcpSocket socket;
|
|
|
|
|
|
socket.connectToHost(host, port);
|
|
|
|
|
|
if (!socket.waitForConnected(3000)) {
|
2026-04-07 19:15:58 +08:00
|
|
|
|
result["message"] = QStringLiteral(u"连接服务失败:") + socket.errorString();
|
2026-04-06 23:46:03 +08:00
|
|
|
|
emit errorOccurred(result["message"].toString());
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 发送命令
|
|
|
|
|
|
QByteArray request_data;
|
|
|
|
|
|
QDataStream request_stream(&request_data, QIODevice::WriteOnly);
|
|
|
|
|
|
request_stream << command << device_guid;
|
|
|
|
|
|
|
|
|
|
|
|
if (!data.isEmpty()) {
|
|
|
|
|
|
request_stream << data;
|
|
|
|
|
|
}
|
|
|
|
|
|
socket.write(request_data);
|
|
|
|
|
|
if (!socket.waitForBytesWritten(1000)) {
|
2026-04-07 19:15:58 +08:00
|
|
|
|
result["message"] = QStringLiteral(u"发送请求失败:") + socket.errorString();
|
2026-04-06 23:46:03 +08:00
|
|
|
|
emit errorOccurred(result["message"].toString());
|
|
|
|
|
|
socket.disconnectFromHost();
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 等待响应
|
|
|
|
|
|
if (!socket.waitForReadyRead(5000)) {
|
2026-04-07 19:15:58 +08:00
|
|
|
|
result["message"] = QStringLiteral(u"服务无响应:") + socket.errorString();
|
2026-04-06 23:46:03 +08:00
|
|
|
|
emit errorOccurred(result["message"].toString());
|
|
|
|
|
|
socket.disconnectFromHost();
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 读取响应
|
|
|
|
|
|
QByteArray response_data = socket.readAll();
|
|
|
|
|
|
QDataStream response_stream(&response_data, QIODevice::ReadOnly);
|
|
|
|
|
|
|
|
|
|
|
|
QString response_command;
|
|
|
|
|
|
bool success;
|
|
|
|
|
|
response_stream >> response_command >> success;
|
|
|
|
|
|
result["success"] = success;
|
|
|
|
|
|
if (response_command == "DEVICE") {
|
|
|
|
|
|
// 处理设备列表响应
|
|
|
|
|
|
if (success) {
|
|
|
|
|
|
int count;
|
|
|
|
|
|
response_stream >> count;
|
|
|
|
|
|
QStringList devices;
|
|
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
|
|
QString device;
|
|
|
|
|
|
response_stream >> device;
|
|
|
|
|
|
devices << device;
|
|
|
|
|
|
}
|
|
|
|
|
|
result["devices"] = devices;
|
|
|
|
|
|
}
|
2026-04-07 19:16:08 +08:00
|
|
|
|
} else if ((response_command == "START") && success) {
|
|
|
|
|
|
// 处理设备列表响应
|
|
|
|
|
|
if (success) {
|
|
|
|
|
|
QString measure_gvf_data_filename;
|
|
|
|
|
|
response_stream >> measure_gvf_data_filename;
|
|
|
|
|
|
result["gvf"] = measure_gvf_data_filename;
|
|
|
|
|
|
}
|
2026-04-06 23:46:03 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
// 处理其他响应
|
|
|
|
|
|
QString message;
|
|
|
|
|
|
response_stream >> message;
|
|
|
|
|
|
result["message"] = message;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 断开连接
|
|
|
|
|
|
socket.disconnectFromHost();
|
2026-04-07 19:15:58 +08:00
|
|
|
|
if (socket.state() != QTcpSocket::UnconnectedState) {
|
|
|
|
|
|
socket.waitForDisconnected(1000);
|
|
|
|
|
|
}
|
2026-04-06 23:46:03 +08:00
|
|
|
|
|
|
|
|
|
|
return result;
|
2026-04-07 19:15:58 +08:00
|
|
|
|
}
|