feat:完成数据服务模块(alarmEventDataService)中历史数据查询逻辑

This commit is contained in:
duanshengchao 2025-11-11 10:51:01 +08:00
parent 0962645144
commit 1037b0b2a9
2 changed files with 104 additions and 0 deletions

View File

@ -6,10 +6,13 @@
#include "qamqpclient.h"
#include "qamqpexchange.h"
#include "qamqpqueue.h"
//#include <QNetworkRequest>
class QAmqpClient;
class QAmqpQueue;
class QAmqpExchange;
class QNetworkAccessManager;
class QNetworkReply;
class QTimer;
class AlarmEventDataService : public QObject
@ -21,9 +24,12 @@ public:
bool initialize(const ServiceConfig& config);
void start();
void stop();
void queryHistoricalEvents(const QDateTime& startTime, const QDateTime& endTime);
signals:
void realTimeEventReceived(const EventData& event);
void historicalQueryError(const QString& msg);
void historicalQuertData(const QList<EventData>& events);
private slots:
void onRabbitMQConnected();
@ -31,6 +37,7 @@ private slots:
void onRabbitMQError(QAMQP::Error error);
void onMessageReceived();
void onReconnectTimeout();
void onHistoricalRequestFinished(QNetworkReply* reply);
private:
explicit AlarmEventDataService();
@ -45,6 +52,10 @@ private:
MessageHandleResult processMessage(const QAmqpMessage& message);
EventData parseEventFromMessage(const QByteArray& data, QString& errorString);
bool validateEvent(const EventData& event);
//历史数据处理
QUrl bulidHistoricalQueryUrl(const QDateTime& startTime, const QDateTime& endTime);
void processHistoricalResponse(const QByteArray& data);
//QNetworkRequest createHistoricalRequest(const QUrl& url);
ServiceConfig m_config;
ServiceStatus m_serviceStatus;
@ -60,6 +71,9 @@ private:
QTimer* m_reconnectTimer;
int m_reconnectAttempts;
int m_maxReconnectAttempts;
//历史数据相关
QNetworkAccessManager* m_networkManager;
};
#endif

View File

@ -1,4 +1,8 @@
#include "alarmEventDataService.h"
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QUrlQuery>
#include <QNetworkReply>
#include <QTimer>
#include <QJsonObject>
#include <QJsonDocument>
@ -24,6 +28,8 @@ AlarmEventDataService::AlarmEventDataService()
m_reconnectTimer = new QTimer(this);
m_reconnectTimer->setSingleShot(true);
connect(m_reconnectTimer, &QTimer::timeout, this, &AlarmEventDataService::onReconnectTimeout);
m_networkManager = new QNetworkAccessManager(this);
}
AlarmEventDataService::~AlarmEventDataService()
@ -60,6 +66,22 @@ void AlarmEventDataService::stop()
m_serviceStatus = ServiceStatus::Disconnected;
}
void AlarmEventDataService::queryHistoricalEvents(const QDateTime& startTime, const QDateTime& endTime)
{
QUrl url = bulidHistoricalQueryUrl(startTime, endTime);
if(!url.isValid())
return;
//创建网络请求
QNetworkRequest request(url);
request.setTransferTimeout(m_config.historicalConfig.timeout);
//发送请求
QNetworkReply* reply = m_networkManager->get(request);
connect(reply, &QNetworkReply::finished, this, [this, reply](){
onHistoricalRequestFinished(reply);
});
}
void AlarmEventDataService::startRealTimeDataService()
{
cleanupRabbitMQConnection();
@ -166,6 +188,58 @@ bool AlarmEventDataService::validateEvent(const EventData& event)
return true;
}
QUrl AlarmEventDataService::bulidHistoricalQueryUrl(const QDateTime& startTime, const QDateTime& endTime)
{
QUrl url = m_config.historicalConfig.baseUrl;
QUrlQuery query;
query.addQueryItem("begin", QString::number(startTime.toMSecsSinceEpoch()));
query.addQueryItem("end", QString::number(startTime.toMSecsSinceEpoch()));
url.setQuery(query);
return url;
}
void AlarmEventDataService::processHistoricalResponse(const QByteArray& data)
{
QJsonParseError parseError;
QJsonDocument doc = QJsonDocument::fromJson(data, &parseError);
if(parseError.error != QJsonParseError::NoError)
{
QString errorMsg = QString("Historical Events JSON parse error: %1 at offset %2")
.arg(parseError.errorString())
.arg(parseError.offset);
emit historicalQueryError(errorMsg);
}
if(!doc.isObject())
{
QString errorMsg = "Historical Events JSON document is not an object";
emit historicalQueryError(errorMsg);
return ;
}
QJsonObject obj = doc.object();
QJsonValue dataValue = obj.value("data");
if(dataValue.isArray())
{
QList<EventData> historicalEvents;
QJsonArray eventArray = dataValue.toArray();
int size = eventArray.size();
for(int i = 0; i < size; ++i)
{
QJsonValue eventValue = eventArray.at(i);
if(!eventValue.isObject())
continue;
QJsonObject eventObj = eventValue.toObject();
EventData event = EventData::fromJson(eventObj);
if(!event.id.isEmpty())
historicalEvents.append(event);
}
emit historicalQuertData(historicalEvents);
}
}
// ==================== 槽函数 ====================
void AlarmEventDataService::onRabbitMQConnected()
{
@ -216,3 +290,19 @@ void AlarmEventDataService::onReconnectTimeout()
m_serviceStatus = ServiceStatus::Reconnecting;
startRealTimeDataService();
}
void AlarmEventDataService::onHistoricalRequestFinished(QNetworkReply* reply)
{
if(reply->error() == QNetworkReply::NoError)
{
QByteArray data = reply->readAll();
processHistoricalResponse(data);
}
else
{
QString errorMsg = QString("Request HistoricalEvents error: %1").arg(reply->errorString());
emit historicalQueryError(errorMsg);
}
reply->deleteLater();
}