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
This commit is contained in:
ZanzyTHEbar 2022-09-08 16:01:52 +01:00
parent 9a8ff6659a
commit 35f61436ab
6 changed files with 115 additions and 184 deletions

View File

@ -1,60 +0,0 @@
: === WELCOME === TO === :
===========================================================================================================================
██████╗ ██████╗ ███████╗███╗ ██╗██╗██████╗ ██╗███████╗
██╔═══██╗██╔══██╗██╔════╝████╗ ██║██║██╔══██╗██║██╔════╝
██║ ██║██████╔╝█████╗ ██╔██╗ ██║██║██████╔╝██║███████╗
██║ ██║██╔═══╝ ██╔══╝ ██║╚██╗██║██║██╔══██╗██║╚════██║
╚██████╔╝██║ ███████╗██║ ╚████║██║██║ ██║██║███████║
╚═════╝ ╚═╝ ╚══════╝╚═╝ ╚═══╝╚═╝╚═╝ ╚═╝╚═╝╚══════╝
██████████████
██▓▓▓▓▓▓▒▒▒▒▒▒▒▒░░▒▒▓▓▓▓██
████▓▓░░░░▒▒░░░░░░▒▒░░░░░░▒▒░░████
██▓▓▒▒▓▓▓▓▒▒▒▒░░░░░░▒▒░░▒▒░░░░░░▒▒░░▒▒▓▓▓▓
██▓▓▒▒▒▒▒▒▒▒░░▒▒░░░░░░░░░░░░▒▒░░░░▒▒░░░░▒▒░░██
██▓▓▓▓░░░░▒▒░░░░▒▒▒▒░░░░░░░░░░▒▒░░ ░░░░░░░░▒▒░░██
██▓▓▓▓▓▓▓▓▓▓░░░░░░▒▒░░░░░░░░░░░░░░ ░░░░░░░░░░░░██
██▓▓▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░▒▒░░░░░░░░░░░░ ░░ ▒▒▒▒██
██▓▓▒▒▒▒▒▒▒▒░░░░▒▒░░░░░░░░░░░░░░░░░░ ░░░░██
▓▓▓▓▒▒▒▒▒▒▒▒▒▒░░░░░░▒▒░░░░░░░░░░░░░░░░░░▓▓▓▓▓▓▓▓▓▓ ░░ ▒▒▓▓
██▒▒▒▒▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░░░▓▓▓▓▒▒▒▒▒▒▒▒▓▓▓▓▓▓ ░░██
▓▓▒▒▒▒▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░ ▓▓▒▒▒▒▒▒▒▒░░░░▒▒▒▒▓▓▒▒ ░░▒▒▓▓
██▓▓▒▒░░░░░░▒▒▒▒▒▒░░░░░░░░░░░░░░░░▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░▒▒▓▓▓▓ ░░██
██▓▓▓▓▒▒▒▒▒▒▒▒░░░░▒▒░░░░░░░░░░░░ ▒▒▒▒▒▒▒▒▒▒▒▒████▓▓░░░░▒▒▓▓ ░░██
██▓▓▒▒▒▒▒▒▓▓▒▒▓▓░░░░░░░░░░░░░░░░░░▓▓▒▒▒▒░░▒▒▒▒████ ▒▒██░░▒▒▓▓▓▓ ░░██
██▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░▓▓░░▒▒▒▒▒▒██████▒▒ ▓▓▓▓░░▓▓▓▓ ░░██
██▓▓▒▒▒▒░░▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░▓▓▒▒░░▒▒░░████████▓▓ ██▓▓▒▒▓▓ ░░██
██▓▓▓▓▓▓▓▓▒▒▒▒▒▒░░░░░░▒▒░░░░░░░░░░▒▒░░▒▒░░▒▒██████████▒▒██▒▒▒▒▓▓ ░░██
██▓▓▒▒▒▒▒▒░░░░░░░░▒▒▒▒░░░░░░░░░░░░▒▒▒▒░░░░▒▒██▒▒██████ ██▒▒▒▒▓▓ ░░██
██▒▒▒▒▒▒░░▓▓▓▓▓▓▒▒░░░░░░░░░░░░░░░░▒▒░░ ▒▒░░██ ▒▒████▒▒██▒▒▒▒▓▓ ░░██
██▓▓▓▓▓▓▓▓░░▒▒▒▒░░▒▒░░░░░░░░░░░░░░▒▒▒▒░░░░░░▒▒██ ██████▒▒▒▒▒▒▓▓░░░░██
██▓▓██▓▓▒▒▒▒▓▓░░░░▒▒░░░░░░░░░░░░░░░░░░▒▒▒▒░░▒▒░░▒▒██████▒▒▒▒▒▒▓▓ ░░██
██▓▓██▒▒▒▒▒▒▒▒▓▓▓▓▒▒░░░░░░░░░░░░░░░░░░▒▒░░▒▒░░▒▒░░▒▒░░▒▒▒▒▒▒▒▒▓▓░░░░██
██▒▒▓▓██▓▓▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░▒▒░░▒▒░░░░░░▒▒▒▒▒▒▒▒▓▓ ░░░░██
██▒▒▒▒▓▓██▓▓▓▓▒▒▒▒▒▒▒▒░░░░░░░░▒▒░░░░░░░░░░░░▒▒▒▒▒▒░░▒▒▒▒▒▒▓▓▓▓ ░░░░██
▓▓▓▓▒▒▒▒▓▓██▓▓▒▒▒▒▒▒░░░░▒▒▒▒░░▒▒░░░░░░░░░░░░░░░░░░▒▒▒▒▓▓▓▓▓▓░░░░░░░░▒▒██
██▒▒▒▒▓▓▒▒▓▓██▒▒▒▒▒▒▒▒▓▓▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ░░ ░░░░██
██▒▒▒▒▓▓░░▒▒▒▒██▓▓▓▓▓▓▒▒▒▒▒▒▒▒▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒░░▒▒██
██▒▒▒▒▓▓▒▒░░▓▓▒▒██▓▓▒▒▒▒▒▒▒▒░░▓▓▒▒▓▓▒▒░░░░▒▒░░░░░░░░░░░░░░░░░░░░░░▓▓██
██▒▒▒▒▒▒░░▒▒▒▒▒▒▓▓██▓▓▒▒▒▒▒▒▓▓▒▒▒▒▒▒▒▒▒▒▓▓░░░░░░░░░░▒▒░░░░▒▒░░▒▒▒▒██
██▒▒▒▒░░▒▒░░▒▒████ ██▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░▓▓▒▒▒▒▒▒▓▓▒▒██
██▒▒▓▓░░▒▒░░▓▓ ████▓▓▒▒▓▓▒▒▒▒▒▒░░▒▒▒▒▒▒▒▒▓▓▒▒▒▒▒▒▒▒▒▒██▓▓
██▒▒▒▒▒▒▒▒ ██ ████▓▓▒▒▓▓▓▓▓▓▒▒▒▒▒▒▒▒▓▓▒▒▒▒▒▒████
██▒▒▒▒▒▒▒▒░░██ ██████▓▓▒▒▒▒▒▒▒▒▓▓▓▓██████
██▒▒▒▒▒▒▒▒░░ ████ ██████████████
██▒▒▒▒▒▒▒▒░░ ░░████
████▒▒▒▒▒▒░░░░ ░░████
████▒▒▒▒▒▒░░░░ ░░██
████▒▒▒▒▒▒░░ ░░██
██▓▓▒▒▒▒░░ ▒▒▓▓
████▒▒░░ ▒▒██
▓▓▒▒░░░░██
██░░ ██
▓▓██ ██░░░░██
██░░██ ██░░░░██
██░░██ ██░░▒▒██
██░░▒▒████░░▒▒██
▓▓▒▒▒▒▒▒▒▒▓▓
████████
============================================================================================================================

View File

@ -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<WiFiState_e> *WiFiStateManager,
const std::string &api_url) : API_Utilities(CONTROL_PORT,
projectConfig,
camera,
WiFiStateManager,
api_url) {}
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);
}

View File

@ -1,12 +1,37 @@
#ifndef BASEAPI_HPP
#define BASEAPI_HPP
#include "network/wifihandler/wifiHandler.hpp"
#include "network/api/utilities/apiUtilities.hpp"
#include <unordered_map>
#include <string>
#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 <ESPAsyncWebServer.h>
#include <AsyncTCP.h>
//#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<int, std::string> _networkMethodsMap = {
{0b00000001, "GET"},
{0b00000010, "POST"},
{0b00001000, "PUT"},
{0b00000100, "DELETE"},
{0b00010000, "PATCH"},
{0b01000000, "OPTIONS"},
};
enum RequestMethods
{
GET,
POST,
PUT,
DELETE,
PATCH,
OPTIONS,
};
std::unordered_map<int, RequestMethods> _networkMethodsMap_enum = {
{0b00000001, GET},
{0b00000010, POST},
{0b00001000, PUT},
{0b00000100, DELETE},
{0b00010000, PATCH},
{0b01000000, OPTIONS},
};
typedef std::unordered_map<std::string, WebRequestMethodComposite> networkMethodsMap_t;
ProjectConfig *projectConfig;
AsyncWebServer *server;
CameraHandler *camera;
StateManager<WiFiState_e> *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

View File

@ -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<WiFiState_e> *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");
}
}

View File

@ -1,90 +1,20 @@
#ifndef APIUTILITIES_HPP
#define APIUTILITIES_HPP
#include <unordered_map>
#include <string>
#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 <ESPAsyncWebServer.h>
#include <AsyncTCP.h>
//#include <SPIFFS.h>
#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<WiFiState_e> *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<int, std::string> _networkMethodsMap = {
{0b00000001, "GET"},
{0b00000010, "POST"},
{0b00001000, "PUT"},
{0b00000100, "DELETE"},
{0b00010000, "PATCH"},
{0b01000000, "OPTIONS"},
};
public:
enum RequestMethods
{
GET,
POST,
PUT,
DELETE,
PATCH,
OPTIONS,
};
std::unordered_map<int, RequestMethods> _networkMethodsMap_enum = {
{0b00000001, GET},
{0b00000010, POST},
{0b00001000, PUT},
{0b00000100, DELETE},
{0b00010000, PATCH},
{0b01000000, OPTIONS},
};
protected:
ProjectConfig *projectConfig;
AsyncWebServer *server;
CameraHandler *camera;
StateManager<WiFiState_e> *WiFiStateManager;
typedef std::unordered_map<std::string, WebRequestMethodComposite> 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;
};
#endif // APIUTILITIES_HPP

View File

@ -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