save config prototype 2

This commit is contained in:
Siwat Sirichai 2024-01-01 22:53:28 +07:00
parent 5302c4c758
commit c1d23495d5
4 changed files with 35 additions and 14 deletions

View File

@ -5,6 +5,7 @@ ESPMegaWebServer::ESPMegaWebServer(uint16_t port, ESPMegaIoT *iot)
this->port = port;
this->iot = iot;
this->server = new AsyncWebServer(port);
this->saveConfigHandler = new AsyncCallbackJsonWebHandler("/save_config", std::bind(&ESPMegaWebServer::saveConfigJSONHandler, this, std::placeholders::_1, std::placeholders::_2));
}
ESPMegaWebServer::~ESPMegaWebServer()
@ -20,8 +21,7 @@ void ESPMegaWebServer::begin()
this->server->on("/", HTTP_GET, bindedDashboardHandler);
auto bindedConfigHandler = std::bind(&ESPMegaWebServer::configHandler, this, std::placeholders::_1);
this->server->on("/config", HTTP_GET, bindedConfigHandler);
auto bindedSaveConfigHandler = std::bind(&ESPMegaWebServer::saveConfigHandler, this, std::placeholders::_1);
this->server->on("/config", HTTP_POST, bindedSaveConfigHandler);
this->server->addHandler(saveConfigHandler);
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);
@ -277,7 +277,7 @@ void ESPMegaWebServer::otaUploadHandler(AsyncWebServerRequest *request, String f
}
}
void ESPMegaWebServer::saveConfigHandler(AsyncWebServerRequest *request)
void ESPMegaWebServer::saveConfigJSONHandler(AsyncWebServerRequest *request, JsonVariant &json)
{
/**
* Request POST body should be a JSON object
@ -296,65 +296,84 @@ void ESPMegaWebServer::saveConfigHandler(AsyncWebServerRequest *request)
* web_username: String, the username of the web server
* web_password: String, the password of the web server
*/
StaticJsonDocument<1024> doc;
DeserializationError error = deserializeJson(doc, request->getParam("plain")->value());
if (error)
{
request->send(400, "text/plain", "Invalid JSON");
return;
}
JsonObject root = doc.as<JsonObject>();
ESP_LOGD("ESPMegaWebServer", "Saving config");
JsonObject root = json.as<JsonObject>();
// Network Config
NetworkConfig networkConfig;
networkConfig.useStaticIp = true;
networkConfig.useWifi = false;
IPAddress ip;
ESP_LOGD("ESPMegaWebServer", "Checking IP Address");
if (!ip.fromString(root["ip_address"].as<String>()))
{
ESP_LOGE("ESPMegaWebServer", "Invalid Config IP Address");
request->send(400, "text/plain", "Invalid IP Address");
return;
}
networkConfig.ip = ip;
ESP_LOGD("ESPMegaWebServer", "Checking Netmask");
if (!ip.fromString(root["netmask"].as<String>()))
{
ESP_LOGE("ESPMegaWebServer", "Invalid Config Netmask");
request->send(400, "text/plain", "Invalid Netmask");
return;
}
networkConfig.subnet = ip;
ESP_LOGD("ESPMegaWebServer", "Checking Gateway");
if (!ip.fromString(root["gateway"].as<String>()))
{
ESP_LOGE("ESPMegaWebServer", "Invalid Config Gateway");
request->send(400, "text/plain", "Invalid Gateway");
return;
}
networkConfig.gateway = ip;
ESP_LOGD("ESPMegaWebServer", "Checking DNS");
if (!ip.fromString(root["dns"].as<String>()))
{
ESP_LOGE("ESPMegaWebServer", "Invalid Config DNS");
request->send(400, "text/plain", "Invalid DNS");
return;
}
networkConfig.dns1 = ip;
ESP_LOGD("ESPMegaWebServer", "Setting Hostname");
strcpy(networkConfig.hostname, root["hostname"].as<String>().c_str());
// MQTT Config
MqttConfig mqttConfig;
ESP_LOGD("ESPMegaWebServer", "Setting MQTT Server");
strcpy(mqttConfig.mqtt_server, root["bms_ip"].as<String>().c_str());
ESP_LOGD("ESPMegaWebServer", "Checking MQTT Port");
uint16_t mqttPort = root["bms_port"].as<int>();
if (mqttConfig.mqtt_port <= 0 || mqttConfig.mqtt_port > 65535)
{
ESP_LOGE("ESPMegaWebServer", "Invalid Config MQTT Port");
request->send(400, "text/plain", "Invalid MQTT Port");
return;
}
mqttConfig.mqtt_port = mqttPort;
ESP_LOGD("ESPMegaWebServer", "Checking MQTT Use Auth");
mqttConfig.mqtt_useauth = root["bms_useauth"].as<bool>();
ESP_LOGD("ESPMegaWebServer", "Setting MQTT Username");
strcpy(mqttConfig.mqtt_user, root["bms_username"].as<String>().c_str());
ESP_LOGD("ESPMegaWebServer", "Setting MQTT Password");
strcpy(mqttConfig.mqtt_password, root["bms_password"].as<String>().c_str());
ESP_LOGD("ESPMegaWebServer", "Setting MQTT Base Topic");
strcpy(mqttConfig.base_topic, root["bms_endpoint"].as<String>().c_str());
// Web Server Config
ESP_LOGD("ESPMegaWebServer", "Setting Web Username");
strcpy(this->webUsername, root["web_username"].as<String>().c_str());
ESP_LOGD("ESPMegaWebServer", "Setting Web Password");
strcpy(this->webPassword, root["web_password"].as<String>().c_str());
// Commit changes to FRAM
ESP_LOGD("ESPMegaWebServer", "Committing Network Config to FRAM");
this->iot->setNetworkConfig(networkConfig);
this->iot->saveNetworkConfig();
ESP_LOGD("ESPMegaWebServer", "Committing MQTT Config to FRAM");
this->iot->setMqttConfig(mqttConfig);
this->iot->saveMqttConfig();
ESP_LOGD("ESPMegaWebServer", "Committing Web Server Config to FRAM");
this->saveCredentialsToFRAM();
ESP_LOGD("ESPMegaWebServer", "Config saved");
// Send response
request->send(200, "text/plain", "OK");
ESP.restart();
}

View File

@ -5,6 +5,7 @@
#include <Update.h>
#include <FRAM.h>
#include <ArduinoJson.h>
#include <AsyncJson.h>
#include <html/all.h>
@ -44,7 +45,8 @@ class ESPMegaWebServer
String dashboardProcessor(const String& var);
void configHandler(AsyncWebServerRequest *request);
String configProcessor(const String& var);
void saveConfigHandler(AsyncWebServerRequest *request);
AsyncCallbackJsonWebHandler *saveConfigHandler;
void saveConfigJSONHandler(AsyncWebServerRequest *request, JsonVariant &json);
void otaRequestHandler(AsyncWebServerRequest *request);
void otaUploadHandler(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final);
void restAPIHandler(AsyncWebServerRequest *request);

View File

@ -31,5 +31,5 @@ lib_deps = adafruit/Adafruit PWM Servo Driver Library@^2.4.1
https://github.com/me-no-dev/ESPAsyncWebServer.git
bblanchon/ArduinoJson@^6.21.4
monitor_speed = 115200
build_flags = -DCORE_DEBUG_LEVEL=1
build_flags = -DCORE_DEBUG_LEVEL=5
extra_scripts = pre:helper_scripts/html2cpp.py

View File

@ -8,7 +8,7 @@
// #define WRITE_DEFAULT_NETCONF
#define CLIMATE_CARD_ENABLE
#define MQTT_CARD_REGISTER
#define DISPLAY_ENABLE
//#define DISPLAY_ENABLE
#define WEB_SERVER_ENABLE
// Demo PLC firmware using the ESPMegaPRO OOP library