recuperator fan control with esp32
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
esp-recuperator/main/web/websrv.c

217 lines
6.2 KiB

#include <esp_log.h>
#include <esp_err.h>
#include <fileserver/token_subs.h>
#include <application.h>
#include <httpd_utils/captive.h>
#include "websrv.h"
#include "esp_http_server.h"
#include "utils.h"
#include "irblast.h"
#include "www_files_enum.h"
static const char *TAG = "websrv";
static httpd_handle_t s_hServer = NULL;
// Embedded files (must also be listed in CMakeLists.txt as COMPONENT_EMBED_TXTFILES)
efile(index_file, "index_html");
static struct tpl_kv_list build_index_replacements_kv(void) {
//char name[TPL_KV_KEY_LEN];
struct tpl_kv_list kv = tpl_kv_init();
tpl_kv_add(&kv, "version", APP_VERSION);
return kv;
}
/* Main page */
static esp_err_t handler_index(httpd_req_t *req) {
struct tpl_kv_list kv = build_index_replacements_kv();
esp_err_t suc = httpd_send_template_file(req, FILE_INDEX_HTML, tpl_kv_replacer, &kv, 0);
tpl_kv_free(&kv);
return suc;
}
/* Update XHR for new index page data */
static esp_err_t handler_update(httpd_req_t *req) {
struct tpl_kv_list kv = build_index_replacements_kv();
esp_err_t suc = tpl_kv_send_as_ascii_map(req, &kv);
tpl_kv_free(&kv);
return suc;
}
/* Set a param */
static esp_err_t handler_set(httpd_req_t *req) {
char buf[64];
int n = httpd_req_recv(req, buf, 63);
if (n < 0) {
ESP_LOGW(TAG, "rx er");
goto err;
}
buf[n] = 0;
char keybuf[20];
char valbuf[20];
if (ESP_OK != httpd_query_key_value(buf, "key", keybuf, 20)) goto err;
if (ESP_OK != httpd_query_key_value(buf, "value", valbuf, 20)) goto err;
// TODO
ESP_LOGW(TAG, "bad key %s", keybuf);
goto err;
return httpd_resp_send(req, NULL, 0);
err:
return httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, NULL);
}
/* Request emulator reboot */
static esp_err_t handler_reboot(httpd_req_t *req) {
httpd_resp_send(req, "<!DOCTYPE html><html><head>"
"<meta http-equiv=\"refresh\" content=\"10; url=/\">"
"</head>"
"<body>"
"Reboot requested. Reloading in 10 seconds.<br>"
"<a href=\"/\">Try now.</a>", -1);
ESP_LOGI(TAG, "Restarting ESP...");
esp_restart();
}
/* Request emulator reboot */
static esp_err_t handler_irblast(httpd_req_t *req) {
char buf[64];
esp_err_t err = httpd_req_get_url_query_str(req, buf, 63);
if (err != ESP_OK) {
ESP_LOGW(TAG, "param er: %s", esp_err_to_name(err));
goto err;
}
ESP_LOGI(TAG, "querystring: %s", buf);
char valbuf[20];
if (ESP_OK != httpd_query_key_value(buf, "do", valbuf, 20)) {
ESP_LOGW(TAG, "fail to get \"do\" param");
goto err;
}
valbuf[19] = 0;
ESP_LOGI(TAG, "IR cmd to send: %s", valbuf);
const char *cmdnames[] = {
[IRBLAST_ONOFF] = "onoff",
[IRBLAST_DAYNIGHT] = "daynight",
[IRBLAST_SPEED1] = "speed1",
[IRBLAST_SPEED2] = "speed2",
[IRBLAST_SPEED3] = "speed3",
[IRBLAST_MODE1] = "mode1",
[IRBLAST_MODE2] = "mode2",
[IRBLAST_MODE3] = "mode3",
[IRBLAST_MODE4] = "mode4",
[IRBLAST_HUM1] = "hum1",
[IRBLAST_HUM2] = "hum2",
[IRBLAST_HUM3] = "hum3",
NULL
};
for (int i = 0; cmdnames[i]; i++) {
if (strncasecmp(cmdnames[i], valbuf, 20) == 0) {
httpd_resp_send(req, "{\"success\":true}", -1);
irblast_send((enum irblast_cmd) i);
return ESP_OK;
}
}
err:
return httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, NULL);
}
/* An HTTP GET handler */
static esp_err_t handler_staticfiles(httpd_req_t *r) {
const struct embedded_file_info *file;
const char *fname = r->user_ctx;
enum file_access_level access = FILE_ACCESS_PROTECTED;
// wildcard files must be public
if (fname == NULL) {
fname = r->uri + 1; // URI always starts with slash, but we dont want a slash in the file name
access = FILE_ACCESS_PUBLIC;
}
#if USE_CAPTIVE_PORTAL
// First check if this is a phone taken here by the captive portal
esp_err_t rv = httpd_captive_redirect(r);
if (rv != ESP_ERR_NOT_FOUND) return rv;
#endif
if (ESP_OK != www_get_static_file(fname, access, &file)) {
ESP_LOGW(TAG, "File not found: %s", fname);
return httpd_resp_send_404(r);
} else {
if (streq(file->mime, "text/html")) {
// using the template func to allow includes
return httpd_send_template_file_struct(r, file, /*replacer*/NULL, /*ctx*/NULL, /*opts*/0);
} else {
return httpd_send_static_file_struct(r, file, TPL_ESCAPE_NONE, 0);
}
}
}
static const httpd_uri_t routes[] = {
{
.uri = "/",
.method = HTTP_GET,
.handler = handler_index,
},
{
.uri = "/data",
.method = HTTP_GET,
.handler = handler_update,
},
{
.uri = "/set",
.method = HTTP_POST,
.handler = handler_set,
},
{
.uri = "/reboot",
.method = HTTP_GET,
.handler = handler_reboot,
},
{
.uri = "/irblast",
.method = HTTP_GET,
.handler = handler_irblast,
},
{
.uri = "*", // any file except protected (e.g. not HTML, PEM etc)
.method = HTTP_GET,
.handler = handler_staticfiles,
},
};
esp_err_t websrv_init(void) {
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
config.max_open_sockets = 3;
config.uri_match_fn = httpd_uri_match_wildcard;
config.lru_purge_enable = true;
ESP_LOGI(TAG, "Starting HTTP server on port: '%d'", config.server_port);
esp_err_t suc = httpd_start(&s_hServer, &config);
if (suc == ESP_OK) {
ESP_LOGI(TAG, "HTTP server started");
for (int i = 0; i < sizeof(routes) / sizeof(httpd_uri_t); i++) {
httpd_register_uri_handler(s_hServer, &routes[i]);
}
return ESP_OK;
}
ESP_LOGE(TAG, "Error starting server!");
return suc;
}