working recovery mode

This commit is contained in:
Siwat Sirichai 2024-05-20 00:05:16 +07:00
parent c4d2db98d5
commit 5ce76bfef7
3 changed files with 38 additions and 3 deletions

View File

@ -75,13 +75,19 @@ void ESPMegaRecovery::enterRecoveryMode() {
this->web_server->loadCredentialsFromFRAM(); this->web_server->loadCredentialsFromFRAM();
ESP_LOGI("ESPMegaRecovery", "Aquiring the web server instance"); ESP_LOGI("ESPMegaRecovery", "Aquiring the web server instance");
AsyncWebServer *server = this->web_server->getServer(); AsyncWebServer *server = this->web_server->getServer();
server->begin();
// Add OTA update and restart endpoint // Add OTA update and restart endpoint
ESP_LOGI("ESPMegaRecovery", "Adding OTA update and reboot endpoints"); ESP_LOGI("ESPMegaRecovery", "Adding OTA update and reboot endpoints");
auto bindedDashboardHandler = std::bind(&ESPMegaWebServer::dashboardHandler, this->web_server, std::placeholders::_1);
auto bindedOtaRequestHandler = std::bind(&ESPMegaWebServer::otaRequestHandler, this->web_server, std::placeholders::_1); auto bindedOtaRequestHandler = std::bind(&ESPMegaWebServer::otaRequestHandler, this->web_server, std::placeholders::_1);
auto bindedOtaUploadHandler = std::bind(&ESPMegaWebServer::otaUploadHandler, this->web_server, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5, std::placeholders::_6); auto bindedOtaUploadHandler = std::bind(&ESPMegaWebServer::otaUploadHandler, this->web_server, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5, std::placeholders::_6);
auto bindedDeviceInfoHandler = std::bind(&ESPMegaRecovery::getDeviceInfoHandler, this, std::placeholders::_1);
auto bindedConfigHandler = std::bind(&ESPMegaRecovery::configHandler, this, std::placeholders::_1);
server->on("/", HTTP_GET, bindedDashboardHandler);
server->on("/ota_update", HTTP_POST, bindedOtaRequestHandler, bindedOtaUploadHandler); server->on("/ota_update", HTTP_POST, bindedOtaRequestHandler, bindedOtaUploadHandler);
server->on("/reboot", HTTP_GET, std::bind(&ESPMegaWebServer::rebootHandler, this->web_server, std::placeholders::_1)); server->on("/reboot", HTTP_GET, std::bind(&ESPMegaWebServer::rebootHandler, this->web_server, std::placeholders::_1));
server->on("/get_device_info", HTTP_GET, bindedDeviceInfoHandler);
server->on("/config", HTTP_GET, bindedConfigHandler);
} }
void ESPMegaRecovery::bindFRAM(FRAM *fram, uint32_t address) { void ESPMegaRecovery::bindFRAM(FRAM *fram, uint32_t address) {
this->fram = fram; this->fram = fram;
@ -105,3 +111,31 @@ void ESPMegaRecovery::resetBootloopCounter() {
bool ESPMegaRecovery::isRecoveryMode() { bool ESPMegaRecovery::isRecoveryMode() {
return this->recovery_mode; return this->recovery_mode;
} }
void ESPMegaRecovery::getDeviceInfoHandler(AsyncWebServerRequest *request) {
if (!request->authenticate(this->web_server->getWebUsername(), this->web_server->getWebPassword()))
{
return request->requestAuthentication();
}
StaticJsonDocument<512> doc;
doc["hostname"] = this->iot->getNetworkConfig()->hostname;
doc["ip_address"] = this->iot->getIp().toString();
doc["mac_address"] = this->iot->getMac();
doc["model"] = BOARD_MODEL;
doc["mqtt_server"] = "Recovery";
doc["mqtt_port"] = "Mode";
doc["base_topic"] = "Recovery Mode";
doc["mqtt_connected"] = "Recovery Mode";
doc["software_version"] = "EMG-SAFE-1.0.0";
doc["sdk_version"] = SDK_VESRION;
doc["idf_version"] = IDF_VER;
char buffer[512];
serializeJson(doc, buffer);
request->send(200, "application/json", buffer);
}
void ESPMegaRecovery::configHandler(AsyncWebServerRequest *request){
// Say Not Available in Recovery Mode
// Wait 3s then redirect to /
request->send(500, "text/html", "<h1>RECOVERY MODE</h1><p>Configuration is not available in recovery mode</p><script>setTimeout(function(){window.location.href = '/';}, 1500);</script>");
}

View File

@ -25,7 +25,8 @@ public:
void resetBootloopCounter(); // Reset the bootloop counter void resetBootloopCounter(); // Reset the bootloop counter
bool isRecoveryMode(); // Check if the device is in recovery mode bool isRecoveryMode(); // Check if the device is in recovery mode
private: private:
void getDeviceInfoHandler(AsyncWebServerRequest *request);
void configHandler(AsyncWebServerRequest *request);
FRAM* fram; FRAM* fram;
uint32_t fram_address; uint32_t fram_address;
uint8_t bootloop_counter; uint8_t bootloop_counter;

View File

@ -14,7 +14,7 @@
// #define FRAM_DEBUG // #define FRAM_DEBUG
// #define MQTT_DEBUG // #define MQTT_DEBUG
// #define WRITE_DEFAULT_NETCONF #define WRITE_DEFAULT_NETCONF
//#define CLIMATE_CARD_ENABLE //#define CLIMATE_CARD_ENABLE
#define MQTT_CARD_REGISTER #define MQTT_CARD_REGISTER
#define DISPLAY_ENABLE #define DISPLAY_ENABLE