Added files web service (directory) and fixed setDT service to fastforward the clock.

This commit is contained in:
Dirk Jahnke 2019-01-29 11:26:35 +01:00
parent 3dfd08e495
commit de9fc112d0
1 changed files with 70 additions and 35 deletions

View File

@ -29,6 +29,7 @@
#include <FS.h>
#include <ESPAsyncTCP.h> // https://github.com/me-no-dev/AsyncTCP
#include <ESPAsyncWebServer.h>
#include <ESP8266HTTPClient.h>
#include "Relays.h"
IOTAppStory IAS(COMPDATE, MODEBUTTON);
@ -161,7 +162,7 @@ void setupIAS(void) {
// Call home interval in seconds, use 60s only for development.
// Please change it to at least 2 hours in production
IAS.setCallHomeInterval(120);
//IAS.callHome(false /*SPIFFS-check*/);
IAS.callHome(true /*SPIFFS-check*/);
clockSpeed_modelMsPerRealSec = atoi(clockSpeed_modelMsPerRealSec_String);
relay1Pin = IAS.dPinConv(relay1Pin_String);
relay2Pin = IAS.dPinConv(relay2Pin_String);
@ -243,46 +244,46 @@ void setupFS() {
void setupWebServer() {
server.on("/fwd", HTTP_GET, [](AsyncWebServerRequest *request){
Serial.println(F("\n WebApp button pressed -- move clock forward"));
Serial.println(F("\n WebApp button pressed -- move clock forward"));
R.toggle();
R.toggle();
// create json return
String json = "{";
json += "\"result\":\"OK\",";
json += "\"clockName\":\"" + String(clockName) + "\",";
json += "\"hours\":\"" + String(hours) + "\",";
json += "\"minutes\":\"" + String(minutes) + "\",";
json += "\"seconds\":\"" + String(seconds) + "\"";
json += "}";
// create json return
String json = "{";
json += "\"result\":\"OK\",";
json += "\"clockName\":\"" + String(clockName) + "\",";
json += "\"hours\":\"" + String(hours) + "\",";
json += "\"minutes\":\"" + String(minutes) + "\",";
json += "\"seconds\":\"" + String(seconds) + "\"";
json += "}";
// return json to WebApp
request->send(200, F("text/json"), json);
json = String();
// return json to WebApp
request->send(200, F("text/json"), json);
json = String();
});
server.on("/clock", HTTP_GET, [](AsyncWebServerRequest *request){
// create json return
String json = "{";
json += "\"clockName\":\""+String(clockName)+"\",";
json += "\"clockSpeed\":\""+String(clockSpeed_modelMsPerRealSec)+"\",";
json += "\"relayHoldTime_ms\":\""+String(R.getHoldTime_ms())+"\",";
json += "\"relayMinOffTime_ms\":\""+String(R.getMinOffTime_ms())+"\",";
json += "\"displayRefresh_ms\":\""+String(displayRefresh_ms)+"\",";
json += "\"displayClockNameEvery_ms\":\""+String(displayClockNameEvery_ms)+"\",";
json += "\"displayClockNameDuration_ms\":\""+String(displayClockNameDuration_ms)+"\",";
json += "\"doNotShowClockNameBeforeAndAfterMinuteChange_s\":\""+String(doNotShowClockNameBeforeAndAfterMinuteChange_s)+"\",";
json += "\"real_hours\":\""+String(hours)+"\",";
json += "\"real_minutes\":\""+String(minutes)+"\",";
json += "\"real_seconds\":\""+String(seconds)+"\",";
json += "\"model_hours\":\""+String(hours)+"\",";
json += "\"model_minutes\":\""+String(minutes)+"\",";
json += "\"model_seconds\":\""+String(seconds)+"\"";
json += "}";
// create json return
String json = "{";
json += "\"clockName\":\""+String(clockName)+"\",";
json += "\"clockSpeed\":\""+String(clockSpeed_modelMsPerRealSec)+"\",";
json += "\"relayHoldTime_ms\":\""+String(R.getHoldTime_ms())+"\",";
json += "\"relayMinOffTime_ms\":\""+String(R.getMinOffTime_ms())+"\",";
json += "\"displayRefresh_ms\":\""+String(displayRefresh_ms)+"\",";
json += "\"displayClockNameEvery_ms\":\""+String(displayClockNameEvery_ms)+"\",";
json += "\"displayClockNameDuration_ms\":\""+String(displayClockNameDuration_ms)+"\",";
json += "\"doNotShowClockNameBeforeAndAfterMinuteChange_s\":\""+String(doNotShowClockNameBeforeAndAfterMinuteChange_s)+"\",";
json += "\"real_hours\":\""+String(hours)+"\",";
json += "\"real_minutes\":\""+String(minutes)+"\",";
json += "\"real_seconds\":\""+String(seconds)+"\",";
json += "\"model_hours\":\""+String(hours)+"\",";
json += "\"model_minutes\":\""+String(minutes)+"\",";
json += "\"model_seconds\":\""+String(seconds)+"\"";
json += "}";
// return json to WebApp
request->send(200, F("text/json"), json);
json = String();
// return json to WebApp
request->send(200, F("text/json"), json);
json = String();
});
server.on("/setDT", HTTP_GET, [](AsyncWebServerRequest *request){
@ -318,8 +319,42 @@ void setupWebServer() {
// return json to WebApp
request->send(200, F("text/json"), json);
json = String();
R.fwdToTime(hours, minutes);
});
server.on("/files", HTTP_GET, [](AsyncWebServerRequest *request){
Serial.println(F("\n Directory of FS requested"));
FSInfo fs_info;
String message = "";
if (!SPIFFS.info(fs_info)) {
message += "Cannot get info about file system! ";
}
// create json return
String json = "{";
if (message.length() > 0) {
json += "\"result\":\"Error\",";
json += "\"message\":\"" + message + "\"";
} else {
json += "\"result\":\"OK\",";
json += "\"files\":\"[";
Dir dir = SPIFFS.openDir("/");
boolean isFirstEntry = true;
while (dir.next()) {
if (isFirstEntry) { isFirstEntry = false; } else { json += ","; }
json += "{\"filename\":\"" + dir.fileName() + "\",\"size\":" + dir.fileSize() + "}";
}
json += "]";
}
json += "}";
// return json to WebApp
request->send(200, F("text/json"), json);
json = String();
});
server.serveStatic("/", SPIFFS, "/");
server.onNotFound(onRequest);
@ -335,8 +370,8 @@ void setup(void)
{
Serial.println(F("setup():"));
setupDisplay();
setupIAS();
setupFS();
setupIAS();
setupWebServer();
delay(200);
R.begin(relay1Pin, relay2Pin);