2021-06-23 15:22:38 +02:00
|
|
|
#include "httpserver.h"
|
|
|
|
|
2021-07-10 04:12:50 +02:00
|
|
|
auto HTTPServer::start() -> bool {
|
|
|
|
if (!mount_fs()) {
|
|
|
|
logf("cant mount filesystem, EXIT !\n\r");
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-30 09:34:11 +02:00
|
|
|
|
2021-07-10 04:12:50 +02:00
|
|
|
logf("LittleFS mounted !\n\r");
|
|
|
|
logf("root:\n\r");
|
|
|
|
this->listRoot();
|
|
|
|
logf("\n\r");
|
2021-06-27 01:56:30 +02:00
|
|
|
|
2021-07-10 04:12:50 +02:00
|
|
|
// 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";
|
2021-06-30 20:33:16 +02:00
|
|
|
|
2021-07-10 04:12:50 +02:00
|
|
|
for (uint8_t i = 0; i < args(); i++) {
|
|
|
|
message += " " + argName(i) + ": " + arg(i) + "\n";
|
2021-06-30 20:33:16 +02:00
|
|
|
}
|
2021-07-10 04:12:50 +02:00
|
|
|
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;
|
2021-06-30 20:33:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPServer::start_apps() {
|
2021-07-10 04:12:50 +02:00
|
|
|
// application handler
|
2021-07-05 18:15:57 +02:00
|
|
|
|
2021-07-10 04:12:50 +02:00
|
|
|
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());
|
2021-07-19 11:29:51 +02:00
|
|
|
|
|
|
|
if (argName(i).equals("range_act_pwm")) {
|
|
|
|
treppe->set_active_pwm(arg(i).toInt(), VORGABE_PROZENT);
|
2021-07-19 12:01:20 +02:00
|
|
|
logt("set_active_pwm = %d %\n", arg(i).toInt());
|
2021-07-19 11:29:51 +02:00
|
|
|
}
|
|
|
|
else if (argName(i).equals("range_idl_pwm")) {
|
|
|
|
treppe->set_idle_pwm_max(arg(i).toInt(), VORGABE_PROZENT);
|
2021-07-19 12:01:20 +02:00
|
|
|
logt("set_idle_pwm_max = %d %\n", arg(i).toInt());
|
2021-07-19 11:29:51 +02:00
|
|
|
}
|
|
|
|
else if (argName(i).equals("range_tim_sta")) {
|
2021-07-19 11:54:37 +02:00
|
|
|
treppe->set_time_per_stair(arg(i).toInt());
|
2021-07-19 12:01:20 +02:00
|
|
|
logt("set_time_per_stair = %d\n", arg(i).toInt());
|
2021-07-05 18:15:57 +02:00
|
|
|
}
|
2021-07-19 11:54:37 +02:00
|
|
|
else if (argName(i).equals("range_tim_ldr")) {
|
|
|
|
treppe->set_time_ldr(arg(i).toInt());
|
2021-07-19 12:01:20 +02:00
|
|
|
logt("set_time_ldr = %d\n", arg(i).toInt());
|
2021-07-19 11:54:37 +02:00
|
|
|
}
|
|
|
|
else if (argName(i).equals("range_ldr_shw")) {
|
|
|
|
treppe->set_ldr_schwelle(arg(i).toInt(), VORGABE_PROZENT);
|
2021-07-19 12:01:20 +02:00
|
|
|
logt("set_ldr_schwelle = %d %\n", arg(i).toInt());
|
2021-07-19 11:29:51 +02:00
|
|
|
}
|
|
|
|
|
2021-07-10 04:12:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
send(200, "text/plain", "accepted");
|
|
|
|
});
|
2021-07-05 18:15:57 +02:00
|
|
|
|
2021-07-10 04:12:50 +02:00
|
|
|
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...);
|
|
|
|
}
|
2021-06-27 01:56:30 +02:00
|
|
|
|
2021-07-10 04:12:50 +02:00
|
|
|
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);
|
2021-06-26 21:11:19 +02:00
|
|
|
}
|