pitrix/src/http_server.cpp

135 lines
4.0 KiB
C++

#include "config.h"
#ifdef HTTP_SERVER_ENABLE
#include "http_server.h"
#include "effects.h"
#include "my_wifi.h"
#include "prototypes.h"
#include <FS.h>
#if defined( ESP8266 )
ESP8266WebServer http_server(HTTP_SERVER_PORT);
#elif defined( ESP32 )
ESP32WebServer http_server(HTTP_SERVER_PORT);
#endif
File upload_file;
void http_server_handle_file_upload() {
if (http_server.uri() != "/upload") return;
HTTPUpload upload = http_server.upload();
if (upload.status == UPLOAD_FILE_START) {
String filename = upload.filename;
if (!filename.startsWith("/")) filename = "/" + filename;
LOGln("HTTP * Upload of %s starting...", upload.filename.c_str());
upload_file = SPIFFS.open(filename, "w");
} else if (upload.status == UPLOAD_FILE_WRITE) {
if (upload_file) upload_file.write(upload.buf, upload.currentSize);
} else if (upload.status == UPLOAD_FILE_END) {
if (upload_file) upload_file.close();
LOGln("HTTP * Upload of %s with %d bytes done.", upload.filename.c_str(), upload.totalSize);
}
}
void http_server_400() {
http_server.send(400);
}
void http_server_setup() {
PGM_P text_plain = PSTR("text/plain");
http_server.on("/", HTTP_GET, [&](){
LOGln("HTTP * GET /");
String message = "<html><head><title>Pitrix</title></head><body><h1>Pitrix</h1><p>Known animations:</p>";
if (!SPIFFS.begin()) {
message += "<strong>No SPIFFS file system found.</strong>";
} else {
message += "<ul>";
Dir dir = SPIFFS.openDir("/");
while (dir.next()) {
message += "<li>" + dir.fileName() + " (<a href='/delete?" + dir.fileName() + "'>delete</a>)</li>";
}
message += "</ul>";
message += "<form action='/upload' method='POST'><input type='file' name='file' /><input type='submit' value='Upload file' /></form>";
}
message += "</body></html>";
http_server.send(200, "text/html", message);
});
http_server.on("/delete", HTTP_GET, [&]() {
LOGln("HTTP * GET /delete");
if (http_server.args()==0) {
http_server.send_P(400, text_plain, PSTR("No filename given"));
return;
}
String file = http_server.arg(0);
if (file == "/") {
http_server.send_P(400, text_plain, PSTR("Invalid path"));
return;
}
if (!SPIFFS.exists(file)) {
http_server.send_P(400, text_plain, PSTR("File does not exist."));
return;
}
SPIFFS.remove(file);
http_server.send_P(200, text_plain, PSTR("OK"));
});
http_server.on("/upload", HTTP_POST, []() {
LOGln("HTTP * POST /upload");
http_server.send(200, "text/plain", "OK");
}, http_server_handle_file_upload);
http_server.on("/free_heap", HTTP_GET, [&](){
LOGln("HTTP * GET /free_heap");
http_server.send(200, "text/plain", String(ESP.getFreeHeap()));
});
http_server.on("/uptime", HTTP_GET, [&](){
LOGln("HTTP * GET /uptime");
http_server.send(200, "text/plain", String(millis()/1000));
});
http_server.on("/fps", HTTP_GET, [](){
LOGln("HTTP * GET /fps");
http_server.send(200, "text/plain", String(FastLED.getFPS()));
});
http_server.on("/reboot", HTTP_POST, [](){
LOGln("HTTP * POST /reboot");
ESP.restart();
});
http_server.on("/brightness", HTTP_POST, [&](){
LOGln("HTTP * POST /brightness with value %s", http_server.arg("plain").c_str());
if (!http_server.hasArg("plain")) {
http_server.send_P(400, text_plain, PSTR("No brightness given"));
return;
}
long val = http_server.arg("plain").toInt();
if (val==0 || val>255) {
http_server.send_P(400, text_plain, PSTR("New value out of bounds. (1-255)"));
return;
}
FastLED.setBrightness(val);
http_server.send(200, "text/plain", "OK");
});
http_server.on("/mode", HTTP_POST, [&](){
LOGln("HTTP * POST /mode with value %s", http_server.arg("plain").c_str());
if (!http_server.hasArg("plain")) {
http_server.send_P(400, text_plain, PSTR("No effect given."));
return;
}
String val = http_server.arg("plain");
if (change_current_effect(val)) {
http_server.send(200, "text/plain", "OK");
} else {
http_server.send_P(400, text_plain, PSTR("Unknown effect."));
}
});
http_server.begin();
MDNS.addService("_http", "_tcp", 80);
}
void http_server_loop() {
http_server.handleClient();
}
#endif