Bladeren bron

add PROGMEM in a few places

Chris Mullins 8 jaren geleden
bovenliggende
commit
5571e34872

+ 26 - 27
lib/WebServer/MiLightHttpServer.cpp

@@ -7,25 +7,23 @@
 #include <GithubClient.h>
 #include <string.h>
 #include <TokenIterator.h>
-#include <ESP8266SSDP.h>
 
 void MiLightHttpServer::begin() {
   applySettings(settings);
 
   server.on("/", HTTP_GET, handleServeFile(WEB_INDEX_FILENAME, "text/html", DEFAULT_INDEX_PAGE));
-  server.on("/settings", HTTP_GET, handleServeFile(SETTINGS_FILE, "application/json"));
+  server.on("/settings", HTTP_GET, handleServeFile(SETTINGS_FILE, APPLICATION_JSON));
   server.on("/settings", HTTP_PUT, [this]() { handleUpdateSettings(); });
-  server.on("/settings", HTTP_POST, [this]() { server.send(200, "text/plain", "success"); }, handleUpdateFile(SETTINGS_FILE));
+  server.on("/settings", HTTP_POST, [this]() { server.send(200, TEXT_PLAIN, "success"); }, handleUpdateFile(SETTINGS_FILE));
   server.on("/radio_configs", HTTP_GET, [this]() { handleGetRadioConfigs(); });
   server.onPattern("/gateway_traffic/:type", HTTP_GET, [this](const UrlTokenBindings* b) { handleListenGateway(b); });
   server.onPattern("/gateways/:device_id/:type/:group_id", HTTP_ANY, [this](const UrlTokenBindings* b) { handleUpdateGroup(b); });
   server.onPattern("/raw_commands/:type", HTTP_ANY, [this](const UrlTokenBindings* b) { handleSendRaw(b); });
   server.onPattern("/download_update/:component", HTTP_GET, [this](const UrlTokenBindings* b) { handleDownloadUpdate(b); });
-  server.on("/web", HTTP_POST, [this]() { server.send(200, "text/plain", "success"); }, handleUpdateFile(WEB_INDEX_FILENAME));
+  server.on("/web", HTTP_POST, [this]() { server.send(200, TEXT_PLAIN, "success"); }, handleUpdateFile(WEB_INDEX_FILENAME));
   server.on("/about", HTTP_GET, [this]() { handleAbout(); });
   server.on("/latest_release", HTTP_GET, [this]() { handleGetLatestRelease(); });
   server.on("/system", HTTP_POST, [this]() { handleSystemPost(); });
-  server.on("/description.xml", HTTP_GET, [this]() { SSDP.schema(server.client()); });
   server.on("/firmware", HTTP_POST,
     [this](){
       server.sendHeader("Connection", "close");
@@ -34,13 +32,13 @@ void MiLightHttpServer::begin() {
       if (Update.hasError()) {
         server.send_P(
           500,
-          "text/plain",
+          TEXT_PLAIN,
           PSTR("Failed updating firmware. Check serial logs for more information. You may need to re-flash the device.")
         );
       } else {
         server.send_P(
           200,
-          "text/plain",
+          TEXT_PLAIN,
           PSTR("Success. Device will now reboot.")
         );
       }
@@ -95,12 +93,12 @@ void MiLightHttpServer::handleGetLatestRelease() {
   }
 
   if (!SPIFFS.exists(fsPath)) {
-    server.send_P(500, "text/plain", PSTR("Failed to stream API request from GitHub. Check Serial logs for more information."));
+    server.send_P(500, TEXT_PLAIN, PSTR("Failed to stream API request from GitHub. Check Serial logs for more information."));
     return;
   }
 
   File file = SPIFFS.open(fsPath, "r");
-  server.streamFile(file, "application/json");
+  server.streamFile(file, APPLICATION_JSON);
   SPIFFS.remove(fsPath);
 }
 
@@ -125,7 +123,7 @@ void MiLightHttpServer::handleSystemPost() {
   if (request.containsKey("command")) {
     if (request["command"] == "restart") {
       Serial.println(F("Restarting..."));
-      server.send(200, "text/plain", "true");
+      server.send(200, TEXT_PLAIN, "true");
 
       delay(100);
 
@@ -134,9 +132,9 @@ void MiLightHttpServer::handleSystemPost() {
   }
 
   if (handled) {
-    server.send(200, "text/plain", "true");
+    server.send(200, TEXT_PLAIN, "true");
   } else {
-    server.send(400, "text/plain", F("{\"error\":\"Unhandled command\"}"));
+    server.send(400, TEXT_PLAIN, F("{\"error\":\"Unhandled command\"}"));
   }
 }
 
@@ -151,14 +149,14 @@ void MiLightHttpServer::handleDownloadUpdate(const UrlTokenBindings* bindings) {
     size_t tries = 0;
 
     while (!result && tries++ <= MAX_DOWNLOAD_ATTEMPTS) {
-      printf("building url\n");
+      Serial.println(F("building url\n"));
       String urlPath = GithubClient::buildRepoPath(
         MILIGHT_GITHUB_USER,
         MILIGHT_GITHUB_REPO,
         MILIGHT_REPO_WEB_PATH
       );
 
-      printf("URL: %s\n", urlPath.c_str());
+      printf_P(PSTR("URL: %s\n"), urlPath.c_str());
 
       result = downloader.download(urlPath, WEB_INDEX_FILENAME);
     }
@@ -169,11 +167,11 @@ void MiLightHttpServer::handleDownloadUpdate(const UrlTokenBindings* bindings) {
       server.sendHeader("Location", "/");
       server.send(302);
     } else {
-      server.send(500, "text/plain", F("Failed to download update from Github. Check serial logs for more information."));
+      server.send(500, TEXT_PLAIN, F("Failed to download update from Github. Check serial logs for more information."));
     }
   } else {
     String body = String("Unknown component: ") + component;
-    server.send(400, "text/plain", body);
+    server.send(400, TEXT_PLAIN, body);
   }
 }
 
@@ -197,6 +195,7 @@ void MiLightHttpServer::handleAbout() {
 
   response["version"] = QUOTE(MILIGHT_HUB_VERSION);
   response["variant"] = QUOTE(FIRMWARE_VARIANT);
+  response["free_heap"] = ESP.getFreeHeap();
 
   String body;
   response.printTo(body);
@@ -216,7 +215,7 @@ void MiLightHttpServer::handleGetRadioConfigs() {
   String body;
   arr.printTo(body);
 
-  server.send(200, "application/json", body);
+  server.send(200, APPLICATION_JSON, body);
 }
 
 ESP8266WebServer::THandlerFunction MiLightHttpServer::handleServeFile(
@@ -274,9 +273,9 @@ void MiLightHttpServer::handleUpdateSettings() {
     this->applySettings(settings);
     this->settingsSavedHandler();
 
-    server.send(200, "application/json", "true");
+    server.send(200, APPLICATION_JSON, "true");
   } else {
-    server.send(400, "application/json", "\"Invalid JSON\"");
+    server.send(400, APPLICATION_JSON, "\"Invalid JSON\"");
   }
 }
 
@@ -288,7 +287,7 @@ void MiLightHttpServer::handleListenGateway(const UrlTokenBindings* bindings) {
     String body = "Unknown device type: ";
     body += bindings->get("type");
 
-    server.send(400, "text/plain", body);
+    server.send(400, TEXT_PLAIN, body);
     return;
   }
 
@@ -312,10 +311,10 @@ void MiLightHttpServer::handleListenGateway(const UrlTokenBindings* bindings) {
   char response[200];
   char* responseBuffer = response;
 
-  responseBuffer += sprintf(responseBuffer, "\nPacket received (%d bytes):\n", sizeof(packet));
+  responseBuffer += sprintf_P(responseBuffer, PSTR("\nPacket received (%d bytes):\n"), sizeof(packet));
   milightClient->formatPacket(packet, responseBuffer);
 
-  server.send(200, "text/plain", response);
+  server.send(200, TEXT_PLAIN, response);
 }
 
 void MiLightHttpServer::handleUpdateGroup(const UrlTokenBindings* urlBindings) {
@@ -323,7 +322,7 @@ void MiLightHttpServer::handleUpdateGroup(const UrlTokenBindings* urlBindings) {
   JsonObject& request = buffer.parse(server.arg("plain"));
 
   if (!request.success()) {
-    server.send(400, "text/plain", F("Invalid JSON"));
+    server.send(400, TEXT_PLAIN, F("Invalid JSON"));
     return;
   }
 
@@ -352,7 +351,7 @@ void MiLightHttpServer::handleUpdateGroup(const UrlTokenBindings* urlBindings) {
     if (config == NULL) {
       String body = "Unknown device type: ";
       body += String(_radioType);
-      server.send(400, "text/plain", body);
+      server.send(400, TEXT_PLAIN, body);
       return;
     }
 
@@ -370,7 +369,7 @@ void MiLightHttpServer::handleUpdateGroup(const UrlTokenBindings* urlBindings) {
     }
   }
 
-  server.send(200, "application/json", "true");
+  server.send(200, APPLICATION_JSON, "true");
 }
 
 void MiLightHttpServer::handleRequest(const JsonObject& request) {
@@ -462,7 +461,7 @@ void MiLightHttpServer::handleSendRaw(const UrlTokenBindings* bindings) {
     String body = "Unknown device type: ";
     body += bindings->get("type");
 
-    server.send(400, "text/plain", body);
+    server.send(400, TEXT_PLAIN, body);
     return;
   }
 
@@ -481,5 +480,5 @@ void MiLightHttpServer::handleSendRaw(const UrlTokenBindings* bindings) {
     milightClient->write(packet);
   }
 
-  server.send(200, "text/plain", "true");
+  server.send(200, TEXT_PLAIN, "true");
 }

+ 3 - 0
lib/WebServer/MiLightHttpServer.h

@@ -12,6 +12,9 @@ typedef std::function<void(void)> SettingsSavedHandler;
 const char DEFAULT_INDEX_PAGE[] PROGMEM
   = "Web app not installed. Click <a href=\"/download_update/web\">here</a> to attempt to download it from GitHub.";
 
+const char TEXT_PLAIN[] PROGMEM = "text/plain";
+const char APPLICATION_JSON[] PROGMEM = "application/json";
+
 class MiLightHttpServer {
 public:
   MiLightHttpServer(Settings& settings, MiLightClient*& milightClient)

+ 2 - 6
lib/WebServer/PatternHandler.cpp

@@ -5,9 +5,7 @@ UrlTokenBindings::UrlTokenBindings(TokenIterator& patternTokens, TokenIterator&
     requestTokens(requestTokens)
 { }
 
-bool UrlTokenBindings::hasBinding(const String &key) const {
-  const char* searchToken = key.c_str();
-
+bool UrlTokenBindings::hasBinding(const char* searchToken) const {
   patternTokens.reset();
   while (patternTokens.hasNext()) {
     const char* token = patternTokens.nextToken();
@@ -20,9 +18,7 @@ bool UrlTokenBindings::hasBinding(const String &key) const {
   return false;
 }
 
-const char* UrlTokenBindings::get(const String &key) const {
-  const char* searchToken = key.c_str();
-
+const char* UrlTokenBindings::get(const char* searchToken) const {
   patternTokens.reset();
   requestTokens.reset();
 

+ 2 - 2
lib/WebServer/PatternHandler.h

@@ -10,8 +10,8 @@ class UrlTokenBindings {
 public:
   UrlTokenBindings(TokenIterator& patternTokens, TokenIterator& requestTokens);
 
-  bool hasBinding(const String& key) const;
-  const char* get(const String& key) const;
+  bool hasBinding(const char* key) const;
+  const char* get(const char* key) const;
 
 private:
   TokenIterator& patternTokens;