EnergySpectrumAnalyer/src/MeasureClient/MeasureWorker.cpp

166 lines
5.4 KiB
C++
Raw Normal View History

2026-04-08 15:38:41 +08:00
#include "MeasureWorker.h"
#include "MeasureClient.h"
#include <QDir>
#include "GlobalDefine.h"
#include "MeasureAnalysisProjectModel.h"
MeasureWorker* MeasureWorker::_s_instance { nullptr };
MeasureWorker::MeasureWorker(QObject* parent)
: QObject(parent)
{
_measure_client = new MeasureClient;
connect(_measure_client, &MeasureClient::getDeviceListResult, this, &MeasureWorker::onGetDeviceListResult);
connect(_measure_client, &MeasureClient::startMeasureResult, this, &MeasureWorker::onStartMeasureResult);
connect(_measure_client, &MeasureClient::stopMeasureResult, this, &MeasureWorker::onStopMeasureResult);
connect(_measure_client, &MeasureClient::setMeasureConfigParamsResult, this, &MeasureWorker::onSetMeasureConfigParamsResult);
connect(_measure_client, &MeasureClient::clearDataResult, this, &MeasureWorker::onClearDataResult);
connect(_measure_client, &MeasureClient::errorOccurred, this, &MeasureWorker::onErrorOccurred);
}
MeasureWorker *MeasureWorker::Instance()
{
if (!_s_instance) {
_s_instance = new MeasureWorker();
}
return _s_instance;
}
MeasureWorker::~MeasureWorker()
{
delete _measure_client;
}
void MeasureWorker::SetMeasureProjectName(const QString &project_name)
{
_measure_project_name = project_name;
}
void MeasureWorker::Connect()
{
LOG_INFO(QStringLiteral(u"查找测量设备... ..."));
_measure_client->getDeviceList();
}
void MeasureWorker::Start()
{
auto project_model = ProjectList::Instance()->GetProjectModel(_measure_project_name);
if (!project_model) {
return;
}
const QString& device_guid = project_model->GetDeviceGuid();
if (device_guid.isEmpty()) {
LOG_WARN(QStringLiteral(u"未选择测量设备GUID."));
return;
}
const QString& config_filename = project_model->GetMeasureDeviceParamsCfgFilename();
QFile json_file(config_filename);
if (!json_file.open(QIODevice::ReadOnly | QIODevice::Text)) {
LOG_WARN(QStringLiteral(u"加载设备参数配置失败:%1").arg(config_filename));
return;
}
QByteArray json_data = json_file.readAll();
json_file.close();
QJsonDocument json_doc = QJsonDocument::fromJson(json_data);
if (json_doc.isNull()) {
return;
}
if (!json_doc.isObject())
return;
QVariantMap device_config_info = json_doc.object().toVariantMap();
if (!device_config_info.contains(QStringLiteral(u"ChannelConfig")))
return;
QVariantList channel_config_list = device_config_info[QStringLiteral(u"ChannelConfig")].toList();
if (channel_config_list.isEmpty()) {
LOG_WARN(QStringLiteral(u"测量设备通道配置参数为空."));
return;
}
LOG_INFO(QStringLiteral(u"开始测量... ..."));
_measure_client->startMeasure(device_guid, device_config_info);
}
void MeasureWorker::Stop()
{
auto project_model = ProjectList::Instance()->GetProjectModel(_measure_project_name);
if (!project_model) {
return;
}
const QString& device_guid = project_model->GetDeviceGuid();
if (device_guid.isEmpty()) {
LOG_WARN(QStringLiteral(u"未选择测量设备GUID."));
return;
}
LOG_INFO(QStringLiteral(u"停止测量... ..."));
_measure_client->stopMeasure(device_guid);
}
void MeasureWorker::ClearData()
{
auto project_model = ProjectList::Instance()->GetProjectModel(_measure_project_name);
if (!project_model) {
return;
}
const QString& device_guid = project_model->GetDeviceGuid();
if (device_guid.isEmpty()) {
LOG_WARN(QStringLiteral(u"未选择测量设备GUID."));
return;
}
LOG_INFO(QStringLiteral(u"清除测量数据... ..."));
_measure_client->clearData(device_guid);
}
void MeasureWorker::onStartMeasureResult(bool success, const QString &info)
{
if (success) {
LOG_INFO(QStringLiteral(u"启动测量成功"));
LOG_INFO(QStringLiteral(u"测量数据GVF文件: %1").arg(info));
} else {
LOG_WARN(QStringLiteral(u"启动测量失败: %1").arg(info));
}
}
void MeasureWorker::onStopMeasureResult(bool success, const QString &message)
{
if (success) {
LOG_INFO(QStringLiteral(u"停止测量成功"));
} else {
LOG_WARN(QStringLiteral(u"停止测量失败: %1").arg(message));
}
}
void MeasureWorker::onSetMeasureConfigParamsResult(bool success, const QString &message)
{
if (success) {
LOG_INFO(QStringLiteral(u"设置测量参数成功"));
} else {
LOG_WARN(QStringLiteral(u"设置测量参数失败: %1").arg(message));
}
}
void MeasureWorker::onClearDataResult(bool success, const QString &message)
{
if (success) {
LOG_INFO(QStringLiteral(u"清除测量数据成功"));
} else {
LOG_WARN(QStringLiteral(u"清除测量数据失败: %1").arg(message));
}
}
void MeasureWorker::onGetDeviceListResult(bool success, const QStringList &devices)
{
if (success && !devices.isEmpty()) {
LOG_INFO(QStringLiteral(u"获取测量设备GUID成功:%1").arg(devices.first()));
auto project_model = ProjectList::Instance()->GetProjectModel(_measure_project_name);
if (project_model) {
project_model->SetDeviceGuid(devices.first());
}
} else {
LOG_WARN(QStringLiteral(u"获取测量设备失败"));
}
}
void MeasureWorker::onErrorOccurred(const QString &errorString)
{
LOG_WARN(QStringLiteral(u"错误: %1").arg(errorString));
}