updates to http terminal

This commit is contained in:
2021-06-30 20:33:16 +02:00
parent a0f6a5aec3
commit 66e8ddb2d9
7 changed files with 64 additions and 42 deletions
+33 -9
View File
@@ -6,8 +6,8 @@ bool HTTPServer::start() {
logf("cant mount filesystem, EXIT !\n\r");
return false;
}
logf("[HTTPServer] LittleFS mounted !\n\r");
logf("[HTTPServer] root:\n\r");
logf("LittleFS mounted !\n\r");
logf("root:\n\r");
this->listRoot();
logf("\n\r");
@@ -30,15 +30,39 @@ bool HTTPServer::start() {
// add static root file handler for http
this->serveStatic("/", LittleFS, "/");
this->begin();
Serial.printf("Server active on Port 80 !\n\r");
return true;
}
void HTTPServer::logf(const char *format, ...) {
va_list args;
va_start(args, format);
char temp[64];
size_t len = vsnprintf(temp, sizeof(temp), format, args);
Serial.print(log_prefix);
Serial.write(temp, len);
va_end(args);
}
void HTTPServer::logt(const char *format, ...) {
va_list args;
va_start(args, format);
// append logging string to local buffer
if(tbuf_head < TBUF_LEN) {
size_t len = vsnprintf(tbuf+tbuf_head, TBUF_LEN-tbuf_head, format, args);
tbuf_head += len;
}
va_end(args);
}
void HTTPServer::start_apps() {
// application handler
this->on("/app=terminal", HTTP_POST, [this]() {
String log_msg = "terminal: millis: ";
log_msg += millis();
send(200, "text/plain", log_msg);
if(tbuf_head) {
send(200, "text/plain", tbuf);
tbuf_head = 0;
}
});
this->begin();
Serial.printf("[HTTPServer] Server active on Port 80 !\n\r");
return true;
}
+14 -10
View File
@@ -2,6 +2,8 @@
#pragma once
#include <vector>
#include <ESP8266WebServer.h>
#include <stdarg.h>
#include "filesys.h"
@@ -9,24 +11,22 @@
// debug log <ESP8266WebServer.h>
// #define DEBUGV(f,...) do { Serial.printf(PSTR(f), ##__VA_ARGS__); } while (0)
#define LOG_STR "[HTTPServer]"
#define TBUF_LEN 256
class HTTPServer : public ESP8266WebServer {
private:
const char* rootDir = "/";
const char* log_prefix = "[HTTPServer] ";
size_t tbuf_head = 0;
char tbuf[TBUF_LEN];
void listRoot() {
ls(rootDir);
}
void logf(const char *format, ...) {
va_list args;
va_start(args, format);
Serial.print(LOG_STR);
Serial.printf(format, args);
va_end(args);
}
public:
HTTPServer(const int _port, const char* _rootDir) :
@@ -35,7 +35,11 @@ public:
~HTTPServer()
{
Serial.printf("[HTTPServer] shut down ...\n\r");
}
}
bool start();
void start_apps();
void logf(const char *format, ...);
void logt(const char *format, ...);
};