#ifndef ALARMSSERVICE_H #define ALARMSSERVICE_H #include "HttpClient.h" #include "alarm_r.h" #include "utils.h" #include #include #include #include #include #include #include struct AlarmSettings{ QMap> groupsWithSensors; qint64 start_time; qint64 end_time; AlarmSettings(QMap> a, qint64 b, qint64 c) { groupsWithSensors = a; start_time = b; end_time = c; } }; class AlarmsService : public QObject { Q_OBJECT public: explicit AlarmsService(HttpClient *client, QObject *parent = nullptr) : QObject(parent) , httpClient(client) {} int getAlarmsAmount(const QMap> &groupsWithSensors, qint64 start, qint64 end) { 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(); QJsonObject responseJson = httpClient->post(utils::API_URL + "/alarms/getAlarmsAmount", QJsonDocument::fromJson(requestData).object()); if (responseJson.contains("amount")) { return responseJson["amount"].toInt(); } return 0; } QList getAlarms(const QMap> &groupsWithSensors, qint64 start, qint64 end, int pageNum, int pageSize) { QJsonObject rootObj; rootObj["start_time"] = start; rootObj["end_time"] = end; rootObj["page_number"] = pageNum; rootObj["page_size"] = pageSize; QJsonArray orderArray; QJsonArray orderItem; orderItem.append("start_time"); orderItem.append("-"); orderArray.append(orderItem); rootObj["order"] = orderArray; 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(); QJsonObject responseJson = httpClient->post(utils::API_URL + "/alarms/getAlarms", QJsonDocument::fromJson(requestData).object()); QList alarmList; if (responseJson.contains("alarms") && responseJson["alarms"].isArray()) { QJsonArray alarmsArray = responseJson["alarms"].toArray(); for (const QJsonValue &value : alarmsArray) { if (value.isObject()) { AlarmR alarm; alarm.parseResponse(value.toObject()); alarmList.append(alarm); } } } return alarmList; } private: HttpClient *httpClient; }; #endif // ALARMSSERVICE_H