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,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