Load all settings before the setup functions

This commit is contained in:
fvanroie 2020-03-29 03:10:37 +02:00
parent 33b62e3583
commit 95a7ed74e1
8 changed files with 87 additions and 37 deletions

View File

@ -174,13 +174,44 @@ void configWriteConfig()
} }
} }
void configSetup(JsonDocument & settings) void configSetup(JsonObject dummy)
{ {
if(!SPIFFS.begin()) { if(!SPIFFS.begin()) {
Log.error(F("FILE: SPI flash init failed. Unable to mount FS.")); Log.error(F("FILE: SPI flash init failed. Unable to mount FS: Using default settings..."));
} else { } else {
DynamicJsonDocument settings(1024 + 128);
configGetConfig(settings, true); configGetConfig(settings, true);
Log.verbose(F("Loading debug settings"));
debugSetConfig(settings[F("debug")]);
Log.verbose(F("Loading GUI settings"));
guiSetConfig(settings[F("gui")]);
Log.verbose(F("Loading HASP settings"));
haspSetConfig(settings[F("hasp")]);
// otaGetConfig(settings[F("ota")]);
#if HASP_USE_WIFI
Log.verbose(F("Loading WiFi settings"));
wifiSetConfig(settings[F("wifi")]);
#if HASP_USE_MQTT
Log.verbose(F("Loading MQTT settings"));
mqttSetConfig(settings[F("mqtt")]);
#endif
#if HASP_USE_TELNET
Log.verbose(F("Loading Telnet settings"));
telnetSetConfig(settings[F("telnet")]);
#endif
#if HASP_USE_MDNS
Log.verbose(F("Loading MDNS settings"));
mdnsSetConfig(settings[F("mdns")]);
#endif
#if HASP_USE_HTTP
Log.verbose(F("Loading HTTP settings"));
httpSetConfig(settings[F("http")]);
#endif
#endif
} }
Log.notice(F("User configuration loaded"));
} }
void configOutput(const JsonObject & settings) void configOutput(const JsonObject & settings)

View File

@ -32,7 +32,7 @@ const char F_DEBUG_TELEPERIOD[] PROGMEM = "teleperiod";
const char HASP_CONFIG_FILE[] PROGMEM = "/config.json"; const char HASP_CONFIG_FILE[] PROGMEM = "/config.json";
void configSetup(JsonDocument & settings); void configSetup(JsonObject settings);
void configStop(void); void configStop(void);
void configSetConfig(JsonObject & settings); void configSetConfig(JsonObject & settings);

View File

@ -16,7 +16,7 @@ void guiTakeScreenshot(ESP8266WebServer & client);
void guiTakeScreenshot(WebServer & client); void guiTakeScreenshot(WebServer & client);
#endif // ESP32 #endif // ESP32
void guiSetup(JsonObject settings); void guiSetup(const JsonObject & settings);
void guiLoop(void); void guiLoop(void);
void guiStop(void); void guiStop(void);

View File

@ -541,7 +541,7 @@ static inline String httpGetNodename()
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
bool httpIsAuthenticated(const String & page) bool httpIsAuthenticated(const __FlashStringHelper * page)
{ {
if(httpPassword[0] != '\0') { // Request HTTP auth if httpPassword is set if(httpPassword[0] != '\0') { // Request HTTP auth if httpPassword is set
if(!webServer.authenticate(httpUser, httpPassword)) { if(!webServer.authenticate(httpUser, httpPassword)) {
@ -551,7 +551,7 @@ bool httpIsAuthenticated(const String & page)
} }
{ {
Log.verbose(F("HTTP: Sending %s page to client connected from: %s"), page.c_str(), Log.verbose(F("HTTP: Sending %s page to client connected from: %s"), page,
webServer.client().remoteIP().toString().c_str()); webServer.client().remoteIP().toString().c_str());
} }
return true; return true;
@ -982,10 +982,7 @@ bool handleFileRead(String path)
{ {
if(!httpIsAuthenticated(F("fileread"))) return false; if(!httpIsAuthenticated(F("fileread"))) return false;
// path = urldecode(path).substring(0, 31); path = webServer.urlDecode(path).substring(0, 31);
path = webServer.urlDecode(path);
if(!httpIsAuthenticated(path)) return false;
if(path.endsWith("/")) { if(path.endsWith("/")) {
path += F("index.htm"); path += F("index.htm");
} }
@ -1829,10 +1826,25 @@ void httpHandleResetConfig()
} }
} }
void webStart()
{
webServer.begin();
webServerStarted = true;
Log.notice(F("HTTP: Server started @ http://%s"),
(WiFi.getMode() == WIFI_AP ? WiFi.softAPIP().toString().c_str() : WiFi.localIP().toString().c_str()));
}
void webStop()
{
webServer.stop();
webServerStarted = false;
Log.warning(F("HTTP: Server stoped"));
}
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
void httpSetup(const JsonObject & settings) void httpSetup(const JsonObject & settings)
{ {
httpSetConfig(settings); // httpSetConfig(settings);
if(WiFi.getMode() == WIFI_AP) { if(WiFi.getMode() == WIFI_AP) {
Log.notice(F("HTTP: Wifi access point")); Log.notice(F("HTTP: Wifi access point"));
@ -1905,8 +1917,8 @@ void httpSetup(const JsonObject & settings)
webServer.on(F("/config"), webHandleConfig); webServer.on(F("/config"), webHandleConfig);
webServer.onNotFound(httpHandleNotFound); webServer.onNotFound(httpHandleNotFound);
httpReconnect();
Log.verbose(F("HTTP: Setup Complete")); Log.verbose(F("HTTP: Setup Complete"));
webStart();
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
@ -1915,15 +1927,9 @@ void httpReconnect()
if(!httpEnable) return; if(!httpEnable) return;
if(webServerStarted) { if(webServerStarted) {
webServer.stop(); webStop();
webServerStarted = false;
Log.warning(F("HTTP: Server stoped"));
} else if(WiFi.status() == WL_CONNECTED || WiFi.getMode() == WIFI_AP) { } else if(WiFi.status() == WL_CONNECTED || WiFi.getMode() == WIFI_AP) {
webServer.begin(); webStart();
webServerStarted = true;
Log.notice(
F("HTTP: Server started @ http://%s"),
(WiFi.getMode() == WIFI_AP ? WiFi.softAPIP().toString().c_str() : WiFi.localIP().toString().c_str()));
} }
} }

View File

@ -22,7 +22,7 @@ uint8_t mdnsEnabled = true;
void mdnsSetup(const JsonObject & settings) void mdnsSetup(const JsonObject & settings)
{ {
mdnsSetConfig(settings); // mdnsSetConfig(settings);
Log.verbose(F("MDNS: Setup Complete")); Log.verbose(F("MDNS: Setup Complete"));
} }
@ -43,7 +43,6 @@ void mdnsStart()
HASP_VERSION_REVISION); HASP_VERSION_REVISION);
MDNS.addServiceTxt(hasp2Node, "tcp", "app_version", buffer); */ MDNS.addServiceTxt(hasp2Node, "tcp", "app_version", buffer); */
if(MDNS.begin(hasp2Node.c_str())) { if(MDNS.begin(hasp2Node.c_str())) {
Log.notice(F("MDNS: Responder started"));
MDNS.addService(F("http"), F("tcp"), 80); MDNS.addService(F("http"), F("tcp"), 80);
MDNS.addService(F("telnet"), F("tcp"), 23); MDNS.addService(F("telnet"), F("tcp"), 23);
MDNS.addServiceTxt(hasp2Node, F("tcp"), F("app_name"), F("HASP-lvgl")); MDNS.addServiceTxt(hasp2Node, F("tcp"), F("app_name"), F("HASP-lvgl"));
@ -53,6 +52,7 @@ void mdnsStart()
addServiceTxt("arduino", "tcp", "ssh_upload", "no"); addServiceTxt("arduino", "tcp", "ssh_upload", "no");
addServiceTxt("arduino", "tcp", "board", ARDUINO_BOARD); addServiceTxt("arduino", "tcp", "board", ARDUINO_BOARD);
addServiceTxt("arduino", "tcp", "auth_upload", (auth) ? "yes" : "no");*/ addServiceTxt("arduino", "tcp", "auth_upload", (auth) ? "yes" : "no");*/
Log.notice(F("MDNS: Responder started"));
} else { } else {
Log.error(F("MDNS: Responder failed to start %s"), hasp2Node.c_str()); Log.error(F("MDNS: Responder failed to start %s"), hasp2Node.c_str());
}; };

View File

@ -44,7 +44,7 @@ void spiffsSetup()
#endif #endif
Log.error(F("FILE: SPI flash init failed. Unable to mount FS.")); Log.error(F("FILE: SPI flash init failed. Unable to mount FS."));
} else { } else {
Log.notice(F("FILE: SPI Flash FS mounted")); Log.verbose(F("FILE: SPI Flash FS mounted"));
} }
#endif #endif
} }

View File

@ -4,7 +4,7 @@
#include "ArduinoJson.h" #include "ArduinoJson.h"
void wifiSetup(JsonObject settings); void wifiSetup(JsonObject settings);
bool wifiLoop(void); bool wifiEvery5Seconds(void);
void wifiStop(void); void wifiStop(void);
bool wifiGetConfig(const JsonObject & settings); bool wifiGetConfig(const JsonObject & settings);

View File

@ -54,6 +54,10 @@ unsigned long mainLastLoopTime = 0;
void setup() void setup()
{ {
/****************************
* Constant initialzations
***************************/
#if defined(ARDUINO_ARCH_ESP8266) #if defined(ARDUINO_ARCH_ESP8266)
pinMode(D1, OUTPUT); pinMode(D1, OUTPUT);
pinMode(D2, INPUT_PULLUP); pinMode(D2, INPUT_PULLUP);
@ -68,20 +72,29 @@ void setup()
spiffsSetup(); spiffsSetup();
#endif #endif
/* Read Config File */
DynamicJsonDocument settings(1024 + 512);
configSetup(settings);
#if HASP_USE_SDCARD #if HASP_USE_SDCARD
sdcardSetup(); sdcardSetup();
#endif #endif
/****************************
* Read & Apply User Configuration
***************************/
DynamicJsonDocument settings(128);
configSetup(settings[F("debug")]);
/****************************
* Apply User Configuration
***************************/
debugSetup(settings[F("debug")]); debugSetup(settings[F("debug")]);
/* Init Graphics */ /* Init Graphics */
// TFT_eSPI screen = TFT_eSPI(); // TFT_eSPI screen = TFT_eSPI();
guiSetup(settings[F("gui")]); guiSetup(settings[F("gui")]);
#if HASP_USE_WIFI
wifiSetup(settings[F("wifi")]);
#endif
/* Init GUI Application */ /* Init GUI Application */
haspSetup(settings[F("hasp")]); haspSetup(settings[F("hasp")]);
@ -89,6 +102,10 @@ void setup()
#if HASP_USE_WIFI #if HASP_USE_WIFI
wifiSetup(settings[F("wifi")]); wifiSetup(settings[F("wifi")]);
#if HASP_USE_HTTP
httpSetup(settings[F("http")]);
#endif
#if HASP_USE_MQTT #if HASP_USE_MQTT
mqttSetup(settings[F("mqtt")]); mqttSetup(settings[F("mqtt")]);
#endif #endif
@ -101,19 +118,15 @@ void setup()
mdnsSetup(settings[F("mdns")]); mdnsSetup(settings[F("mdns")]);
#endif #endif
#if HASP_USE_HTTP
httpSetup(settings[F("http")]);
#endif
#if HASP_USE_BUTTON
buttonSetup();
#endif
#if HASP_USE_OTA #if HASP_USE_OTA
otaSetup(settings[F("ota")]); otaSetup(settings[F("ota")]);
#endif #endif
#endif // WIFI #endif // WIFI
#if HASP_USE_BUTTON
buttonSetup();
#endif
} }
void loop() void loop()
@ -183,7 +196,7 @@ void loop()
/* Run Every 5 Seconds */ /* Run Every 5 Seconds */
if(mainLoopCounter == 0 || mainLoopCounter == 5) { if(mainLoopCounter == 0 || mainLoopCounter == 5) {
httpEvery5Seconds(); httpEvery5Seconds();
isConnected = wifiLoop(); isConnected = wifiEvery5Seconds();
mqttEvery5Seconds(isConnected); mqttEvery5Seconds(isConnected);
} }
} }