From 9d25ce8b7e30874b97359d30d0db39c2e00480ef Mon Sep 17 00:00:00 2001 From: andreas Date: Fri, 11 Mar 2022 15:18:38 +0100 Subject: [PATCH] further memory optimization of config update --- src/main.cpp | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index bb17036..582372e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -458,18 +458,21 @@ protected: class SetConfigRequest : public GwRequestMessage { public: - SetConfigRequest() : GwRequestMessage(F("application/json"),F("setConfig")){}; - StringMap args; + //we rely on the message living not longer then the request + AsyncWebServerRequest *request; + SetConfigRequest(AsyncWebServerRequest *rq) : GwRequestMessage(F("application/json"),F("setConfig")), + request(rq) + {}; virtual int getTimeout(){return 4000;} protected: virtual void processRequest() { bool ok = true; + const char * hashArg="_hash"; String error; String hash; - auto it=args.find("_hash"); - if (it != args.end()){ - hash=it->second; + if (request->hasArg(hashArg)){ + hash=request->arg(hashArg); } if (! checkPass(hash)){ result=JSON_INVALID_PASS; @@ -479,18 +482,19 @@ protected: (long)xPortGetFreeHeapSize(), (long)xPortGetMinimumEverFreeHeapSize() ); - for (StringMap::iterator it = args.begin(); it != args.end(); it++) - { - if (it->first.indexOf("_")>= 0) continue; - if (it->first == GwConfigDefinitions::apPassword && fixedApPass) continue; - bool rt = config.updateValue(it->first, it->second); + for (int i = 0; i < request->args(); i++){ + String name=request->argName(i); + String value=request->arg(i); + if (name.indexOf("_")>= 0) continue; + if (name == GwConfigDefinitions::apPassword && fixedApPass) continue; + bool rt = config.updateValue(name, value); if (!rt) { - logger.logDebug(GwLog::ERROR,"ERR: unable to update %s to %s", it->first.c_str(), it->second.c_str()); + logger.logDebug(GwLog::ERROR,"ERR: unable to update %s to %s", name.c_str(), value.c_str()); ok = false; - error += it->first; + error += name; error += "="; - error += it->second; + error += value; error += ","; } logger.flush(); @@ -523,6 +527,7 @@ public: ResetConfigRequest(String hash) : GwRequestMessage(F("application/json"),F("resetConfig")){ this->hash=hash; }; + virtual int getTimeout(){return 4000;} protected: virtual void processRequest() @@ -643,11 +648,7 @@ void setup() { webserver.registerMainHandler("/api/setConfig", [](AsyncWebServerRequest *request)->GwRequestMessage * { - SetConfigRequest *msg = new SetConfigRequest(); - for (int i = 0; i < request->args(); i++) - { - msg->args[request->argName(i)] = request->arg(i); - } + SetConfigRequest *msg = new SetConfigRequest(request); return msg; }); webserver.registerMainHandler("/api/resetConfig", [](AsyncWebServerRequest *request)->GwRequestMessage *