Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge ddbb #9

Merged
merged 11 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docker/server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 26 additions & 3 deletions migration/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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', '[email protected]', CURRENT_TIMESTAMP),
('ESP00002-123-0033', '1', 'ZL', 'Zhi Lin Lo', 'Unknown', '[email protected]', CURRENT_TIMESTAMP),
('ESP00003-123-0033', '2', 'EC', 'Elena Clofent', 'Unknown', '[email protected]', CURRENT_TIMESTAMP);

-- ('AUS78825-778-0001', '3', 'MZ', 'Maria Zapata', 'Unknown', '[email protected]', CURRENT_TIMESTAMP),
-- ('ESP88712-998-0871', '4', 'HW', 'Haitao Wu', 'Unknown', '[email protected]', 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
15 changes: 9 additions & 6 deletions src/c/base_DB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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;
Expand All @@ -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");

}
116 changes: 80 additions & 36 deletions src/c/main_RouteServer.cpp
Original file line number Diff line number Diff line change
@@ -1,65 +1,109 @@
#include <crow.h>
#include <iostream>
#include <string>
#include <json/json.h>


// 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/<string>").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/<string>").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;
}
8 changes: 4 additions & 4 deletions src/h/base_DB.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading