Massive changes. Added a quite nice webinterface with live data using WebSockets. Removed the FTP server (wasn't that useful anyways). JSON creating using ArduinoJson instead of String concatenation. Ans, and, and.

This commit is contained in:
2019-11-16 23:03:13 +01:00
parent e471a57578
commit b9a4770ff2
21 changed files with 620 additions and 2806 deletions

View File

@@ -1,32 +1,23 @@
/*
#include "http_server.h"
#include <ESPmDNS.h>
HTTPServer::HTTPServer(Player* p, Controller* c) {
_player = p;
_controller = c;
_http_server = new ESP8266WebServer(80);
//_http_server->onFileUpload([&]() { _handle_upload(); yield();});
_http_server->on("/upload", HTTP_POST, [&]() {
_http_server->sendHeader("Connection", "close");
_http_server->send(200, "text/plain", "OK");
}, [&]() {
_handle_upload();
yield();
});
_http_server->on("/", HTTP_GET, [&](){ _handle_index(); });
_http_server->on("/status", HTTP_GET, [&](){ _handle_status(); });
_http_server->begin();
_server = new AsyncWebServer(80);
ws = new AsyncWebSocket("/ws");
_server->addHandler(ws);
ws->onEvent([&](AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){this->_onEvent(server, client, type, arg, data, len);});
_server->on("/", [&](AsyncWebServerRequest* req) {_handle_index(req);});
_server->begin();
MDNS.addService("http", "tcp", 80);
}
void HTTPServer::_handle_upload() {
void HTTPServer::_handle_upload(AsyncWebServerRequest* request, String filename, size_t index, uint8_t* data, size_t len, bool final) {
// https://www.gnu.org/software/tar/manual/html_node/Standard.html
// https://www.mkssoftware.com/docs/man4/tar.4.asp
HTTPUpload* upload = &_http_server->upload();
DEBUG("_handle_upload Status: %d, length: %d\n", upload->status, upload->currentSize);
if (upload->status == UPLOAD_FILE_START) {
if (index == 0) { // Starting upload
_chunk = new uint8_t[512];
_chunk_length = 0;
_upload_position = 0;
@@ -35,19 +26,13 @@ void HTTPServer::_handle_upload() {
_need_header = true;
}
if (upload->status == UPLOAD_FILE_END || upload->status == UPLOAD_FILE_ABORTED) {
// Close the file
delete _chunk;
return;
}
uint32_t upload_offset = 0;
while (upload_offset < upload->currentSize) {
while (upload_offset < len) {
// Load a chunk
if (_chunk_length < 512 && upload->currentSize > upload_offset) {
if (_chunk_length < 512 && len > upload_offset) {
uint16_t needed = 512 - _chunk_length;
if (needed > upload->currentSize - upload_offset) needed = upload->currentSize - upload_offset;
memcpy(_chunk + _chunk_length, upload->buf + upload_offset, needed);
if (needed > len - upload_offset) needed = len - upload_offset;
memcpy(_chunk + _chunk_length, data + upload_offset, needed);
_chunk_length += needed;
upload_offset += needed;
_upload_position += needed;
@@ -97,29 +82,30 @@ void HTTPServer::_handle_upload() {
_chunk_length = 0;
}
}
}
if (final == true) {
// Close the file
delete _chunk;
return;
}
}
void HTTPServer::_handle_index() {
String response = String("<html><head><title>ESMP3</title><script>function play_album(e) {}</script></head><body>");
response.concat("Albums on SD card:<table>");
std::list<String> files = _player->ls("/", false, true, false);
for(std::list<String>::iterator it=files.begin(); it!=files.end(); it++) {
response.concat("<tr><td>");
response.concat(*it);
response.concat("</td><td><a href='#' onclick='play_album();'>Play</a></td></tr>\n");
void HTTPServer::_onEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) {
if (type==WS_EVT_CONNECT) {
_controller->inform_new_client(client);
} else if (type==WS_EVT_DATA) {
AwsFrameInfo* info = (AwsFrameInfo*) arg;
if (info->final && info->index==0 && info->len==len && info->opcode==WS_TEXT) {
DEBUG("Received ws message: %s\n", (char*)data);
_controller->queue_command((char*)data);
}
}
response.concat("</table></body></html>");
_http_server->send(200, "text/html", response);
}
void HTTPServer::_handle_status() {
_http_server->send(200, "application/json", _controller->get_status_json());
}
void HTTPServer::_handle_index(AsyncWebServerRequest* r) {
void HTTPServer::loop() {
_http_server->handleClient();
MDNS.update();
}
*/
#include "index.html"
r->send(200, "text/html", html);
}