-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathConfigManager.ino
176 lines (130 loc) · 4.97 KB
/
ConfigManager.ino
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson
#include <FS.h>
bool isConfigLoaded = false;
void loadSettings() {
Serial.println("loadSettings");
ensureSpiffsStarted();
File configFile = SPIFFS.open("/config.json", "r");
if (!configFile) {
config.setDeviceName("XY-Clock");
config.setTimezone("UTC");
Serial.println("Failed to open config file for reading");
saveSettings();
return;
}
DynamicJsonDocument doc(2048);
deserializeJson(doc, configFile);
configFile.close();
// Debug output
serializeJson(doc, Serial);
Serial.println();
loadSettingsFromJson(doc);
Serial.print("Device name: ");
Serial.println(config.getDeviceName());
Serial.print("Timezone: ");
Serial.println(config.getTimezone());
isConfigLoaded = true;
Serial.println("loadSettings complete");
}
// Loads the settings from a JSON document
void loadSettingsFromJson(DynamicJsonDocument json) {
Serial.println("loadSettingsFromJson");
serializeJson(json, Serial);
config.setDeviceName(json["deviceName"]);
config.setTimezone(json["timezone"]);
config.settwelvehourMode(json["twelvehourMode"].as<bool>());
// Set defaults
if (!config.getDeviceName()) {
config.setDeviceName("XY-Clock");
}
if (!config.getTimezone()) {
config.setTimezone("Europe/London");
}
if (!config.gettwelvehourMode()) {
config.settwelvehourMode(false);
}
if (json["dayBrightness"])
{
parseBrightnessAlarmConfig(json["dayBrightness"], config.dayBrightnessAlarm);
}
if (json["nightBrightness"])
{
parseBrightnessAlarmConfig(json["nightBrightness"], config.nightBrightnessAlarm);
}
if (json["alarms"])
{
auto jsonAlarms = json["alarms"].as<JsonArray>();
int index = 0;
for (JsonVariant jsonAlarm : jsonAlarms) {
config.alarms[index].setHour(jsonAlarm["hour"].as<byte>());
config.alarms[index].setMinute(jsonAlarm["minute"].as<byte>());
config.alarms[index].clearDaysOfWeek();
auto jsonDaysOfWeek = jsonAlarm["daysOfWeek"].as<JsonArray>();
int dayIndex = 1;
config.alarms[index].clearDaysOfWeek();
for (JsonVariant jsonDay : jsonDaysOfWeek) {
bool isDayOfWeek = jsonDay.as<bool>();
// Serial.print("Day of week ");
// Serial.print(dayIndex);
// Serial.print(" is ");
// Serial.println(isDayOfWeek);
if (isDayOfWeek) {
config.alarms[index].enableDayOfWeek(dayIndex);
}
dayIndex++;
}
index++;
}
}
}
// Set the brightness alarm config
void parseBrightnessAlarmConfig(JsonObject json, BrightnessAlarm* brightnessAlarm) {
brightnessAlarm->setBrightness(json["brightness"].as<byte>());
brightnessAlarm->setHour(json["hour"].as<byte>());
brightnessAlarm->setMinute(json["minute"].as<byte>());
}
// Converts a config to JSON
DynamicJsonDocument convertConfigToJson() {
Serial.println("convertConfigToJson");
DynamicJsonDocument doc(2048);
doc["deviceName"] = config.getDeviceName();
doc["timezone"] = config.getTimezone();
doc["twelvehourMode"] = config.gettwelvehourMode();
doc["dayBrightness"] = doc.createNestedObject();
doc["dayBrightness"]["hour"] = config.dayBrightnessAlarm->getHour();
doc["dayBrightness"]["minute"] = config.dayBrightnessAlarm->getMinute();
doc["dayBrightness"]["brightness"] = config.dayBrightnessAlarm->getBrightness();
doc["nightBrightness"] = doc.createNestedObject();
doc["nightBrightness"]["hour"] = config.nightBrightnessAlarm->getHour();
doc["nightBrightness"]["minute"] = config.nightBrightnessAlarm->getMinute();
doc["nightBrightness"]["brightness"] = config.nightBrightnessAlarm->getBrightness();
JsonArray alarmsJson = doc.createNestedArray("alarms");
for (Alarm& alarm : config.alarms) {
JsonObject alarmJson = alarmsJson.createNestedObject();
alarmJson["hour"] = alarm.getHour();
alarmJson["minute"] = alarm.getMinute();
JsonArray daysOfWeek = alarmJson.createNestedArray("daysOfWeek");
for (byte day = 1; day <= 7; day++) {
bool isDayOfWeekEnabled = alarm.isForDayOfWeek(day);
daysOfWeek.add(isDayOfWeekEnabled);
}
}
return doc;
}
void saveSettings() {
Serial.println("");
Serial.println("saveSettings");
ensureSpiffsStarted();
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("Failed to open config file for writing");
return;
}
DynamicJsonDocument doc = convertConfigToJson();
serializeJson(doc, configFile);
// Debug output
serializeJson(doc, Serial);
Serial.println();
configFile.close();
Serial.println("Settings saved");
}