Переглянути джерело

serve default values for newly added settings

Chris Mullins 8 роки тому
батько
коміт
85091d1acd

+ 7 - 1
lib/WebServer/MiLightHttpServer.cpp

@@ -12,7 +12,7 @@ void MiLightHttpServer::begin() {
   applySettings(settings);
 
   server.on("/", HTTP_GET, handleServe_P(index_html_gz, index_html_gz_len));
-  server.on("/settings", HTTP_GET, handleServeFile(SETTINGS_FILE, APPLICATION_JSON));
+  server.on("/settings", HTTP_GET, [this]() { serveSettings(); });
   server.on("/settings", HTTP_PUT, [this]() { handleUpdateSettings(); });
   server.on("/settings", HTTP_POST, [this]() { server.send_P(200, TEXT_PLAIN, PSTR("success.")); }, handleUpdateFile(SETTINGS_FILE));
   server.on("/radio_configs", HTTP_GET, [this]() { handleGetRadioConfigs(); });
@@ -130,6 +130,12 @@ void MiLightHttpServer::handleSystemPost() {
   }
 }
 
+void MiLightHttpServer::serveSettings() {
+  // Save first to set defaults
+  settings.save();
+  serveFile(SETTINGS_FILE, APPLICATION_JSON);
+}
+
 void MiLightHttpServer::applySettings(Settings& settings) {
   if (settings.hasAuthSettings()) {
     server.requireAuthentication(settings.adminUsername, settings.adminPassword);

+ 1 - 0
lib/WebServer/MiLightHttpServer.h

@@ -40,6 +40,7 @@ protected:
     const char* contentType,
     const char* defaultText = NULL);
 
+  void serveSettings();
   bool serveFile(const char* file, const char* contentType = "text/html");
   ESP8266WebServer::THandlerFunction handleUpdateFile(const char* filename);
   ESP8266WebServer::THandlerFunction handleServe_P(const char* data, size_t length);