ESP RFID Webui
Architecture and call graph
Loading...
Searching...
No Matches
ConfigManager Class Reference

#include <ConfigManager.h>

Static Public Member Functions

static bool loadConfig (String &ssid, String &pass, String &serverBase)
static bool saveConfig (const String &ssid, const String &pass, const String &serverBase)
static String readConfigJson ()
static void listFiles ()

Static Private Attributes

static const char * CONFIG_FILE = "/config.json"

Detailed Description

Definition at line 5 of file ConfigManager.h.

Member Function Documentation

◆ listFiles()

void ConfigManager::listFiles ( )
static

Definition at line 93 of file ConfigManager.cpp.

93 {
94 File root = LittleFS.open("/");
95 if (!root) {
96 Serial.println("LittleFS root open failed");
97 return;
98 }
99
100 Serial.println("LittleFS contents:");
101 File f = root.openNextFile();
102
103 if (!f) {
104 Serial.println(" (empty)");
105 }
106
107 while (f) {
108 Serial.printf(" %s (%u bytes)\n", f.name(), static_cast<unsigned>(f.size()));
109 f = root.openNextFile();
110 }
111}

◆ loadConfig()

bool ConfigManager::loadConfig ( String & ssid,
String & pass,
String & serverBase )
static

Definition at line 18 of file ConfigManager.cpp.

18 {
19 String json = readConfigJson();
20 if (json.length() == 0) return false;
21
22 JsonDocument doc;
23 DeserializationError err = deserializeJson(doc, json);
24 if (err) {
25 Serial.printf("Config parse error: %s\n", err.c_str());
26 return false;
27 }
28
29 ssid = String(doc["ssid"] | ssid.c_str());
30 pass = String(doc["password"] | pass.c_str());
31 serverBase = String(doc["server_base"] | serverBase.c_str());
32
33 return true;
34}
static String readConfigJson()

References readConfigJson().

Referenced by setup().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readConfigJson()

String ConfigManager::readConfigJson ( )
static

Definition at line 69 of file ConfigManager.cpp.

69 {
70 File f = LittleFS.open(CONFIG_FILE, "r");
71 if (!f) {
72 Serial.println("Config file not found");
73 return {};//string
74 }
75
76 const size_t size = f.size();
77 String contents;
78 contents.reserve(size + 1);
79
80 while (f.available()) {
81 contents += static_cast<char>(f.read());
82 }
83
84 f.close();
85 return contents;
86}
static const char * CONFIG_FILE

References CONFIG_FILE.

Referenced by loadConfig().

Here is the caller graph for this function:

◆ saveConfig()

bool ConfigManager::saveConfig ( const String & ssid,
const String & pass,
const String & serverBase )
static

Definition at line 42 of file ConfigManager.cpp.

42 {
43 JsonDocument doc;
44 doc["ssid"] = ssid;
45 doc["password"] = pass;
46 doc["server_base"] = serverBase;
47
48 File f = LittleFS.open(CONFIG_FILE, "w");
49 if (!f) {
50 Serial.println("Failed to open config file for writing");
51 return false;
52 }
53
54 if (serializeJson(doc, f) == 0) {
55 f.close();
56 Serial.println("Failed to write config JSON");
57 return false;
58 }
59
60 f.close();
61 return true;
62}

References CONFIG_FILE.

Member Data Documentation

◆ CONFIG_FILE

const char * ConfigManager::CONFIG_FILE = "/config.json"
staticprivate

Definition at line 20 of file ConfigManager.h.

Referenced by readConfigJson(), and saveConfig().


The documentation for this class was generated from the following files: