forked from external-repos/esp32cam-rtsp
use moustache library
This commit is contained in:
@@ -1,5 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "template_render",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"description": "A mini template renderer"
|
|
||||||
}
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <Arduino.h>
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
const char *key;
|
|
||||||
const String value;
|
|
||||||
} template_variable_t;
|
|
||||||
|
|
||||||
template <typename T, size_t n>
|
|
||||||
inline String template_render(const String& format, T (&values)[n])
|
|
||||||
{
|
|
||||||
auto s = String(format);
|
|
||||||
// Conditional sections
|
|
||||||
for (size_t i = 0; i < n; i++)
|
|
||||||
{
|
|
||||||
// Include Section {{#expr}}
|
|
||||||
auto match_section_begin = "{{#" + String(values[i].key) + "}}";
|
|
||||||
// Inverted section {{^expr}}
|
|
||||||
auto match_section_inverted_begin = "{{^" + String(values[i].key) + "}}";
|
|
||||||
// End section {{/expr}}
|
|
||||||
auto match_section_end = "{{/" + String(values[i].key) + "}}";
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
bool inverted = false;
|
|
||||||
auto first = s.indexOf(match_section_begin);
|
|
||||||
if (first < 0)
|
|
||||||
{
|
|
||||||
inverted = true;
|
|
||||||
first = s.indexOf(match_section_inverted_begin);
|
|
||||||
if (first < 0)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto second = s.indexOf(match_section_end, first + match_section_begin.length());
|
|
||||||
if (second < 0)
|
|
||||||
break;
|
|
||||||
|
|
||||||
// Arduino returns 0 and 1 for bool.toString()
|
|
||||||
if ((!inverted && (values[i].value == "1")) || (inverted && (values[i].value == "0")))
|
|
||||||
s = s.substring(0, first) + s.substring(first + match_section_begin.length(), second) + s.substring(second + match_section_end.length());
|
|
||||||
else
|
|
||||||
s = s.substring(0, first) + s.substring(second + match_section_end.length());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Replace variables {{variable}}
|
|
||||||
for (size_t i = 0; i < n; i++)
|
|
||||||
s.replace("{{" + String(values[i].key) + "}}", values[i].value);
|
|
||||||
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
@@ -33,3 +33,4 @@ build_flags =
|
|||||||
lib_deps =
|
lib_deps =
|
||||||
prampec/IotWebConf @ ^3.2.1
|
prampec/IotWebConf @ ^3.2.1
|
||||||
geeksville/Micro-RTSP @ ^0.1.6
|
geeksville/Micro-RTSP @ ^0.1.6
|
||||||
|
rzeldent/micro-moustache @ ^1.0.0
|
||||||
10
src/main.cpp
10
src/main.cpp
@@ -10,7 +10,7 @@
|
|||||||
#include <camera_config.h>
|
#include <camera_config.h>
|
||||||
#include <format_duration.h>
|
#include <format_duration.h>
|
||||||
#include <format_number.h>
|
#include <format_number.h>
|
||||||
#include <template_render.h>
|
#include <moustache.h>
|
||||||
#include <html_data.h>
|
#include <html_data.h>
|
||||||
#include <html_data_gzip.h>
|
#include <html_data_gzip.h>
|
||||||
#include <settings.h>
|
#include <settings.h>
|
||||||
@@ -68,7 +68,7 @@ void handle_root()
|
|||||||
// Wifi Modes
|
// Wifi Modes
|
||||||
const char *wifi_modes[] = {"NULL", "STA", "AP", "STA+AP"};
|
const char *wifi_modes[] = {"NULL", "STA", "AP", "STA+AP"};
|
||||||
|
|
||||||
const template_variable_t substitutions[] = {
|
const moustache_variable_t substitutions[] = {
|
||||||
// Config Changed?
|
// Config Changed?
|
||||||
{"ConfigChanged", String(config_changed)},
|
{"ConfigChanged", String(config_changed)},
|
||||||
// Version / CPU
|
// Version / CPU
|
||||||
@@ -112,7 +112,7 @@ void handle_root()
|
|||||||
{"RtspPort", String(RTSP_PORT)}};
|
{"RtspPort", String(RTSP_PORT)}};
|
||||||
|
|
||||||
web_server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
|
web_server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||||
auto html = template_render(file_data_index_html, substitutions);
|
auto html = moustache_render(file_data_index_html, substitutions);
|
||||||
web_server.send(200, "text/html", html);
|
web_server.send(200, "text/html", html);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -128,12 +128,12 @@ void handle_restart()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const template_variable_t substitutions[] = {
|
const moustache_variable_t substitutions[] = {
|
||||||
{"AppTitle", APP_TITLE},
|
{"AppTitle", APP_TITLE},
|
||||||
{"AppVersion", APP_VERSION},
|
{"AppVersion", APP_VERSION},
|
||||||
{"ThingName", iotWebConf.getThingName()}};
|
{"ThingName", iotWebConf.getThingName()}};
|
||||||
|
|
||||||
auto html = template_render(file_data_restart_html, substitutions);
|
auto html = moustache_render(file_data_restart_html, substitutions);
|
||||||
web_server.send(200, "text/html", html);
|
web_server.send(200, "text/html", html);
|
||||||
log_v("Restarting... Press refresh to connect again");
|
log_v("Restarting... Press refresh to connect again");
|
||||||
sleep(100);
|
sleep(100);
|
||||||
|
|||||||
Reference in New Issue
Block a user