Compare commits
No commits in common. "d5caa22930569839ccbd3b2743caf620a7955a8d" and "905614ff6f41a1f107522537fc8d852ee8092374" have entirely different histories.
d5caa22930
...
905614ff6f
|
|
@ -1,9 +1,10 @@
|
||||||
#include "MeasureClient.h"
|
#include "MeasureClient.h"
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
MeasureClient::MeasureClient(QObject *parent) : QObject(parent)
|
MeasureClient::MeasureClient(QObject *parent) : QObject(parent)
|
||||||
{
|
{
|
||||||
_host = "127.0.0.1";
|
_host = "localhost";
|
||||||
_port = 9999;
|
_port = 96966;
|
||||||
|
|
||||||
// 创建工作线程
|
// 创建工作线程
|
||||||
_workerThread = new QThread(this);
|
_workerThread = new QThread(this);
|
||||||
|
|
@ -81,13 +82,7 @@ void MeasureClient::processCommand(const QString &command, const QString &device
|
||||||
QString message = response["message"].toString();
|
QString message = response["message"].toString();
|
||||||
|
|
||||||
if (command == "START") {
|
if (command == "START") {
|
||||||
QString gvf_filename;
|
emit startMeasureResult(success, message);
|
||||||
if (success) {
|
|
||||||
gvf_filename = response["devices"].toString();
|
|
||||||
emit startMeasureResult(success, gvf_filename);
|
|
||||||
} else {
|
|
||||||
emit startMeasureResult(success, message);
|
|
||||||
}
|
|
||||||
} else if (command == "STOP") {
|
} else if (command == "STOP") {
|
||||||
emit stopMeasureResult(success, message);
|
emit stopMeasureResult(success, message);
|
||||||
} else if (command == "SET") {
|
} else if (command == "SET") {
|
||||||
|
|
@ -100,8 +95,6 @@ void MeasureClient::processCommand(const QString &command, const QString &device
|
||||||
devices = response["devices"].toStringList();
|
devices = response["devices"].toStringList();
|
||||||
}
|
}
|
||||||
emit getDeviceListResult(success, devices);
|
emit getDeviceListResult(success, devices);
|
||||||
} else {
|
|
||||||
emit errorOccurred(message);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -118,11 +111,13 @@ QVariantMap MeasureClient::sendCommand(const QString &command, const QString &de
|
||||||
// 创建临时socket(短连接)
|
// 创建临时socket(短连接)
|
||||||
QTcpSocket socket;
|
QTcpSocket socket;
|
||||||
socket.connectToHost(host, port);
|
socket.connectToHost(host, port);
|
||||||
|
|
||||||
if (!socket.waitForConnected(3000)) {
|
if (!socket.waitForConnected(3000)) {
|
||||||
result["message"] = QStringLiteral(u"连接服务失败:") + socket.errorString();
|
result["message"] = "Failed to connect to server: " + socket.errorString();
|
||||||
emit errorOccurred(result["message"].toString());
|
emit errorOccurred(result["message"].toString());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 发送命令
|
// 发送命令
|
||||||
QByteArray request_data;
|
QByteArray request_data;
|
||||||
QDataStream request_stream(&request_data, QIODevice::WriteOnly);
|
QDataStream request_stream(&request_data, QIODevice::WriteOnly);
|
||||||
|
|
@ -131,16 +126,18 @@ QVariantMap MeasureClient::sendCommand(const QString &command, const QString &de
|
||||||
if (!data.isEmpty()) {
|
if (!data.isEmpty()) {
|
||||||
request_stream << data;
|
request_stream << data;
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.write(request_data);
|
socket.write(request_data);
|
||||||
if (!socket.waitForBytesWritten(1000)) {
|
if (!socket.waitForBytesWritten(1000)) {
|
||||||
result["message"] = QStringLiteral(u"发送请求失败:") + socket.errorString();
|
result["message"] = "Failed to send command: " + socket.errorString();
|
||||||
emit errorOccurred(result["message"].toString());
|
emit errorOccurred(result["message"].toString());
|
||||||
socket.disconnectFromHost();
|
socket.disconnectFromHost();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 等待响应
|
// 等待响应
|
||||||
if (!socket.waitForReadyRead(5000)) {
|
if (!socket.waitForReadyRead(5000)) {
|
||||||
result["message"] = QStringLiteral(u"服务无响应:") + socket.errorString();
|
result["message"] = "No response from server: " + socket.errorString();
|
||||||
emit errorOccurred(result["message"].toString());
|
emit errorOccurred(result["message"].toString());
|
||||||
socket.disconnectFromHost();
|
socket.disconnectFromHost();
|
||||||
return result;
|
return result;
|
||||||
|
|
@ -153,7 +150,9 @@ QVariantMap MeasureClient::sendCommand(const QString &command, const QString &de
|
||||||
QString response_command;
|
QString response_command;
|
||||||
bool success;
|
bool success;
|
||||||
response_stream >> response_command >> success;
|
response_stream >> response_command >> success;
|
||||||
|
|
||||||
result["success"] = success;
|
result["success"] = success;
|
||||||
|
|
||||||
if (response_command == "DEVICE") {
|
if (response_command == "DEVICE") {
|
||||||
// 处理设备列表响应
|
// 处理设备列表响应
|
||||||
if (success) {
|
if (success) {
|
||||||
|
|
@ -167,13 +166,6 @@ QVariantMap MeasureClient::sendCommand(const QString &command, const QString &de
|
||||||
}
|
}
|
||||||
result["devices"] = devices;
|
result["devices"] = devices;
|
||||||
}
|
}
|
||||||
} 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;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// 处理其他响应
|
// 处理其他响应
|
||||||
QString message;
|
QString message;
|
||||||
|
|
@ -183,9 +175,7 @@ QVariantMap MeasureClient::sendCommand(const QString &command, const QString &de
|
||||||
|
|
||||||
// 断开连接
|
// 断开连接
|
||||||
socket.disconnectFromHost();
|
socket.disconnectFromHost();
|
||||||
if (socket.state() != QTcpSocket::UnconnectedState) {
|
socket.waitForDisconnected(1000);
|
||||||
socket.waitForDisconnected(1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
60
src/MeasureClient/MeasureClient.h
Normal file
60
src/MeasureClient/MeasureClient.h
Normal file
|
|
@ -0,0 +1,60 @@
|
||||||
|
#ifndef MEASURECLIENT_H
|
||||||
|
#define MEASURECLIENT_H
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
#include <QTcpSocket>
|
||||||
|
#include <QDataStream>
|
||||||
|
#include <QJsonDocument>
|
||||||
|
#include <QJsonObject>
|
||||||
|
#include <QVariantMap>
|
||||||
|
#include <QVariantList>
|
||||||
|
#include <QThread>
|
||||||
|
#include <QMutex>
|
||||||
|
|
||||||
|
class MeasureClient : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit MeasureClient(QObject *parent = nullptr);
|
||||||
|
~MeasureClient();
|
||||||
|
|
||||||
|
// 设置服务器地址和端口
|
||||||
|
void setServerAddress(const QString &host, quint16 port);
|
||||||
|
// 启动测量(异步)
|
||||||
|
void startMeasure(const QString &deviceGuid, const QVariantMap &config);
|
||||||
|
// 停止测量(异步)
|
||||||
|
void stopMeasure(const QString &deviceGuid);
|
||||||
|
// 设置测量参数(异步)
|
||||||
|
void setMeasureConfigParams(const QString &deviceGuid, const QVariantMap &config);
|
||||||
|
// 清除数据(异步)
|
||||||
|
void clearData(const QString &deviceGuid);
|
||||||
|
// 获取设备列表(异步)
|
||||||
|
void getDeviceList();
|
||||||
|
|
||||||
|
signals:
|
||||||
|
// 操作结果信号
|
||||||
|
void startMeasureResult(bool success, const QString &message);
|
||||||
|
void stopMeasureResult(bool success, const QString &message);
|
||||||
|
void setMeasureConfigParamsResult(bool success, const QString &message);
|
||||||
|
void clearDataResult(bool success, const QString &message);
|
||||||
|
void getDeviceListResult(bool success, const QStringList &devices);
|
||||||
|
// 错误信号
|
||||||
|
void errorOccurred(const QString &errorString);
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
// 处理命令执行
|
||||||
|
void processCommand(const QString &command, const QString &device_guid, const QVariantMap &config);
|
||||||
|
|
||||||
|
private:
|
||||||
|
// 发送命令并获取响应(短连接模式)
|
||||||
|
QVariantMap sendCommand(const QString &command, const QString &device_guid, const QString &data = QString());
|
||||||
|
|
||||||
|
private:
|
||||||
|
QString _host;
|
||||||
|
quint16 _port;
|
||||||
|
QThread *_workerThread;
|
||||||
|
QMutex _mutex;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // MEASURECLIENT_H
|
||||||
20
src/src.pro
20
src/src.pro
|
|
@ -7,7 +7,15 @@ msvc {
|
||||||
QMAKE_CFLAGS += /utf-8
|
QMAKE_CFLAGS += /utf-8
|
||||||
QMAKE_CXXFLAGS += /utf-8
|
QMAKE_CXXFLAGS += /utf-8
|
||||||
}
|
}
|
||||||
|
DEFINES += ENABLE_DEBUG
|
||||||
|
contains(DEFINES, ENABLE_DEBUG) {
|
||||||
|
CONFIG += console
|
||||||
|
win32-msvc* {
|
||||||
|
QMAKE_CXXFLAGS_RELEASE -= -O2
|
||||||
|
QMAKE_CXXFLAGS_RELEASE += /MD /Zi /O
|
||||||
|
QMAKE_LFLAGS_RELEASE += /DEBUG
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
include($${PWD}/../Common.pri)
|
include($${PWD}/../Common.pri)
|
||||||
include($${PROJECT_DIR}/3rdlib/QsLog/QsLog.pri)
|
include($${PROJECT_DIR}/3rdlib/QsLog/QsLog.pri)
|
||||||
|
|
@ -159,13 +167,5 @@ FORMS += \
|
||||||
ThreeDimensionalConformityAnalysisView/ConformityAnalysis.ui
|
ThreeDimensionalConformityAnalysisView/ConformityAnalysis.ui
|
||||||
|
|
||||||
|
|
||||||
DEFINES += ENABLE_DEBUG
|
|
||||||
contains(DEFINES, ENABLE_DEBUG) {
|
|
||||||
CONFIG += console
|
|
||||||
win32-msvc* {
|
|
||||||
QMAKE_CXXFLAGS_RELEASE -= -O2
|
|
||||||
QMAKE_CXXFLAGS_RELEASE += /MD /Zi /O
|
|
||||||
QMAKE_LFLAGS_RELEASE += /DEBUG
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user