-
Notifications
You must be signed in to change notification settings - Fork 2
/
salusdatamodel.h
143 lines (106 loc) · 5.06 KB
/
salusdatamodel.h
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#ifndef SALUSDATAMODEL_H
#define SALUSDATAMODEL_H
#include <tuple>
#include <QScxmlCppDataModel>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QtMath>
static const int g_MAX_PROGRAM_ITEMS_COUNT = 5;
typedef std::tuple<int/*Hour*/,int/*Minute*/,double/*Temperature*/> ProgramItem;
typedef enum {Hour,Minute,Temperature} ItemType;
typedef enum {WorkDays, WeekEnds} WeekType;
static const int g_MAX_WEEK_TYPE_COUNT = 2;
typedef enum {tmdNormal, tmdTemporary, tmdFreeze} TemperatureMode;
class SalusDataModel : public QScxmlCppDataModel
{
Q_OBJECT
Q_SCXML_DATAMODEL
public:
SalusDataModel(QObject *parent = nullptr);
/*
* we will remember the current state of inputs here
* later we will use it, when we check a couple of buttons pressed
*/
QVariantMap inputs;
/* Program Record depending on current day and time settings */
std::pair<int/*Number*/,ProgramItem> currentProgramItem() const;
/* Temporary Temperature Setup */
inline int getTemporaryTemperatureInt() { return qFloor(_temporaryTemperature); }
inline int getTemporaryTemperatureFrac() { return (_temporaryTemperature - getTemporaryTemperatureInt())*10.0f; }
inline void incrementTemporaryTemperature() { if (_temporaryTemperature < 30.0) _temporaryTemperature+= 0.5; }
inline void decrementTemporaryTemperature() { if (_temporaryTemperature < 30.0) _temporaryTemperature-= 0.5; }
inline TemperatureMode temperatureMode() const { return _temperatureMode; }
inline double temporaryTemperature() const { return _temporaryTemperature; }
inline double freezeTemperature() const { return _freezeTemperature; }
inline bool isFreezeMode() const { return _temperatureMode==tmdFreeze; }
void resetTemporaryTemperature();
inline void setNormalTemperatureMode() { _temperatureMode=tmdNormal; }
inline void setTemporaryTemperatureMode() { _temperatureMode=tmdTemporary; }
inline void flipFreezeTemperatureMode() { _temperatureMode=_temperatureMode==tmdFreeze ? tmdNormal : tmdFreeze; }
/* Day and Time Setup */
inline int hour() const { return _hour; }
inline int minute() const { return _minute; }
inline int day() const { return _day; }
void incrementHour();
void decrementHour();
void incrementMinute();
void decrementMinute();
void incrementDay();
void decrementDay();
/* Program Setup */
// there can be 5/2 mode or 7 mode
// in this program we are simulating 5/2 mode, so just simply invert
inline void incrementProgramDay() { _programRecord.first = _programRecord.first == WorkDays ? WeekEnds : WorkDays; }
inline void decrementProgramDay() { incrementProgramDay(); }
void incrementProgramHour();
void decrementProgramHour();
void incrementProgramMinute();
void decrementProgramMinute();
void incrementProgramTemperature();
void decrementProgramTemperature();
void incrementProgramNumber();
void decrementProgramNumber();
inline void resetProgramRecord() { _programRecord = std::make_pair(WorkDays,0); }
inline WeekType getProgramWeekType() const { return _programRecord.first; }
inline int getProgramNumber() const { return _programRecord.second; }
inline const ProgramItem &programRecordToProgramItem() const {
return _program[_programRecord.first][_programRecord.second]; }
/* HARD RESET */
void resetToFactorySettings();
void loadFromFile(const QString &sFileName);
void saveToFile(const QString &sFileName);
private:
ProgramItem _program[g_MAX_WEEK_TYPE_COUNT][g_MAX_PROGRAM_ITEMS_COUNT];
int _hour = 12; // 0...23
int _minute = 0; // 0...59
int _day = 0; // Day of week: 0 ... 6
TemperatureMode _temperatureMode = tmdNormal;
double _temporaryTemperature = 15.0f;
const double _freezeTemperature = 5.0f;
std::pair<WeekType,int/*ProgramNumber*/> _programRecord;
/* Serialization */
void loadFromJSON(const QJsonDocument &program);
void saveToJSON(QJsonDocument &program);
/* Default */
const QString defaultSettings = "{\n"
" \"Hour\" : 12,\n"
" \"Minute\" : 0,\n"
" \"Day\": 0,\n"
" \"WorkDays\": [\n"
" [ 6, 0, 22 ],\n"
" [ 12, 0, 22 ],\n"
" [ 16, 0, 22 ],\n"
" [ 18, 0, 22 ],\n"
" [ 22, 0, 22 ]\n"
" ],\n"
" \"WeekEnds\": [\n"
" [ 6, 0, 22 ],\n"
" [ 12, 0, 22 ],\n"
" [ 16, 0, 22 ],\n"
" [ 18, 0, 22 ],\n"
" [ 22, 0, 22 ]\n"
" ]\n"
"}";
};
#endif // SALUSDATAMODEL_H