diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index a060dea..ef800bc 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -41,12 +41,15 @@ jobs: exit 1 fi + docker ps + docker container ls - - name: Test docker container - run: | - cd ../test - python3 -m pip install -r requirements.txt + # - name: Test docker container + # run: | + # cd ./test + + # python3 -m pip install -r requeriments.txt - python3 pytester.py 2 - python3 pytester.py 1 + # python3 pytester.py 2 + # python3 pytester.py 1 diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 0e244c4..b268d41 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -21,7 +21,7 @@ services: oroneta.core-system.server: container_name: oroneta.core-system.server - # restart: unless-stopped + restart: unless-stopped build: context: .. dockerfile: ./docker/server/Dockerfile diff --git a/docker/server/Dockerfile b/docker/server/Dockerfile index f6169b2..f7e0745 100644 --- a/docker/server/Dockerfile +++ b/docker/server/Dockerfile @@ -49,6 +49,8 @@ WORKDIR /opt/oronetaBuilder/crow/build RUN cmake .. -DCROW_BUILD_EXAMPLES=OFF -DCROW_BUILD_TESTS=OFF RUN make install +# # Descargar y copiar nlohmann/json +# RUN wget https://github.com/nlohmann/json/releases/download/v3.10.2/json.hpp -O /usr/local/include/json.hpp # MAKE PROJECT WORKDIR /opt/oronetaBuilder/src diff --git a/migration/init.sql b/migration/init.sql index 7c0c56b..b3d40a8 100644 --- a/migration/init.sql +++ b/migration/init.sql @@ -39,8 +39,31 @@ FOREIGN KEY (dic) REFERENCES drones(dic); --Create some fictitious datas for testing --- INSERT to registrants +-- Insert registrants +INSERT INTO registrants (id, name, country) VALUES +('ZLL', 'Zheng Lin Lei', 'Unknown'), +('EC', 'Elena Clofent', 'Unknown'), +('ZL', 'Zhi Lin Lo', 'Unknown' ), +('MZ', 'Maria Zapata', 'Unknown'), +('HW', 'Haitao Wu', 'Unknown'); + +-- Insert drones with references to the registrants +INSERT INTO drones (dic, auth_code, registrants, owner_name, owner_identify, owner_email, registered_at) VALUES +('ESP00001-123-0033', '0', 'ZLL', 'Zheng Lin Lei', 'Unknown', 'llzheng@oroneta.com', CURRENT_TIMESTAMP), +('ESP00002-123-0033', '1', 'ZL', 'Zhi Lin Lo', 'Unknown', 'zli@oroneta.com', CURRENT_TIMESTAMP), +('ESP00003-123-0033', '2', 'EC', 'Elena Clofent', 'Unknown', 'eclemuo@oroneta.com', CURRENT_TIMESTAMP); + +-- ('AUS78825-778-0001', '3', 'MZ', 'Maria Zapata', 'Unknown', 'mzapale@oroneta.com', CURRENT_TIMESTAMP), +-- ('ESP88712-998-0871', '4', 'HW', 'Haitao Wu', 'Unknown', 'hwu@oroneta.com', CURRENT_TIMESTAMP); + + +-- INSERT to routes for testing + +-- INSERT INTO routes (dic, auth_code, flight) VALUES +-- ('ESP00001-123-0033', '0', '[[21,32], [4,-117], [40,-74], [36,-122], [30,-97]]'), +-- ('ESP00002-123-0033', '1', '[[52,-0.13], [40,-3.68], [41,29], [38,-9], [55,-3]]'), +-- ('ESP00003-123-0033', '2', '[[48,2], [50,14], [45,9], [52,13], [60,24]]'); +-- ('AUS78825-778-0001', '3', '[[33.86,151.21], [-37.81,144.96], [-31.95,115.86], [-27.47,153.03], [-42.88,147.33]]'), +-- ('ESP88712-998-0871', '4', '[[35.68,139.69], [22.32,114.17], [1.35,103.82], [13.75,100.51], [37.57,126.98]]'); --- INSERT to drones --- INSERT to routes diff --git a/src/c/base_DB.cpp b/src/c/base_DB.cpp index 0c1e1d0..e792af0 100644 --- a/src/c/base_DB.cpp +++ b/src/c/base_DB.cpp @@ -30,7 +30,7 @@ PGconn* DB_connection(const char* user, const char* password) { } //check if drone dic exist in postgres -bool checkDic(PGconn* conn, char* dic) { +bool checkDic(PGconn* conn, const char* dic) { if (PQstatus(conn) == CONNECTION_OK) { char query[256]; PGresult* result; @@ -55,7 +55,7 @@ bool checkDic(PGconn* conn, char* dic) { } // insert route -void insertRoute(PGconn* conn, char* dic, char* auth_code, char* route) { +void insertRoute(PGconn* conn, const char* dic, const char* auth_code, const char* route) { if (PQstatus(conn) == CONNECTION_OK) { char query[256]; snprintf(query, sizeof(query), "INSERT INTO routes (dic, auth_code, flight) VALUES ('%s', '%s', '%s')", dic, auth_code, route); @@ -67,12 +67,12 @@ void insertRoute(PGconn* conn, char* dic, char* auth_code, char* route) { } // check if dic and auth-code match -bool checkAuthCode(PGconn* conn, char* dic, char* authCode) { +bool checkAuthCode(PGconn* conn, const char* dic, const char* authCode) { if (PQstatus(conn) == CONNECTION_OK) { char query[256]; PGresult* result; - snprintf(query, sizeof(query), "SELECT EXISTS (SELECT 1 FROM drones WHERE dic = '%s' AND authCode = '%s)", dic, authCode); + snprintf(query, sizeof(query), "SELECT EXISTS (SELECT 1 FROM drones WHERE dic = '%s' AND auth_Code = '%s')", dic, authCode); result = PQexec(conn, query); @@ -89,11 +89,11 @@ bool checkAuthCode(PGconn* conn, char* dic, char* authCode) { printf("No coincide\n"); } - return true; + return false; } // check colissions route -int checkColissions(PGconn* conn, char* dic, char* route) { +int checkColissions(PGconn* conn, const char* dic, const char* route) { if (PQstatus(conn) == CONNECTION_OK) { char query[256]; PGresult* result; @@ -116,4 +116,7 @@ int checkColissions(PGconn* conn, char* dic, char* route) { int count = atoi(PQgetvalue(result, 0, 0)); return count; } + return -1; + printf("Error: No se pudo establecer la conexión.\n"); + } \ No newline at end of file diff --git a/src/c/main_RouteServer.cpp b/src/c/main_RouteServer.cpp index f3381c9..44fec7b 100644 --- a/src/c/main_RouteServer.cpp +++ b/src/c/main_RouteServer.cpp @@ -1,65 +1,109 @@ #include #include #include +#include + // Dependencies -// #include "base_ServerComm.h" #include "base_DB.h" //variables -#define MAX_COLLISION 2 +#define MAX_COLLISION 2 using namespace std; int main(){ - //Create Crow Server - // crow::SimpleApp DroneSystem; - // //Handle the POST request to verify the route - // CROW_ROUTE(DroneSystem, "/route/").methods(crow::HTTPMethod::POST)([](const crow::request &req, std::string dic) { + //conection to the ddbb + PGconn *conn = DB_connection("Oroneta_Admin", "Oroneta_Password"); + + crow::SimpleApp DroneSystem; + + //Handle the POST request to verify the route + CROW_ROUTE(DroneSystem, "/route/").methods(crow::HTTPMethod::POST)([&conn](const crow::request &req, std::string dic) { + + //Get auth-code from the header + //get_header_value is predefined in crow (http_response.h) + string authCode = req.get_header_value("Authorization"); + + //If auth-code is missing, return unauthorized + if(authCode.empty()){ + return crow::response(401,"Unauthorized"); + } + + //If the drone is not registered in the database, return 404 (drone does not exist) + + if(!checkDic(conn, dic.c_str())){ + return crow::response(404,"Drone does not exist"); + } + + //Verify if the auth-code matches with the drone's + + if (!checkAuthCode(conn, dic.c_str(), authCode.c_str())) { + return crow::response(401, "Auth-code incorrect"); + } + + // If the drone exists, obtain the request body req + string requestBody = req.body; + - // //Get auth-code from the header - // //get_header_value is predefined in crow (http_response.h) - // string authCode = req.get_header_value("Authorization"); + //Parse the Json from the request body + + crow::json::rvalue CoordData; + try{ + CoordData = crow::json::load(requestBody); + }catch(const std::exception& e){ + return crow::response(400, "Error parsing JSON"); + } - // //If auth-code is missing, return unauthorized - // if(authCode.empty()){ - // return crow::response(401,"No autorizado"); - // } + //check if the coord filed exists + if(!CoordData.has("coord")){ + return crow::response(400, "Empty coord field"); + } - // //If the drone is not registered in the database, return 404 (drone does not exist) - // //Missing method to check if the record exists in PostgreSQL - // if(!checkDic(dic)){ - // return crow::response(404,"No existe el dron"); - // } + int collisionsCount = 0; + + auto coordsArray = CoordData["coord"]; - // //Verify if the auth-code matches with the drone's + //manual conversion json to string + ostringstream os; + os << "["; + for (size_t i = 0; i < coordsArray.size(); ++i) { + os << "[" << coordsArray[i][0].d() << "," << coordsArray[i][1].d() << "]"; + if (i < coordsArray.size() - 1) { + os << ", "; + } + } + os << "]"; - // if (!checkAuthCode(dic, authCode)) { - // return crow::response(401, "Auth-code no coincide con el del dron"); - // } + string coordsString = os.str(); - // // If the drone exists, obtain the request body req - // string requestBody = req.body; - // //Check the level of danger + //check collisions + collisionsCount = checkColissions(conn, dic.c_str(), coordsString.c_str()); - // //g + if(collisionsCount > MAX_COLLISION) { + crow::json::wvalue responseJson; + responseJson["status"] = 0; + responseJson["message"] = "Route is very dangerous to be used"; + return crow::response(200, responseJson); + } + - // //FOR NOW, always return an HTTP 200 to test the server - // crow::json::wvalue responseJson; - // responseJson["status"] = 1; - // responseJson["dangerous_level"] = 1; - // responseJson["message"] = "Route registered"; - // return crow::response(200, responseJson); + //Insert drone data and coordinates + insertRoute(conn, dic.c_str(),authCode.c_str(), coordsString.c_str()); + crow::json::wvalue responseJson; + responseJson["status"] = 1; + //responseJson["dangerous_level"] = 1; + responseJson["message"] = "Route registered"; + return crow::response(200, responseJson); - // }); + }); - // //Start the server on port 60000 - // DroneSystem.port(6000).multithreaded().run(); + //Start the server on port 60000 + DroneSystem.port(60000).multithreaded().run(); - DB_connection("Oroneta_Admin", "Oroneta_Password"); return 0; } \ No newline at end of file diff --git a/src/h/base_DB.h b/src/h/base_DB.h index 2306879..83cd077 100644 --- a/src/h/base_DB.h +++ b/src/h/base_DB.h @@ -9,12 +9,12 @@ PGconn* DB_connection(const char* user, const char* password); -bool checkDic(PGconn* conn, char* dic); +bool checkDic(PGconn* conn, const char* dic); -void insertRoute(PGconn* conn, char* dic, char* auth_code, char* route); +void insertRoute(PGconn* conn, const char* dic, const char* auth_code, const char* route); -bool checkAuthCode(PGconn* conn, char* dic, char* authCode); +bool checkAuthCode(PGconn* conn, const char* dic, const char* authCode); -int checkColissions(PGconn* conn, char* dic, char* route); +int checkColissions(PGconn* conn, const char* dic, const char* route); #endif \ No newline at end of file