Skip to content

Commit

Permalink
Move network reconfiguration to main loop to prevent crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed Jan 24, 2025
1 parent e164113 commit 226ed94
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/WebApi_network.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@

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

private:
void onNetworkStatus(AsyncWebServerRequest* request);
void onNetworkAdminGet(AsyncWebServerRequest* request);
void onNetworkAdminPost(AsyncWebServerRequest* request);

Task _applyDataTask;
void applyDataTaskCb();
};
13 changes: 13 additions & 0 deletions src/WebApi_network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@
#include "helper.h"
#include <AsyncJson.h>

WebApiNetworkClass::WebApiNetworkClass()
: _applyDataTask(500 * TASK_MILLISECOND, TASK_ONCE, std::bind(&WebApiNetworkClass::applyDataTaskCb, this))
{
}

void WebApiNetworkClass::init(AsyncWebServer& server, Scheduler& scheduler)
{
using std::placeholders::_1;

server.on("/api/network/status", HTTP_GET, std::bind(&WebApiNetworkClass::onNetworkStatus, this, _1));
server.on("/api/network/config", HTTP_GET, std::bind(&WebApiNetworkClass::onNetworkAdminGet, this, _1));
server.on("/api/network/config", HTTP_POST, std::bind(&WebApiNetworkClass::onNetworkAdminPost, this, _1));

scheduler.addTask(_applyDataTask);
}

void WebApiNetworkClass::onNetworkStatus(AsyncWebServerRequest* request)
Expand Down Expand Up @@ -204,6 +211,12 @@ void WebApiNetworkClass::onNetworkAdminPost(AsyncWebServerRequest* request)

WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__);

_applyDataTask.enable();
_applyDataTask.restart();
}

void WebApiNetworkClass::applyDataTaskCb()
{
NetworkSettings.enableAdminMode();
NetworkSettings.applyConfig();
}

0 comments on commit 226ed94

Please sign in to comment.