Merge remote-tracking branch 'upstream/master'

This commit is contained in:
norbert-walter 2022-03-11 16:14:35 +01:00
commit 0773de6e08
6 changed files with 52 additions and 33 deletions

View File

@ -66,13 +66,10 @@ bool GwConfigHandler::loadConfig(){
bool GwConfigHandler::saveConfig(){ bool GwConfigHandler::saveConfig(){
prefs.begin(PREF_NAME,false); prefs.begin(PREF_NAME,false);
for (int i=0;i<getNumConfig();i++){ for (int i=0;i<getNumConfig();i++){
String val=configs[i]->asString(); if (configs[i]->hasChangedValue){
auto it=changedValues.find(configs[i]->getName()); LOG_DEBUG(GwLog::LOG,"saving %s=%s",configs[i]->getName().c_str(),configs[i]->changedValue.c_str());
if (it != changedValues.end()){ prefs.putString(configs[i]->getName().c_str(),configs[i]->changedValue);
val=it->second;
} }
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(); prefs.end();
LOG_DEBUG(GwLog::LOG,"saved config"); LOG_DEBUG(GwLog::LOG,"saved config");
@ -87,14 +84,14 @@ bool GwConfigHandler::updateValue(String name, String value){
} }
else{ else{
LOG_DEBUG(GwLog::LOG,"update config %s=>%s",name.c_str(),i->isSecret()?"***":value.c_str()); LOG_DEBUG(GwLog::LOG,"update config %s=>%s",name.c_str(),i->isSecret()?"***":value.c_str());
changedValues[name]=value; i->updateValue(value);
} }
return true; return true;
} }
bool GwConfigHandler::reset(bool save){ bool GwConfigHandler::reset(bool save){
LOG_DEBUG(GwLog::LOG,"reset config"); LOG_DEBUG(GwLog::LOG,"reset config");
for (int i=0;i<getNumConfig();i++){ for (int i=0;i<getNumConfig();i++){
changedValues[configs[i]->getName()]=configs[i]->getDefault(); configs[i]->updateValue(configs[i]->getDefault());
} }
if (!save) return true; if (!save) return true;
return saveConfig(); return saveConfig();

View File

@ -13,7 +13,6 @@ class GwConfigHandler: public GwConfigDefinitions{
Preferences prefs; Preferences prefs;
GwLog *logger; GwLog *logger;
typedef std::map<String,String> StringMap; typedef std::map<String,String> StringMap;
StringMap changedValues;
boolean allowChanges=true; boolean allowChanges=true;
public: public:
public: public:

View File

@ -7,11 +7,23 @@ class GwConfigHandler;
class GwConfigInterface{ class GwConfigInterface{
private: private:
String name; String name;
String initialValue; const char * initialValue;
String value; String value;
bool secret=false; bool secret=false;
String changedValue;
bool hasChangedValue=false;
void updateValue(String value)
{
hasChangedValue = false;
if (value != this->value)
{
changedValue = value;
hasChangedValue = true;
}
}
public: 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->name=name;
this->initialValue=initialValue; this->initialValue=initialValue;
this->value=initialValue; this->value=initialValue;

View File

@ -56,6 +56,7 @@ class GwRequestMessage : public GwMessage{
String getContentType(){ String getContentType(){
return contentType; return contentType;
} }
virtual int getTimeout(){return 500;}
}; };

View File

@ -68,7 +68,7 @@ GwWebServer::~GwWebServer(){
} }
void GwWebServer::handleAsyncWebRequest(AsyncWebServerRequest *request, GwRequestMessage *msg) 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) if (st == GwRequestQueue::MSG_ERR)
{ {
msg->unref(); //our msg->unref(); //our

View File

@ -458,43 +458,58 @@ 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;}
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;
return; return;
} }
for (StringMap::iterator it = args.begin(); it != args.end(); it++) logger.logDebug(GwLog::DEBUG,"Heap free=%ld, minFree=%ld",
{ (long)xPortGetFreeHeapSize(),
if (it->first.indexOf("_")>= 0) continue; (long)xPortGetMinimumEverFreeHeapSize()
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) 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();
} }
if (ok) if (ok)
{ {
result = JSON_OK; result = JSON_OK;
logger.logDebug(GwLog::ERROR,"update config and restart"); logger.logDebug(GwLog::ERROR,"update config and restart");
config.saveConfig(); config.saveConfig();
logger.flush();
logger.logDebug(GwLog::DEBUG,"Heap free=%ld, minFree=%ld",
(long)xPortGetFreeHeapSize(),
(long)xPortGetMinimumEverFreeHeapSize()
);
logger.flush();
delayedRestart(); delayedRestart();
} }
else else
@ -512,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()
@ -632,13 +648,7 @@ void setup() {
webserver.registerMainHandler("/api/setConfig", webserver.registerMainHandler("/api/setConfig",
[](AsyncWebServerRequest *request)->GwRequestMessage * [](AsyncWebServerRequest *request)->GwRequestMessage *
{ {
StringMap args; SetConfigRequest *msg = new SetConfigRequest(request);
for (int i = 0; i < request->args(); i++)
{
args[request->argName(i)] = request->arg(i);
}
SetConfigRequest *msg = new SetConfigRequest();
msg->args = args;
return msg; return msg;
}); });
webserver.registerMainHandler("/api/resetConfig", [](AsyncWebServerRequest *request)->GwRequestMessage * webserver.registerMainHandler("/api/resetConfig", [](AsyncWebServerRequest *request)->GwRequestMessage *