FemaLocalSoftware/savesensorservice.h
lopata29435_NSK 3af06d02ec finish
2025-01-08 05:13:48 +07:00

81 lines
2.4 KiB
C++

#ifndef SAVESENSORSERVICE_H
#define SAVESENSORSERVICE_H
#include "getsensors_r.h"
#include "httpclient.h"
#include "sensorlogs.h"
#include "utils.h"
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QObject>
#include <QString>
class SaveSensorService : public QObject
{
Q_OBJECT
public:
explicit SaveSensorService(HttpClient *client, QObject *parent = nullptr)
: QObject(parent)
, httpClient(client)
{}
bool saveSensorLog(const Sensor &sensor, const SensorLogs &newSensorLogs)
{
QJsonObject responseSettingsJson = httpClient->get(utils::API_URL
+ "/settings/deviceSettings");
QJsonObject sensors = responseSettingsJson["SENSORS"].toObject();
if (!sensors.contains(sensor.group)) {
return false;
}
QJsonObject groupData = sensors[sensor.group].toObject();
if (!groupData.contains(sensor.id)) {
return false;
}
QJsonObject sensorData = groupData[sensor.id].toObject();
sensorData["LOG_ERROR"] = newSensorLogs.getLogError();
sensorData["LOG_MANUAL"] = newSensorLogs.getLogManual();
sensorData["ALARM_SLIPPAGE"] = newSensorLogs.getAlarmSleepage();
sensorData["LOG_HUGE_ERROR"] = newSensorLogs.getLogHugeError();
sensorData["NAME"] = sensor.name;
QJsonObject extraObject;
extraObject["isBool"] = sensor.isBooled;
extraObject["isHide"] = sensor.isHide;
QString extraString = QString(QJsonDocument(extraObject).toJson(QJsonDocument::Compact));
sensorData["EXTRA"] = extraString;
sensorData["UNIT_NAME"] = sensor.unitName;
sensorData["IS_ALARM_ENABLED"] = sensor.isAlarmEnabled;
sensorData["ALARM_UPPERBOUND"] = sensor.alarmUpperBound;
sensorData["ALARM_LOWERBOUND"] = sensor.alarmLowerBound;
sensorData["ALARM_COOLDOWN"] = sensor.alarmCooldown;
groupData[sensor.id] = sensorData;
sensors[sensor.group] = groupData;
responseSettingsJson["SENSORS"] = sensors;
QJsonObject putResponse = httpClient->post(utils::API_URL + "/settings/deviceSettings",
responseSettingsJson);
return putResponse["message"] == "OK";
}
private:
HttpClient *httpClient;
};
#endif // SAVESENSORSERVICE_H