-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectConfig.cpp
More file actions
314 lines (282 loc) · 9.17 KB
/
Copy pathprojectConfig.cpp
File metadata and controls
314 lines (282 loc) · 9.17 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include "projectConfig.h"
#include <FS.h>
#include "SPIFFS.h"
#include <ArduinoJson.h>
#include "brightness.h"
#include "clockFaces.h" // FACE_COUNT - clamp the saved face index
#include "logBuffer.h" // Log
#include "textSanitize.h" // puretext::sanitizeHostname - host-tested core
ProjectConfig projectConfig;
static const char *PROJECT_CONFIG_TMP_JSON = "/project_config.tmp";
static const char *PROJECT_CONFIG_BAK_JSON = "/project_config.bak";
// Thin String wrapper over the host-tested puretext::sanitizeHostname.
String sanitizeHostname(const String &raw)
{
return String(puretext::sanitizeHostname(raw.c_str()).c_str());
}
// Serialize all settings into json. Shared by saveConfigFile and the
// /api/config backup so the two can never drift apart.
static void fillJson(ProjectConfig &c, JsonDocument &json)
{
json[PROJECT_TIME_ZONE_LABEL] = c.timeZone;
json[PROJECT_TIME_TWENTY_FOUR_HOUR] = c.twentyFourHour;
json[PROJECT_TIME_US_DATE] = c.usDateFormat;
for (int i = 0; i < 4; i++)
{
json[String(PROJECT_ZONE_NAME_PREFIX) + String(i)] = c.zoneName[i];
json[String(PROJECT_ZONE_TZ_PREFIX) + String(i)] = c.zoneTZ[i];
}
json[PROJECT_BRIGHTNESS] = c.brightness;
json[PROJECT_CLOCK_FACE] = c.clockFace;
json[PROJECT_SHOW_GRID] = c.showGrid;
json[PROJECT_HOSTNAME] = c.hostname;
json[PROJECT_MAC_OVERRIDE] = c.staMacOverride;
json[PROJECT_AUTO_BRIGHTNESS] = c.autoBrightness;
json[PROJECT_NIGHT_START] = c.nightStartHour;
json[PROJECT_NIGHT_END] = c.nightEndHour;
json[PROJECT_NIGHT_BRIGHTNESS] = c.nightBrightness;
json[PROJECT_SMOOTH_FONT] = c.smoothTimeFont;
json[PROJECT_DAYNIGHT_ICONS] = c.dayNightIcons;
json[PROJECT_HOME_MARKER] = c.homeMarker;
json[PROJECT_QUAD_WEATHER] = c.quadWeather;
json[PROJECT_DAYLIGHT_BAR] = c.daylightBar;
json[PROJECT_MARKET_BAR] = c.marketProgressBar;
json[PROJECT_WEATHER_ALERTS] = c.weatherAlerts;
json[PROJECT_USE_FAHRENHEIT] = c.useFahrenheit;
json[PROJECT_FLIP_DISPLAY] = c.flipDisplay;
json[PROJECT_WEEK_START_MONDAY] = c.weekStartMonday;
json[PROJECT_WEATHER_REFRESH_MIN] = c.weatherRefreshMin;
json[PROJECT_TIMER_REMINDER_MIN] = c.timerReminderMin;
json[PROJECT_COUNTDOWN_DEFAULT_MIN] = c.countdownDefaultMin;
json[PROJECT_TIMER_HIDE_SECONDS] = c.timerHideSeconds;
// Written only once a real calibration exists, so a missing key keeps
// meaning "never calibrated" across config backup/restore.
if (c.touchCalSet)
{
JsonArray cal = json.createNestedArray(PROJECT_TOUCH_CAL);
for (int i = 0; i < 5; i++)
{
cal.add(c.touchCal[i]);
}
}
}
// Apply json onto the settings; missing keys keep their current values and
// out-of-range values are clamped. Shared by fetchConfigFile and the
// /api/config restore. Returns true if at least one known key was present.
static bool applyDoc(ProjectConfig &c, JsonDocument &json)
{
bool any = false;
if (json.containsKey(PROJECT_TIME_ZONE_LABEL))
{
c.timeZone = String(json[PROJECT_TIME_ZONE_LABEL].as<String>());
any = true;
}
if (json.containsKey(PROJECT_TIME_TWENTY_FOUR_HOUR))
{
c.twentyFourHour = json[PROJECT_TIME_TWENTY_FOUR_HOUR].as<bool>();
any = true;
}
if (json.containsKey(PROJECT_TIME_US_DATE))
{
c.usDateFormat = json[PROJECT_TIME_US_DATE].as<bool>();
any = true;
}
for (int i = 0; i < 4; i++)
{
String nameKey = String(PROJECT_ZONE_NAME_PREFIX) + String(i);
String tzKey = String(PROJECT_ZONE_TZ_PREFIX) + String(i);
if (json.containsKey(nameKey))
{
c.zoneName[i] = json[nameKey].as<String>();
any = true;
}
if (json.containsKey(tzKey))
{
c.zoneTZ[i] = json[tzKey].as<String>();
any = true;
}
}
if (json.containsKey(PROJECT_SHOW_GRID))
{
c.showGrid = json[PROJECT_SHOW_GRID].as<bool>();
any = true;
}
if (json.containsKey(PROJECT_HOSTNAME))
{
c.hostname = sanitizeHostname(json[PROJECT_HOSTNAME].as<String>());
any = true;
}
if (json.containsKey(PROJECT_MAC_OVERRIDE))
{
// Same canonicalisation the web settings form applies, so a restored
// /api/config backup can't smuggle in an un-normalized value (which
// would then flow into logs and the setup-hotspot SSID).
c.staMacOverride = String(
puretext::normalizeMac(json[PROJECT_MAC_OVERRIDE].as<String>().c_str()).c_str());
any = true;
}
if (json.containsKey(PROJECT_AUTO_BRIGHTNESS))
{
c.autoBrightness = json[PROJECT_AUTO_BRIGHTNESS].as<bool>();
any = true;
}
// Clamped integer settings - every numeric field is range-checked here so
// both the settings form and a restored backup land on valid values.
struct { const char *key; int *value; int lo, hi; } clampedInts[] = {
{PROJECT_BRIGHTNESS, &c.brightness, BRIGHTNESS_MIN, BRIGHTNESS_MAX},
{PROJECT_CLOCK_FACE, &c.clockFace, 0, FACE_COUNT - 1},
{PROJECT_NIGHT_START, &c.nightStartHour, 0, 23},
{PROJECT_NIGHT_END, &c.nightEndHour, 0, 23},
{PROJECT_NIGHT_BRIGHTNESS, &c.nightBrightness, BRIGHTNESS_MIN, BRIGHTNESS_MAX},
{PROJECT_WEATHER_REFRESH_MIN, &c.weatherRefreshMin, 5, 120},
{PROJECT_TIMER_REMINDER_MIN, &c.timerReminderMin, 1, 1440},
// 5999 min = 99:59, matching the countdown's 99:59:59 ceiling
{PROJECT_COUNTDOWN_DEFAULT_MIN, &c.countdownDefaultMin, 1, 5999},
};
for (auto &f : clampedInts)
{
if (json.containsKey(f.key))
{
*f.value = constrain(json[f.key].as<int>(), f.lo, f.hi);
any = true;
}
}
// Bool toggles (home-screen extras + display/weather preferences)
struct { const char *key; bool *value; } toggles[] = {
{PROJECT_SMOOTH_FONT, &c.smoothTimeFont},
{PROJECT_DAYNIGHT_ICONS, &c.dayNightIcons},
{PROJECT_HOME_MARKER, &c.homeMarker},
{PROJECT_QUAD_WEATHER, &c.quadWeather},
{PROJECT_DAYLIGHT_BAR, &c.daylightBar},
{PROJECT_MARKET_BAR, &c.marketProgressBar},
{PROJECT_WEATHER_ALERTS, &c.weatherAlerts},
{PROJECT_USE_FAHRENHEIT, &c.useFahrenheit},
{PROJECT_FLIP_DISPLAY, &c.flipDisplay},
{PROJECT_WEEK_START_MONDAY, &c.weekStartMonday},
{PROJECT_TIMER_HIDE_SECONDS, &c.timerHideSeconds},
};
for (auto &toggle : toggles)
{
if (json.containsKey(toggle.key))
{
*toggle.value = json[toggle.key].as<bool>();
any = true;
}
}
if (json.containsKey(PROJECT_TOUCH_CAL))
{
JsonArray cal = json[PROJECT_TOUCH_CAL].as<JsonArray>();
if (cal.size() == 5)
{
for (int i = 0; i < 5; i++)
{
c.touchCal[i] = cal[i].as<uint16_t>();
}
c.touchCalSet = true;
any = true;
}
}
return any;
}
static bool loadConfigPath(ProjectConfig &c, const char *path)
{
// file exists, reading and loading
Log.println("reading config file");
File configFile = SPIFFS.open(path, "r");
if (configFile)
{
Log.println("opened config file");
StaticJsonDocument<2048> json;
DeserializationError error = deserializeJson(json, configFile);
serializeJsonPretty(json, Serial);
configFile.close();
if (!error)
{
Log.println("\nparsed json");
if (applyDoc(c, json))
{
return true;
}
Log.println("config contained no known settings");
return false;
}
Log.println("failed to load json config");
}
return false;
}
bool ProjectConfig::fetchConfigFile()
{
if (SPIFFS.exists(PROJECT_CONFIG_JSON) && loadConfigPath(*this, PROJECT_CONFIG_JSON))
{
return true;
}
if (SPIFFS.exists(PROJECT_CONFIG_BAK_JSON))
{
Log.println("loading backup config");
if (loadConfigPath(*this, PROJECT_CONFIG_BAK_JSON))
{
return true;
}
}
Log.println("No usable config file found");
return false;
}
bool ProjectConfig::saveConfigFile()
{
Log.println(F("Saving config"));
StaticJsonDocument<2048> json;
fillJson(*this, json);
SPIFFS.remove(PROJECT_CONFIG_TMP_JSON);
File configFile = SPIFFS.open(PROJECT_CONFIG_TMP_JSON, "w");
if (!configFile)
{
Log.println("failed to open temp config file for writing");
return false;
}
serializeJsonPretty(json, Serial);
if (serializeJson(json, configFile) == 0)
{
Log.println(F("Failed to write to file"));
configFile.close();
SPIFFS.remove(PROJECT_CONFIG_TMP_JSON);
return false;
}
configFile.close();
SPIFFS.remove(PROJECT_CONFIG_BAK_JSON);
bool hadConfig = SPIFFS.exists(PROJECT_CONFIG_JSON);
if (hadConfig && !SPIFFS.rename(PROJECT_CONFIG_JSON, PROJECT_CONFIG_BAK_JSON))
{
Log.println(F("Failed to back up existing config"));
SPIFFS.remove(PROJECT_CONFIG_TMP_JSON);
return false;
}
if (!SPIFFS.rename(PROJECT_CONFIG_TMP_JSON, PROJECT_CONFIG_JSON))
{
Log.println(F("Failed to replace config"));
if (hadConfig && SPIFFS.exists(PROJECT_CONFIG_BAK_JSON))
{
SPIFFS.rename(PROJECT_CONFIG_BAK_JSON, PROJECT_CONFIG_JSON);
}
SPIFFS.remove(PROJECT_CONFIG_TMP_JSON);
return false;
}
SPIFFS.remove(PROJECT_CONFIG_BAK_JSON);
return true;
}
String ProjectConfig::toJsonString()
{
StaticJsonDocument<2048> json;
fillJson(*this, json);
String out;
serializeJsonPretty(json, out);
return out;
}
bool ProjectConfig::applyFromJsonString(const String &body)
{
StaticJsonDocument<2048> json;
if (deserializeJson(json, body))
{
return false;
}
return applyDoc(*this, json);
}