123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- #include "httpserver.h"
-
- auto HTTPServer::start() -> bool {
- 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";
- }
- send(404, "text/plain", message);
- });
-
- // add static root file handler for http
- this->serveStatic("/", LittleFS, "/");
- this->begin();
- logf("Server active on Port 80 !\n\r");
- return true;
- }
-
- void HTTPServer::start_apps() {
- this->on("/action", HTTP_POST, [this]() {
- if (args()) {
- logf("%s=%s\n", argName(0).c_str(), arg(0).c_str());
-
- if (argName(0).equals("control")) {
- if (arg(0).equals("s_oben")) {
- treppe->overwrite_sensors(true, false);
- logt("control => s_oben !\n");
- }
- else if (arg(0).equals("s_unten")) {
- treppe->overwrite_sensors(false, true);
- logt("control => s_unten !\n");
- }
- }
- }
- send(200, "text/plain", "accepted");
- });
-
- this->on("/update", HTTP_POST, [this]() {
- if (args()) {
- for (int i = 0; i < args() - 1; i++) {
- logf("%s=%s\n", argName(i).c_str(), arg(i).c_str());
-
- if (argName(i).equals("range_act_pwm")) {
- treppe->set_active_pwm(arg(i).toInt(), VORGABE_PROZENT);
- logt("set_active_pwm = %d %\n", arg(i).toInt());
- }
- else if (argName(i).equals("range_idl_pwm")) {
- treppe->set_idle_pwm_max(arg(i).toInt(), VORGABE_PROZENT);
- logt("set_idle_pwm_max = %d %\n", arg(i).toInt());
- }
- else if (argName(i).equals("range_tim_sta")) {
- treppe->set_time_per_stair(arg(i).toInt());
- logt("set_time_per_stair = %d\n", arg(i).toInt());
- }
- else if (argName(i).equals("range_tim_ldr")) {
- treppe->set_time_ldr(arg(i).toInt());
- logt("set_time_ldr = %d\n", arg(i).toInt());
- }
- else if (argName(i).equals("range_ldr_shw")) {
- treppe->set_ldr_schwelle(arg(i).toInt(), VORGABE_12BIT);
- logt("set_ldr_schwelle = %d %\n", arg(i).toInt());
- }
-
- }
- }
- send(200, "text/plain", "accepted");
- });
-
- this->on("/terminal", HTTP_POST, [this]() {
- // logf("got /terminal\n");
- if (tbuf_head) {
- send(200, "text/plain", tbuf);
- tbuf_head = 0;
- } else {
- send(202, "text/plain", "");
- }
- });
-
- this->on("/parameters", HTTP_POST, [this]() {
- logt("got /parameters\n");
- char json_str[255];
- treppe->param_to_json(json_str, 255);
- logt("%s\n", json_str);
- send(200, "application/json", json_str);
- });
- }
-
- 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);
- }
|