diff --git a/src/mesh/http/ContentHandler.cpp b/src/mesh/http/ContentHandler.cpp index f87c6e3b0..7b7ebb595 100644 --- a/src/mesh/http/ContentHandler.cpp +++ b/src/mesh/http/ContentHandler.cpp @@ -148,6 +148,8 @@ void registerHandlers(HTTPServer *insecureServer, HTTPSServer *secureServer) void handleAPIv1FromRadio(HTTPRequest *req, HTTPResponse *res) { + if (webServerThread) + webServerThread->markActivity(); LOG_DEBUG("webAPI handleAPIv1FromRadio"); @@ -391,6 +393,9 @@ void handleFsDeleteStatic(HTTPRequest *req, HTTPResponse *res) void handleStatic(HTTPRequest *req, HTTPResponse *res) { + if (webServerThread) + webServerThread->markActivity(); + // Get access to the parameters ResourceParameters *params = req->getParams(); diff --git a/src/mesh/http/WebServer.cpp b/src/mesh/http/WebServer.cpp index bf170de59..3a264fa5a 100644 --- a/src/mesh/http/WebServer.cpp +++ b/src/mesh/http/WebServer.cpp @@ -49,6 +49,12 @@ Preferences prefs; using namespace httpsserver; #include "mesh/http/ContentHandler.h" +static const uint32_t ACTIVE_THRESHOLD_MS = 5000; +static const uint32_t MEDIUM_THRESHOLD_MS = 30000; +static const int32_t ACTIVE_INTERVAL_MS = 50; +static const int32_t MEDIUM_INTERVAL_MS = 200; +static const int32_t IDLE_INTERVAL_MS = 1000; + static SSLCert *cert; static HTTPSServer *secureServer; static HTTPServer *insecureServer; @@ -175,6 +181,32 @@ WebServerThread::WebServerThread() : concurrency::OSThread("WebServer") if (!config.network.wifi_enabled && !config.network.eth_enabled) { disable(); } + lastActivityTime = millis(); +} + +void WebServerThread::markActivity() +{ + lastActivityTime = millis(); +} + +int32_t WebServerThread::getAdaptiveInterval() +{ + uint32_t currentTime = millis(); + uint32_t timeSinceActivity; + + if (currentTime >= lastActivityTime) { + timeSinceActivity = currentTime - lastActivityTime; + } else { + timeSinceActivity = (UINT32_MAX - lastActivityTime) + currentTime + 1; + } + + if (timeSinceActivity < ACTIVE_THRESHOLD_MS) { + return ACTIVE_INTERVAL_MS; + } else if (timeSinceActivity < MEDIUM_THRESHOLD_MS) { + return MEDIUM_INTERVAL_MS; + } else { + return IDLE_INTERVAL_MS; + } } int32_t WebServerThread::runOnce() @@ -189,8 +221,7 @@ int32_t WebServerThread::runOnce() ESP.restart(); } - // Loop every 5ms. - return (5); + return getAdaptiveInterval(); } void initWebServer() diff --git a/src/mesh/http/WebServer.h b/src/mesh/http/WebServer.h index 815d87432..e7a29a5a7 100644 --- a/src/mesh/http/WebServer.h +++ b/src/mesh/http/WebServer.h @@ -10,13 +10,17 @@ void createSSLCert(); class WebServerThread : private concurrency::OSThread { + private: + uint32_t lastActivityTime = 0; public: WebServerThread(); uint32_t requestRestart = 0; + void markActivity(); protected: virtual int32_t runOnce() override; + int32_t getAdaptiveInterval(); }; extern WebServerThread *webServerThread;