From 829e322b7ccdc386bbb546cac13bb40dae2f645d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B5=B7?= Date: Mon, 6 Apr 2026 23:46:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B5=8B=E9=87=8F=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E7=AB=AF=E5=AE=9E=E7=8E=B0=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EnergySpectrumAnalyer.pro | 3 + src/MainWindow.cpp | 1 - src/MeasureClient/MeasureClient.cpp | 181 ++++++++++++++++++ src/MeasureClient/MeasureClient.h | 60 ++++++ src/MeasureClient/MeasureClient.pri | 8 + .../DeviceParamsTableForm.cpp | 8 +- src/src.pro | 6 +- 7 files changed, 261 insertions(+), 6 deletions(-) create mode 100644 src/MeasureClient/MeasureClient.cpp create mode 100644 src/MeasureClient/MeasureClient.h create mode 100644 src/MeasureClient/MeasureClient.pri diff --git a/EnergySpectrumAnalyer.pro b/EnergySpectrumAnalyer.pro index 8bcf6a8..77df457 100644 --- a/EnergySpectrumAnalyer.pro +++ b/EnergySpectrumAnalyer.pro @@ -4,6 +4,9 @@ CONFIG += ordered SUBDIRS += \ src +OTHER_FILES += \ + $${PWD}/Common.pri + OTHER_FILES += \ $${PWD}/configure/* \ $${PWD}/style/* \ diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 8b6f68f..1439148 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -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)); } diff --git a/src/MeasureClient/MeasureClient.cpp b/src/MeasureClient/MeasureClient.cpp new file mode 100644 index 0000000..5d47959 --- /dev/null +++ b/src/MeasureClient/MeasureClient.cpp @@ -0,0 +1,181 @@ +#include "MeasureClient.h" +#include + +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; +} \ No newline at end of file diff --git a/src/MeasureClient/MeasureClient.h b/src/MeasureClient/MeasureClient.h new file mode 100644 index 0000000..1e97b05 --- /dev/null +++ b/src/MeasureClient/MeasureClient.h @@ -0,0 +1,60 @@ +#ifndef MEASURECLIENT_H +#define MEASURECLIENT_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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 \ No newline at end of file diff --git a/src/MeasureClient/MeasureClient.pri b/src/MeasureClient/MeasureClient.pri new file mode 100644 index 0000000..4282815 --- /dev/null +++ b/src/MeasureClient/MeasureClient.pri @@ -0,0 +1,8 @@ +INCLUDEPATH += $${PWD} +DEPENDPATH += $${PWD} + +SOURCES += \ + $$PWD/MeasureClient.cpp + +HEADERS += \ + $$PWD/MeasureClient.h \ No newline at end of file diff --git a/src/MeasureDeviceParamsConfigView/DeviceParamsTableForm.cpp b/src/MeasureDeviceParamsConfigView/DeviceParamsTableForm.cpp index 5dedea7..d865761 100644 --- a/src/MeasureDeviceParamsConfigView/DeviceParamsTableForm.cpp +++ b/src/MeasureDeviceParamsConfigView/DeviceParamsTableForm.cpp @@ -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; diff --git a/src/src.pro b/src/src.pro index f6dd284..0aeaa9f 100644 --- a/src/src.pro +++ b/src/src.pro @@ -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