4#if USE_WEBSERVER_VERSION == 1
9void write_row(AsyncResponseStream *stream, EntityBase *obj,
const std::string &klass,
const std::string &action,
10 const std::function<
void(AsyncResponseStream &stream, EntityBase *obj)> &action_func =
nullptr) {
11 stream->print(
"<tr class=\"");
12 stream->print(klass.c_str());
13 if (obj->is_internal())
14 stream->print(
" internal");
15 stream->print(
"\" id=\"");
16 stream->print(klass.c_str());
18 stream->print(obj->get_object_id().c_str());
19 stream->print(
"\"><td>");
20 stream->print(obj->get_name().c_str());
21 stream->print(
"</td><td></td><td>");
22 stream->print(action.c_str());
24 action_func(*stream, obj);
26 stream->print(
"</td>");
27 stream->print(
"</tr>");
35 AsyncResponseStream *stream = request->beginResponseStream(
"text/html");
37 stream->print(F(
"<!DOCTYPE html><html lang=\"en\"><head><meta charset=UTF-8><meta "
38 "name=viewport content=\"width=device-width, initial-scale=1,user-scalable=no\"><title>"));
39 stream->print(title.c_str());
40 stream->print(F(
"</title>"));
41#ifdef USE_WEBSERVER_CSS_INCLUDE
42 stream->print(F(
"<link rel=\"stylesheet\" href=\"/0.css\">"));
45 stream->print(F(R
"(<link rel="stylesheet" href=")"));
47 stream->print(F("\">"));
49 stream->print(F(
"</head><body>"));
50 stream->print(F(
"<article class=\"markdown-body\"><h1>"));
51 stream->print(title.c_str());
52 stream->print(F(
"</h1>"));
53 stream->print(F(
"<h2>States</h2><table id=\"states\"><thead><tr><th>Name<th>State<th>Actions<tbody>"));
56 for (
auto *obj :
App.get_sensors()) {
58 write_row(stream, obj,
"sensor",
"");
63 for (
auto *obj :
App.get_switches()) {
65 write_row(stream, obj,
"switch",
"<button>Toggle</button>");
70 for (
auto *obj :
App.get_buttons())
71 write_row(stream, obj,
"button",
"<button>Press</button>");
74#ifdef USE_BINARY_SENSOR
77 write_row(stream, obj,
"binary_sensor",
"");
82 for (
auto *obj :
App.get_fans()) {
84 write_row(stream, obj,
"fan",
"<button>Toggle</button>");
89 for (
auto *obj :
App.get_lights()) {
91 write_row(stream, obj,
"light",
"<button>Toggle</button>");
96 for (
auto *obj :
App.get_text_sensors()) {
98 write_row(stream, obj,
"text_sensor",
"");
103 for (
auto *obj :
App.get_covers()) {
105 write_row(stream, obj,
"cover",
"<button>Open</button><button>Close</button>");
110 for (
auto *obj :
App.get_numbers()) {
112 write_row(stream, obj,
"number",
"", [](AsyncResponseStream &stream, EntityBase *obj) {
113 number::Number *number = (number::Number *) obj;
114 stream.print(R
"(<input type="number" min=")");
115 stream.print(number->traits.get_min_value());
116 stream.print(R"(" max=")");
117 stream.print(number->traits.get_max_value());
118 stream.print(R"(" step=")");
119 stream.print(number->traits.get_step());
120 stream.print(R"(" value=")");
121 stream.print(number->state);
122 stream.print(R"("/>)");
129 for (
auto *obj :
App.get_texts()) {
131 write_row(stream, obj,
"text",
"", [](AsyncResponseStream &stream, EntityBase *obj) {
132 text::Text *text = (text::Text *) obj;
133 auto mode = (int) text->traits.get_mode();
134 stream.print(R
"(<input type=")");
136 stream.print(R
"(password)");
138 stream.print(R
"(text)");
140 stream.print(R"(" minlength=")");
141 stream.print(text->traits.get_min_length());
142 stream.print(R"(" maxlength=")");
143 stream.print(text->traits.get_max_length());
144 stream.print(R"(" pattern=")");
145 stream.print(text->traits.get_pattern().c_str());
146 stream.print(R"(" value=")");
147 stream.print(text->state.c_str());
148 stream.print(R"("/>)");
155 for (
auto *obj :
App.get_selects()) {
157 write_row(stream, obj,
"select",
"", [](AsyncResponseStream &stream, EntityBase *obj) {
158 select::Select *select = (select::Select *) obj;
159 stream.print(
"<select>");
160 stream.print(
"<option></option>");
161 for (
auto const &option : select->traits.get_options()) {
162 stream.print(
"<option>");
163 stream.print(option.c_str());
164 stream.print(
"</option>");
166 stream.print(
"</select>");
173 for (
auto *obj :
App.get_locks()) {
175 write_row(stream, obj,
"lock",
"", [](AsyncResponseStream &stream, EntityBase *obj) {
176 lock::Lock *lock = (lock::Lock *) obj;
177 stream.print(
"<button>Lock</button><button>Unlock</button>");
178 if (lock->traits.get_supports_open()) {
179 stream.print(
"<button>Open</button>");
187 for (
auto *obj :
App.get_climates()) {
189 write_row(stream, obj,
"climate",
"");
193 stream->print(F(
"</tbody></table><p>See <a href=\"https://esphome.io/web-api/index.html\">ESPHome Web API</a> for "
194 "REST API documentation.</p>"));
195#if defined(USE_WEBSERVER_OTA) && !defined(USE_WEBSERVER_OTA_DISABLED)
198 stream->print(F(
"<h2>OTA Update</h2><form method=\"POST\" action=\"/update\" enctype=\"multipart/form-data\"><input "
199 "type=\"file\" name=\"update\"><input type=\"submit\" value=\"Update\"></form>"));
201 stream->print(F(
"<h2>Debug Log</h2><pre id=\"log\"></pre>"));
202#ifdef USE_WEBSERVER_JS_INCLUDE
204 stream->print(F(
"<script type=\"module\" src=\"/0.js\"></script>"));
207 if (strlen(this->
js_url_) > 0) {
208 stream->print(F(
"<script src=\""));
210 stream->print(F(
"\"></script>"));
212 stream->print(F(
"</article></body></html>"));
213 request->send(stream);
BedjetMode mode
BedJet operating mode.
const std::string & get_name() const
Get the name of this Application set by pre_setup().
auto & get_binary_sensors() const
void set_css_url(const char *css_url)
Set the URL to the CSS <link> that's sent to each client.
void set_js_url(const char *js_url)
Set the URL to the script that's embedded in the index page.
void handle_index_request(AsyncWebServerRequest *request)
Handle an index request under '/'.
Providing packet encoding functions for exchanging data with a remote host.
Application App
Global storage of Application pointer - only one Application can exist.