61 lines
1.4 KiB
C++
61 lines
1.4 KiB
C++
#include "Config.hpp"
|
|
|
|
#include "FS.h"
|
|
#include "SPIFFS.h"
|
|
Config config;
|
|
|
|
void Config::Init() {
|
|
SPIFFS.begin(true);
|
|
File f = SPIFFS.open("/config.json", FILE_READ);
|
|
if (!f) {
|
|
SetDefaults();
|
|
return;
|
|
}
|
|
DeserializationError err = deserializeJson(conf_json, f);
|
|
if (err == DeserializationError::Ok) {
|
|
Load();
|
|
} else {
|
|
SetDefaults();
|
|
}
|
|
f.close();
|
|
}
|
|
|
|
void Config::SetDefaults() {
|
|
Serial.println("Setting default config");
|
|
// Set default MAC address
|
|
mac_address[0] = 0x34;
|
|
mac_address[1] = 0x85;
|
|
mac_address[2] = 0x18;
|
|
mac_address[3] = 0xa9;
|
|
mac_address[4] = 0x3d;
|
|
mac_address[5] = 0xfc;
|
|
|
|
// Set default beacon ID
|
|
beacon_id = esp_random();
|
|
|
|
// Save the defaults to SPIFFS
|
|
Save();
|
|
}
|
|
|
|
void Config::Save() {
|
|
conf_json.to<JsonObject>();
|
|
JsonArray arr = conf_json["mac"].to<JsonArray>();
|
|
for (int i = 0; i < 6; i++) {
|
|
arr.add<int>(mac_address[i]);
|
|
}
|
|
conf_json["beacon_id"] = beacon_id;
|
|
String out;
|
|
serializeJson(conf_json, out);
|
|
File f = SPIFFS.open("/config.json", FILE_WRITE, true);
|
|
f.print(out);
|
|
f.close();
|
|
Serial.printf("Config saved: %s\n", out.c_str());
|
|
}
|
|
|
|
void Config::Load() {
|
|
JsonArray arr = conf_json["mac"];
|
|
for (int i = 0; i < 6; i++) {
|
|
mac_address[i] = arr[i];
|
|
}
|
|
beacon_id = conf_json["beacon_id"];
|
|
} |