浏览代码

Handle auto restarts

Chris Mullins 8 年之前
父节点
当前提交
3818c67a3e
共有 1 个文件被更改,包括 17 次插入0 次删除
  1. 17 0
      src/main.cpp

+ 17 - 0
src/main.cpp

@@ -21,6 +21,8 @@ MiLightHttpServer *httpServer;
 int numUdpServers = 0;
 MiLightUdpServer** udpServers;
 
+uint64_t startTime;
+
 void initMilightUdpServers() {
   if (udpServers) {
     for (int i = 0; i < numUdpServers; i++) {
@@ -68,6 +70,14 @@ void applySettings() {
   initMilightUdpServers();
 }
 
+bool shouldRestart() {
+  if (! settings.isAutoRestartEnabled()) {
+    return false;
+  }
+  
+  return settings.getAutoRestartPeriod()*60*1000 < millis();
+}
+
 void setup() {
   Serial.begin(9600);
   wifiManager.autoConnect();
@@ -78,6 +88,8 @@ void setup() {
   httpServer = new MiLightHttpServer(settings, milightClient);
   httpServer->onSettingsSaved(applySettings);
   httpServer->begin();
+  
+  startTime = millis();
 }
 
 void loop() {
@@ -88,4 +100,9 @@ void loop() {
       udpServers[i]->handleClient();
     }
   }
+  
+  if (shouldRestart()) {
+    Serial.println("Auto-restart triggered. Restarting...");
+    ESP.restart();
+  }
 }