-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrules.cpp
149 lines (110 loc) · 3.19 KB
/
rules.cpp
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
144
145
146
147
148
#include "rules.h"
StaticJsonBuffer<JSON_BUFFER_SIZE> jsonBuffer;
JsonObject* jsonRules = NULL;
bool bRulesModified = true;
bool rules_load(){
//minimize file access
if (!rules_modified()) return true;
if (SPIFFS.exists(RULES_FILE)) {
File file = SPIFFS.open(RULES_FILE, "r");
jsonBuffer.clear();
JsonObject& l_jsonToRet = jsonBuffer.parseObject(file);
jsonRules = &l_jsonToRet;
if (!l_jsonToRet.success()){
DBG_OUTPUT_PORT.println(F("rules_load()|Failed to read rules file!"));
file.close();
return false;
}
file.close();
rules_set_modified(false);
return true;
}
else{
DBG_OUTPUT_PORT.print(F("rules_load()|Unable To Find rules file!"));
DBG_OUTPUT_PORT.println(RULES_FILE);
DBG_OUTPUT_PORT.println();
return false;
}
return false;
}
uint8_t rules_add(const int x_hour, const int x_minute, bool x_action){
if (!rules_load()){
DBG_OUTPUT_PORT.println(F("rules_add()|Can't load rules!"));
return RULES_ADD_LOAD_FAILED;
}
int l_nIndex = 1;
//get the first rules free index
while(jsonRules->containsKey("rule"+String(l_nIndex))){
l_nIndex++;
}
//check if rules are all used
if (l_nIndex > MAX_RULES_NUM){
DBG_OUTPUT_PORT.println(F("rules_add()|max rules num excedeed!"));
return RULES_ADD_MAX_RULES;
}
//check if exists another rule at the same time
for(size_t l_nIndex = 1; l_nIndex < MAX_RULES_NUM; l_nIndex++){
if(jsonRules->containsKey("rule"+String(l_nIndex))){
JsonVariant l_rule = jsonRules->get<JsonVariant>("rule"+String(l_nIndex));
int l_ruleHour = atoi(l_rule["hour"]);
int l_ruleMinute = atoi(l_rule["minute"]);
if (l_ruleHour == x_hour && l_ruleMinute == x_minute) return RULES_ADD_ALREADY_EXISTS;
}
}
JsonObject& l_newRule = jsonRules->createNestedObject("rule"+String(l_nIndex));
l_newRule["hour"] = x_hour;
l_newRule["minute"] = x_minute;
l_newRule["action"] = x_action;
bRulesModified = true;
return RULES_ADD_OK;
}
bool rules_remove(const String x_name){
if (!rules_load()){
DBG_OUTPUT_PORT.println(F("rules_remove()|Can't load rules!"));
return false;
}
jsonRules->remove(x_name);
if(!rules_save()){
DBG_OUTPUT_PORT.println(F("rules_remove()|Can't save rules!"));
return false;
}
bRulesModified = true;
return true;
}
bool rules_save(){
SPIFFS.remove(RULES_FILE);
File file = SPIFFS.open(RULES_FILE, "w");
if (!file){
DBG_OUTPUT_PORT.println(F("rules_save()|Failed to create rules file!"));
return false;
}
if (jsonRules->prettyPrintTo(file) == 0){
DBG_OUTPUT_PORT.println(F("rules_save()|Failed to write to rules file!"));
}
file.close();
return true;
}
bool rules_modified(){
return bRulesModified;
}
void rules_set_modified(bool x_modified){
bRulesModified = x_modified;
}
void rules_dbg_print(){
if (!rules_load()){
DBG_OUTPUT_PORT.println(F("rules_dbg_print()|Can't load rules!"));
return;
}
jsonRules->prettyPrintTo(DBG_OUTPUT_PORT);
DBG_OUTPUT_PORT.println(F(""));
}
bool rules_get_list(String& x_sJsonRules){
//DBG_OUTPUT_PORT.println(F("rules_get()|execute"));
if (!rules_load()){
DBG_OUTPUT_PORT.println(F("rules_get()|Can't load rules!"));
x_sJsonRules = "{}";
return false;
}
jsonRules->prettyPrintTo(x_sJsonRules);
return true;
}