Skip to content

Commit

Permalink
Fix: Restart was triggered before all website data was sent
Browse files Browse the repository at this point in the history
This led to the effect that e.g. the confirmation messages where  not shown.

It is somehow related to ESPAsyncWebServer 3.3.0
  • Loading branch information
tbnobody committed Sep 23, 2024
1 parent 5c460e2 commit e785904
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 38 deletions.
18 changes: 18 additions & 0 deletions include/RestartHelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once

#include <TaskSchedulerDeclarations.h>

class RestartHelperClass {
public:
RestartHelperClass();
void init(Scheduler& scheduler);
void triggerRestart();

private:
void loop();

Task _rebootTask;
};

extern RestartHelperClass RestartHelper;
1 change: 0 additions & 1 deletion include/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class Utils {
static uint32_t getChipId();
static uint64_t generateDtuSerial();
static int getTimezoneOffset();
static void restartDtu();
static bool checkJsonAlloc(const JsonDocument& doc, const char* function, const uint16_t line);
static void removeAllFiles();
};
4 changes: 0 additions & 4 deletions include/WebApi_firmware.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@

class WebApiFirmwareClass {
public:
WebApiFirmwareClass();
void init(AsyncWebServer& server, Scheduler& scheduler);

private:
void onFirmwareUpdateFinish(AsyncWebServerRequest* request);
void onFirmwareUpdateUpload(AsyncWebServerRequest* request, String filename, size_t index, uint8_t* data, size_t len, bool final);

Task _rebootTask;
void rebootTaskCb();
};
36 changes: 36 additions & 0 deletions src/RestartHelper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2024 Thomas Basler and others
*/
#include "RestartHelper.h"
#include "Display_Graphic.h"
#include "Led_Single.h"
#include <Esp.h>

RestartHelperClass RestartHelper;

RestartHelperClass::RestartHelperClass()
: _rebootTask(2 * TASK_SECOND, TASK_FOREVER, std::bind(&RestartHelperClass::loop, this))
{
}

void RestartHelperClass::init(Scheduler& scheduler)
{
scheduler.addTask(_rebootTask);
}

void RestartHelperClass::triggerRestart()
{
_rebootTask.enable();
_rebootTask.restart();
}

void RestartHelperClass::loop()
{
if (_rebootTask.isFirstIteration()) {
LedSingle.turnAllOff();
Display.setStatus(false);
} else {
ESP.restart();
}
}
14 changes: 1 addition & 13 deletions src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
*/

#include "Utils.h"
#include "Display_Graphic.h"
#include "Led_Single.h"
#include "MessageOutput.h"
#include <Esp.h>
#include "PinMapping.h"
#include <LittleFS.h>

uint32_t Utils::getChipId()
Expand Down Expand Up @@ -59,16 +57,6 @@ int Utils::getTimezoneOffset()
return static_cast<int>(difftime(rawtime, gmt));
}

void Utils::restartDtu()
{
LedSingle.turnAllOff();
Display.setStatus(false);
yield();
delay(1000);
yield();
ESP.restart();
}

bool Utils::checkJsonAlloc(const JsonDocument& doc, const char* function, const uint16_t line)
{
if (doc.overflowed()) {
Expand Down
5 changes: 3 additions & 2 deletions src/WebApi_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
#include "WebApi_config.h"
#include "Configuration.h"
#include "RestartHelper.h"
#include "Utils.h"
#include "WebApi.h"
#include "WebApi_errors.h"
Expand Down Expand Up @@ -82,7 +83,7 @@ void WebApiConfigClass::onConfigDelete(AsyncWebServerRequest* request)
WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__);

Utils::removeAllFiles();
Utils::restartDtu();
RestartHelper.triggerRestart();
}

void WebApiConfigClass::onConfigListGet(AsyncWebServerRequest* request)
Expand Down Expand Up @@ -124,7 +125,7 @@ void WebApiConfigClass::onConfigUploadFinish(AsyncWebServerRequest* request)
response->addHeader("Connection", "close");
response->addHeader("Access-Control-Allow-Origin", "*");
request->send(response);
Utils::restartDtu();
RestartHelper.triggerRestart();
}

void WebApiConfigClass::onConfigUpload(AsyncWebServerRequest* request, String filename, size_t index, uint8_t* data, size_t len, bool final)
Expand Down
4 changes: 2 additions & 2 deletions src/WebApi_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "Configuration.h"
#include "Display_Graphic.h"
#include "PinMapping.h"
#include "Utils.h"
#include "RestartHelper.h"
#include "WebApi.h"
#include "WebApi_errors.h"
#include "helper.h"
Expand Down Expand Up @@ -149,6 +149,6 @@ void WebApiDeviceClass::onDeviceAdminPost(AsyncWebServerRequest* request)
WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__);

if (performRestart) {
Utils::restartDtu();
RestartHelper.triggerRestart();
}
}
16 changes: 2 additions & 14 deletions src/WebApi_firmware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
*/
#include "WebApi_firmware.h"
#include "Configuration.h"
#include "RestartHelper.h"
#include "Update.h"
#include "Utils.h"
#include "WebApi.h"
#include "helper.h"
#include <AsyncJson.h>

WebApiFirmwareClass::WebApiFirmwareClass()
: _rebootTask(TASK_IMMEDIATE, TASK_ONCE, std::bind(&WebApiFirmwareClass::rebootTaskCb, this))
{
}

void WebApiFirmwareClass::init(AsyncWebServer& server, Scheduler& scheduler)
{
using std::placeholders::_1;
Expand All @@ -27,8 +23,6 @@ void WebApiFirmwareClass::init(AsyncWebServer& server, Scheduler& scheduler)
server.on("/api/firmware/update", HTTP_POST,
std::bind(&WebApiFirmwareClass::onFirmwareUpdateFinish, this, _1),
std::bind(&WebApiFirmwareClass::onFirmwareUpdateUpload, this, _1, _2, _3, _4, _5, _6));

scheduler.addTask(_rebootTask);
}

void WebApiFirmwareClass::onFirmwareUpdateFinish(AsyncWebServerRequest* request)
Expand All @@ -44,8 +38,7 @@ void WebApiFirmwareClass::onFirmwareUpdateFinish(AsyncWebServerRequest* request)
response->addHeader("Connection", "close");
response->addHeader("Access-Control-Allow-Origin", "*");
request->send(response);
_rebootTask.enable();
_rebootTask.restart();
RestartHelper.triggerRestart();
}

void WebApiFirmwareClass::onFirmwareUpdateUpload(AsyncWebServerRequest* request, String filename, size_t index, uint8_t* data, size_t len, bool final)
Expand Down Expand Up @@ -86,8 +79,3 @@ void WebApiFirmwareClass::onFirmwareUpdateUpload(AsyncWebServerRequest* request,
return;
}
}

void WebApiFirmwareClass::rebootTaskCb()
{
Utils::restartDtu();
}
4 changes: 2 additions & 2 deletions src/WebApi_maintenance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

#include "WebApi_maintenance.h"
#include "Utils.h"
#include "RestartHelper.h"
#include "WebApi.h"
#include "WebApi_errors.h"
#include <AsyncJson.h>
Expand Down Expand Up @@ -43,7 +43,7 @@ void WebApiMaintenanceClass::onRebootPost(AsyncWebServerRequest* request)
retMsg["code"] = WebApiError::MaintenanceRebootTriggered;

WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__);
Utils::restartDtu();
RestartHelper.triggerRestart();
} else {
retMsg["message"] = "Reboot cancled!";
retMsg["code"] = WebApiError::MaintenanceRebootCancled;
Expand Down
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "NetworkSettings.h"
#include "NtpSettings.h"
#include "PinMapping.h"
#include "RestartHelper.h"
#include "Scheduler.h"
#include "SunPosition.h"
#include "Utils.h"
Expand Down Expand Up @@ -154,6 +155,7 @@ void setup()
InverterSettings.init(scheduler);

Datastore.init(scheduler);
RestartHelper.init(scheduler);
}

void loop()
Expand Down

0 comments on commit e785904

Please sign in to comment.