2021-06-23 16:44:37 +03:00
|
|
|
#include "server.hpp"
|
2021-06-28 11:29:15 +03:00
|
|
|
#include "collector.hpp"
|
2021-06-28 11:36:43 +03:00
|
|
|
#include "serial.hpp"
|
2021-06-23 16:44:37 +03:00
|
|
|
|
2021-06-28 11:36:43 +03:00
|
|
|
#include <FS.h>
|
2021-06-23 16:44:37 +03:00
|
|
|
|
|
|
|
|
|
|
|
String getContentType(String filename){
|
2021-06-27 21:46:11 +03:00
|
|
|
if(filename.indexOf(".htm") > 0) return "text/html";
|
|
|
|
else if(filename.indexOf(".html")> 0) return "text/html";
|
|
|
|
else if(filename.indexOf(".css")> 0) return "text/css";
|
|
|
|
else if(filename.indexOf(".js")> 0) return "application/javascript";
|
|
|
|
else if(filename.indexOf(".png")> 0) return "image/png";
|
|
|
|
else if(filename.indexOf(".gif")> 0) return "image/gif";
|
|
|
|
else if(filename.indexOf(".jpg")> 0) return "image/jpeg";
|
|
|
|
else if(filename.indexOf(".ico")> 0) return "image/x-icon";
|
|
|
|
else if(filename.indexOf(".xml")> 0) return "text/xml";
|
|
|
|
else if(filename.indexOf(".pdf")> 0) return "application/x-pdf";
|
2021-06-23 16:44:37 +03:00
|
|
|
return "text/plain";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool handleFileRead(String path ){
|
2021-06-28 11:36:43 +03:00
|
|
|
DEBUG_OUT.println("handleFileRead: " + path);
|
2021-06-25 09:14:06 +03:00
|
|
|
if(path.endsWith("/")) path += "index.html";
|
2021-06-27 21:46:11 +03:00
|
|
|
String contentType = getContentType(path) + ";charset=utf-8";
|
|
|
|
if(LittleFS.exists(path)){
|
2021-06-28 11:36:43 +03:00
|
|
|
DEBUG_OUT.println("handle: " + path);
|
2021-06-23 16:44:37 +03:00
|
|
|
File file = LittleFS.open(path, "r");
|
2021-06-27 21:46:11 +03:00
|
|
|
if(path.endsWith(".gz")){
|
|
|
|
//server.sendHeader("Content-Encoding" , "gzip");
|
|
|
|
} else {
|
|
|
|
contentType += ";charset=utf-8";
|
|
|
|
}
|
|
|
|
server.streamFile(file, contentType);
|
2021-06-23 16:44:37 +03:00
|
|
|
file.close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-28 11:29:15 +03:00
|
|
|
void getWeekly() {
|
2021-06-28 11:36:43 +03:00
|
|
|
DEBUG_OUT.println("Weekly start");
|
2021-06-28 11:29:15 +03:00
|
|
|
String data = collector.week_data();
|
2021-06-28 11:36:43 +03:00
|
|
|
DEBUG_OUT.println("Weekly end");
|
2021-06-28 11:29:15 +03:00
|
|
|
server.send(200, "text/html", data);
|
2021-06-23 16:44:37 +03:00
|
|
|
}
|
2021-06-28 11:29:15 +03:00
|
|
|
void getMonthly() {
|
2021-06-28 11:36:43 +03:00
|
|
|
DEBUG_OUT.println("Monthly start");
|
2021-06-28 11:29:15 +03:00
|
|
|
String data = collector.month_data();
|
2021-06-28 11:36:43 +03:00
|
|
|
DEBUG_OUT.println("Monthly end");
|
2021-06-28 11:29:15 +03:00
|
|
|
server.send(200, "text/html", data);
|
2021-06-27 21:46:11 +03:00
|
|
|
}
|
|
|
|
|
2021-06-23 16:44:37 +03:00
|
|
|
void server_setup(){
|
2021-06-27 21:46:11 +03:00
|
|
|
LittleFS.begin();
|
2021-06-28 11:29:15 +03:00
|
|
|
server.on("/weekly", getWeekly);
|
|
|
|
server.on("/monthly", getMonthly);
|
2021-06-27 21:46:11 +03:00
|
|
|
|
|
|
|
server.begin();
|
2021-06-28 11:36:43 +03:00
|
|
|
DEBUG_OUT.println("HTTP server started");
|
2021-06-27 21:46:11 +03:00
|
|
|
|
|
|
|
server.onNotFound([](){
|
|
|
|
if(!handleFileRead(server.uri()))
|
|
|
|
server.send(404, "text/plain", "FileNotFound");
|
|
|
|
});
|
2021-06-23 16:44:37 +03:00
|
|
|
}
|
2021-06-27 21:46:11 +03:00
|
|
|
|
|
|
|
void server_loop() {
|
|
|
|
server.handleClient();
|
2021-06-28 11:36:43 +03:00
|
|
|
}
|