Add /get_device_info and /get_config

This commit is contained in:
Siwat Sirichai 2024-05-18 13:14:43 +07:00
parent d75b028cd2
commit 2a497625de
4 changed files with 57 additions and 1 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.DS_Store

View File

@ -52,6 +52,10 @@ void ESPMegaWebServer::begin()
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);
this->server->on("/reboot", HTTP_GET, std::bind(&ESPMegaWebServer::rebootHandler, this, std::placeholders::_1));
auto bindedGetConfigHandler = std::bind(&ESPMegaWebServer::getConfigHandler, this, std::placeholders::_1);
this->server->on("/get_config", HTTP_GET, bindedGetConfigHandler);
auto bindedGetDeviceInfoHandler = std::bind(&ESPMegaWebServer::getDeviceInfoHandler, this, std::placeholders::_1);
this->server->on("/get_device_info", HTTP_GET, bindedGetDeviceInfoHandler);
}
/**
@ -546,3 +550,51 @@ void ESPMegaWebServer::rebootHandler(AsyncWebServerRequest *request)
request->send(200, "text/plain", "Rebooting ESPMega PRO...");
esp_restart();
}
void ESPMegaWebServer::getConfigHandler(AsyncWebServerRequest *request) {
if (!request->authenticate(this->webUsername, this->webPassword))
{
return request->requestAuthentication();
}
StaticJsonDocument<512> doc;
NetworkConfig *networkConfig = this->iot->getNetworkConfig();
MqttConfig *mqttConfig = this->iot->getMqttConfig();
doc["ip_address"] = networkConfig->ip.toString();
doc["netmask"] = networkConfig->subnet.toString();
doc["gateway"] = networkConfig->gateway.toString();
doc["dns"] = networkConfig->dns1.toString();
doc["hostname"] = networkConfig->hostname;
doc["bms_ip"] = mqttConfig->mqtt_server;
doc["bms_port"] = mqttConfig->mqtt_port;
doc["bms_useauth"] = mqttConfig->mqtt_useauth;
doc["bms_username"] = mqttConfig->mqtt_user;
doc["bms_password"] = mqttConfig->mqtt_password;
doc["bms_endpoint"] = mqttConfig->base_topic;
doc["web_username"] = this->webUsername;
doc["web_password"] = this->webPassword;
char buffer[512];
serializeJson(doc, buffer);
request->send(200, "application/json", buffer);
}
void ESPMegaWebServer::getDeviceInfoHandler(AsyncWebServerRequest *request) {
if (!request->authenticate(this->webUsername, this->webPassword))
{
return request->requestAuthentication();
}
StaticJsonDocument<512> doc;
doc["hostname"] = this->iot->getNetworkConfig()->hostname;
doc["ip_address"] = this->iot->getIp().toString();
doc["mac_address"] = this->iot->getMac();
doc["model"] = BOARD_MODEL;
doc["mqtt_server"] = this->iot->getMqttConfig()->mqtt_server;
doc["mqtt_port"] = this->iot->getMqttConfig()->mqtt_port;
doc["base_topic"] = this->iot->getMqttConfig()->base_topic;
doc["mqtt_connected"] = this->iot->mqttConnected() ? "Connected" : "Standalone";
doc["software_version"] = SW_VERSION;
doc["sdk_version"] = SDK_VERSION;
doc["idf_version"] = IDF_VER;
char buffer[512];
serializeJson(doc, buffer);
request->send(200, "application/json", buffer);
}

View File

@ -53,6 +53,8 @@ class ESPMegaWebServer
String configProcessor(const String& var);
AsyncCallbackJsonWebHandler *saveConfigHandler;
void saveConfigJSONHandler(AsyncWebServerRequest *request, JsonVariant &json);
void getConfigHandler(AsyncWebServerRequest *request);
void getDeviceInfoHandler(AsyncWebServerRequest *request);
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,6 +31,6 @@ 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=5
build_flags = -DCORE_DEBUG_LEVEL=5 -DSW_VERSION='"1.0.0"' -DSDK_VERSION='"2.6.0"' -DBOARD_MODEL='"ESPMegaPRO R3.3c"'
extra_scripts = pre:helper_scripts/html2cpp.py
monitor_port = COM36