1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-16 02:02:21 +01:00

Webserver utilize Component Iterator to not overload eventstream (#3310)

This commit is contained in:
Jesse Hills
2022-03-23 09:45:05 +13:00
committed by GitHub
parent e621b938e3
commit bfbf88b2ea
13 changed files with 217 additions and 134 deletions

View File

@@ -1,6 +1,7 @@
#ifdef USE_ARDUINO
#include "web_server.h"
#include "esphome/core/log.h"
#include "esphome/core/application.h"
#include "esphome/core/entity_base.h"
@@ -17,7 +18,7 @@
#endif
#ifdef USE_LOGGER
#include <esphome/components/logger/logger.h>
#include "esphome/components/logger/logger.h"
#endif
#ifdef USE_FAN
@@ -106,87 +107,7 @@ void WebServer::setup() {
}).c_str(),
"ping", millis(), 30000);
#ifdef USE_SENSOR
for (auto *obj : App.get_sensors()) {
if (this->include_internal_ || !obj->is_internal())
client->send(this->sensor_json(obj, obj->state, DETAIL_ALL).c_str(), "state");
}
#endif
#ifdef USE_SWITCH
for (auto *obj : App.get_switches()) {
if (this->include_internal_ || !obj->is_internal())
client->send(this->switch_json(obj, obj->state, DETAIL_ALL).c_str(), "state");
}
#endif
#ifdef USE_BUTTON
for (auto *obj : App.get_buttons())
client->send(this->button_json(obj, DETAIL_ALL).c_str(), "state");
#endif
#ifdef USE_BINARY_SENSOR
for (auto *obj : App.get_binary_sensors()) {
if (this->include_internal_ || !obj->is_internal())
client->send(this->binary_sensor_json(obj, obj->state, DETAIL_ALL).c_str(), "state");
}
#endif
#ifdef USE_FAN
for (auto *obj : App.get_fans()) {
if (this->include_internal_ || !obj->is_internal())
client->send(this->fan_json(obj, DETAIL_ALL).c_str(), "state");
}
#endif
#ifdef USE_LIGHT
for (auto *obj : App.get_lights()) {
if (this->include_internal_ || !obj->is_internal())
client->send(this->light_json(obj, DETAIL_ALL).c_str(), "state");
}
#endif
#ifdef USE_TEXT_SENSOR
for (auto *obj : App.get_text_sensors()) {
if (this->include_internal_ || !obj->is_internal())
client->send(this->text_sensor_json(obj, obj->state, DETAIL_ALL).c_str(), "state");
}
#endif
#ifdef USE_COVER
for (auto *obj : App.get_covers()) {
if (this->include_internal_ || !obj->is_internal())
client->send(this->cover_json(obj, DETAIL_ALL).c_str(), "state");
}
#endif
#ifdef USE_NUMBER
for (auto *obj : App.get_numbers()) {
if (this->include_internal_ || !obj->is_internal())
client->send(this->number_json(obj, obj->state, DETAIL_ALL).c_str(), "state");
}
#endif
#ifdef USE_SELECT
for (auto *obj : App.get_selects()) {
if (this->include_internal_ || !obj->is_internal())
client->send(this->select_json(obj, obj->state, DETAIL_ALL).c_str(), "state");
}
#endif
#ifdef USE_CLIMATE
for (auto *obj : App.get_climates()) {
if (this->include_internal_ || !obj->is_internal())
client->send(this->climate_json(obj, DETAIL_ALL).c_str(), "state");
}
#endif
#ifdef USE_LOCK
for (auto *obj : App.get_locks()) {
if (this->include_internal_ || !obj->is_internal())
client->send(this->lock_json(obj, obj->state, DETAIL_ALL).c_str(), "state");
}
#endif
this->entities_iterator_.begin(this->include_internal_);
});
#ifdef USE_LOGGER
@@ -203,6 +124,7 @@ void WebServer::setup() {
this->set_interval(10000, [this]() { this->events_.send("", "ping", millis(), 30000); });
}
void WebServer::loop() { this->entities_iterator_.advance(); }
void WebServer::dump_config() {
ESP_LOGCONFIG(TAG, "Web Server:");
ESP_LOGCONFIG(TAG, " Address: %s:%u", network::get_use_address().c_str(), this->base_->get_port());