forked from electro/esp-irblaster
blaster working now
This commit is contained in:
+88
-44
@@ -8,18 +8,18 @@
|
||||
#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 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)
|
||||
{
|
||||
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);
|
||||
@@ -27,8 +27,7 @@ static struct tpl_kv_list build_index_replacements_kv(void)
|
||||
}
|
||||
|
||||
/* Main page */
|
||||
static esp_err_t handler_index(httpd_req_t *req)
|
||||
{
|
||||
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);
|
||||
@@ -37,8 +36,7 @@ static esp_err_t handler_index(httpd_req_t *req)
|
||||
}
|
||||
|
||||
/* Update XHR for new index page data */
|
||||
static esp_err_t handler_update(httpd_req_t *req)
|
||||
{
|
||||
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);
|
||||
@@ -47,15 +45,14 @@ static esp_err_t handler_update(httpd_req_t *req)
|
||||
}
|
||||
|
||||
/* Set a param */
|
||||
static esp_err_t handler_set(httpd_req_t *req)
|
||||
{
|
||||
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;
|
||||
buf[n] = 0;
|
||||
|
||||
char keybuf[20];
|
||||
char valbuf[20];
|
||||
@@ -68,13 +65,12 @@ static esp_err_t handler_set(httpd_req_t *req)
|
||||
|
||||
return httpd_resp_send(req, NULL, 0);
|
||||
|
||||
err:
|
||||
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)
|
||||
{
|
||||
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>"
|
||||
@@ -86,9 +82,54 @@ static esp_err_t handler_reboot(httpd_req_t *req)
|
||||
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)
|
||||
{
|
||||
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;
|
||||
@@ -108,8 +149,7 @@ static esp_err_t handler_staticfiles(httpd_req_t *r)
|
||||
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 {
|
||||
} 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);
|
||||
@@ -120,35 +160,39 @@ static esp_err_t handler_staticfiles(httpd_req_t *r)
|
||||
}
|
||||
|
||||
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 = "*", // any file except protected (e.g. not HTML, PEM etc)
|
||||
.method = HTTP_GET,
|
||||
.handler = handler_staticfiles,
|
||||
},
|
||||
{
|
||||
.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)
|
||||
{
|
||||
esp_err_t websrv_init(void) {
|
||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||
config.max_open_sockets = 3;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user