-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import esphome.config_validation as cv | ||
import esphome.codegen as cg | ||
from esphome.const import CONF_ID | ||
from esphome.components.web_server_base import CONF_WEB_SERVER_BASE_ID | ||
from esphome.components import web_server_base | ||
|
||
DEPENDENCIES = ["libretuya"] | ||
AUTO_LOAD = ["web_server_base"] | ||
|
||
hub_api_ns = cg.esphome_ns.namespace("hub_api") | ||
HubAPIHandler = hub_api_ns.class_("HubAPI", cg.Component) | ||
|
||
CONFIG_SCHEMA = cv.Schema( | ||
{ | ||
cv.GenerateID(): cv.declare_id(HubAPIHandler), | ||
cv.GenerateID(CONF_WEB_SERVER_BASE_ID): cv.use_id( | ||
web_server_base.WebServerBase | ||
), | ||
}, | ||
cv.only_with_arduino, | ||
).extend(cv.COMPONENT_SCHEMA) | ||
|
||
|
||
async def to_code(config): | ||
paren = await cg.get_variable(config[CONF_WEB_SERVER_BASE_ID]) | ||
|
||
var = cg.new_Pvariable(config[CONF_ID], paren) | ||
await cg.register_component(var, config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#ifdef USE_ARDUINO | ||
|
||
#include "hub_api.h" | ||
#include "esphome/core/application.h" | ||
|
||
#include <Flash.h> | ||
|
||
#undef min | ||
|
||
namespace esphome { | ||
namespace hub_api { | ||
|
||
static const char *const TAG = "hub_api"; | ||
|
||
void HubAPI::handleRequest(AsyncWebServerRequest *req) { | ||
if (req->url().substring(4) == "/flash_read") { | ||
uint32_t offset, length; | ||
|
||
if (req->hasParam("offset")) { | ||
offset = req->getParam("offset")->value().toInt(); | ||
} else { | ||
offset = 0; | ||
} | ||
|
||
if (req->hasParam("length")) { | ||
length = req->getParam("length")->value().toInt(); | ||
} else { | ||
length = FLASH_LENGTH; | ||
} | ||
|
||
auto callback = [offset, length](uint8_t *buffer, size_t maxLen, size_t position) -> size_t { | ||
size_t blockStart = offset + position; | ||
size_t blockSize = std::min(static_cast<size_t>(maxLen), static_cast<size_t>(length - position)); | ||
blockSize = std::min(blockSize, static_cast<size_t>(4096)); | ||
|
||
if (blockSize) { | ||
ESP_LOGD(TAG, "Reading flash: offset=%06x, length=%u", blockStart, blockSize); | ||
Flash.readBlock(blockStart, buffer, blockSize); | ||
} | ||
|
||
return blockSize; | ||
}; | ||
|
||
req->send("application/octet-stream", length, callback); | ||
return; | ||
} | ||
|
||
EMPTY: | ||
#ifdef LT_BANNER_STR | ||
req->send(200, "text/plain", LT_BANNER_STR); | ||
#else | ||
req->send(200, "text/plain", "LibreTuya " LT_VERSION_STR); | ||
#endif | ||
} | ||
|
||
} // namespace hub_api | ||
} // namespace esphome | ||
|
||
#endif // USE_ARDUINO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#pragma once | ||
|
||
#ifdef USE_ARDUINO | ||
|
||
#include <map> | ||
#include <utility> | ||
|
||
#include "esphome/components/web_server_base/web_server_base.h" | ||
#include "esphome/core/controller.h" | ||
#include "esphome/core/component.h" | ||
|
||
namespace esphome { | ||
namespace hub_api { | ||
|
||
class HubAPI : public AsyncWebHandler, public Component { | ||
public: | ||
HubAPI(web_server_base::WebServerBase *base) : base_(base) {} | ||
|
||
bool canHandle(AsyncWebServerRequest *request) override { | ||
if (request->method() == HTTP_GET) { | ||
if (request->url().startsWith("/hub")) | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
void handleRequest(AsyncWebServerRequest *req) override; | ||
|
||
void setup() override { | ||
this->base_->init(); | ||
this->base_->add_handler(this); | ||
} | ||
float get_setup_priority() const override { | ||
// After WiFi | ||
return setup_priority::WIFI - 1.0f; | ||
} | ||
|
||
protected: | ||
web_server_base::WebServerBase *base_; | ||
}; | ||
|
||
} // namespace hub_api | ||
} // namespace esphome | ||
|
||
#endif // USE_ARDUINO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters