Browse Source

some formating, add variadic templ for logf()

tags/v1.0.0
Simon Schmidt 3 years ago
parent
commit
2820869e14
4 changed files with 90 additions and 99 deletions
  1. 4
    1
      include/ota.h
  2. 2
    2
      lib/httpserver/filesys.cpp
  3. 65
    71
      lib/httpserver/httpserver.cpp
  4. 19
    25
      lib/httpserver/httpserver.h

+ 4
- 1
include/ota.h View File

#pragma once
#ifndef __OTA_H
#define __OTA_H


#include <ArduinoOTA.h> #include <ArduinoOTA.h>
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
ArduinoOTA.begin(); ArduinoOTA.begin();


} }

#endif // __OTA_H

+ 2
- 2
lib/httpserver/filesys.cpp View File



FSInfo fsinfo; FSInfo fsinfo;


bool mount_fs() {
auto mount_fs() -> bool {
if(!LittleFS.begin()) { if(!LittleFS.begin()) {
Serial.println("[ERROR] LittleFS.info(), reset ..."); Serial.println("[ERROR] LittleFS.info(), reset ...");
return false; return false;
return true; return true;
} }


bool format_fs() {
auto format_fs() -> bool {
printf("Formatting FS ! \n\r"); printf("Formatting FS ! \n\r");
return LittleFS.format(); return LittleFS.format();
} }

+ 65
- 71
lib/httpserver/httpserver.cpp View File

#include "httpserver.h" #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";
logf("LittleFS mounted !\n\r");
logf("root:\n\r");
this->listRoot();
logf("\n\r");


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;
}
// 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";


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;
for (uint8_t i = 0; i < args(); i++) {
message += " " + argName(i) + ": " + arg(i) + "\n";
} }
va_end(args);
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::start_apps() { void HTTPServer::start_apps() {
// application handler
// application handler


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());
}
}
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 <class... Args>
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);
} }

+ 19
- 25
lib/httpserver/httpserver.h View File

#define __HTTPSERVER_H #define __HTTPSERVER_H
// Wrapper for ESP8266WebServer with Filesystem as HTTP source // Wrapper for ESP8266WebServer with Filesystem as HTTP source


#include <ESP8266WebServer.h>
#include <stdarg.h>
#include "filesys.h" #include "filesys.h"
#include "treppe.h" #include "treppe.h"
#include <ESP8266WebServer.h>
#include <stdarg.h>


// debug log <ESP8266WebServer.h> // debug log <ESP8266WebServer.h>
// #define DEBUGV(f,...) do { Serial.printf(PSTR(f), ##__VA_ARGS__); } while (0) // #define DEBUGV(f,...) do { Serial.printf(PSTR(f), ##__VA_ARGS__); } while (0)


#define TBUF_LEN 256 #define TBUF_LEN 256



class HTTPServer : public ESP8266WebServer { class HTTPServer : public ESP8266WebServer {


private: 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];


size_t tbuf_head = 0;
char tbuf[TBUF_LEN];
void listRoot() {
ls(rootDir);
}
Treppe* treppe;
void listRoot() { ls(rootDir); }
Treppe *treppe;


public: 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 <class... Args>
void logf(const char *format, Args... args) const;
void logt(const char *format, ...);
}; };


#endif // __HTTPSERVER_H #endif // __HTTPSERVER_H

Loading…
Cancel
Save