From 3267dcfd80e8f2da8a2499c37bd018d71eb98ab4 Mon Sep 17 00:00:00 2001 From: Dirk Jahnke Date: Sun, 3 Feb 2019 10:42:20 +0100 Subject: [PATCH] Added new web services for reboot and sw-update-check. --- src/WebServer.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++ src/WebServer.h | 7 ++++- src/data/index.htm | 18 +++++++++++++ src/main.cpp | 1 + 4 files changed, 91 insertions(+), 1 deletion(-) diff --git a/src/WebServer.cpp b/src/WebServer.cpp index 66432b7..3477ee3 100644 --- a/src/WebServer.cpp +++ b/src/WebServer.cpp @@ -133,6 +133,51 @@ void WebServer::begin() { json = String(); }); + server.on("/sys/swupdate", HTTP_GET, [&](AsyncWebServerRequest *request){ + #define UPDATE_DELAY 100 + Serial.println(F("\n Check for SWUpdate")); + + // create json return + String json = "{"; + if (ias != NULL) { + json += "\"result\":\"OK\""; + } else { + json += "\"result\":\"FAILED\","; + json += "\"message\":\"IAS not initialized in web server, use setIAS method.\""; + } + json += "}"; + + // return json to WebApp + request->send(200, F("text/json"), json); + json = String(); + if (ias != NULL) { + Serial.println("Checking for software updates"); + checkSoftwareUpdate_ts = millis() + UPDATE_DELAY; + } + }); + + server.on("/sys/reboot", HTTP_GET, [&](AsyncWebServerRequest *request){ + #define REBOOT_DELAY 100 + Serial.println(F("\n Reboot device")); + + String json = "{"; + json += "\"result\":\"OK\""; + json += "}"; + + // return json to WebApp + request->send(200, F("text/json"), json); + json = String(); + Serial.println("Rebooting device"); + rebootDevice_ts = millis() + REBOOT_DELAY; + }); + + server.on("/sys/heap", HTTP_GET, [](AsyncWebServerRequest *request){ + String json = "{"; + json += "\"result\":\"OK\","; + json += "\"heap\":" + String(ESP.getFreeHeap()); + json += "}"; + request->send(200, "text/json", json); + }); server.serveStatic("/", SPIFFS, "/"); server.onNotFound(onRequest); @@ -143,3 +188,24 @@ void WebServer::begin() { Serial.println(WiFi.localIP()); Serial.println(""); } + +void WebServer::loop() { + if (rebootDevice_ts > 0 && rebootDevice_ts < millis()) { + rebootDevice_ts = 0; + Serial.println("Rebooting device."); + delay(20); + ESP.restart(); + } + if (checkSoftwareUpdate_ts > 0 && checkSoftwareUpdate_ts < millis()) { + checkSoftwareUpdate_ts = 0; + Serial.println("Checking for software updates."); + delay(20); + if (ias != NULL) { + ias->callHome(true); + } else { + Serial.println("ERROR: IAS not set, use setIAS method in web server"); + } + } + + return; +} diff --git a/src/WebServer.h b/src/WebServer.h index 22fda44..9e4fb82 100644 --- a/src/WebServer.h +++ b/src/WebServer.h @@ -5,18 +5,23 @@ #include // https://github.com/me-no-dev/AsyncTCP #include #include +#include #include "Relays.h" #include "Clock.h" class WebServer { public: - WebServer(Relays *r, Clock *real_t, Clock *model_t):relays(r),realTime(real_t),modelTime(model_t) {}; + WebServer(Relays *r, Clock *real_t, Clock *model_t): + relays(r),realTime(real_t),modelTime(model_t), checkSoftwareUpdate_ts(0), rebootDevice_ts(0) {}; void begin(); void loop(); + void setIAS(IOTAppStory *_ias) { ias = _ias;}; protected: Relays *relays; Clock *realTime, *modelTime; + unsigned long checkSoftwareUpdate_ts, rebootDevice_ts; + IOTAppStory *ias; }; #endif diff --git a/src/data/index.htm b/src/data/index.htm index 4211b9d..c8373ad 100644 --- a/src/data/index.htm +++ b/src/data/index.htm @@ -83,6 +83,22 @@ ajaxGet("/clock/real", handleChangeClockResponse); } + function handleTriggerSWUpdateCheck(result) { + showMessage(JSON.stringify(result, null, 2)); + }; + + function triggerSWUpdateCheck() { + ajaxGet("/sys/swupdate", handleTriggerSWUpdateCheck); + } + + function handleTriggerReboot(result) { + showMessage(JSON.stringify(result, null, 2)); + }; + + function triggerReboot() { + ajaxGet("/sys/reboot", handleTriggerReboot); + } + function onBodyLoad() { var button = document.getElementById("button"); button.onmousedown = function(event) { @@ -117,6 +133,8 @@
+ +
diff --git a/src/main.cpp b/src/main.cpp index 60abd71..202091f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -183,6 +183,7 @@ void setup(void) setupFS(); setupIAS(); W.begin(); + W.setIAS(&IAS); delay(100); R.begin(relay1Pin, relay2Pin); timeClient.begin();