添加测量客户端实现类
This commit is contained in:
parent
47251ebc70
commit
829e322b7c
|
|
@ -4,6 +4,9 @@ CONFIG += ordered
|
|||
SUBDIRS += \
|
||||
src
|
||||
|
||||
OTHER_FILES += \
|
||||
$${PWD}/Common.pri
|
||||
|
||||
OTHER_FILES += \
|
||||
$${PWD}/configure/* \
|
||||
$${PWD}/style/* \
|
||||
|
|
|
|||
|
|
@ -33,7 +33,6 @@ MainWindow* MainWindow::_s_main_win = nullptr;
|
|||
|
||||
void MainWindow::OutputInfo(OutputInfoType out_type, const QString& ouput_info)
|
||||
{
|
||||
|
||||
QMetaObject::invokeMethod(_s_main_win, "onOutputInfo", Qt::QueuedConnection, Q_ARG(int, int(out_type)), Q_ARG(QString, ouput_info));
|
||||
}
|
||||
|
||||
|
|
|
|||
181
src/MeasureClient/MeasureClient.cpp
Normal file
181
src/MeasureClient/MeasureClient.cpp
Normal file
|
|
@ -0,0 +1,181 @@
|
|||
#include "MeasureClient.h"
|
||||
#include <QDebug>
|
||||
|
||||
MeasureClient::MeasureClient(QObject *parent) : QObject(parent)
|
||||
{
|
||||
_host = "localhost";
|
||||
_port = 96966;
|
||||
|
||||
// 创建工作线程
|
||||
_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") {
|
||||
emit startMeasureResult(success, message);
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
|
||||
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)) {
|
||||
result["message"] = "Failed to connect to server: " + socket.errorString();
|
||||
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)) {
|
||||
result["message"] = "Failed to send command: " + socket.errorString();
|
||||
emit errorOccurred(result["message"].toString());
|
||||
socket.disconnectFromHost();
|
||||
return result;
|
||||
}
|
||||
|
||||
// 等待响应
|
||||
if (!socket.waitForReadyRead(5000)) {
|
||||
result["message"] = "No response from server: " + socket.errorString();
|
||||
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;
|
||||
}
|
||||
} else {
|
||||
// 处理其他响应
|
||||
QString message;
|
||||
response_stream >> message;
|
||||
result["message"] = message;
|
||||
}
|
||||
|
||||
// 断开连接
|
||||
socket.disconnectFromHost();
|
||||
socket.waitForDisconnected(1000);
|
||||
|
||||
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
|
||||
8
src/MeasureClient/MeasureClient.pri
Normal file
8
src/MeasureClient/MeasureClient.pri
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
INCLUDEPATH += $${PWD}
|
||||
DEPENDPATH += $${PWD}
|
||||
|
||||
SOURCES += \
|
||||
$$PWD/MeasureClient.cpp
|
||||
|
||||
HEADERS += \
|
||||
$$PWD/MeasureClient.h
|
||||
|
|
@ -25,7 +25,7 @@ QWidget* DeviceParamsItemDelegate::createEditor(QWidget* parent, const QStyleOpt
|
|||
QComboBox *combobox = new QComboBox(parent);
|
||||
for (int index = 0; index < value_list.size(); ++index) {
|
||||
QString value = value_list.at(index);
|
||||
combobox->addItem(value, value.toInt());
|
||||
combobox->addItem(value);
|
||||
}
|
||||
editor = combobox;
|
||||
} break;
|
||||
|
|
@ -34,7 +34,7 @@ QWidget* DeviceParamsItemDelegate::createEditor(QWidget* parent, const QStyleOpt
|
|||
QComboBox *combobox = new QComboBox(parent);
|
||||
for (int index = 0; index < value_list.size(); ++index) {
|
||||
QString value = value_list.at(index);
|
||||
combobox->addItem(value, index);
|
||||
combobox->addItem(value, index + 1);
|
||||
}
|
||||
editor = combobox;
|
||||
} break;
|
||||
|
|
@ -228,6 +228,8 @@ void DeviceParamsTableForm::SetConfigFilename(const QString& filename)
|
|||
ui->params_cfg_table->item(row, 1)->setText(channel_config_info["AddrCount"].toString());
|
||||
if (channel_config_info.contains("DeviceGain"))
|
||||
ui->params_cfg_table->item(row, 2)->setText(channel_config_info["DeviceGain"].toString());
|
||||
if (channel_config_info.contains("DeviceGainSelectIndex"))
|
||||
ui->params_cfg_table->item(row, 2)->setData(Qt::UserRole, channel_config_info["DeviceGainSelectIndex"]);
|
||||
if (channel_config_info.contains("SoftGain"))
|
||||
ui->params_cfg_table->item(row, 3)->setText(channel_config_info["SoftGain"].toString());
|
||||
if (channel_config_info.contains("TimeConst"))
|
||||
|
|
@ -337,6 +339,7 @@ void DeviceParamsTableForm::onSaveSelectedChannelConfig()
|
|||
int channel_id = channel_info["channel_id"].toInt();
|
||||
int addr_count = ui->params_cfg_table->item(row, 1)->text().toInt();
|
||||
int device_gain = ui->params_cfg_table->item(row, 2)->text().toInt();
|
||||
int device_gain_index = ui->params_cfg_table->item(row, 2)->data(Qt::UserRole).toInt();
|
||||
uint soft_gain = ui->params_cfg_table->item(row, 3)->text().toUInt();
|
||||
int time_const = ui->params_cfg_table->item(row, 4)->text().toInt();
|
||||
int dc_offset = ui->params_cfg_table->item(row, 5)->text().toInt();
|
||||
|
|
@ -349,6 +352,7 @@ void DeviceParamsTableForm::onSaveSelectedChannelConfig()
|
|||
device_channel_params["ChannelId"] = channel_id;
|
||||
device_channel_params["AddrCount"] = addr_count;
|
||||
device_channel_params["DeviceGain"] = device_gain;
|
||||
device_channel_params["DeviceGainSelectIndex"] = device_gain_index;
|
||||
device_channel_params["SoftGain"] = soft_gain;
|
||||
device_channel_params["TimeConst"] = time_const;
|
||||
device_channel_params["DcOffset"] = dc_offset;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
TARGET = EnergySpectrumAnalyer
|
||||
|
||||
QT += core gui widgets concurrent datavisualization
|
||||
QT += core gui widgets concurrent datavisualization network
|
||||
|
||||
CONFIG += c++17 release
|
||||
msvc {
|
||||
|
|
@ -22,8 +22,8 @@ include($${PROJECT_DIR}/3rdlib/QsLog/QsLog.pri)
|
|||
include($${PROJECT_DIR}/3rdlib/QtAdvancedDockingSystem/ads.pri)
|
||||
include($${PROJECT_DIR}/3rdlib/csv/csv.pri)
|
||||
include($${PROJECT_DIR}/3rdlib/qwt/qwt.pri)
|
||||
|
||||
include(DataCalcProcess/DataCalcProcess.pri)
|
||||
include($${PWD}/DataCalcProcess/DataCalcProcess.pri)
|
||||
include($${PWD}/MeasureClient/MeasureClient.pri)
|
||||
|
||||
DESTDIR = $${BUILD_BIN}
|
||||
OBJECTS_DIR = $${BUILD_OBJ}/$${TARGET}/objs
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user