-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregime.cpp
More file actions
54 lines (50 loc) · 1.56 KB
/
Copy pathregime.cpp
File metadata and controls
54 lines (50 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "regime.h"
#include <QJsonObject>
QJsonObject Condition::toJson() const {
QJsonObject json;
json["type"] = type;
json["temp"] = temp;
json["time"] = time;
return json;
}
Condition Condition::fromJson(const QJsonObject &json) {
Condition c;
c.type = json["type"].toString();
// If type is empty or invalid, default to "none"
if (c.type.isEmpty() || (c.type != "none" && c.type != "time" && c.type != "temp")) {
c.type = "none";
}
c.temp = json["temp"].toDouble();
c.time = json["time"].toInt();
return c;
}
QJsonObject Regime::toJson() const {
QJsonObject json;
json["name"] = m_name;
json["condition"] = m_condition.toJson();
json["max_time"] = m_maxTime / 60;
json["note"] = ""; // Add empty note to match original structure
json["repeat"] = m_repeatCount;
if (m_cycleId != -1) {
QJsonObject cycleObj;
cycleObj["id"] = m_cycleId;
cycleObj["cycleRepeat"] = m_cycleRepeat;
json["cycle"] = cycleObj;
} else {
json["cycle"] = QJsonValue();
}
return json;
}
Regime Regime::fromJson(const QJsonObject &json) {
Regime r;
r.m_name = json["name"].toString();
r.m_condition = Condition::fromJson(json["condition"].toObject());
r.m_repeatCount = json["repeat"].toInt();
r.m_maxTime = json["max_time"].toInt() * 60;
if (!json["cycle"].isNull()) {
QJsonObject cycleObj = json["cycle"].toObject();
r.m_cycleId = cycleObj["id"].toInt();
r.m_cycleRepeat = cycleObj["cycleRepeat"].toInt();
}
return r;
}