Refactoring of Clock and WebServer (separate classes)

This commit is contained in:
Dirk Jahnke 2019-01-29 13:54:34 +01:00
parent de9fc112d0
commit 52bfedb2aa
4 changed files with 200 additions and 161 deletions

31
src/Clock.h Normal file
View File

@ -0,0 +1,31 @@
#ifndef CLOCK_h_included
#define CLOCK_h_included
#include <Arduino.h>
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

137
src/WebServer.cpp Normal file
View File

@ -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("");
}

22
src/WebServer.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef WebServer_h_included
#define WebServer_h_included
#include <Arduino.h>
#include <ESPAsyncTCP.h> // https://github.com/me-no-dev/AsyncTCP
#include <ESPAsyncWebServer.h>
#include <FS.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) {};
void begin();
void loop();
protected:
Relays *relays;
Clock *realTime, *modelTime;
};
#endif

View File

@ -27,43 +27,28 @@
#include <WiFiUdp.h> #include <WiFiUdp.h>
#include "MD_RobotEyes.h" #include "MD_RobotEyes.h"
#include <FS.h> #include <FS.h>
#include <ESPAsyncTCP.h> // https://github.com/me-no-dev/AsyncTCP #include "WebServer.h"
#include <ESPAsyncWebServer.h>
#include <ESP8266HTTPClient.h> #include <ESP8266HTTPClient.h>
#include "Relays.h" #include "Relays.h"
#include "Clock.h"
IOTAppStory IAS(COMPDATE, MODEBUTTON); IOTAppStory IAS(COMPDATE, MODEBUTTON);
String deviceName = "wemosMatrixDisplay"; String deviceName = "wemosMatrixDisplay";
String chipId; 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 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_DATA_PIN, DISPLAY_CLK_PIN, DISPLAY_CS_PIN, MAX_DISPLAY_DEVICES);
// 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_RobotEyes E; MD_RobotEyes E;
Relays R; Relays R;
WebServer W(&R, &realTime, &modelTime);
WiFiUDP ntpUDP; WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 3600, 60000); NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 3600, 60000);
AsyncWebServer server(80);
// Field default values // Field default values
char *clockName = "FREMO"; char *clockName = "FREMO";
@ -229,12 +214,6 @@ void setupDisplay() {
P.print(intro); 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() { void setupFS() {
if(!SPIFFS.begin()){ if(!SPIFFS.begin()){
Serial.println(F(" SPIFFS Mount Failed")); 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) void setup(void)
{ {
Serial.println(F("setup():")); Serial.println(F("setup():"));
setupDisplay(); setupDisplay();
setupFS(); setupFS();
setupIAS(); setupIAS();
setupWebServer(); W.begin();
delay(200); delay(100);
R.begin(relay1Pin, relay2Pin); R.begin(relay1Pin, relay2Pin);
timeClient.begin(); timeClient.begin();
Serial.println(F("setup() finished")); Serial.println(F("setup() finished"));
@ -478,12 +333,7 @@ void reInitializeDisplay() {
void loop(void) void loop(void)
{ {
int currentDisplayState;
static int lastMinutes = 0; 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; static unsigned long firstLoop_ts = 0;
if (firstLoop_ts == 0) firstLoop_ts = millis(); if (firstLoop_ts == 0) firstLoop_ts = millis();
@ -562,7 +412,6 @@ void loop(void)
R.toggle(); R.toggle();
lastMinutes = minutes; lastMinutes = minutes;
} }
lastSeconds = seconds;
R.loop(); R.loop();
} }