From de9fc112d0898d76591c2302b0bf97577346f1e1 Mon Sep 17 00:00:00 2001 From: Dirk Jahnke Date: Tue, 29 Jan 2019 11:26:35 +0100 Subject: [PATCH] Added files web service (directory) and fixed setDT service to fastforward the clock. --- src/main.cpp | 105 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 70 insertions(+), 35 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index ecc35df..4f328dc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -29,6 +29,7 @@ #include #include // https://github.com/me-no-dev/AsyncTCP #include +#include #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);