From 35f61436ab0ad68b16da6217c22ffd9f6c627b21 Mon Sep 17 00:00:00 2001 From: ZanzyTHEbar Date: Thu, 8 Sep 2022 16:01:52 +0100 Subject: [PATCH] Major Update - Remove unneeded `data` folder at root of project - Deprecate API_Utilities class (kept only for the shaEncoder function) - Begin migration to better organization --- ESP/data/ascii.txt | 60 -------------- ESP/lib/src/network/api/baseAPI/baseAPI.cpp | 45 ++++++++--- ESP/lib/src/network/api/baseAPI/baseAPI.hpp | 75 ++++++++++++++++- .../network/api/utilities/apiUtilities.cpp | 35 +------- .../network/api/utilities/apiUtilities.hpp | 80 ++----------------- ESP/lib/src/network/api/webserverHandler.cpp | 4 +- 6 files changed, 115 insertions(+), 184 deletions(-) delete mode 100644 ESP/data/ascii.txt diff --git a/ESP/data/ascii.txt b/ESP/data/ascii.txt deleted file mode 100644 index c515e2a..0000000 --- a/ESP/data/ascii.txt +++ /dev/null @@ -1,60 +0,0 @@ - : === WELCOME === TO === : - =========================================================================================================================== - ██████╗ ██████╗ ███████╗███╗ ██╗██╗██████╗ ██╗███████╗ - ██╔═══██╗██╔══██╗██╔════╝████╗ ██║██║██╔══██╗██║██╔════╝ - ██║ ██║██████╔╝█████╗ ██╔██╗ ██║██║██████╔╝██║███████╗ - ██║ ██║██╔═══╝ ██╔══╝ ██║╚██╗██║██║██╔══██╗██║╚════██║ - ╚██████╔╝██║ ███████╗██║ ╚████║██║██║ ██║██║███████║ - ╚═════╝ ╚═╝ ╚══════╝╚═╝ ╚═══╝╚═╝╚═╝ ╚═╝╚═╝╚══════╝ - - ██████████████ - ██▓▓▓▓▓▓▒▒▒▒▒▒▒▒░░▒▒▓▓▓▓██ - ████▓▓░░░░▒▒░░░░░░▒▒░░░░░░▒▒░░████ - ██▓▓▒▒▓▓▓▓▒▒▒▒░░░░░░▒▒░░▒▒░░░░░░▒▒░░▒▒▓▓▓▓ - ██▓▓▒▒▒▒▒▒▒▒░░▒▒░░░░░░░░░░░░▒▒░░░░▒▒░░░░▒▒░░██ - ██▓▓▓▓░░░░▒▒░░░░▒▒▒▒░░░░░░░░░░▒▒░░ ░░░░░░░░▒▒░░██ - ██▓▓▓▓▓▓▓▓▓▓░░░░░░▒▒░░░░░░░░░░░░░░ ░░░░░░░░░░░░██ - ██▓▓▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░▒▒░░░░░░░░░░░░ ░░ ▒▒▒▒██ - ██▓▓▒▒▒▒▒▒▒▒░░░░▒▒░░░░░░░░░░░░░░░░░░ ░░░░██ - ▓▓▓▓▒▒▒▒▒▒▒▒▒▒░░░░░░▒▒░░░░░░░░░░░░░░░░░░▓▓▓▓▓▓▓▓▓▓ ░░ ▒▒▓▓ - ██▒▒▒▒▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░░░▓▓▓▓▒▒▒▒▒▒▒▒▓▓▓▓▓▓ ░░██ - ▓▓▒▒▒▒▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░ ▓▓▒▒▒▒▒▒▒▒░░░░▒▒▒▒▓▓▒▒ ░░▒▒▓▓ - ██▓▓▒▒░░░░░░▒▒▒▒▒▒░░░░░░░░░░░░░░░░▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░▒▒▓▓▓▓ ░░██ - ██▓▓▓▓▒▒▒▒▒▒▒▒░░░░▒▒░░░░░░░░░░░░ ▒▒▒▒▒▒▒▒▒▒▒▒████▓▓░░░░▒▒▓▓ ░░██ - ██▓▓▒▒▒▒▒▒▓▓▒▒▓▓░░░░░░░░░░░░░░░░░░▓▓▒▒▒▒░░▒▒▒▒████ ▒▒██░░▒▒▓▓▓▓ ░░██ - ██▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░▓▓░░▒▒▒▒▒▒██████▒▒ ▓▓▓▓░░▓▓▓▓ ░░██ - ██▓▓▒▒▒▒░░▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░▓▓▒▒░░▒▒░░████████▓▓ ██▓▓▒▒▓▓ ░░██ - ██▓▓▓▓▓▓▓▓▒▒▒▒▒▒░░░░░░▒▒░░░░░░░░░░▒▒░░▒▒░░▒▒██████████▒▒██▒▒▒▒▓▓ ░░██ - ██▓▓▒▒▒▒▒▒░░░░░░░░▒▒▒▒░░░░░░░░░░░░▒▒▒▒░░░░▒▒██▒▒██████ ██▒▒▒▒▓▓ ░░██ - ██▒▒▒▒▒▒░░▓▓▓▓▓▓▒▒░░░░░░░░░░░░░░░░▒▒░░ ▒▒░░██ ▒▒████▒▒██▒▒▒▒▓▓ ░░██ - ██▓▓▓▓▓▓▓▓░░▒▒▒▒░░▒▒░░░░░░░░░░░░░░▒▒▒▒░░░░░░▒▒██ ██████▒▒▒▒▒▒▓▓░░░░██ - ██▓▓██▓▓▒▒▒▒▓▓░░░░▒▒░░░░░░░░░░░░░░░░░░▒▒▒▒░░▒▒░░▒▒██████▒▒▒▒▒▒▓▓ ░░██ - ██▓▓██▒▒▒▒▒▒▒▒▓▓▓▓▒▒░░░░░░░░░░░░░░░░░░▒▒░░▒▒░░▒▒░░▒▒░░▒▒▒▒▒▒▒▒▓▓░░░░██ - ██▒▒▓▓██▓▓▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░▒▒░░▒▒░░░░░░▒▒▒▒▒▒▒▒▓▓ ░░░░██ - ██▒▒▒▒▓▓██▓▓▓▓▒▒▒▒▒▒▒▒░░░░░░░░▒▒░░░░░░░░░░░░▒▒▒▒▒▒░░▒▒▒▒▒▒▓▓▓▓ ░░░░██ - ▓▓▓▓▒▒▒▒▓▓██▓▓▒▒▒▒▒▒░░░░▒▒▒▒░░▒▒░░░░░░░░░░░░░░░░░░▒▒▒▒▓▓▓▓▓▓░░░░░░░░▒▒██ - ██▒▒▒▒▓▓▒▒▓▓██▒▒▒▒▒▒▒▒▓▓▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ░░ ░░░░██ - ██▒▒▒▒▓▓░░▒▒▒▒██▓▓▓▓▓▓▒▒▒▒▒▒▒▒▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒░░▒▒██ - ██▒▒▒▒▓▓▒▒░░▓▓▒▒██▓▓▒▒▒▒▒▒▒▒░░▓▓▒▒▓▓▒▒░░░░▒▒░░░░░░░░░░░░░░░░░░░░░░▓▓██ - ██▒▒▒▒▒▒░░▒▒▒▒▒▒▓▓██▓▓▒▒▒▒▒▒▓▓▒▒▒▒▒▒▒▒▒▒▓▓░░░░░░░░░░▒▒░░░░▒▒░░▒▒▒▒██ - ██▒▒▒▒░░▒▒░░▒▒████ ██▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░▓▓▒▒▒▒▒▒▓▓▒▒██ - ██▒▒▓▓░░▒▒░░▓▓ ████▓▓▒▒▓▓▒▒▒▒▒▒░░▒▒▒▒▒▒▒▒▓▓▒▒▒▒▒▒▒▒▒▒██▓▓ - ██▒▒▒▒▒▒▒▒ ██ ████▓▓▒▒▓▓▓▓▓▓▒▒▒▒▒▒▒▒▓▓▒▒▒▒▒▒████ - ██▒▒▒▒▒▒▒▒░░██ ██████▓▓▒▒▒▒▒▒▒▒▓▓▓▓██████ - ██▒▒▒▒▒▒▒▒░░ ████ ██████████████ - ██▒▒▒▒▒▒▒▒░░ ░░████ - ████▒▒▒▒▒▒░░░░ ░░████ - ████▒▒▒▒▒▒░░░░ ░░██ - ████▒▒▒▒▒▒░░ ░░██ - ██▓▓▒▒▒▒░░ ▒▒▓▓ - ████▒▒░░ ▒▒██ - ▓▓▒▒░░░░██ - ██░░ ██ - ▓▓██ ██░░░░██ - ██░░██ ██░░░░██ - ██░░██ ██░░▒▒██ - ██░░▒▒████░░▒▒██ - ▓▓▒▒▒▒▒▒▒▒▓▓ - ████████ - - ============================================================================================================================ \ No newline at end of file diff --git a/ESP/lib/src/network/api/baseAPI/baseAPI.cpp b/ESP/lib/src/network/api/baseAPI/baseAPI.cpp index ec431ae..e3d03e1 100644 --- a/ESP/lib/src/network/api/baseAPI/baseAPI.cpp +++ b/ESP/lib/src/network/api/baseAPI/baseAPI.cpp @@ -1,14 +1,24 @@ #include "baseAPI.hpp" +//! These have to be called before the constructor of the class because they are static +//! C++ 11 does not have inline variables, sadly. So we have to do this. +//const char *BaseAPI::MIMETYPE_HTML{"text/html"}; +// const char *BaseAPI::MIMETYPE_CSS{"text/css"}; +// const char *BaseAPI::MIMETYPE_JS{"application/javascript"}; +// const char *BaseAPI::MIMETYPE_PNG{"image/png"}; +// const char *BaseAPI::MIMETYPE_JPG{"image/jpeg"}; +// const char *BaseAPI::MIMETYPE_ICO{"image/x-icon"}; +const char *BaseAPI::MIMETYPE_JSON{"application/json"}; + BaseAPI::BaseAPI(int CONTROL_PORT, - ProjectConfig *projectConfig, - CameraHandler *camera, - StateManager *WiFiStateManager, - const std::string &api_url) : API_Utilities(CONTROL_PORT, - projectConfig, - camera, - WiFiStateManager, - api_url) {} + ProjectConfig *projectConfig, + CameraHandler *camera, + StateManager *WiFiStateManager, + const std::string &api_url) : server(new AsyncWebServer(CONTROL_PORT)), + projectConfig(projectConfig), + camera(camera), + WiFiStateManager(WiFiStateManager), + api_url(api_url) {} BaseAPI::~BaseAPI() {} @@ -29,11 +39,26 @@ void BaseAPI::begin() DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", "*"); - // std::bind(&BaseAPI::API_Utilities::notFound, &api_utilities, std::placeholders::_1); + // std::bind(&BaseAPI::notFound, &std::placeholders::_1); server->onNotFound([&](AsyncWebServerRequest *request) { notFound(request); }); } +void BaseAPI::notFound(AsyncWebServerRequest *request) const +{ + if (_networkMethodsMap.find(request->method()) != _networkMethodsMap.end()) + { + log_i("%s: http://%s%s/\n", _networkMethodsMap.at(request->method()).c_str(), request->host().c_str(), request->url().c_str()); + char buffer[100]; + snprintf(buffer, sizeof(buffer), "Request %s Not found: %s", _networkMethodsMap.at(request->method()).c_str(), request->url().c_str()); + request->send(404, "text/plain", buffer); + } + else + { + request->send(404, "text/plain", "Request Not found using unknown method"); + } +} + //********************************************************************************************* //! Command Functions //********************************************************************************************* @@ -76,7 +101,7 @@ void BaseAPI::setWiFi(AsyncWebServerRequest *request) projectConfig->setWifiConfig(ssid, ssid, password, &channel, adhoc, true); - /* if (WiFitateManager->getCurrentState() == WiFiState_e::WiFiState_ADHOC) + /* if (WiFiStateManager->getCurrentState() == WiFiState_e::WiFiState_ADHOC) { projectConfig->setAPWifiConfig(ssid, password, &channel, adhoc, true); } diff --git a/ESP/lib/src/network/api/baseAPI/baseAPI.hpp b/ESP/lib/src/network/api/baseAPI/baseAPI.hpp index 21c3824..706f833 100644 --- a/ESP/lib/src/network/api/baseAPI/baseAPI.hpp +++ b/ESP/lib/src/network/api/baseAPI/baseAPI.hpp @@ -1,12 +1,37 @@ #ifndef BASEAPI_HPP #define BASEAPI_HPP -#include "network/wifihandler/wifiHandler.hpp" -#include "network/api/utilities/apiUtilities.hpp" + +#include +#include + +#define WEBSERVER_H + +/* #define XHTTP_GET 0b00000001; +#define XHTTP_POST 0b00000010; +#define XHTTP_DELETE 0b00000100; +#define XHTTP_PUT 0b00001000; +#define XHTTP_PATCH 0b00010000; +#define XHTTP_HEAD 0b00100000; +#define XHTTP_OPTIONS 0b01000000; +#define XHTTP_ANY 0b01111111; */ + +#define HTTP_ANY 0b01111111 +#define HTTP_GET 0b00000001 + +#include +#include + +//#include "network/api/utilities/apiUtilities.hpp" //! Only needed for the shaEncoder function (for now) #include "data/utilities/network_utilities.hpp" -class BaseAPI : public API_Utilities +#include "data/config/project_config.hpp" +#include "data/StateManager/StateManager.hpp" +#include "io/camera/cameraHandler.hpp" + +class BaseAPI { protected: + std::string api_url; enum JSON_TYPES { @@ -29,6 +54,14 @@ protected: {"wifiap", WIFIAP}, }; + static const char *MIMETYPE_HTML; + /* static const char *MIMETYPE_CSS; */ + /* static const char *MIMETYPE_JS; */ + /* static const char *MIMETYPE_PNG; */ + /* static const char *MIMETYPE_JPG; */ + /* static const char *MIMETYPE_ICO; */ + static const char *MIMETYPE_JSON; + protected: /* Commands */ void setWiFi(AsyncWebServerRequest *request); @@ -51,6 +84,41 @@ protected: route_t routes; route_map_t route_map; + std::unordered_map _networkMethodsMap = { + {0b00000001, "GET"}, + {0b00000010, "POST"}, + {0b00001000, "PUT"}, + {0b00000100, "DELETE"}, + {0b00010000, "PATCH"}, + {0b01000000, "OPTIONS"}, + }; + + enum RequestMethods + { + GET, + POST, + PUT, + DELETE, + PATCH, + OPTIONS, + }; + + std::unordered_map _networkMethodsMap_enum = { + {0b00000001, GET}, + {0b00000010, POST}, + {0b00001000, PUT}, + {0b00000100, DELETE}, + {0b00010000, PATCH}, + {0b01000000, OPTIONS}, + }; + + typedef std::unordered_map networkMethodsMap_t; + + ProjectConfig *projectConfig; + AsyncWebServer *server; + CameraHandler *camera; + StateManager *WiFiStateManager; + public: BaseAPI(int CONTROL_PORT, ProjectConfig *projectConfig, @@ -60,6 +128,7 @@ public: virtual ~BaseAPI(); virtual void begin(); + void notFound(AsyncWebServerRequest *request) const; }; #endif // BASEAPI_HPP \ No newline at end of file diff --git a/ESP/lib/src/network/api/utilities/apiUtilities.cpp b/ESP/lib/src/network/api/utilities/apiUtilities.cpp index 43be408..0be0dff 100644 --- a/ESP/lib/src/network/api/utilities/apiUtilities.cpp +++ b/ESP/lib/src/network/api/utilities/apiUtilities.cpp @@ -1,28 +1,10 @@ #include "apiUtilities.hpp" -//! These have to be called before the constructor of the class because they are static -//! C++ 11 does not have inline variables, sadly. So we have to do this. -const char *API_Utilities::MIMETYPE_HTML{"text/html"}; -// const char *BaseAPI::MIMETYPE_CSS{"text/css"}; -// const char *BaseAPI::MIMETYPE_JS{"application/javascript"}; -// const char *BaseAPI::MIMETYPE_PNG{"image/png"}; -// const char *BaseAPI::MIMETYPE_JPG{"image/jpeg"}; -// const char *BaseAPI::MIMETYPE_ICO{"image/x-icon"}; -const char *API_Utilities::MIMETYPE_JSON{"application/json"}; - //********************************************************************************************* //! API Utilities //********************************************************************************************* -API_Utilities::API_Utilities(int CONTROL_PORT, - ProjectConfig *projectConfig, - CameraHandler *camera, - StateManager *WiFiStateManager, - const std::string &api_url) : server(new AsyncWebServer(CONTROL_PORT)), - projectConfig(projectConfig), - WiFiStateManager(WiFiStateManager), - camera(camera), - api_url(api_url) {} +API_Utilities::API_Utilities() {}; API_Utilities::~API_Utilities() {} @@ -54,18 +36,3 @@ std::string API_Utilities::shaEncoder(std::string data) } return hash_string; } - -void API_Utilities::notFound(AsyncWebServerRequest *request) const -{ - if (_networkMethodsMap.find(request->method()) != _networkMethodsMap.end()) - { - log_i("%s: http://%s%s/\n", _networkMethodsMap.at(request->method()).c_str(), request->host().c_str(), request->url().c_str()); - char buffer[100]; - snprintf(buffer, sizeof(buffer), "Request %s Not found: %s", _networkMethodsMap.at(request->method()).c_str(), request->url().c_str()); - request->send(404, "text/plain", buffer); - } - else - { - request->send(404, "text/plain", "Request Not found using unknown method"); - } -} \ No newline at end of file diff --git a/ESP/lib/src/network/api/utilities/apiUtilities.hpp b/ESP/lib/src/network/api/utilities/apiUtilities.hpp index 13cfc29..38df323 100644 --- a/ESP/lib/src/network/api/utilities/apiUtilities.hpp +++ b/ESP/lib/src/network/api/utilities/apiUtilities.hpp @@ -1,90 +1,20 @@ #ifndef APIUTILITIES_HPP #define APIUTILITIES_HPP -#include #include - -#define WEBSERVER_H - -/* #define XHTTP_GET 0b00000001; -#define XHTTP_POST 0b00000010; -#define XHTTP_DELETE 0b00000100; -#define XHTTP_PUT 0b00001000; -#define XHTTP_PATCH 0b00010000; -#define XHTTP_HEAD 0b00100000; -#define XHTTP_OPTIONS 0b01000000; -#define XHTTP_ANY 0b01111111; */ - -#define HTTP_ANY 0b01111111 -#define HTTP_GET 0b00000001 - -#include -#include //#include #include "mbedtls/md.h" -#include "data/config/project_config.hpp" -#include "network/wifihandler/WifiHandler.hpp" -#include "data/StateManager/StateManager.hpp" -#include "io/camera/cameraHandler.hpp" +#include "data/utilities/network_utilities.hpp" class API_Utilities { public: - API_Utilities(int CONTROL_PORT, - ProjectConfig *projectConfig, - CameraHandler *camera, - StateManager *WiFiStateManager, - const std::string &api_url); + API_Utilities(); virtual ~API_Utilities(); + static std::string shaEncoder(std::string data); -protected: - void notFound(AsyncWebServerRequest *request) const; - std::string shaEncoder(std::string data); - std::unordered_map _networkMethodsMap = { - {0b00000001, "GET"}, - {0b00000010, "POST"}, - {0b00001000, "PUT"}, - {0b00000100, "DELETE"}, - {0b00010000, "PATCH"}, - {0b01000000, "OPTIONS"}, - }; - - enum RequestMethods - { - GET, - POST, - PUT, - DELETE, - PATCH, - OPTIONS, - }; - - std::unordered_map _networkMethodsMap_enum = { - {0b00000001, GET}, - {0b00000010, POST}, - {0b00001000, PUT}, - {0b00000100, DELETE}, - {0b00010000, PATCH}, - {0b01000000, OPTIONS}, - }; - -protected: - ProjectConfig *projectConfig; - AsyncWebServer *server; - CameraHandler *camera; - StateManager *WiFiStateManager; - typedef std::unordered_map networkMethodsMap_t; - -protected: - std::string api_url; - - static const char *MIMETYPE_HTML; - /* static const char *MIMETYPE_CSS; */ - /* static const char *MIMETYPE_JS; */ - /* static const char *MIMETYPE_PNG; */ - /* static const char *MIMETYPE_JPG; */ - /* static const char *MIMETYPE_ICO; */ - static const char *MIMETYPE_JSON; +public: + }; #endif // APIUTILITIES_HPP \ No newline at end of file diff --git a/ESP/lib/src/network/api/webserverHandler.cpp b/ESP/lib/src/network/api/webserverHandler.cpp index f878e7d..788445d 100644 --- a/ESP/lib/src/network/api/webserverHandler.cpp +++ b/ESP/lib/src/network/api/webserverHandler.cpp @@ -37,10 +37,10 @@ void APIServer::setupServer() routes.emplace("resetConfig", &APIServer::factoryReset); routes.emplace("rebootDevice", &APIServer::rebootDevice); routes.emplace("setJson", &APIServer::handleJson); - routes.emplace("setCamera", &APIServer::setCamera); routes.emplace("deleteRoute", &APIServer::deleteRoute); - routes.emplace("restartCamera", &APIServer::restartCamera); // Camera Routes + routes.emplace("setCamera", &APIServer::setCamera); + routes.emplace("restartCamera", &APIServer::restartCamera); //! reserve enough memory for all routes - must be called after adding routes and before adding routes to route_map indexes.reserve(routes.size()); // this is done to avoid reallocation of memory and copying of data