Browse Source

yield to other app-level processes during longpoll

Chris Mullins 8 years ago
parent
commit
23ec90c961
3 changed files with 26 additions and 11 deletions
  1. 8 0
      lib/WebServer/MiLightHttpServer.cpp
  2. 3 0
      lib/WebServer/MiLightHttpServer.h
  3. 15 11
      src/main.cpp

+ 8 - 0
lib/WebServer/MiLightHttpServer.cpp

@@ -129,6 +129,10 @@ void MiLightHttpServer::applySettings(Settings& settings) {
 void MiLightHttpServer::onSettingsSaved(SettingsSavedHandler handler) {
   this->settingsSavedHandler = handler;
 }
+
+void MiLightHttpServer::onLongPollLoop(LongPollLoopFn fn) {
+  this->longPollLoopFn = fn;
+}
   
 void MiLightHttpServer::handleAbout() {
   DynamicJsonBuffer buffer;
@@ -242,6 +246,10 @@ void MiLightHttpServer::handleListenGateway(const UrlTokenBindings* bindings) {
       available = true;
     }
     
+    if (this->longPollLoopFn) {
+      longPollLoopFn();
+    }
+    
     yield();
   }
   

+ 3 - 0
lib/WebServer/MiLightHttpServer.h

@@ -8,6 +8,7 @@
 #define MAX_DOWNLOAD_ATTEMPTS 3
 
 typedef std::function<void(void)> SettingsSavedHandler;
+typedef std::function<void(void)> LongPollLoopFn;
 
 const char DEFAULT_INDEX_PAGE[] PROGMEM
   = "Web app not installed. Click <a href=\"/download_update/web\">here</a> to attempt to download it from GitHub.";
@@ -25,6 +26,7 @@ public:
   void begin();
   void handleClient();
   void onSettingsSaved(SettingsSavedHandler handler);
+  void onLongPollLoop(LongPollLoopFn);
   
 protected:
   ESP8266WebServer::THandlerFunction handleServeFile(
@@ -51,6 +53,7 @@ protected:
   Settings& settings;
   MiLightClient*& milightClient;
   SettingsSavedHandler settingsSavedHandler;
+  LongPollLoopFn longPollLoopFn;
   
 };
 

+ 15 - 11
src/main.cpp

@@ -77,6 +77,19 @@ bool shouldRestart() {
   return settings.getAutoRestartPeriod()*60*1000 < millis();
 }
 
+void handleLoop() {
+  if (udpServers) {
+    for (size_t i = 0; i < settings.numGatewayConfigs; i++) {
+      udpServers[i]->handleClient();
+    }
+  }
+  
+  if (shouldRestart()) {
+    Serial.println("Auto-restart triggered. Restarting...");
+    ESP.restart();
+  }
+}
+
 void setup() {
   Serial.begin(9600);
   wifiManager.autoConnect();
@@ -86,20 +99,11 @@ void setup() {
   
   httpServer = new MiLightHttpServer(settings, milightClient);
   httpServer->onSettingsSaved(applySettings);
+  httpServer->onLongPollLoop(handleLoop);
   httpServer->begin();
 }
 
 void loop() {
   httpServer->handleClient();
-  
-  if (udpServers) {
-    for (size_t i = 0; i < settings.numGatewayConfigs; i++) {
-      udpServers[i]->handleClient();
-    }
-  }
-  
-  if (shouldRestart()) {
-    Serial.println("Auto-restart triggered. Restarting...");
-    ESP.restart();
-  }
+  handleLoop();
 }