FemaLocalSoftware/alarmsservice.h

119 lines
3.6 KiB
C
Raw Permalink Normal View History

2024-12-28 17:15:48 +00:00
#ifndef ALARMSSERVICE_H
#define ALARMSSERVICE_H
#include "HttpClient.h"
#include "alarm_r.h"
2025-01-07 22:13:48 +00:00
#include "utils.h"
2024-12-28 17:15:48 +00:00
#include <QJsonArray>
2025-01-07 22:13:48 +00:00
#include <QJsonDocument>
#include <QJsonObject>
2024-12-28 17:15:48 +00:00
#include <QList>
2025-01-07 22:13:48 +00:00
#include <QObject>
#include <QString>
2024-12-28 17:15:48 +00:00
#include <QStringList>
2025-01-07 22:13:48 +00:00
struct AlarmSettings{
QMap<QString, QList<QString>> groupsWithSensors;
qint64 start_time;
qint64 end_time;
AlarmSettings(QMap<QString, QList<QString>> a, qint64 b, qint64 c) {
groupsWithSensors = a;
start_time = b;
end_time = c;
}
};
2024-12-28 17:15:48 +00:00
class AlarmsService : public QObject
{
Q_OBJECT
public:
2025-01-07 22:13:48 +00:00
explicit AlarmsService(HttpClient *client, QObject *parent = nullptr)
: QObject(parent)
, httpClient(client)
{}
int getAlarmsAmount(const QMap<QString, QList<QString>> &groupsWithSensors,
qint64 start,
qint64 end)
{
2024-12-28 17:15:48 +00:00
QJsonObject rootObj;
rootObj["start_time"] = start;
rootObj["end_time"] = end;
QJsonObject sensorsObj;
for (auto it = groupsWithSensors.constBegin(); it != groupsWithSensors.constEnd(); ++it) {
QJsonArray sensorArray;
for (QString sensorId : it.value()) {
sensorArray.append(sensorId);
}
sensorsObj[it.key()] = sensorArray;
}
rootObj["sensors"] = sensorsObj;
QJsonDocument doc(rootObj);
QByteArray requestData = doc.toJson();
2025-01-07 22:13:48 +00:00
QJsonObject responseJson = httpClient->post(utils::API_URL + "/alarms/getAlarmsAmount",
QJsonDocument::fromJson(requestData).object());
2024-12-28 17:15:48 +00:00
if (responseJson.contains("amount")) {
return responseJson["amount"].toInt();
}
return 0;
}
2025-01-07 22:13:48 +00:00
QList<AlarmR> getAlarms(const QMap<QString, QList<QString>> &groupsWithSensors,
qint64 start,
qint64 end,
int pageNum,
int pageSize)
{
2024-12-28 17:15:48 +00:00
QJsonObject rootObj;
rootObj["start_time"] = start;
rootObj["end_time"] = end;
rootObj["page_number"] = pageNum;
rootObj["page_size"] = pageSize;
2025-01-07 22:13:48 +00:00
QJsonArray orderArray;
QJsonArray orderItem;
orderItem.append("start_time");
orderItem.append("-");
orderArray.append(orderItem);
rootObj["order"] = orderArray;
2024-12-28 17:15:48 +00:00
QJsonObject sensorsObj;
for (auto it = groupsWithSensors.constBegin(); it != groupsWithSensors.constEnd(); ++it) {
QJsonArray sensorArray;
for (QString sensorId : it.value()) {
sensorArray.append(sensorId);
}
sensorsObj[it.key()] = sensorArray;
}
rootObj["sensors"] = sensorsObj;
QJsonDocument doc(rootObj);
QByteArray requestData = doc.toJson();
2025-01-07 22:13:48 +00:00
QJsonObject responseJson = httpClient->post(utils::API_URL + "/alarms/getAlarms",
QJsonDocument::fromJson(requestData).object());
2024-12-28 17:15:48 +00:00
QList<AlarmR> alarmList;
if (responseJson.contains("alarms") && responseJson["alarms"].isArray()) {
QJsonArray alarmsArray = responseJson["alarms"].toArray();
2025-01-07 22:13:48 +00:00
for (const QJsonValue &value : alarmsArray) {
2024-12-28 17:15:48 +00:00
if (value.isObject()) {
AlarmR alarm;
alarm.parseResponse(value.toObject());
alarmList.append(alarm);
}
}
}
return alarmList;
}
private:
2025-01-07 22:13:48 +00:00
HttpClient *httpClient;
2024-12-28 17:15:48 +00:00
};
#endif // ALARMSSERVICE_H