optimize memory usage for config data, allow 4s timeout for setConfig
This commit is contained in:
parent
6741222574
commit
4557e6c0bb
|
@ -66,13 +66,10 @@ bool GwConfigHandler::loadConfig(){
|
|||
bool GwConfigHandler::saveConfig(){
|
||||
prefs.begin(PREF_NAME,false);
|
||||
for (int i=0;i<getNumConfig();i++){
|
||||
String val=configs[i]->asString();
|
||||
auto it=changedValues.find(configs[i]->getName());
|
||||
if (it != changedValues.end()){
|
||||
val=it->second;
|
||||
if (configs[i]->hasChangedValue){
|
||||
LOG_DEBUG(GwLog::LOG,"saving %s=%s",configs[i]->getName().c_str(),configs[i]->changedValue.c_str());
|
||||
prefs.putString(configs[i]->getName().c_str(),configs[i]->changedValue);
|
||||
}
|
||||
LOG_DEBUG(GwLog::LOG,"saving %s=%s",configs[i]->getName().c_str(),val.c_str());
|
||||
prefs.putString(configs[i]->getName().c_str(),val);
|
||||
}
|
||||
prefs.end();
|
||||
LOG_DEBUG(GwLog::LOG,"saved config");
|
||||
|
@ -87,14 +84,14 @@ bool GwConfigHandler::updateValue(String name, String value){
|
|||
}
|
||||
else{
|
||||
LOG_DEBUG(GwLog::LOG,"update config %s=>%s",name.c_str(),i->isSecret()?"***":value.c_str());
|
||||
changedValues[name]=value;
|
||||
i->updateValue(value);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool GwConfigHandler::reset(bool save){
|
||||
LOG_DEBUG(GwLog::LOG,"reset config");
|
||||
for (int i=0;i<getNumConfig();i++){
|
||||
changedValues[configs[i]->getName()]=configs[i]->getDefault();
|
||||
configs[i]->updateValue(configs[i]->getDefault(),true);
|
||||
}
|
||||
if (!save) return true;
|
||||
return saveConfig();
|
||||
|
|
|
@ -13,7 +13,6 @@ class GwConfigHandler: public GwConfigDefinitions{
|
|||
Preferences prefs;
|
||||
GwLog *logger;
|
||||
typedef std::map<String,String> StringMap;
|
||||
StringMap changedValues;
|
||||
boolean allowChanges=true;
|
||||
public:
|
||||
public:
|
||||
|
|
|
@ -7,11 +7,28 @@ class GwConfigHandler;
|
|||
class GwConfigInterface{
|
||||
private:
|
||||
String name;
|
||||
String initialValue;
|
||||
const char * initialValue;
|
||||
String value;
|
||||
bool secret=false;
|
||||
String changedValue;
|
||||
bool hasChangedValue=false;
|
||||
void updateValue(String value,bool cmpDefault=false){
|
||||
hasChangedValue=false;
|
||||
if (cmpDefault){
|
||||
if (value != initialValue) {
|
||||
changedValue=value;
|
||||
hasChangedValue=true;
|
||||
}
|
||||
}
|
||||
else{
|
||||
if (value != this->value) {
|
||||
changedValue=value;
|
||||
hasChangedValue=true;
|
||||
}
|
||||
}
|
||||
}
|
||||
public:
|
||||
GwConfigInterface(const String &name, const String initialValue, bool secret=false){
|
||||
GwConfigInterface(const String &name, const char * initialValue, bool secret=false){
|
||||
this->name=name;
|
||||
this->initialValue=initialValue;
|
||||
this->value=initialValue;
|
||||
|
|
|
@ -56,6 +56,7 @@ class GwRequestMessage : public GwMessage{
|
|||
String getContentType(){
|
||||
return contentType;
|
||||
}
|
||||
virtual int getTimeout(){return 500;}
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -68,7 +68,7 @@ GwWebServer::~GwWebServer(){
|
|||
}
|
||||
void GwWebServer::handleAsyncWebRequest(AsyncWebServerRequest *request, GwRequestMessage *msg)
|
||||
{
|
||||
GwRequestQueue::MessageSendStatus st=queue->sendAndWait(msg,500);
|
||||
GwRequestQueue::MessageSendStatus st=queue->sendAndWait(msg,msg->getTimeout());
|
||||
if (st == GwRequestQueue::MSG_ERR)
|
||||
{
|
||||
msg->unref(); //our
|
||||
|
|
19
src/main.cpp
19
src/main.cpp
|
@ -460,7 +460,7 @@ class SetConfigRequest : public GwRequestMessage
|
|||
public:
|
||||
SetConfigRequest() : GwRequestMessage(F("application/json"),F("setConfig")){};
|
||||
StringMap args;
|
||||
|
||||
virtual int getTimeout(){return 4000;}
|
||||
protected:
|
||||
virtual void processRequest()
|
||||
{
|
||||
|
@ -475,6 +475,10 @@ protected:
|
|||
result=JSON_INVALID_PASS;
|
||||
return;
|
||||
}
|
||||
logger.logDebug(GwLog::DEBUG,"Heap free=%ld, minFree=%ld",
|
||||
(long)xPortGetFreeHeapSize(),
|
||||
(long)xPortGetMinimumEverFreeHeapSize()
|
||||
);
|
||||
for (StringMap::iterator it = args.begin(); it != args.end(); it++)
|
||||
{
|
||||
if (it->first.indexOf("_")>= 0) continue;
|
||||
|
@ -489,12 +493,19 @@ protected:
|
|||
error += it->second;
|
||||
error += ",";
|
||||
}
|
||||
logger.flush();
|
||||
}
|
||||
if (ok)
|
||||
{
|
||||
result = JSON_OK;
|
||||
logger.logDebug(GwLog::ERROR,"update config and restart");
|
||||
config.saveConfig();
|
||||
logger.flush();
|
||||
logger.logDebug(GwLog::DEBUG,"Heap free=%ld, minFree=%ld",
|
||||
(long)xPortGetFreeHeapSize(),
|
||||
(long)xPortGetMinimumEverFreeHeapSize()
|
||||
);
|
||||
logger.flush();
|
||||
delayedRestart();
|
||||
}
|
||||
else
|
||||
|
@ -632,13 +643,11 @@ void setup() {
|
|||
webserver.registerMainHandler("/api/setConfig",
|
||||
[](AsyncWebServerRequest *request)->GwRequestMessage *
|
||||
{
|
||||
StringMap args;
|
||||
SetConfigRequest *msg = new SetConfigRequest();
|
||||
for (int i = 0; i < request->args(); i++)
|
||||
{
|
||||
args[request->argName(i)] = request->arg(i);
|
||||
msg->args[request->argName(i)] = request->arg(i);
|
||||
}
|
||||
SetConfigRequest *msg = new SetConfigRequest();
|
||||
msg->args = args;
|
||||
return msg;
|
||||
});
|
||||
webserver.registerMainHandler("/api/resetConfig", [](AsyncWebServerRequest *request)->GwRequestMessage *
|
||||
|
|
Loading…
Reference in New Issue