-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectConfig.cpp
More file actions
203 lines (177 loc) · 4.98 KB
/
Copy pathprojectConfig.cpp
File metadata and controls
203 lines (177 loc) · 4.98 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
#include "projectConfig.h"
#include <FS.h>
#include "SPIFFS.h"
#include <ArduinoJson.h>
#include "clockFaces.h" // FACE_COUNT - clamp the saved face index
#include "logBuffer.h" // Log
ProjectConfig projectConfig;
String sanitizeHostname(const String &raw)
{
String out;
for (unsigned int i = 0; i < raw.length() && out.length() < 32; i++)
{
char c = raw[i];
if (c >= 'A' && c <= 'Z') c = c - 'A' + 'a';
if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-')
{
out += c;
}
}
while (out.startsWith("-")) out.remove(0, 1);
while (out.endsWith("-")) out.remove(out.length() - 1);
if (out.length() == 0) out = "esp32worldclock";
return out;
}
// 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_HOSTNAME] = c.hostname;
json[PROJECT_NIGHT_START] = c.nightStartHour;
json[PROJECT_NIGHT_END] = c.nightEndHour;
json[PROJECT_NIGHT_BRIGHTNESS] = c.nightBrightness;
}
// 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_BRIGHTNESS))
{
c.brightness = constrain(json[PROJECT_BRIGHTNESS].as<int>(), 1, 255);
any = true;
}
if (json.containsKey(PROJECT_CLOCK_FACE))
{
c.clockFace = constrain(json[PROJECT_CLOCK_FACE].as<int>(), 0, FACE_COUNT - 1);
any = true;
}
if (json.containsKey(PROJECT_HOSTNAME))
{
c.hostname = sanitizeHostname(json[PROJECT_HOSTNAME].as<String>());
any = true;
}
if (json.containsKey(PROJECT_NIGHT_START))
{
c.nightStartHour = constrain(json[PROJECT_NIGHT_START].as<int>(), 0, 23);
any = true;
}
if (json.containsKey(PROJECT_NIGHT_END))
{
c.nightEndHour = constrain(json[PROJECT_NIGHT_END].as<int>(), 0, 23);
any = true;
}
if (json.containsKey(PROJECT_NIGHT_BRIGHTNESS))
{
c.nightBrightness = constrain(json[PROJECT_NIGHT_BRIGHTNESS].as<int>(), 1, 255);
any = true;
}
return any;
}
bool ProjectConfig::fetchConfigFile()
{
if (SPIFFS.exists(PROJECT_CONFIG_JSON))
{
// file exists, reading and loading
Log.println("reading config file");
File configFile = SPIFFS.open(PROJECT_CONFIG_JSON, "r");
if (configFile)
{
Log.println("opened config file");
StaticJsonDocument<2048> json;
DeserializationError error = deserializeJson(json, configFile);
serializeJsonPretty(json, Serial);
if (!error)
{
Log.println("\nparsed json");
applyDoc(*this, json);
return true;
}
else
{
Log.println("failed to load json config");
return false;
}
}
}
Log.println("Config file does not exist");
return false;
}
bool ProjectConfig::saveConfigFile()
{
Log.println(F("Saving config"));
StaticJsonDocument<2048> json;
fillJson(*this, json);
File configFile = SPIFFS.open(PROJECT_CONFIG_JSON, "w");
if (!configFile)
{
Log.println("failed to open config file for writing");
return false;
}
serializeJsonPretty(json, Serial);
if (serializeJson(json, configFile) == 0)
{
Log.println(F("Failed to write to file"));
return false;
}
configFile.close();
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);
}