2024-01-01 16:40:32 +00:00
|
|
|
/**
|
|
|
|
* @file ESPMegaWebServer.cpp
|
|
|
|
* @brief Implementation file for the ESPMegaWebServer class.
|
|
|
|
*
|
|
|
|
* This file contains the implementation of the ESPMegaWebServer class, which is responsible for handling web server functionality for the ESPMegaPRO firmware.
|
|
|
|
* The ESPMegaWebServer class provides methods for starting the web server, handling HTTP requests, and managing credentials and configurations.
|
|
|
|
*/
|
2024-01-01 06:28:15 +00:00
|
|
|
#include <ESPMegaWebServer.hpp>
|
|
|
|
|
2024-01-01 16:40:32 +00:00
|
|
|
/**
|
|
|
|
* @brief Construct a new ESPMegaWebServer::ESPMegaWebServer objecy
|
|
|
|
*
|
|
|
|
* @note Although you can instantiate this class directly, it is recommended to use the ESPMegaPRO.webServer object instead.
|
|
|
|
*
|
|
|
|
* @param port The TCP port to listen on
|
|
|
|
* @param iot A pointer to the ESPMegaIoT object
|
|
|
|
*/
|
2024-01-01 10:52:59 +00:00
|
|
|
ESPMegaWebServer::ESPMegaWebServer(uint16_t port, ESPMegaIoT *iot)
|
|
|
|
{
|
2024-01-01 06:28:15 +00:00
|
|
|
this->port = port;
|
|
|
|
this->iot = iot;
|
|
|
|
this->server = new AsyncWebServer(port);
|
2024-01-01 15:53:28 +00:00
|
|
|
this->saveConfigHandler = new AsyncCallbackJsonWebHandler("/save_config", std::bind(&ESPMegaWebServer::saveConfigJSONHandler, this, std::placeholders::_1, std::placeholders::_2));
|
2024-01-01 06:28:15 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 16:40:32 +00:00
|
|
|
/**
|
|
|
|
* @brief Destroy the ESPMegaWebServer::ESPMegaWebServer object
|
|
|
|
*/
|
2024-01-01 10:52:59 +00:00
|
|
|
ESPMegaWebServer::~ESPMegaWebServer()
|
|
|
|
{
|
2024-01-01 06:28:15 +00:00
|
|
|
delete this->server;
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:40:32 +00:00
|
|
|
/**
|
|
|
|
* @brief Start the web server
|
|
|
|
*
|
|
|
|
* This method starts the web server and registers the necessary handlers.
|
|
|
|
*
|
|
|
|
* @note This method should be called after the ESPMegaIoT object has been initialized.
|
|
|
|
* @note This method is automatically called if you use ESPMegaPRO::enableWebServer()
|
|
|
|
*/
|
2024-01-01 10:52:59 +00:00
|
|
|
void ESPMegaWebServer::begin()
|
|
|
|
{
|
2024-01-01 13:30:17 +00:00
|
|
|
this->loadCredentialsFromFRAM();
|
2024-01-01 06:28:15 +00:00
|
|
|
this->server->begin();
|
|
|
|
auto bindedDashboardHandler = std::bind(&ESPMegaWebServer::dashboardHandler, this, std::placeholders::_1);
|
|
|
|
this->server->on("/", HTTP_GET, bindedDashboardHandler);
|
2024-01-01 10:52:59 +00:00
|
|
|
auto bindedConfigHandler = std::bind(&ESPMegaWebServer::configHandler, this, std::placeholders::_1);
|
|
|
|
this->server->on("/config", HTTP_GET, bindedConfigHandler);
|
2024-01-01 15:53:28 +00:00
|
|
|
this->server->addHandler(saveConfigHandler);
|
2024-01-01 15:13:54 +00:00
|
|
|
auto bindedOtaRequestHandler = std::bind(&ESPMegaWebServer::otaRequestHandler, this, std::placeholders::_1);
|
|
|
|
auto bindedOtaUploadHandler = std::bind(&ESPMegaWebServer::otaUploadHandler, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5, std::placeholders::_6);
|
|
|
|
this->server->on("/ota_update", HTTP_POST, bindedOtaRequestHandler, bindedOtaUploadHandler);
|
2024-01-01 06:28:15 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 16:40:32 +00:00
|
|
|
/**
|
|
|
|
* @brief The loop function for the web server
|
|
|
|
*
|
|
|
|
* @note This method is not used by the ESPMegaWebServer class as of now.
|
|
|
|
*/
|
2024-01-01 10:52:59 +00:00
|
|
|
void ESPMegaWebServer::loop()
|
|
|
|
{
|
2024-01-01 06:28:15 +00:00
|
|
|
// AsyncWebServer doesn't have a loop function
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:40:32 +00:00
|
|
|
/**
|
|
|
|
* @brief Bind the FRAM object to the web server
|
|
|
|
*
|
|
|
|
* This method binds the FRAM object to the web server so that the web server can read and write credentials to the FRAM.
|
|
|
|
*
|
|
|
|
* @note The FRAM object must be bound to the web server before calling ESPMegaWebServer::begin()
|
|
|
|
* @note This class takes 64 bytes of FRAM starting from address 301, however address 301-400 is reserved for it.
|
|
|
|
*
|
|
|
|
* @param fram A pointer to the FRAM object
|
|
|
|
*/
|
2024-01-01 13:30:17 +00:00
|
|
|
void ESPMegaWebServer::bindFRAM(FRAM *fram)
|
|
|
|
{
|
|
|
|
this->fram = fram;
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:40:32 +00:00
|
|
|
/**
|
|
|
|
* @brief Load web username and password from FRAM
|
|
|
|
*
|
|
|
|
* This method loads the web server credentials from the FRAM.
|
|
|
|
*
|
|
|
|
* @note This method is automatically called by ESPMegaWebServer::begin()
|
|
|
|
*/
|
2024-01-01 13:30:17 +00:00
|
|
|
void ESPMegaWebServer::loadCredentialsFromFRAM()
|
|
|
|
{
|
2024-01-01 15:13:54 +00:00
|
|
|
this->fram->read(301, (uint8_t *)this->webUsername, 32);
|
|
|
|
this->fram->read(333, (uint8_t *)this->webPassword, 32);
|
2024-01-01 13:30:17 +00:00
|
|
|
// Verify if credentials are valid
|
|
|
|
// A valid username and password is null terminated
|
|
|
|
// Scan for null terminator
|
|
|
|
bool validUsername = false;
|
|
|
|
bool validPassword = false;
|
|
|
|
for (int i = 0; i < 32; i++)
|
|
|
|
{
|
|
|
|
if (this->webUsername[i] == '\0')
|
|
|
|
{
|
|
|
|
validUsername = true;
|
|
|
|
break;
|
2024-01-01 15:13:54 +00:00
|
|
|
}
|
2024-01-01 13:30:17 +00:00
|
|
|
}
|
|
|
|
for (int i = 0; i < 32; i++)
|
|
|
|
{
|
|
|
|
if (this->webPassword[i] == '\0')
|
|
|
|
{
|
|
|
|
validPassword = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!validUsername || !validPassword)
|
|
|
|
{
|
|
|
|
this->resetCredentials();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// A valid username and password is at least 1 character long
|
2024-01-01 15:13:54 +00:00
|
|
|
if (strlen(this->webUsername) == 0 || strlen(this->webPassword) == 0)
|
2024-01-01 13:30:17 +00:00
|
|
|
{
|
|
|
|
this->resetCredentials();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:40:32 +00:00
|
|
|
/**
|
|
|
|
* @brief Save web username and password to FRAM
|
|
|
|
*
|
|
|
|
* This method saves the web server credentials to the FRAM.
|
|
|
|
*/
|
2024-01-01 13:30:17 +00:00
|
|
|
void ESPMegaWebServer::saveCredentialsToFRAM()
|
|
|
|
{
|
2024-01-01 15:13:54 +00:00
|
|
|
this->fram->write(301, (uint8_t *)this->webUsername, 32);
|
|
|
|
this->fram->write(333, (uint8_t *)this->webPassword, 32);
|
2024-01-01 13:30:17 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 16:40:32 +00:00
|
|
|
/**
|
|
|
|
* @brief Reset web username and password to default
|
|
|
|
*
|
|
|
|
* This method resets the web server credentials to the default username and password.
|
|
|
|
*
|
|
|
|
* @note The default username and password is both "admin"
|
|
|
|
*/
|
2024-01-01 13:30:17 +00:00
|
|
|
void ESPMegaWebServer::resetCredentials()
|
|
|
|
{
|
|
|
|
// The default username and password is "admin"
|
|
|
|
strcpy(this->webUsername, "admin");
|
|
|
|
strcpy(this->webPassword, "admin");
|
|
|
|
this->saveCredentialsToFRAM();
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:40:32 +00:00
|
|
|
/**
|
|
|
|
* @brief Get the web username
|
|
|
|
*
|
|
|
|
* @warning The returned pointer should not be freed or modified.
|
|
|
|
*
|
|
|
|
* @return The web username
|
|
|
|
*/
|
2024-01-01 15:13:54 +00:00
|
|
|
char *ESPMegaWebServer::getWebUsername()
|
2024-01-01 13:30:17 +00:00
|
|
|
{
|
|
|
|
return this->webUsername;
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:40:32 +00:00
|
|
|
/**
|
|
|
|
* @brief Get the web password
|
|
|
|
*
|
|
|
|
* @warning The returned pointer should not be freed or modified.
|
|
|
|
*
|
|
|
|
* @return The web password
|
|
|
|
*/
|
2024-01-01 15:13:54 +00:00
|
|
|
char *ESPMegaWebServer::getWebPassword()
|
2024-01-01 13:30:17 +00:00
|
|
|
{
|
|
|
|
return this->webPassword;
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:40:32 +00:00
|
|
|
/**
|
|
|
|
* @brief Set the web username
|
|
|
|
*
|
|
|
|
* @param username The new web username
|
|
|
|
*/
|
2024-01-01 15:13:54 +00:00
|
|
|
void ESPMegaWebServer::setWebUsername(const char *username)
|
2024-01-01 13:30:17 +00:00
|
|
|
{
|
|
|
|
strcpy(this->webUsername, username);
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:40:32 +00:00
|
|
|
/**
|
|
|
|
* @brief Set the web password
|
|
|
|
*
|
|
|
|
* @param password The new web password
|
|
|
|
*/
|
2024-01-01 15:13:54 +00:00
|
|
|
void ESPMegaWebServer::setWebPassword(const char *password)
|
2024-01-01 13:30:17 +00:00
|
|
|
{
|
|
|
|
strcpy(this->webPassword, password);
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:40:32 +00:00
|
|
|
/**
|
|
|
|
* @brief Handle HTTP requests to the dashboard (/) page
|
|
|
|
*
|
|
|
|
* @param request The AsyncWebServerRequest object
|
|
|
|
*/
|
2024-01-01 10:52:59 +00:00
|
|
|
void ESPMegaWebServer::dashboardHandler(AsyncWebServerRequest *request)
|
|
|
|
{
|
2024-01-01 15:13:54 +00:00
|
|
|
if (!request->authenticate(this->webUsername, this->webPassword))
|
|
|
|
{
|
|
|
|
return request->requestAuthentication();
|
|
|
|
}
|
2024-01-01 09:38:14 +00:00
|
|
|
auto bindedDashboardProcessor = std::bind(&ESPMegaWebServer::dashboardProcessor, this, std::placeholders::_1);
|
|
|
|
request->send_P(200, "text/html", ota_html, bindedDashboardProcessor);
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:40:32 +00:00
|
|
|
/**
|
|
|
|
* @brief Replace placeholders in the dashboard HTML with values
|
|
|
|
*
|
|
|
|
* @param var The placeholder name
|
|
|
|
* @return The value to replace the placeholder with
|
|
|
|
*/
|
2024-01-01 10:52:59 +00:00
|
|
|
String ESPMegaWebServer::dashboardProcessor(const String &var)
|
|
|
|
{
|
|
|
|
if (var == "hostname")
|
|
|
|
{
|
2024-01-01 09:38:14 +00:00
|
|
|
return String(this->iot->getNetworkConfig()->hostname);
|
|
|
|
}
|
2024-01-01 10:52:59 +00:00
|
|
|
else if (var == "ip_address")
|
|
|
|
{
|
2024-01-01 09:38:14 +00:00
|
|
|
return this->iot->getIp().toString();
|
|
|
|
}
|
2024-01-01 10:52:59 +00:00
|
|
|
else if (var == "mac_address")
|
|
|
|
{
|
2024-01-01 09:38:14 +00:00
|
|
|
return this->iot->getMac();
|
|
|
|
}
|
2024-01-01 10:52:59 +00:00
|
|
|
else if (var == "model")
|
|
|
|
{
|
2024-01-01 09:38:14 +00:00
|
|
|
return String("ESPMega PRO R3.3c");
|
|
|
|
}
|
2024-01-01 10:52:59 +00:00
|
|
|
else if (var == "mqtt_connection_string")
|
|
|
|
{
|
2024-01-01 09:38:14 +00:00
|
|
|
MqttConfig *mqttConfig = this->iot->getMqttConfig();
|
|
|
|
String connectionString;
|
|
|
|
connectionString += mqttConfig->mqtt_server;
|
|
|
|
connectionString += ":";
|
|
|
|
connectionString += mqttConfig->mqtt_port;
|
|
|
|
return connectionString;
|
|
|
|
}
|
2024-01-01 10:52:59 +00:00
|
|
|
else if (var == "base_topic")
|
|
|
|
{
|
2024-01-01 09:38:14 +00:00
|
|
|
return String(this->iot->getMqttConfig()->base_topic);
|
|
|
|
}
|
2024-01-01 10:52:59 +00:00
|
|
|
else if (var == "mqtt_connected")
|
|
|
|
{
|
2024-01-01 09:38:14 +00:00
|
|
|
return this->iot->mqttConnected() ? "Connected" : "Standalone";
|
|
|
|
}
|
|
|
|
return "";
|
2024-01-01 10:52:59 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 16:40:32 +00:00
|
|
|
/**
|
|
|
|
* @brief Handle HTTP requests to the config (/config) page
|
|
|
|
*
|
|
|
|
* @param request The AsyncWebServerRequest object
|
|
|
|
*/
|
2024-01-01 10:52:59 +00:00
|
|
|
void ESPMegaWebServer::configHandler(AsyncWebServerRequest *request)
|
|
|
|
{
|
2024-01-01 15:13:54 +00:00
|
|
|
if (!request->authenticate(this->webUsername, this->webPassword))
|
|
|
|
{
|
|
|
|
return request->requestAuthentication();
|
|
|
|
}
|
2024-01-01 10:52:59 +00:00
|
|
|
auto bindedConfigProcessor = std::bind(&ESPMegaWebServer::configProcessor, this, std::placeholders::_1);
|
|
|
|
request->send_P(200, "text/html", config_html, bindedConfigProcessor);
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:40:32 +00:00
|
|
|
/**
|
|
|
|
* @brief Replace placeholders in the config HTML with values
|
|
|
|
*
|
|
|
|
* @param var The placeholder name
|
|
|
|
*
|
|
|
|
* @return The value to replace the placeholder with
|
|
|
|
*/
|
2024-01-01 10:52:59 +00:00
|
|
|
String ESPMegaWebServer::configProcessor(const String &var)
|
|
|
|
{
|
|
|
|
MqttConfig *mqttConfig = this->iot->getMqttConfig();
|
|
|
|
NetworkConfig *networkConfig = this->iot->getNetworkConfig();
|
|
|
|
if (var == "ip_address")
|
|
|
|
{
|
|
|
|
return networkConfig->ip.toString();
|
|
|
|
}
|
|
|
|
else if (var == "netmask")
|
|
|
|
{
|
|
|
|
return networkConfig->subnet.toString();
|
|
|
|
}
|
|
|
|
else if (var == "gateway")
|
|
|
|
{
|
|
|
|
return networkConfig->gateway.toString();
|
|
|
|
}
|
|
|
|
else if (var == "dns")
|
|
|
|
{
|
|
|
|
return networkConfig->dns1.toString();
|
|
|
|
}
|
|
|
|
else if (var == "hostname")
|
|
|
|
{
|
|
|
|
return String(networkConfig->hostname);
|
|
|
|
}
|
|
|
|
else if (var == "bms_ip")
|
|
|
|
{
|
|
|
|
return String(mqttConfig->mqtt_server);
|
|
|
|
}
|
|
|
|
else if (var == "bms_port")
|
|
|
|
{
|
|
|
|
return String(mqttConfig->mqtt_port);
|
|
|
|
}
|
2024-01-01 15:13:54 +00:00
|
|
|
else if (var == "bms_useauth")
|
|
|
|
{
|
2024-01-01 10:52:59 +00:00
|
|
|
return mqttConfig->mqtt_useauth ? "checked=\"checked\"" : "";
|
|
|
|
}
|
|
|
|
else if (var == "bms_username")
|
|
|
|
{
|
|
|
|
return String(mqttConfig->mqtt_user);
|
|
|
|
}
|
|
|
|
else if (var == "bms_password")
|
|
|
|
{
|
|
|
|
return String(mqttConfig->mqtt_password);
|
|
|
|
}
|
|
|
|
else if (var == "bms_endpoint")
|
|
|
|
{
|
|
|
|
return String(mqttConfig->base_topic);
|
|
|
|
}
|
2024-01-01 13:30:17 +00:00
|
|
|
else if (var == "web_username")
|
|
|
|
{
|
|
|
|
return String(this->webUsername);
|
|
|
|
}
|
|
|
|
else if (var == "web_password")
|
|
|
|
{
|
|
|
|
return String(this->webPassword);
|
|
|
|
}
|
2024-01-01 10:52:59 +00:00
|
|
|
return "";
|
2024-01-01 13:30:17 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 16:40:32 +00:00
|
|
|
/**
|
|
|
|
* @brief Handle HTTP requests to the OTA update (/ota_update) page
|
|
|
|
*
|
|
|
|
* @param request The AsyncWebServerRequest object
|
|
|
|
*/
|
2024-01-01 15:13:54 +00:00
|
|
|
void ESPMegaWebServer::otaRequestHandler(AsyncWebServerRequest *request)
|
2024-01-01 13:30:17 +00:00
|
|
|
{
|
|
|
|
// Prepare to receive firmware
|
2024-01-01 15:13:54 +00:00
|
|
|
if (!request->authenticate(this->webUsername, this->webPassword))
|
|
|
|
{
|
|
|
|
return request->requestAuthentication();
|
|
|
|
}
|
|
|
|
AsyncWebServerResponse *response = request->beginResponse(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
|
|
|
|
response->addHeader("Connection", "close");
|
|
|
|
request->send(response);
|
|
|
|
// Restart ESPMega
|
|
|
|
ESP.restart();
|
|
|
|
}
|
2024-01-01 13:30:17 +00:00
|
|
|
|
2024-01-01 16:40:32 +00:00
|
|
|
/**
|
|
|
|
* @brief Handle HTTP upload session to the OTA update (/ota_update) page
|
|
|
|
*
|
|
|
|
* @param request The AsyncWebServerRequest object
|
|
|
|
* @param filename The filename of the firmware
|
|
|
|
* @param index The index of the firmware
|
|
|
|
* @param data The firmware data
|
|
|
|
* @param len The length of the firmware data
|
|
|
|
* @param final Whether this is the final chunk of firmware
|
|
|
|
*/
|
2024-01-01 15:13:54 +00:00
|
|
|
void ESPMegaWebServer::otaUploadHandler(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final)
|
|
|
|
{
|
|
|
|
// Receive firmware
|
|
|
|
if (!request->authenticate(this->webUsername, this->webPassword))
|
|
|
|
{
|
|
|
|
return request->requestAuthentication();
|
|
|
|
}
|
|
|
|
if (index == 0)
|
|
|
|
{
|
|
|
|
ESP_LOGI("ESPMegaWebServer", "OTA Update Start");
|
|
|
|
if (!Update.begin(UPDATE_SIZE_UNKNOWN))
|
|
|
|
{ // start with max available size
|
|
|
|
ESP_LOGE("ESPMegaWebServer", "OTA Update Start Error");
|
|
|
|
Update.printError(Serial);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Update.write(data, len) != len)
|
|
|
|
{
|
|
|
|
ESP_LOGE("ESPMegaWebServer", "OTA Update Write Error");
|
|
|
|
Update.printError(Serial);
|
|
|
|
} else {
|
|
|
|
ESP_LOGI("ESPMegaWebServer", "OTA Update Write Success: %uB", index + len);
|
|
|
|
}
|
|
|
|
if (final)
|
|
|
|
{
|
|
|
|
if (Update.end(true))
|
|
|
|
{ // true to set the size to the current progress
|
|
|
|
ESP_LOGI("ESPMegaWebServer", "OTA Update Success: %uB", index + len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ESP_LOGE("ESPMegaWebServer", "OTA Update End Error");
|
|
|
|
Update.printError(Serial);
|
|
|
|
}
|
|
|
|
}
|
2024-01-01 13:30:17 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 16:40:32 +00:00
|
|
|
/**
|
|
|
|
* @brief Handle JSON POST requests to the save_config (/save_config) page
|
|
|
|
*
|
|
|
|
* @param request The AsyncWebServerRequest object
|
|
|
|
* @param json The JSON object representing the request body
|
|
|
|
*/
|
2024-01-01 15:53:28 +00:00
|
|
|
void ESPMegaWebServer::saveConfigJSONHandler(AsyncWebServerRequest *request, JsonVariant &json)
|
2024-01-01 15:13:54 +00:00
|
|
|
{
|
|
|
|
/**
|
2024-01-01 13:56:37 +00:00
|
|
|
* Request POST body should be a JSON object
|
|
|
|
* containing the following fields:
|
|
|
|
* ip_address: String, the IP address of the device
|
|
|
|
* netmask: String, the netmask of the device
|
|
|
|
* gateway: String, the gateway of the device
|
|
|
|
* dns: String, the DNS of the device
|
|
|
|
* hostname: String, the hostname of the device
|
|
|
|
* bms_ip: String, the IP address of the MQTT broker
|
|
|
|
* bms_port: int, the port of the MQTT broker
|
|
|
|
* bms_useauth: Boolean, true if the MQTT broker requires authentication
|
|
|
|
* bms_username: String, the username of the MQTT broker
|
|
|
|
* bms_password: String, the password of the MQTT broker
|
|
|
|
* bms_endpoint: String, the base topic of the MQTT broker
|
|
|
|
* web_username: String, the username of the web server
|
|
|
|
* web_password: String, the password of the web server
|
|
|
|
*/
|
2024-01-01 15:53:28 +00:00
|
|
|
ESP_LOGD("ESPMegaWebServer", "Saving config");
|
|
|
|
JsonObject root = json.as<JsonObject>();
|
2024-01-01 13:56:37 +00:00
|
|
|
// Network Config
|
|
|
|
NetworkConfig networkConfig;
|
2024-01-01 15:53:28 +00:00
|
|
|
networkConfig.useStaticIp = true;
|
|
|
|
networkConfig.useWifi = false;
|
2024-01-01 13:56:37 +00:00
|
|
|
IPAddress ip;
|
2024-01-01 15:53:28 +00:00
|
|
|
ESP_LOGD("ESPMegaWebServer", "Checking IP Address");
|
2024-01-01 15:13:54 +00:00
|
|
|
if (!ip.fromString(root["ip_address"].as<String>()))
|
|
|
|
{
|
2024-01-01 15:53:28 +00:00
|
|
|
ESP_LOGE("ESPMegaWebServer", "Invalid Config IP Address");
|
2024-01-01 13:56:37 +00:00
|
|
|
request->send(400, "text/plain", "Invalid IP Address");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
networkConfig.ip = ip;
|
2024-01-01 15:53:28 +00:00
|
|
|
ESP_LOGD("ESPMegaWebServer", "Checking Netmask");
|
2024-01-01 15:13:54 +00:00
|
|
|
if (!ip.fromString(root["netmask"].as<String>()))
|
|
|
|
{
|
2024-01-01 15:53:28 +00:00
|
|
|
ESP_LOGE("ESPMegaWebServer", "Invalid Config Netmask");
|
2024-01-01 13:56:37 +00:00
|
|
|
request->send(400, "text/plain", "Invalid Netmask");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
networkConfig.subnet = ip;
|
2024-01-01 15:53:28 +00:00
|
|
|
ESP_LOGD("ESPMegaWebServer", "Checking Gateway");
|
2024-01-01 15:13:54 +00:00
|
|
|
if (!ip.fromString(root["gateway"].as<String>()))
|
|
|
|
{
|
2024-01-01 15:53:28 +00:00
|
|
|
ESP_LOGE("ESPMegaWebServer", "Invalid Config Gateway");
|
2024-01-01 13:56:37 +00:00
|
|
|
request->send(400, "text/plain", "Invalid Gateway");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
networkConfig.gateway = ip;
|
2024-01-01 15:53:28 +00:00
|
|
|
ESP_LOGD("ESPMegaWebServer", "Checking DNS");
|
2024-01-01 15:13:54 +00:00
|
|
|
if (!ip.fromString(root["dns"].as<String>()))
|
|
|
|
{
|
2024-01-01 15:53:28 +00:00
|
|
|
ESP_LOGE("ESPMegaWebServer", "Invalid Config DNS");
|
2024-01-01 13:56:37 +00:00
|
|
|
request->send(400, "text/plain", "Invalid DNS");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
networkConfig.dns1 = ip;
|
2024-01-01 15:53:28 +00:00
|
|
|
ESP_LOGD("ESPMegaWebServer", "Setting Hostname");
|
2024-01-01 13:56:37 +00:00
|
|
|
strcpy(networkConfig.hostname, root["hostname"].as<String>().c_str());
|
|
|
|
// MQTT Config
|
|
|
|
MqttConfig mqttConfig;
|
2024-01-01 15:53:28 +00:00
|
|
|
ESP_LOGD("ESPMegaWebServer", "Setting MQTT Server");
|
2024-01-01 13:56:37 +00:00
|
|
|
strcpy(mqttConfig.mqtt_server, root["bms_ip"].as<String>().c_str());
|
2024-01-01 15:53:28 +00:00
|
|
|
ESP_LOGD("ESPMegaWebServer", "Checking MQTT Port");
|
2024-01-01 13:56:37 +00:00
|
|
|
uint16_t mqttPort = root["bms_port"].as<int>();
|
2024-01-01 15:13:54 +00:00
|
|
|
if (mqttConfig.mqtt_port <= 0 || mqttConfig.mqtt_port > 65535)
|
|
|
|
{
|
2024-01-01 15:53:28 +00:00
|
|
|
ESP_LOGE("ESPMegaWebServer", "Invalid Config MQTT Port");
|
2024-01-01 13:56:37 +00:00
|
|
|
request->send(400, "text/plain", "Invalid MQTT Port");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mqttConfig.mqtt_port = mqttPort;
|
2024-01-01 15:53:28 +00:00
|
|
|
ESP_LOGD("ESPMegaWebServer", "Checking MQTT Use Auth");
|
2024-01-01 13:56:37 +00:00
|
|
|
mqttConfig.mqtt_useauth = root["bms_useauth"].as<bool>();
|
2024-01-01 15:53:28 +00:00
|
|
|
ESP_LOGD("ESPMegaWebServer", "Setting MQTT Username");
|
2024-01-01 13:56:37 +00:00
|
|
|
strcpy(mqttConfig.mqtt_user, root["bms_username"].as<String>().c_str());
|
2024-01-01 15:53:28 +00:00
|
|
|
ESP_LOGD("ESPMegaWebServer", "Setting MQTT Password");
|
2024-01-01 13:56:37 +00:00
|
|
|
strcpy(mqttConfig.mqtt_password, root["bms_password"].as<String>().c_str());
|
2024-01-01 15:53:28 +00:00
|
|
|
ESP_LOGD("ESPMegaWebServer", "Setting MQTT Base Topic");
|
2024-01-01 13:56:37 +00:00
|
|
|
strcpy(mqttConfig.base_topic, root["bms_endpoint"].as<String>().c_str());
|
|
|
|
// Web Server Config
|
2024-01-01 15:53:28 +00:00
|
|
|
ESP_LOGD("ESPMegaWebServer", "Setting Web Username");
|
2024-01-01 13:56:37 +00:00
|
|
|
strcpy(this->webUsername, root["web_username"].as<String>().c_str());
|
2024-01-01 15:53:28 +00:00
|
|
|
ESP_LOGD("ESPMegaWebServer", "Setting Web Password");
|
2024-01-01 13:56:37 +00:00
|
|
|
strcpy(this->webPassword, root["web_password"].as<String>().c_str());
|
|
|
|
// Commit changes to FRAM
|
2024-01-01 15:53:28 +00:00
|
|
|
ESP_LOGD("ESPMegaWebServer", "Committing Network Config to FRAM");
|
2024-01-01 13:56:37 +00:00
|
|
|
this->iot->setNetworkConfig(networkConfig);
|
|
|
|
this->iot->saveNetworkConfig();
|
2024-01-01 15:53:28 +00:00
|
|
|
ESP_LOGD("ESPMegaWebServer", "Committing MQTT Config to FRAM");
|
2024-01-01 13:56:37 +00:00
|
|
|
this->iot->setMqttConfig(mqttConfig);
|
|
|
|
this->iot->saveMqttConfig();
|
2024-01-01 15:53:28 +00:00
|
|
|
ESP_LOGD("ESPMegaWebServer", "Committing Web Server Config to FRAM");
|
2024-01-01 13:56:37 +00:00
|
|
|
this->saveCredentialsToFRAM();
|
2024-01-01 15:53:28 +00:00
|
|
|
ESP_LOGD("ESPMegaWebServer", "Config saved");
|
2024-01-01 13:56:37 +00:00
|
|
|
// Send response
|
|
|
|
request->send(200, "text/plain", "OK");
|
2024-01-01 15:53:28 +00:00
|
|
|
ESP.restart();
|
2024-01-01 16:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the AsyncWebServer object
|
|
|
|
*
|
|
|
|
* @return The AsyncWebServer object
|
|
|
|
*/
|
|
|
|
AsyncWebServer *ESPMegaWebServer::getServer() {
|
|
|
|
return this->server;
|
2024-01-15 07:20:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Request authentication from the client
|
|
|
|
*
|
|
|
|
* This method requests authentication from the client.
|
|
|
|
*
|
|
|
|
* @param request The AsyncWebServerRequest object
|
|
|
|
*/
|
|
|
|
bool ESPMegaWebServer::checkAuthentication(AsyncWebServerRequest *request) {
|
|
|
|
if (!request->authenticate(this->webUsername, this->webPassword))
|
|
|
|
{
|
|
|
|
request->requestAuthentication();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2024-01-01 13:56:37 +00:00
|
|
|
}
|