This repository has been archived by the owner on Mar 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Config.ino
74 lines (66 loc) · 2.75 KB
/
Config.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
bool loadSystemConfig() {
Serial.println("mounting FS...");
if (SPIFFS.begin()) {
Serial.println("mounted file system");
if (SPIFFS.exists("/" + configJsonFile)) {
Serial.println("reading config file");
File configFile = SPIFFS.open("/" + configJsonFile, "r");
if (configFile) {
Serial.println("opened config file");
size_t size = configFile.size();
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
Serial.println("Content of JSON Config-File: /"+configJsonFile);
json.printTo(Serial);
if (json.success()) {
Serial.println("\nJSON OK");
((json["IP"]).as<String>()).toCharArray(WemosNetConfig.IP, IPSIZE);
((json["NETMASK"]).as<String>()).toCharArray(WemosNetConfig.NETMASK, IPSIZE);
((json["GW"]).as<String>()).toCharArray(WemosNetConfig.GW, IPSIZE);
((json["CCUIP"]).as<String>()).toCharArray(HomeMaticConfig.CCUIP, IPSIZE);
((json["VAR_COMMAND"]).as<String>()).toCharArray(HomeMaticConfig.VAR_RFID_command, CCUVARSIZE);
((json["VAR_SCHARFSCHALTBAR"]).as<String>()).toCharArray(HomeMaticConfig.VAR_Alarmanlage_scharfschaltbar, CCUVARSIZE);
((json["VAR_SCHARF"]).as<String>()).toCharArray(HomeMaticConfig.VAR_Alarmanlage_scharf, CCUVARSIZE);
((json["VAR_WIRD_SCHARF"]).as<String>()).toCharArray(HomeMaticConfig.VAR_Alarmanlage_wird_scharf, CCUVARSIZE);
RFID.Mode = json["RFID_MODE"];
} else {
Serial.println("\nERROR loading config");
}
}
return true;
} else {
Serial.println("/" + configJsonFile + " not found.");
return false;
}
SPIFFS.end();
} else {
Serial.println("failed to mount FS");
return false;
}
}
bool saveSystemConfig() {
SPIFFS.begin();
Serial.println("saving config");
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["IP"] = WemosNetConfig.IP;
json["NETMASK"] = WemosNetConfig.NETMASK;
json["GW"] = WemosNetConfig.GW;
json["CCUIP"] = HomeMaticConfig.CCUIP;
json["VAR_COMMAND"] = HomeMaticConfig.VAR_RFID_command;
json["VAR_SCHARFSCHALTBAR"] = HomeMaticConfig.VAR_Alarmanlage_scharfschaltbar;
json["VAR_SCHARF"] = HomeMaticConfig.VAR_Alarmanlage_scharf;
json["VAR_WIRD_SCHARF"] = HomeMaticConfig.VAR_Alarmanlage_wird_scharf;
json["RFID_MODE"] = RFID.Mode;
SPIFFS.remove("/" + configJsonFile);
File configFile = SPIFFS.open("/" + configJsonFile, "w");
if (!configFile) {
Serial.println("failed to open config file for writing");
}
json.printTo(Serial);
json.printTo(configFile);
configFile.close();
SPIFFS.end();
}