Compare commits
2 Commits
905614ff6f
...
d5caa22930
| Author | SHA1 | Date | |
|---|---|---|---|
| d5caa22930 | |||
| 24bf6cb113 |
|
|
@ -1,10 +1,9 @@
|
||||||
#include "MeasureClient.h"
|
#include "MeasureClient.h"
|
||||||
#include <QDebug>
|
|
||||||
|
|
||||||
MeasureClient::MeasureClient(QObject *parent) : QObject(parent)
|
MeasureClient::MeasureClient(QObject *parent) : QObject(parent)
|
||||||
{
|
{
|
||||||
_host = "localhost";
|
_host = "127.0.0.1";
|
||||||
_port = 96966;
|
_port = 9999;
|
||||||
|
|
||||||
// 创建工作线程
|
// 创建工作线程
|
||||||
_workerThread = new QThread(this);
|
_workerThread = new QThread(this);
|
||||||
|
|
@ -82,7 +81,13 @@ 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;
|
||||||
|
if (success) {
|
||||||
|
gvf_filename = response["devices"].toString();
|
||||||
|
emit startMeasureResult(success, gvf_filename);
|
||||||
|
} else {
|
||||||
emit startMeasureResult(success, message);
|
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") {
|
||||||
|
|
@ -95,6 +100,8 @@ 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -111,13 +118,11 @@ 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"] = "Failed to connect to server: " + socket.errorString();
|
result["message"] = QStringLiteral(u"连接服务失败:") + 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);
|
||||||
|
|
@ -126,18 +131,16 @@ 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"] = "Failed to send command: " + socket.errorString();
|
result["message"] = QStringLiteral(u"发送请求失败:") + 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"] = "No response from server: " + socket.errorString();
|
result["message"] = QStringLiteral(u"服务无响应:") + socket.errorString();
|
||||||
emit errorOccurred(result["message"].toString());
|
emit errorOccurred(result["message"].toString());
|
||||||
socket.disconnectFromHost();
|
socket.disconnectFromHost();
|
||||||
return result;
|
return result;
|
||||||
|
|
@ -150,9 +153,7 @@ 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) {
|
||||||
|
|
@ -166,6 +167,13 @@ 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;
|
||||||
|
|
@ -175,7 +183,9 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
@ -1,60 +0,0 @@
|
||||||
#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,15 +7,7 @@ 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)
|
||||||
|
|
@ -167,5 +159,13 @@ 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