esp32-sk6812/src/HTTPServer.cpp

135 lines
3.8 KiB
C++
Raw Normal View History

/* HTTP Server setup and handler functions */
#include "HTTPServer.h"
#include "Fader.h"
HTTPServer::HTTPServer(void)
: m_fader(NULL)
{
m_server = new httpsserver::HTTPServer();
}
void HTTPServer::handleRoot(httpsserver::HTTPRequest *req, httpsserver::HTTPResponse *res)
{
res->setHeader("Content-Type", "text/plain");
res->println("Hello World!");
res->print("Uptime: ");
res->print(millis());
res->println(" ms");
}
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;
}