FemaLocalSoftware/savesensorservice.h

77 lines
2.3 KiB
C
Raw Normal View History

2024-12-16 08:38:04 +00:00
#ifndef SAVESENSORSERVICE_H
#define SAVESENSORSERVICE_H
#include "utils.h"
#include "getsensors_r.h"
#include "sensorlogs.h"
#include "httpclient.h"
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QString>
#include <QFile>
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