From 2a497625de3687f748c53dd17a8347d92b6563da Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Sat, 18 May 2024 13:14:43 +0700 Subject: [PATCH] Add /get_device_info and /get_config --- .gitignore | 2 + .../lib/ESPMegaPRO/ESPMegaWebServer.cpp | 52 +++++++++++++++++++ .../lib/ESPMegaPRO/ESPMegaWebServer.hpp | 2 + ESPMegaPRO-OS-SDK/platformio.ini | 2 +- 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9bea433 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +.DS_Store diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaWebServer.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaWebServer.cpp index b2e8782..f1dc3fe 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaWebServer.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaWebServer.cpp @@ -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); } /** @@ -545,4 +549,52 @@ 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); } \ No newline at end of file diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaWebServer.hpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaWebServer.hpp index 6559b67..4b5181c 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaWebServer.hpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaWebServer.hpp @@ -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); diff --git a/ESPMegaPRO-OS-SDK/platformio.ini b/ESPMegaPRO-OS-SDK/platformio.ini index 3333355..d01db73 100644 --- a/ESPMegaPRO-OS-SDK/platformio.ini +++ b/ESPMegaPRO-OS-SDK/platformio.ini @@ -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 \ No newline at end of file