moved url under http, and removed the http option from the rp2040 platform

This commit is contained in:
Gábor Poczkodi 2024-11-12 22:14:06 +01:00
parent cace9706be
commit a042bfc9ba
3 changed files with 57 additions and 13 deletions

View file

@ -10,12 +10,25 @@ from esphome.components.web_server_base import CONF_WEB_SERVER_BASE_ID
from esphome.const import (
CONF_ID,
CONF_URL,
PLATFORM_ESP32,
PLATFORM_ESP8266,
PLATFORM_BK72XX,
PLATFORM_RTL87XX,
)
platforms = [
PLATFORM_ESP32,
PLATFORM_ESP8266,
PLATFORM_BK72XX,
PLATFORM_RTL87XX,
]
CODEOWNERS = ["@gabest11"]
AUTO_LOAD = ["web_server_base"]
if CORE.target_platform in platforms:
AUTO_LOAD = ["web_server_base"]
CONF_SHOW_IN_DUMP_CONFIG = "show_in_dump_config"
CONF_SHOW_SECRETS = "show_secrets"
CONF_HTTP = "http"
store_yaml_ns = cg.esphome_ns.namespace("store_yaml")
StoreYamlComponent = store_yaml_ns.class_("StoreYamlComponent", cg.Component)
@ -25,9 +38,14 @@ CONFIG_SCHEMA = cv.Schema(
cv.GenerateID(): cv.declare_id(StoreYamlComponent),
cv.Optional(CONF_SHOW_IN_DUMP_CONFIG, default=False): cv.boolean,
cv.Optional(CONF_SHOW_SECRETS, default=False): cv.boolean,
cv.Optional(CONF_URL): cv.string_strict,
cv.GenerateID(CONF_WEB_SERVER_BASE_ID): cv.use_id(
web_server_base.WebServerBase
cv.Optional(CONF_HTTP): cv.Schema(
{
cv.Optional(CONF_URL): cv.string_strict,
cv.GenerateID(CONF_WEB_SERVER_BASE_ID): cv.All(
cv.use_id(web_server_base.WebServerBase),
cv.only_on(platforms),
),
}
),
}
)
@ -96,9 +114,11 @@ async def to_code(config):
size_t = f"const size_t ESPHOME_YAML_SIZE = {size}"
cg.add_global(cg.RawExpression(uint8_t))
cg.add_global(cg.RawExpression(size_t))
if CONF_URL in config:
webserver = await cg.get_variable(config[CONF_WEB_SERVER_BASE_ID])
cg.add(var.set_web_server(webserver, config[CONF_URL]))
if CONF_HTTP in config:
http = config[CONF_HTTP]
if CONF_URL in http:
webserver = await cg.get_variable(http[CONF_WEB_SERVER_BASE_ID])
cg.add(var.set_web_server(webserver, http[CONF_URL]))
LogAction = store_yaml_ns.class_("LogAction", automation.Action)

View file

@ -10,6 +10,11 @@ static const char *const TAG = "store_yaml";
void StoreYamlComponent::dump_config() {
if (this->show_in_dump_config_) {
ESP_LOGCONFIG(TAG, "YAML:");
ESP_LOGCONFIG(TAG, " Compressed size: %zu", ESPHOME_YAML_SIZE);
#ifndef USE_RP2040
const char *url = (this->web_server_ != nullptr) ? this->web_server_url_.c_str() : "not configured!";
ESP_LOGCONFIG(TAG, " Web server url: %s", url);
#endif
RowDecompressor dec(ESPHOME_YAML, ESPHOME_YAML_SIZE);
std::string row;
while (dec.get_row(row)) {
@ -19,10 +24,13 @@ void StoreYamlComponent::dump_config() {
}
void StoreYamlComponent::setup() {
#ifndef USE_RP2040
if (this->web_server_ != nullptr) {
this->web_server_->init();
this->web_server_->add_handler(this);
ESP_LOGD(TAG, "Web server configured to serve at: %s", this->web_server_url_.c_str());
}
#endif
}
void StoreYamlComponent::loop() {
@ -37,10 +45,12 @@ void StoreYamlComponent::loop() {
}
void StoreYamlComponent::log() {
ESP_LOGI(TAG, "YAML:");
ESP_LOGI(TAG, "Decompressed YAML:");
this->dec_ = make_unique<RowDecompressor>(ESPHOME_YAML, ESPHOME_YAML_SIZE);
}
#ifndef USE_RP2040
void StoreYamlComponent::set_web_server(web_server_base::WebServerBase *web_server, const std::string &url) {
this->web_server_ = web_server;
this->web_server_url_ = url;
@ -52,10 +62,10 @@ bool StoreYamlComponent::canHandle(AsyncWebServerRequest *request) {
void StoreYamlComponent::handleRequest(AsyncWebServerRequest *request) {
#ifdef USE_ARDUINO
auto cb = [this](uint8_t *buffer, size_t maxLen, size_t index) -> size_t {
auto cb = [this](uint8_t *buffer, size_t max_len, size_t index) -> size_t {
uint8_t *ptr = buffer;
// 5KB+ config file with a single character repeating will result in a 100 byte long word, not likely
while (maxLen > 100 && !(this->web_dec_ && this->web_dec_->is_eof())) {
while (max_len > 100 && !(this->web_dec_ && this->web_dec_->is_eof())) {
std::string s;
if (!this->web_dec_) {
this->web_dec_ = make_unique<Decompressor>(ESPHOME_YAML, ESPHOME_YAML_SIZE);
@ -63,10 +73,10 @@ void StoreYamlComponent::handleRequest(AsyncWebServerRequest *request) {
} else {
s = this->web_dec_->get_next();
}
size_t len = std::min(maxLen, s.size());
size_t len = std::min(max_len, s.size());
memcpy(ptr, s.c_str(), len);
ptr += len;
maxLen -= len;
max_len -= len;
}
return ptr - buffer;
};
@ -84,5 +94,7 @@ void StoreYamlComponent::handleRequest(AsyncWebServerRequest *request) {
request->send(response);
}
#endif
} // namespace store_yaml
} // namespace esphome

View file

@ -3,7 +3,9 @@
#include "esphome/core/component.h"
#include "esphome/core/automation.h"
#include "esphome/core/hal.h"
#ifndef USE_RP2040
#include "esphome/components/web_server_base/web_server_base.h"
#endif
#include <memory>
#include "decompressor.h"
@ -13,15 +15,23 @@ extern const size_t ESPHOME_YAML_SIZE;
namespace esphome {
namespace store_yaml {
class StoreYamlComponent : public Component, public AsyncWebHandler {
#ifndef USE_RP2040
class StoreYamlComponent : public Component,
public AsyncWebHandler
#else
class StoreYamlComponent : public Component
#endif
{
std::unique_ptr<RowDecompressor> dec_;
bool show_in_dump_config_{false};
#ifndef USE_RP2040
web_server_base::WebServerBase *web_server_;
std::string web_server_url_;
std::unique_ptr<Decompressor> web_dec_;
bool canHandle(AsyncWebServerRequest *request) override;
void handleRequest(AsyncWebServerRequest *request) override;
#endif
public:
void dump_config() override;
@ -30,7 +40,9 @@ class StoreYamlComponent : public Component, public AsyncWebHandler {
void loop() override;
void log();
void set_show_in_dump_config(bool show) { this->show_in_dump_config_ = show; }
#ifndef USE_RP2040
void set_web_server(web_server_base::WebServerBase *web_server, const std::string &url);
#endif
};
template<typename... Ts> class LogAction : public Action<Ts...>, public Parented<StoreYamlComponent> {