Added new web services for reboot and sw-update-check.

This commit is contained in:
Dirk Jahnke 2019-02-03 10:42:20 +01:00
parent 3e2aba52ec
commit 3267dcfd80
4 changed files with 91 additions and 1 deletions

View File

@ -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;
}

View File

@ -5,18 +5,23 @@
#include <ESPAsyncTCP.h> // https://github.com/me-no-dev/AsyncTCP
#include <ESPAsyncWebServer.h>
#include <FS.h>
#include <IOTAppStory.h>
#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

View File

@ -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 @@
<div>
<button onClick="setClockToModel()">Model time</button>
<button onClick="setClockToReal()">Real time</button>
<button onClick="triggerSWUpdateCheck()">Check SW Update</button>
<button onClick="triggerReboot()">Reboot</button>
</div>
<div id="displayMessageId" class="logmsg"></div>
<div class="btn_cnt"><img id="led" src="led-off.png"><a href="#"><img id="button" src="btn.png"></a></div>

View File

@ -183,6 +183,7 @@ void setup(void)
setupFS();
setupIAS();
W.begin();
W.setIAS(&IAS);
delay(100);
R.begin(relay1Pin, relay2Pin);
timeClient.begin();