save config prototype

This commit is contained in:
Siwat Sirichai 2024-01-01 20:56:37 +07:00
parent cd271efddd
commit f1db2c0487
5 changed files with 638 additions and 409 deletions

View file

@ -20,6 +20,8 @@ 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);
}
void ESPMegaWebServer::loop()
@ -109,6 +111,7 @@ void ESPMegaWebServer::dashboardHandler(AsyncWebServerRequest *request)
request->authenticate(this->webUsername, this->webPassword);
auto bindedDashboardProcessor = std::bind(&ESPMegaWebServer::dashboardProcessor, this, std::placeholders::_1);
request->send_P(200, "text/html", ota_html, bindedDashboardProcessor);
}
String ESPMegaWebServer::dashboardProcessor(const String &var)
@ -220,3 +223,77 @@ void ESPMegaWebServer::otaHandler(AsyncWebServerRequest *request)
}
void ESPMegaWebServer::saveConfigHandler(AsyncWebServerRequest *request) {
/**
* 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
*/
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>();
// Network Config
NetworkConfig networkConfig;
IPAddress ip;
if (!ip.fromString(root["ip_address"].as<String>())) {
request->send(400, "text/plain", "Invalid IP Address");
return;
}
networkConfig.ip = ip;
if (!ip.fromString(root["netmask"].as<String>())) {
request->send(400, "text/plain", "Invalid Netmask");
return;
}
networkConfig.subnet = ip;
if (!ip.fromString(root["gateway"].as<String>())) {
request->send(400, "text/plain", "Invalid Gateway");
return;
}
networkConfig.gateway = ip;
if (!ip.fromString(root["dns"].as<String>())) {
request->send(400, "text/plain", "Invalid DNS");
return;
}
networkConfig.dns1 = ip;
strcpy(networkConfig.hostname, root["hostname"].as<String>().c_str());
// MQTT Config
MqttConfig mqttConfig;
strcpy(mqttConfig.mqtt_server, root["bms_ip"].as<String>().c_str());
uint16_t mqttPort = root["bms_port"].as<int>();
if (mqttConfig.mqtt_port <= 0 || mqttConfig.mqtt_port > 65535) {
request->send(400, "text/plain", "Invalid MQTT Port");
return;
}
mqttConfig.mqtt_port = mqttPort;
mqttConfig.mqtt_useauth = root["bms_useauth"].as<bool>();
strcpy(mqttConfig.mqtt_user, root["bms_username"].as<String>().c_str());
strcpy(mqttConfig.mqtt_password, root["bms_password"].as<String>().c_str());
strcpy(mqttConfig.base_topic, root["bms_endpoint"].as<String>().c_str());
// Web Server Config
strcpy(this->webUsername, root["web_username"].as<String>().c_str());
strcpy(this->webPassword, root["web_password"].as<String>().c_str());
// Commit changes to FRAM
this->iot->setNetworkConfig(networkConfig);
this->iot->saveNetworkConfig();
this->iot->setMqttConfig(mqttConfig);
this->iot->saveMqttConfig();
this->saveCredentialsToFRAM();
// Send response
request->send(200, "text/plain", "OK");
}