-
Notifications
You must be signed in to change notification settings - Fork 7
/
Config.h
75 lines (59 loc) · 1.71 KB
/
Config.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
#pragma once
#include <ArduinoJson.h>
#include <FS.h>
namespace Config {
char mqtt_server[80] = "example.tld";
char username[24] = "";
char password[65] = "";
bool imperial_temp = false;
bool imperial_qfe = false;
bool imperial_alt = false;
bool offline_mode = false;
void save() {
DynamicJsonDocument json(512);
json["mqtt_server"] = mqtt_server;
json["username"] = username;
json["password"] = password;
json["imperial_temp"] = imperial_temp;
json["imperial_qfe"] = imperial_qfe;
json["imperial_alt"] = imperial_alt;
json["offline_mode"] = offline_mode;
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
return;
}
serializeJson(json, configFile);
configFile.close();
}
void load() {
if (SPIFFS.begin()) {
if (SPIFFS.exists("/config.json")) {
File configFile = SPIFFS.open("/config.json", "r");
if (configFile) {
const size_t size = configFile.size();
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonDocument json(512);
if (DeserializationError::Ok == deserializeJson(json, buf.get())) {
strcpy(mqtt_server, json["mqtt_server"]);
strcpy(username, json["username"]);
strcpy(password, json["password"]);
imperial_temp = json["imperial_temp"];
imperial_qfe = json["imperial_qfe"];
imperial_alt = json["imperial_alt"];
offline_mode = json["offline_mode"];
}
}
}
}
}
void reset() {
if (SPIFFS.begin()) {
if (SPIFFS.exists("/config.json")) {
File configFile = SPIFFS.open("/config.json", "w");
configFile.println("");
configFile.close();
}
}
}
} // namespace Config