From 271a8285c2cfdbf2701d9e2129c0c9203dac1db0 Mon Sep 17 00:00:00 2001 From: DaOfficialWizard <45744329+ZanzyTHEbar@users.noreply.github.com> Date: Tue, 20 Jun 2023 13:43:51 +0100 Subject: [PATCH] feat: begin working on the API --- .../src/local/network/api/api.cpp | 22 ++++++++++++++----- .../src/local/network/api/api.hpp | 5 +++-- Code/PlatformIO_Project/src/main.cpp | 4 +--- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/Code/PlatformIO_Project/lib/GreenHouseTowerDIY/src/local/network/api/api.cpp b/Code/PlatformIO_Project/lib/GreenHouseTowerDIY/src/local/network/api/api.cpp index 86e1d26..f1d2627 100644 --- a/Code/PlatformIO_Project/lib/GreenHouseTowerDIY/src/local/network/api/api.cpp +++ b/Code/PlatformIO_Project/lib/GreenHouseTowerDIY/src/local/network/api/api.cpp @@ -1,14 +1,12 @@ #include "api.hpp" -API::API(APIServer& server, GreenHouseConfig& configManager) - : configManager(configManager), server(server) {} +API::API(ProjectConfig& projectConfig, GreenHouseConfig& configManager) + : projectConfig(projectConfig), + configManager(configManager), + server(80, projectConfig, "/control", "/wifimanager", "/tower") {} API::~API() {} -void API::setupServer() { - server.begin(); -} - void API::begin() { // handle the WiFi connection state changes switch (wifiStateManager.getCurrentState()) { @@ -37,6 +35,18 @@ void API::begin() { } } +/** + * @brief Setup the API Server + * @note Add all the routes and handlers here + */ +void API::setupServer() { + server.updateCommandHandlers( + "/setDHT", + [this](AsyncWebServerRequest* request) { this->setDHT(request); }); + + server.begin(); +} + void API::setDHT(AsyncWebServerRequest* request) { switch (server._networkMethodsMap_enum[request->method()]) { case APIServer::POST: { diff --git a/Code/PlatformIO_Project/lib/GreenHouseTowerDIY/src/local/network/api/api.hpp b/Code/PlatformIO_Project/lib/GreenHouseTowerDIY/src/local/network/api/api.hpp index 37add05..37e4b94 100644 --- a/Code/PlatformIO_Project/lib/GreenHouseTowerDIY/src/local/network/api/api.hpp +++ b/Code/PlatformIO_Project/lib/GreenHouseTowerDIY/src/local/network/api/api.hpp @@ -6,12 +6,13 @@ #include class API { private: - APIServer& server; + APIServer server; + ProjectConfig& projectConfig; GreenHouseConfig& configManager; void setupServer(); public: - API(APIServer& server, GreenHouseConfig& configManager); + API(ProjectConfig& projectConfig, GreenHouseConfig& configManager); virtual ~API(); void begin(); void setTopic(AsyncWebServerRequest* request); diff --git a/Code/PlatformIO_Project/src/main.cpp b/Code/PlatformIO_Project/src/main.cpp index 31cf09a..e2b3900 100644 --- a/Code/PlatformIO_Project/src/main.cpp +++ b/Code/PlatformIO_Project/src/main.cpp @@ -39,8 +39,6 @@ ConfigHandler configHandler(config); GreenHouseConfig greenhouseConfig(config); //* Network -APIServer server(80, config, "/control", "/wifimanager", "/tower"); - WiFiHandler network(config, WIFI_SSID, WIFI_PASS, 1); OTA ota(config); MDNSHandler mDNS(config, "_tower", "data", "_tcp", "api_port", "80"); @@ -49,7 +47,7 @@ MQTTClient mqttClient; BaseMQTT mqtt(greenhouseConfig, config, mqttClient); //* API -API api(server, greenhouseConfig); +API api(config, greenhouseConfig); //* Sensors TowerTemp tower_temp(greenhouseConfig);