From 52bfedb2aa55e260da4396a5fdef785f4caf4729 Mon Sep 17 00:00:00 2001 From: Dirk Jahnke Date: Tue, 29 Jan 2019 13:54:34 +0100 Subject: [PATCH] Refactoring of Clock and WebServer (separate classes) --- src/Clock.h | 31 +++++++++ src/WebServer.cpp | 137 +++++++++++++++++++++++++++++++++++++ src/WebServer.h | 22 ++++++ src/main.cpp | 171 +++------------------------------------------- 4 files changed, 200 insertions(+), 161 deletions(-) create mode 100644 src/Clock.h create mode 100644 src/WebServer.cpp create mode 100644 src/WebServer.h diff --git a/src/Clock.h b/src/Clock.h new file mode 100644 index 0000000..2fedbc9 --- /dev/null +++ b/src/Clock.h @@ -0,0 +1,31 @@ +#ifndef CLOCK_h_included +#define CLOCK_h_included + +#include + +class Clock { +public: + Clock(String name):clockSpeed_modelMsPerRealSec(1000), clockName(name) {}; + Clock(String name, unsigned int h, unsigned int m):hours(h), minutes(m), clockSpeed_modelMsPerRealSec(1000), clockName(name) {}; + Clock(String name, unsigned int h, unsigned int m, unsigned int s):hours(h), minutes(m), seconds(s), clockSpeed_modelMsPerRealSec(1000), clockName(name) {}; + void addSeconds(unsigned int n) { seconds++; if (seconds >= 60) { addMinutes(1); seconds -= 60; }}; + void addMinutes(unsigned int n) { minutes++; seconds -= 60; if (minutes >= 60) { addHours(1); minutes -= 60; }}; + void addHours(unsigned int n) { hours++; if (hours >= 24) { hours -= 24; }}; + void setTime(unsigned int h, unsigned int m) { hours=h; minutes=m; }; + void setTime(unsigned int h, unsigned int m, unsigned int s) { hours=h; minutes=m; seconds=s; }; + unsigned int getHours() { return hours; }; + unsigned int getMinutes() { return minutes; }; + unsigned int getSeconds() { return seconds; }; + void setSpeed_modelMsPerRealSecond(unsigned int speed) { clockSpeed_modelMsPerRealSec = speed; }; + unsigned int getSpeed_modelMsPerRealSecond() { return clockSpeed_modelMsPerRealSec; }; + void setClockName(char *name) { clockName = String(name); }; + void setClockName(String name) { clockName = name; }; + String getClockName() { return clockName; }; + +protected: + unsigned int hours, minutes, seconds; + unsigned int clockSpeed_modelMsPerRealSec; + String clockName; +}; + +#endif diff --git a/src/WebServer.cpp b/src/WebServer.cpp new file mode 100644 index 0000000..e65c16c --- /dev/null +++ b/src/WebServer.cpp @@ -0,0 +1,137 @@ +#include "WebServer.h" + +static AsyncWebServer server(80); + +//called when the url is not defined here return 404 +static void onRequest(AsyncWebServerRequest *request){ + //Handle Unknown Request + request->send(404); +} + +void WebServer::begin() { + server.on("/fwd", HTTP_GET, [&](AsyncWebServerRequest *request){ + Serial.println(F("\n Move clock forward")); + + relays->toggle(); + + // create json return + String json = "{"; + json += "\"result\":\"OK\","; + json += "\"clockName\":\"" + String(modelTime->getClockName()) + "\","; + json += "\"real_hours\":\""+String(realTime->getHours())+"\","; + json += "\"real_minutes\":\""+String(realTime->getMinutes())+"\","; + json += "\"real_seconds\":\""+String(realTime->getSeconds())+"\","; + json += "\"model_hours\":\""+String(modelTime->getHours())+"\","; + json += "\"model_minutes\":\""+String(modelTime->getMinutes())+"\","; + json += "\"model_seconds\":\""+String(modelTime->getSeconds())+"\""; + json += "}"; + + // 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(modelTime->getClockName())+"\","; + json += "\"clockSpeed\":\""+String(modelTime->getSpeed_modelMsPerRealSecond())+"\","; + json += "\"relayHoldTime_ms\":\""+String(relays->getHoldTime_ms())+"\","; + json += "\"relayMinOffTime_ms\":\""+String(relays->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(realTime->getHours())+"\","; + json += "\"real_minutes\":\""+String(realTime->getMinutes())+"\","; + json += "\"real_seconds\":\""+String(realTime->getSeconds())+"\","; + json += "\"model_hours\":\""+String(modelTime->getHours())+"\","; + json += "\"model_minutes\":\""+String(modelTime->getMinutes())+"\","; + json += "\"model_seconds\":\""+String(modelTime->getSeconds())+"\""; + json += "}"; + + // return json to WebApp + request->send(200, F("text/json"), json); + json = String(); + }); + + server.on("/setDT", HTTP_GET, [&](AsyncWebServerRequest *request){ + String h, m, message; + Serial.println(F("\n Setting displayed time of clock")); + + message = ""; + if (request->hasParam("h")) { + h = request->getParam("h")->value(); + } else { + message += "Parameter h for hours missing. "; + } + if (request->hasParam("m")) { + m = request->getParam("m")->value(); + } else { + message += "Parameter m for minutes missing. "; + } + + relays->setDisplayedTime(h.toInt(), m.toInt()); + h = String(); + m = String(); + + // create json return + String json = "{"; + if (message.length() > 0) { + json += "\"result\":\"Error\","; + json += "\"message\": \"" + message + "\""; + } else { + json += "\"result\":\"OK\""; + } + json += "}"; + + // return json to WebApp + request->send(200, F("text/json"), json); + json = String(); + relays->fwdToTime(realTime->getHours(), realTime->getMinutes()); + }); + + 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); + + // start the HTTP server + server.begin(); + Serial.print(F("HTTP server started at: ")); + Serial.println(WiFi.localIP()); + Serial.println(""); +} diff --git a/src/WebServer.h b/src/WebServer.h new file mode 100644 index 0000000..22fda44 --- /dev/null +++ b/src/WebServer.h @@ -0,0 +1,22 @@ +#ifndef WebServer_h_included +#define WebServer_h_included + +#include +#include // https://github.com/me-no-dev/AsyncTCP +#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) {}; + void begin(); + void loop(); + +protected: + Relays *relays; + Clock *realTime, *modelTime; +}; + +#endif diff --git a/src/main.cpp b/src/main.cpp index 4f328dc..ad265c0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,43 +27,28 @@ #include #include "MD_RobotEyes.h" #include -#include // https://github.com/me-no-dev/AsyncTCP -#include +#include "WebServer.h" #include #include "Relays.h" +#include "Clock.h" IOTAppStory IAS(COMPDATE, MODEBUTTON); String deviceName = "wemosMatrixDisplay"; String chipId; -// Define the number of devices we have in the chain and the hardware interface -// NOTE: These pin numbers will probably not work with your hardware and may -// need to be adapted - -/* Mapper result, connector to ESP is at right (backside): -Your responses produce these hardware parameters - -HW_DIG_ROWS 1 -HW_REV_COLS 0 -HW_REV_ROWS 0 - -Your hardware matches the setting for FC-16 modules. Please set FC16_HW. - -*/ - #define HARDWARE_TYPE MD_MAX72XX::FC16_HW -#define MAX_DEVICES 4 +#define MAX_DISPLAY_DEVICES 4 +Clock realTime("real"); +Clock modelTime("Fremo"); -// Hardware SPI connection -// MD_Parola P = MD_Parola(HARDWARE_TYPE, DISPLAY_CS_PIN, MAX_DEVICES); -// Arbitrary output pins -MD_Parola P = MD_Parola(HARDWARE_TYPE, DISPLAY_DATA_PIN, DISPLAY_CLK_PIN, DISPLAY_CS_PIN, MAX_DEVICES); +MD_Parola P = MD_Parola(HARDWARE_TYPE, DISPLAY_DATA_PIN, DISPLAY_CLK_PIN, DISPLAY_CS_PIN, MAX_DISPLAY_DEVICES); MD_RobotEyes E; Relays R; +WebServer W(&R, &realTime, &modelTime); WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 3600, 60000); -AsyncWebServer server(80); + // Field default values char *clockName = "FREMO"; @@ -229,12 +214,6 @@ void setupDisplay() { P.print(intro); } -//called when the url is not defined here return 404 -void onRequest(AsyncWebServerRequest *request){ - //Handle Unknown Request - request->send(404); -} - void setupFS() { if(!SPIFFS.begin()){ Serial.println(F(" SPIFFS Mount Failed")); @@ -242,138 +221,14 @@ void setupFS() { } } -void setupWebServer() { - server.on("/fwd", HTTP_GET, [](AsyncWebServerRequest *request){ - Serial.println(F("\n WebApp button pressed -- move clock forward")); - - 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 += "}"; - - // 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 += "}"; - - // return json to WebApp - request->send(200, F("text/json"), json); - json = String(); - }); - - server.on("/setDT", HTTP_GET, [](AsyncWebServerRequest *request){ - String h, m, message; - Serial.println(F("\n Setting displayed time of clock")); - - message = ""; - if (request->hasParam("h")) { - h = request->getParam("h")->value(); - } else { - message += "Parameter h for hours missing. "; - } - if (request->hasParam("m")) { - m = request->getParam("m")->value(); - } else { - message += "Parameter m for minutes missing. "; - } - - R.setDisplayedTime(h.toInt(), m.toInt()); - h = String(); - m = String(); - - // create json return - String json = "{"; - if (message.length() > 0) { - json += "\"result\":\"Error\","; - json += "\"message\": \"" + message + "\""; - } else { - json += "\"result\":\"OK\""; - } - json += "}"; - - // 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); - - // start the HTTP server - server.begin(); - Serial.print(F("HTTP server started at: ")); - Serial.println(WiFi.localIP()); - Serial.println(""); -} - - void setup(void) { Serial.println(F("setup():")); setupDisplay(); setupFS(); setupIAS(); - setupWebServer(); - delay(200); + W.begin(); + delay(100); R.begin(relay1Pin, relay2Pin); timeClient.begin(); Serial.println(F("setup() finished")); @@ -478,12 +333,7 @@ void reInitializeDisplay() { void loop(void) { - int currentDisplayState; static int lastMinutes = 0; - static int lastSeconds = 0; - #define MsgSize 10 - static char debugMsg[MsgSize+1]; - static int recentDisplayState = -1; static unsigned long firstLoop_ts = 0; if (firstLoop_ts == 0) firstLoop_ts = millis(); @@ -562,7 +412,6 @@ void loop(void) R.toggle(); lastMinutes = minutes; } - lastSeconds = seconds; R.loop(); }