2019-11-17 01:16:46 +01:00
|
|
|
/* HTTP Server setup and handler functions */
|
|
|
|
|
2019-11-20 22:42:35 +01:00
|
|
|
#include <SPIFFS.h>
|
|
|
|
|
2019-11-17 01:16:46 +01:00
|
|
|
#include "HTTPServer.h"
|
|
|
|
|
|
|
|
#include "Fader.h"
|
|
|
|
|
|
|
|
HTTPServer::HTTPServer(void)
|
|
|
|
: m_fader(NULL)
|
|
|
|
{
|
|
|
|
m_server = new httpsserver::HTTPServer();
|
|
|
|
}
|
|
|
|
|
2019-11-20 22:42:35 +01:00
|
|
|
void HTTPServer::serveFile(String filename, httpsserver::HTTPResponse *res)
|
2019-11-17 01:16:46 +01:00
|
|
|
{
|
2019-11-20 22:42:35 +01:00
|
|
|
uint8_t buf[1024];
|
|
|
|
File f = SPIFFS.open(filename.c_str(), "r");
|
|
|
|
|
|
|
|
size_t nread = 1;
|
|
|
|
while(nread > 0) {
|
|
|
|
nread = f.readBytes(reinterpret_cast<char*>(buf), 1024);
|
|
|
|
if(nread <= 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
res->write(buf, nread);
|
|
|
|
}
|
2019-11-17 01:16:46 +01:00
|
|
|
|
2019-11-20 22:42:35 +01:00
|
|
|
f.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPServer::handleRoot(httpsserver::HTTPRequest *req, httpsserver::HTTPResponse *res)
|
|
|
|
{
|
|
|
|
serveFile("/index.html", res);
|
|
|
|
res->setHeader("Content-Type", "text/html");
|
2019-11-17 01:16:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void error400(httpsserver::HTTPResponse *res, const std::string &reason)
|
|
|
|
{
|
|
|
|
res->println("Error 400: Bad Request");
|
|
|
|
res->println(reason.c_str());
|
|
|
|
|
|
|
|
res->setStatusCode(400);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPServer::handleColor(httpsserver::HTTPRequest *req, httpsserver::HTTPResponse *res)
|
|
|
|
{
|
|
|
|
res->setHeader("Content-Type", "text/plain");
|
|
|
|
|
|
|
|
httpsserver::ResourceParameters * params = req->getParams();
|
|
|
|
|
|
|
|
if(!params->isRequestParameterSet("color")) {
|
|
|
|
error400(res, "Parameter 'color' not set.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string colorstr = params->getRequestParameter("color");
|
|
|
|
|
|
|
|
if(colorstr.length() != 8) {
|
|
|
|
error400(res, "Wrong length of color string (expected 8 hex digits).");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t color = 0;
|
|
|
|
|
|
|
|
for(size_t i = 0; i < 8; i++) {
|
|
|
|
color <<= 4;
|
|
|
|
|
|
|
|
char c = colorstr[i];
|
|
|
|
if(c >= 'A' && c <= 'F') {
|
|
|
|
color |= c - 'A' + 10;
|
|
|
|
} else if(c >= 'a' && c <= 'f') {
|
|
|
|
color |= c - 'a' + 10;
|
|
|
|
} else if(c >= '0' && c <= '9') {
|
|
|
|
color |= c - '0';
|
|
|
|
} else {
|
|
|
|
error400(res, "Invalid character in color string (allowed: 0-9, a-f, A-F)");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t r = (color & 0xFF000000) >> 24;
|
|
|
|
uint8_t g = (color & 0x00FF0000) >> 16;
|
|
|
|
uint8_t b = (color & 0x0000FF00) >> 8;
|
|
|
|
uint8_t w = (color & 0x000000FF) >> 0;
|
|
|
|
|
|
|
|
res->print("Color changed to r:");
|
|
|
|
res->print(r);
|
|
|
|
res->print(" g:");
|
|
|
|
res->print(g);
|
|
|
|
res->print(" b:");
|
|
|
|
res->print(b);
|
|
|
|
res->print(" w:");
|
|
|
|
res->println(w);
|
|
|
|
|
|
|
|
HTTPServer::instance().m_fader->fade_color({r,g,b,w});
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPServer::handle404(httpsserver::HTTPRequest *req, httpsserver::HTTPResponse *res)
|
|
|
|
{
|
|
|
|
req->discardRequestBody();
|
|
|
|
|
|
|
|
res->setStatusCode(404);
|
|
|
|
res->setHeader("Content-Type", "text/plain");
|
|
|
|
|
|
|
|
res->println("Error 404: Not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPServer::serverTask(void *arg)
|
|
|
|
{
|
|
|
|
httpsserver::ResourceNode *nodeRoot = new httpsserver::ResourceNode("/", "GET",
|
|
|
|
HTTPServer::handleRoot);
|
|
|
|
httpsserver::ResourceNode *nodeColor = new httpsserver::ResourceNode("/color", "GET",
|
|
|
|
HTTPServer::handleColor);
|
|
|
|
httpsserver::ResourceNode *node404 = new httpsserver::ResourceNode("", "GET",
|
|
|
|
HTTPServer::handle404);
|
|
|
|
|
|
|
|
HTTPServer &server = HTTPServer::instance();
|
|
|
|
|
|
|
|
server.m_server->registerNode(nodeRoot);
|
|
|
|
server.m_server->registerNode(nodeColor);
|
|
|
|
server.m_server->setDefaultNode(node404);
|
|
|
|
|
|
|
|
Serial.println("[server] Starting HTTP Server...");
|
|
|
|
server.m_server->start();
|
|
|
|
if (server.m_server->isRunning())
|
|
|
|
{
|
|
|
|
Serial.println("[server] Server ready.");
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
server.m_server->loop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Serial.println("[server] Server died?! This should never happen.");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HTTPServer::start(void)
|
|
|
|
{
|
|
|
|
xTaskCreate(
|
|
|
|
HTTPServer::serverTask, /* Task function. */
|
|
|
|
"HTTP Server Task", /* name of task. */
|
|
|
|
6144, /* Stack size of task */
|
|
|
|
NULL, /* parameter of the task */
|
|
|
|
1, /* priority of the task */
|
|
|
|
NULL); /* Task handle to keep track of created task */
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|