mirror of
https://github.com/Laxilef/OTGateway.git
synced 2026-11-23 14:53:07 +05:00
refactor: migrate ESP32 firmware to task-managed services
Split the header-based firmware into services for OpenTherm, heating control, sensors, networking, logging, and the portal. Run these services with MycilaTaskManager and use ESP32OT for the boiler bus. Move MQTT and Home Assistant discovery to Mycila libraries, expose native crash metrics, and align the board profiles and build hooks with the new dependencies.
This commit is contained in:
1 parent
b10d91b8fa
commit
3fb433d3eb
48 files changed
+14464
-12650
No files matched your search
@@ -1,99 +0,0 @@
|
||||
#include <FS.h>
|
||||
#include <detail/mimetable.h>
|
||||
|
||||
using namespace mime;
|
||||
|
||||
class StaticPage : public RequestHandler {
|
||||
public:
|
||||
typedef std::function<bool(HTTPMethod, const String&)> CanHandleCallback;
|
||||
typedef std::function<bool()> BeforeSendCallback;
|
||||
|
||||
template <class T>
|
||||
StaticPage(const char* uri, FS* fs, T path, const char* cacheHeader = nullptr) {
|
||||
this->uri = uri;
|
||||
this->fs = fs;
|
||||
this->path = path;
|
||||
this->cacheHeader = cacheHeader;
|
||||
}
|
||||
|
||||
StaticPage* setCanHandleCallback(CanHandleCallback callback = nullptr) {
|
||||
this->canHandleCallback = callback;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
StaticPage* setBeforeSendCallback(BeforeSendCallback callback = nullptr) {
|
||||
this->beforeSendCallback = callback;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
bool canHandle(WebServer &server, HTTPMethod method, const String &uri) override {
|
||||
return this->canHandle(method, uri);
|
||||
}
|
||||
|
||||
bool canHandle(HTTPMethod method, const String& uri) override {
|
||||
return method == HTTP_GET && uri.equals(this->uri) && (!this->canHandleCallback || this->canHandleCallback(method, uri));
|
||||
}
|
||||
|
||||
bool handle(WebServer& server, HTTPMethod method, const String& uri) override {
|
||||
if (!this->canHandle(method, uri)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this->beforeSendCallback && !this->beforeSendCallback()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (server._eTagEnabled) {
|
||||
if (this->eTag.isEmpty()) {
|
||||
if (server._eTagFunction) {
|
||||
this->eTag = (server._eTagFunction)(*this->fs, this->path);
|
||||
}
|
||||
}
|
||||
|
||||
if (!this->eTag.isEmpty() && server.header("If-None-Match").equals(this->eTag.c_str())) {
|
||||
server.send(304);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!this->path.endsWith(mimeTable[gz].endsWith) && !this->fs->exists(path)) {
|
||||
String pathWithGz = this->path + mimeTable[gz].endsWith;
|
||||
|
||||
if (this->fs->exists(pathWithGz)) {
|
||||
this->path += mimeTable[gz].endsWith;
|
||||
}
|
||||
}
|
||||
|
||||
File file = this->fs->open(this->path, "r");
|
||||
if (!file) {
|
||||
return false;
|
||||
|
||||
} else if (file.isDirectory()) {
|
||||
file.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this->cacheHeader != nullptr) {
|
||||
server.sendHeader("Cache-Control", this->cacheHeader);
|
||||
}
|
||||
|
||||
if (server._eTagEnabled && !this->eTag.isEmpty()) {
|
||||
server.sendHeader("ETag", this->eTag);
|
||||
}
|
||||
|
||||
server.streamFile(file, "text/html", 200);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected:
|
||||
FS* fs = nullptr;
|
||||
CanHandleCallback canHandleCallback;
|
||||
BeforeSendCallback beforeSendCallback;
|
||||
String eTag;
|
||||
const char* uri = nullptr;
|
||||
String path;
|
||||
const char* cacheHeader = nullptr;
|
||||
};
|
||||
@@ -1,4 +1,6 @@
|
||||
#include <Arduino.h>
|
||||
#include <esp_log.h>
|
||||
#include <strings_shared.h>
|
||||
|
||||
class UpgradeHandler : public AsyncWebHandler {
|
||||
public:
|
||||
@@ -27,12 +29,13 @@ public:
|
||||
} UpgradeResult;
|
||||
|
||||
typedef std::function<bool(AsyncWebServerRequest *request, UpgradeType)> BeforeUpgradeCallback;
|
||||
// A null request reports an upload disconnected after preparation was accepted.
|
||||
typedef std::function<void(AsyncWebServerRequest *request, const UpgradeResult&, const UpgradeResult&)> AfterUpgradeCallback;
|
||||
|
||||
explicit UpgradeHandler(AsyncURIMatcher uri) : uri(uri) {}
|
||||
|
||||
bool canHandle(AsyncWebServerRequest *request) const final {
|
||||
if (!request->isHTTP()) {
|
||||
if (!request->isHTTP() || request->method() != HTTP_POST) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -52,18 +55,28 @@ public:
|
||||
}
|
||||
|
||||
void handleRequest(AsyncWebServerRequest *request) final {
|
||||
if (this->afterUpgradeCallback) {
|
||||
this->afterUpgradeCallback(request, this->firmwareResult, this->filesystemResult);
|
||||
if (request->getResponse()) {
|
||||
return;
|
||||
}
|
||||
|
||||
this->firmwareResult.status = UpgradeStatus::NONE;
|
||||
this->firmwareResult.error.clear();
|
||||
if (this->activeRequest != request) {
|
||||
request->send(this->activeRequest ? 409 : 400);
|
||||
return;
|
||||
}
|
||||
|
||||
this->filesystemResult.status = UpgradeStatus::NONE;
|
||||
this->filesystemResult.error.clear();
|
||||
finishRequest(request);
|
||||
}
|
||||
|
||||
void handleUpload(AsyncWebServerRequest *request, const String &fileName, size_t index, uint8_t *data, size_t dataLength, bool isFinal) final {
|
||||
if (request->getResponse()) {
|
||||
return;
|
||||
}
|
||||
if (this->activeRequest && this->activeRequest != request) {
|
||||
// Keep the rejection on this request even if the active upload finishes first.
|
||||
request->send(409);
|
||||
return;
|
||||
}
|
||||
|
||||
UpgradeResult* result = nullptr;
|
||||
|
||||
if (!request->hasParam(asyncsrv::T_name, true, true)) {
|
||||
@@ -97,6 +110,15 @@ public:
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this->activeRequest) {
|
||||
this->activeRequest = request;
|
||||
request->onDisconnect([this, request] {
|
||||
if (this->activeRequest == request) {
|
||||
finishRequest(nullptr);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// check result status
|
||||
if (result->status != UpgradeStatus::NONE) {
|
||||
return;
|
||||
@@ -106,6 +128,7 @@ public:
|
||||
result->status = UpgradeStatus::PROHIBITED;
|
||||
return;
|
||||
}
|
||||
this->preparationAccepted = true;
|
||||
|
||||
if (!fileName.length()) {
|
||||
result->status = UpgradeStatus::NO_FILE;
|
||||
@@ -132,11 +155,11 @@ public:
|
||||
result->status = UpgradeStatus::ERROR_ON_START;
|
||||
result->error = Update.errorString();
|
||||
|
||||
Log.serrorln(L_PORTAL_OTA, "File '%s', on start: %s", fileName.c_str(), result->error.c_str());
|
||||
ESP_LOGE(L_PORTAL_OTA, "File '%s', on start: %s", fileName.c_str(), result->error.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
Log.sinfoln(L_PORTAL_OTA, "File '%s', started", fileName.c_str());
|
||||
ESP_LOGI(L_PORTAL_OTA, "File '%s', started", fileName.c_str());
|
||||
}
|
||||
|
||||
if (dataLength) {
|
||||
@@ -145,7 +168,7 @@ public:
|
||||
result->status = UpgradeStatus::ERROR_ON_WRITE;
|
||||
result->error = Update.errorString();
|
||||
|
||||
Log.serrorln(
|
||||
ESP_LOGE(
|
||||
L_PORTAL_OTA, "File '%s', on write %d bytes, %d of %d bytes",
|
||||
fileName.c_str(),
|
||||
dataLength,
|
||||
@@ -156,7 +179,7 @@ public:
|
||||
}
|
||||
|
||||
result->progress += dataLength;
|
||||
Log.sinfoln(
|
||||
ESP_LOGI(
|
||||
L_PORTAL_OTA, "File '%s', write %d bytes, %d of %d bytes",
|
||||
fileName.c_str(),
|
||||
dataLength,
|
||||
@@ -170,7 +193,7 @@ public:
|
||||
Update.end(false);
|
||||
result->status = UpgradeStatus::SIZE_MISMATCH;
|
||||
|
||||
Log.serrorln(
|
||||
ESP_LOGE(
|
||||
L_PORTAL_OTA, "File '%s', size mismatch: %d of %d bytes",
|
||||
fileName.c_str(),
|
||||
result->progress,
|
||||
@@ -185,12 +208,12 @@ public:
|
||||
result->status = UpgradeStatus::ERROR_ON_FINISH;
|
||||
result->error = Update.errorString();
|
||||
|
||||
Log.serrorln(L_PORTAL_OTA, "File '%s', on finish: %s", fileName.c_str(), result->error.c_str());
|
||||
ESP_LOGE(L_PORTAL_OTA, "File '%s', on finish: %s", fileName.c_str(), result->error.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
result->status = UpgradeStatus::SUCCESS;
|
||||
Log.sinfoln(L_PORTAL_OTA, "File '%s': finish", fileName.c_str());
|
||||
ESP_LOGI(L_PORTAL_OTA, "File '%s': finish", fileName.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -199,10 +222,31 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
void finishRequest(AsyncWebServerRequest *request) {
|
||||
if (request) {
|
||||
request->onDisconnect(nullptr);
|
||||
}
|
||||
if (Update.isRunning()) {
|
||||
// Abort only the unfinished file; an earlier successful firmware still needs a restart.
|
||||
Update.abort();
|
||||
}
|
||||
if (this->afterUpgradeCallback && (request || this->preparationAccepted)) {
|
||||
this->afterUpgradeCallback(request, this->firmwareResult, this->filesystemResult);
|
||||
}
|
||||
|
||||
this->activeRequest = nullptr;
|
||||
this->preparationAccepted = false;
|
||||
this->firmwareResult = {UpgradeType::FIRMWARE, UpgradeStatus::NONE};
|
||||
this->filesystemResult = {UpgradeType::FILESYSTEM, UpgradeStatus::NONE};
|
||||
}
|
||||
|
||||
BeforeUpgradeCallback beforeUpgradeCallback;
|
||||
AfterUpgradeCallback afterUpgradeCallback;
|
||||
AsyncURIMatcher uri;
|
||||
|
||||
// Upload, completion and disconnect callbacks share the AsyncTCP worker.
|
||||
AsyncWebServerRequest* activeRequest = nullptr;
|
||||
bool preparationAccepted = false;
|
||||
UpgradeResult firmwareResult{UpgradeType::FIRMWARE, UpgradeStatus::NONE};
|
||||
UpgradeResult filesystemResult{UpgradeType::FILESYSTEM, UpgradeStatus::NONE};
|
||||
};
|
||||
Reference in new issue
Block a user