From 2820869e141132c06f81ad82977896cbb71a508d Mon Sep 17 00:00:00 2001 From: Simon Schmidt Date: Sat, 10 Jul 2021 04:12:50 +0200 Subject: [PATCH] some formating, add variadic templ for logf() --- include/ota.h | 5 +- lib/httpserver/filesys.cpp | 4 +- lib/httpserver/httpserver.cpp | 138 ++++++++++++++++------------------ lib/httpserver/httpserver.h | 44 +++++------ 4 files changed, 91 insertions(+), 100 deletions(-) diff --git a/include/ota.h b/include/ota.h index 9df5c0b..8a0fb76 100644 --- a/include/ota.h +++ b/include/ota.h @@ -1,4 +1,5 @@ -#pragma once +#ifndef __OTA_H +#define __OTA_H #include #include @@ -46,3 +47,5 @@ void ota_setup() { ArduinoOTA.begin(); } + +#endif // __OTA_H diff --git a/lib/httpserver/filesys.cpp b/lib/httpserver/filesys.cpp index 1759a79..5801683 100644 --- a/lib/httpserver/filesys.cpp +++ b/lib/httpserver/filesys.cpp @@ -2,7 +2,7 @@ FSInfo fsinfo; -bool mount_fs() { +auto mount_fs() -> bool { if(!LittleFS.begin()) { Serial.println("[ERROR] LittleFS.info(), reset ..."); return false; @@ -25,7 +25,7 @@ bool mount_fs() { return true; } -bool format_fs() { +auto format_fs() -> bool { printf("Formatting FS ! \n\r"); return LittleFS.format(); } diff --git a/lib/httpserver/httpserver.cpp b/lib/httpserver/httpserver.cpp index c9820a7..3ea27d4 100644 --- a/lib/httpserver/httpserver.cpp +++ b/lib/httpserver/httpserver.cpp @@ -1,85 +1,79 @@ #include "httpserver.h" +auto HTTPServer::start() -> bool { + if (!mount_fs()) { + logf("cant mount filesystem, EXIT !\n\r"); + return false; + } -bool HTTPServer::start() { - if(!mount_fs()) { - logf("cant mount filesystem, EXIT !\n\r"); - return false; + logf("LittleFS mounted !\n\r"); + logf("root:\n\r"); + this->listRoot(); + logf("\n\r"); + + // default handler + this->onNotFound([this]() { + String message = "File Not Found\n\n"; + message += "URI: "; + message += uri(); + message += "\nMethod: "; + message += (method() == HTTP_GET) ? "GET" : "POST"; + message += "\nArguments: "; + message += args(); + message += "\n"; + + for (uint8_t i = 0; i < args(); i++) { + message += " " + argName(i) + ": " + arg(i) + "\n"; } - logf("LittleFS mounted !\n\r"); - logf("root:\n\r"); - this->listRoot(); - logf("\n\r"); + send(404, "text/plain", message); + }); - // default handler - this->onNotFound([this]() { - String message = "File Not Found\n\n"; - message += "URI: "; - message += uri(); - message += "\nMethod: "; - message += (method() == HTTP_GET) ? "GET" : "POST"; - message += "\nArguments: "; - message += args(); - message += "\n"; - - for (uint8_t i = 0; i < args(); i++) { - message += " " + argName(i) + ": " + arg(i) + "\n"; - } - send(404, "text/plain", message); - }); - - // 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); + // 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::start_apps() { - // application handler + // application handler - this->on("/update", HTTP_POST, [this]() { - if(args()) { - for(int i=0; iset_idle_prozent(arg(i).toInt()); - } - } + this->on("/update", HTTP_POST, [this]() { + if (args()) { + for (int i = 0; i < args() - 1; i++) { + Serial.printf("%s=%s\n", argName(i).c_str(), arg(i).c_str()); + if (argName(i).equals("range_idl_pwm")) { + treppe->set_idle_prozent(arg(i).toInt()); } - send(200, "text/plain", "accepted"); - }); + } + } + send(200, "text/plain", "accepted"); + }); - this->on("/terminal", HTTP_POST, [this]() { - // Serial.printf("got /terminal\n"); - if(tbuf_head) { - send(200, "text/plain", tbuf); - tbuf_head = 0; - } - else { - send(202, "text/plain", ""); - } - }); + this->on("/terminal", HTTP_POST, [this]() { + // Serial.printf("got /terminal\n"); + if (tbuf_head) { + send(200, "text/plain", tbuf); + tbuf_head = 0; + } else { + send(202, "text/plain", ""); + } + }); +} +template +void HTTPServer::logf(const char *format, Args... args) const { + Serial.print(log_prefix); + Serial.printf(format, 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) { + tbuf_head += + vsnprintf(tbuf + tbuf_head, TBUF_LEN - tbuf_head, format, args); + } + va_end(args); } \ No newline at end of file diff --git a/lib/httpserver/httpserver.h b/lib/httpserver/httpserver.h index 0db5feb..751149b 100644 --- a/lib/httpserver/httpserver.h +++ b/lib/httpserver/httpserver.h @@ -2,45 +2,39 @@ #define __HTTPSERVER_H // Wrapper for ESP8266WebServer with Filesystem as HTTP source -#include -#include #include "filesys.h" #include "treppe.h" +#include +#include // debug log // #define DEBUGV(f,...) do { Serial.printf(PSTR(f), ##__VA_ARGS__); } while (0) #define TBUF_LEN 256 - class HTTPServer : public ESP8266WebServer { private: - const char* rootDir = "/"; - const char* log_prefix = "[HTTPServer] "; + const char *rootDir = "/"; + const char *log_prefix = "[HTTPServer] "; - size_t tbuf_head = 0; - char tbuf[TBUF_LEN]; - - void listRoot() { - ls(rootDir); - } - Treppe* treppe; + size_t tbuf_head = 0; + char tbuf[TBUF_LEN]; + + void listRoot() { ls(rootDir); } + Treppe *treppe; public: - HTTPServer(const int _port, const char* _rootDir, Treppe* _treppe) : - ESP8266WebServer(_port), rootDir(_rootDir), treppe(_treppe) - { } - ~HTTPServer() - { - Serial.printf("[HTTPServer] shut down ...\n\r"); - } - - bool start(); - void start_apps(); - - void logf(const char *format, ...); - void logt(const char *format, ...); + HTTPServer(const int _port, const char *_rootDir, Treppe *_treppe) + : ESP8266WebServer(_port), rootDir(_rootDir), treppe(_treppe) {} + ~HTTPServer() { Serial.printf("[HTTPServer] shut down ...\n\r"); } + + bool start(); + void start_apps(); + + template + void logf(const char *format, Args... args) const; + void logt(const char *format, ...); }; #endif // __HTTPSERVER_H \ No newline at end of file