further memory optimization of config update

This commit is contained in:
andreas 2022-03-11 15:18:38 +01:00
parent 4557e6c0bb
commit 9d25ce8b7e
1 changed files with 19 additions and 18 deletions

View File

@ -458,18 +458,21 @@ protected:
class SetConfigRequest : public GwRequestMessage class SetConfigRequest : public GwRequestMessage
{ {
public: public:
SetConfigRequest() : GwRequestMessage(F("application/json"),F("setConfig")){}; //we rely on the message living not longer then the request
StringMap args; AsyncWebServerRequest *request;
SetConfigRequest(AsyncWebServerRequest *rq) : GwRequestMessage(F("application/json"),F("setConfig")),
request(rq)
{};
virtual int getTimeout(){return 4000;} virtual int getTimeout(){return 4000;}
protected: protected:
virtual void processRequest() virtual void processRequest()
{ {
bool ok = true; bool ok = true;
const char * hashArg="_hash";
String error; String error;
String hash; String hash;
auto it=args.find("_hash"); if (request->hasArg(hashArg)){
if (it != args.end()){ hash=request->arg(hashArg);
hash=it->second;
} }
if (! checkPass(hash)){ if (! checkPass(hash)){
result=JSON_INVALID_PASS; result=JSON_INVALID_PASS;
@ -479,18 +482,19 @@ protected:
(long)xPortGetFreeHeapSize(), (long)xPortGetFreeHeapSize(),
(long)xPortGetMinimumEverFreeHeapSize() (long)xPortGetMinimumEverFreeHeapSize()
); );
for (StringMap::iterator it = args.begin(); it != args.end(); it++) for (int i = 0; i < request->args(); i++){
{ String name=request->argName(i);
if (it->first.indexOf("_")>= 0) continue; String value=request->arg(i);
if (it->first == GwConfigDefinitions::apPassword && fixedApPass) continue; if (name.indexOf("_")>= 0) continue;
bool rt = config.updateValue(it->first, it->second); if (name == GwConfigDefinitions::apPassword && fixedApPass) continue;
bool rt = config.updateValue(name, value);
if (!rt) 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; ok = false;
error += it->first; error += name;
error += "="; error += "=";
error += it->second; error += value;
error += ","; error += ",";
} }
logger.flush(); logger.flush();
@ -523,6 +527,7 @@ public:
ResetConfigRequest(String hash) : GwRequestMessage(F("application/json"),F("resetConfig")){ ResetConfigRequest(String hash) : GwRequestMessage(F("application/json"),F("resetConfig")){
this->hash=hash; this->hash=hash;
}; };
virtual int getTimeout(){return 4000;}
protected: protected:
virtual void processRequest() virtual void processRequest()
@ -643,11 +648,7 @@ void setup() {
webserver.registerMainHandler("/api/setConfig", webserver.registerMainHandler("/api/setConfig",
[](AsyncWebServerRequest *request)->GwRequestMessage * [](AsyncWebServerRequest *request)->GwRequestMessage *
{ {
SetConfigRequest *msg = new SetConfigRequest(); SetConfigRequest *msg = new SetConfigRequest(request);
for (int i = 0; i < request->args(); i++)
{
msg->args[request->argName(i)] = request->arg(i);
}
return msg; return msg;
}); });
webserver.registerMainHandler("/api/resetConfig", [](AsyncWebServerRequest *request)->GwRequestMessage * webserver.registerMainHandler("/api/resetConfig", [](AsyncWebServerRequest *request)->GwRequestMessage *