Skip to content

Commit

Permalink
Remove boost dependency and update REST Interface with QTNetwork
Browse files Browse the repository at this point in the history
  • Loading branch information
rpashchapur committed Mar 25, 2024
1 parent 31ceb61 commit 0924af2
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 316 deletions.
1 change: 0 additions & 1 deletion QGCExternalLibs.pri
Original file line number Diff line number Diff line change
Expand Up @@ -232,5 +232,4 @@ contains (DEFINES, DISABLE_ZEROCONF) {
# UTM Adapter Enabled
contains (DEFINES, QGC_CONFIG_UTM_ADAPTER){
INCLUDEPATH += $$PWD/libs/libevents/libevents/libs/cpp/parse/nlohmann_json/include
LIBS += -lboost_system -lboost_thread -lssl -lcrypto
}
21 changes: 3 additions & 18 deletions src/UTMSP/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
find_package(Qt6 REQUIRED COMPONENTS Core)
find_package(Qt6 REQUIRED COMPONENTS Core Network)
find_package(Threads REQUIRED)
find_package(Boost REQUIRED COMPONENTS system thread)
find_package(OpenSSL REQUIRED)

qt_add_library(UTMSP STATIC)
option(QGC_CONFIG_UTM_ADAPTER "Enable UTM Adapter" OFF) # TODO: Make this QGC_CONFIG_UTM_ADAPTER
option(QGC_CONFIG_UTM_ADAPTER "Enable UTM Adapter" OFF)

if(QGC_CONFIG_UTM_ADAPTER)
add_definitions(-DQGC_CONFIG_UTM_ADAPTER)
add_definitions(-DQT_DEBUG)

set(CMAKE_BUILD_TYPE Debug)
# set(QGC_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../)
# set(NLOHMANN_JSON_SOURCE_DIR "${QGC_ROOT_DIR}/libs/libevents/libevents/libs/cpp/parse/nlohmann_json")
# set(NLOHMANN_JSON_BINARY_DIR "${CMAKE_BINARY_DIR}/nlohmann_json")

# add_subdirectory(${NLOHMANN_JSON_SOURCE_DIR} ${NLOHMANN_JSON_BINARY_DIR})
message(STATUS "UTMSP is Initialized")

target_sources(UTMSP
Expand Down Expand Up @@ -55,21 +49,15 @@ option(QGC_CONFIG_UTM_ADAPTER "Enable UTM Adapter" OFF) # TODO: Make this QGC_CO
target_include_directories(UTMSP
PUBLIC
services
# ${QGC_ROOT_DIR}/libs/libevents/libevents/libs/cpp/parse/nlohmann_json/include
)

target_link_libraries(UTMSP

PRIVATE
libevents_parser
PUBLIC
Boost::system
Boost::thread
OpenSSL::SSL
OpenSSL::Crypto
nlohmann_json::nlohmann_json
Qt6::Core
Qt6::Location
Qt6::Network
Qt6::Widgets
Threads::Threads
qgc
Expand All @@ -88,9 +76,6 @@ else()
)

target_link_libraries(UTMSP

PRIVATE
libevents_parser
PUBLIC
Qt6::Core
Qt6::Location
Expand Down
55 changes: 15 additions & 40 deletions src/UTMSP/UTMSPAuthorization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@
*
****************************************************************************/

#include <boost/beast/version.hpp>
#include <nlohmann/json.hpp>
#include <string>
#include <QByteArray>
#include <QBitArray>
#include <QString>

#include "UTMSPLogger.h"
#include "UTMSPAuthorization.h"

using json = nlohmann::ordered_json;

UTMSPAuthorization::UTMSPAuthorization():
UTMSPRestInterface("passport.utm.dev.airoplatform.com")
{

}

UTMSPAuthorization::~UTMSPAuthorization()
UTMSPAuthorization::UTMSPAuthorization(QObject *parent):
QObject(parent)
{

}
Expand All @@ -30,43 +28,20 @@ thread_local std::string clientToken = "";

bool UTMSPAuthorization::requestOAuth2Client(const QString &clientID, const QString &clientSecret)
{
// Convert QString to std::string
_clientID = clientID.toStdString();
_clientSecret = clientSecret.toStdString();

// Generate the basic Token
std::string combinedCredential = _clientID + ":" + _clientSecret;
BIO *bio, *b64;
BUF_MEM *bufferPtr;

b64 = BIO_new(BIO_f_base64());
bio = BIO_new(BIO_s_mem());
bio = BIO_push(b64, bio);

BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
BIO_write(bio, combinedCredential.c_str(), combinedCredential.length());
BIO_flush(bio);
BIO_get_mem_ptr(bio, &bufferPtr);
BIO_set_close(bio, BIO_NOCLOSE);
BIO_free_all(bio);

std::string encodedBasicToken(bufferPtr->data, bufferPtr->length);
BUF_MEM_free(bufferPtr);
setBasicToken(encodedBasicToken);

// Get the Access Token
setHost("AuthClient");
connectNetwork();
const std::string target = "/oauth/token/";
std::string body = "grant_type=client_credentials&scope=blender.write blender.read&audience=blender.utm.dev.airoplatform.com&client_id=" + _clientID + "&client_secret=" + _clientSecret + "\r\n\r\n";
modifyRequest(target, http::verb::post, body);
auto [status, response] = executeRequest();
QString combinedCredential = clientID + ":" + clientSecret;
QString encodedBasicToken = combinedCredential.toUtf8().toBase64();
_utmspRestInterface.setBasicToken(encodedBasicToken);
_utmspRestInterface.setHost("AuthClient");
const QString target = "/oauth/token/";
QString body = "grant_type=client_credentials&scope=blender.write blender.read&audience=blender.utm.dev.airoplatform.com&client_id=" + clientID + "&client_secret=" + clientSecret + "\r\n\r\n";
_utmspRestInterface.modifyRequest(target, QNetworkAccessManager::PostOperation, body);
auto [status, response] = _utmspRestInterface.executeRequest();
UTMSP_LOG_INFO() << "UTMSPAuthorization: Authorization Response: " << response;

if(status == 200)
{
try {
json responseJson = json::parse(response);
json responseJson = json::parse(response.toStdString());
clientToken = responseJson["access_token"];
_isValidToken = true;
}
Expand Down
15 changes: 7 additions & 8 deletions src/UTMSP/UTMSPAuthorization.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,22 @@

#include "UTMSPRestInterface.h"

class UTMSPAuthorization: public QObject, public UTMSPRestInterface
class UTMSPAuthorization: public QObject
{
Q_OBJECT
public:
UTMSPAuthorization();
~UTMSPAuthorization();
UTMSPAuthorization(QObject *parent = nullptr);
virtual ~UTMSPAuthorization() = default;

const std::string& getOAuth2Token();
std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len);

protected slots:
bool requestOAuth2Client(const QString& clientID, const QString& clientSecret);

protected:
http::request<http::string_body> _request;

private:
std::string _clientID;
std::string _clientSecret;
bool _isValidToken;
UTMSPRestInterface _utmspRestInterface;

static const std::string base64_chars;
};
24 changes: 12 additions & 12 deletions src/UTMSP/UTMSPBlenderRestInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,34 @@

#include "UTMSPBlenderRestInterface.h"

UTMSPBlenderRestInterface::UTMSPBlenderRestInterface():
UTMSPRestInterface("blender.utm.dev.airoplatform.com")
UTMSPBlenderRestInterface::UTMSPBlenderRestInterface(QObject *parent):
UTMSPRestInterface(parent)
{

setHost("BlenderClient");
}

std::pair<int, std::string> UTMSPBlenderRestInterface::setFlightPlan(const std::string& body)
QPair<int, QString> UTMSPBlenderRestInterface::setFlightPlan(const QString& body)
{
// Post Flight plan
const std::string setFlightPlanTarget = "/flight_declaration_ops/set_flight_declaration";
modifyRequest(setFlightPlanTarget, http::verb::post, body);
QString setFlightPlanTarget = "/flight_declaration_ops/set_flight_declaration";
modifyRequest(setFlightPlanTarget, QNetworkAccessManager::PostOperation, body);

return executeRequest();
}

std::pair<int, std::string> UTMSPBlenderRestInterface::requestTelemetry(const std::string& body)
QPair<int, QString> UTMSPBlenderRestInterface::requestTelemetry(const QString& body)
{
// Post RID data
const std::string target = "/flight_stream/set_telemetry";
modifyRequest(target, http::verb::put, body);
QString target = "/flight_stream/set_telemetry";
modifyRequest(target, QNetworkAccessManager::PutOperation, body);

return executeRequest();
}

std::pair<int, std::string> UTMSPBlenderRestInterface::ping()
QPair<int, QString> UTMSPBlenderRestInterface::ping()
{
const std::string target = "/ping";
modifyRequest(target, http::verb::get);
QString target = "/ping";
modifyRequest(target, QNetworkAccessManager::GetOperation);

return executeRequest();
}
15 changes: 8 additions & 7 deletions src/UTMSP/UTMSPBlenderRestInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@
#pragma once

#include "UTMSPRestInterface.h"
#include <QPair>
#include <QString>

class UTMSPBlenderRestInterface: public UTMSPRestInterface
class UTMSPBlenderRestInterface : public UTMSPRestInterface
{
public:
UTMSPBlenderRestInterface();
UTMSPBlenderRestInterface(QObject *parent = nullptr);

std::pair<int, std::string> setFlightPlan(const std::string& body);
std::pair<int, std::string> requestTelemetry(const std::string& body);
std::pair<int, std::string> ping();
QPair<int, QString> setFlightPlan(const QString& body);
QPair<int, QString> requestTelemetry(const QString& body);
QPair<int, QString> ping();

private:
http::request<http::string_body> _request;
};

10 changes: 5 additions & 5 deletions src/UTMSP/UTMSPFlightPlanManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,20 @@ void UTMSPFlightPlanManager::registerFlightPlan(const std::string &token,


json data = _flightDataJson;
auto dataString = QString::fromStdString(data.dump(4));
setHost("BlenderClient");
connectNetwork();
setBearerToken(token);
auto [statusCode, response] = setFlightPlan(data.dump(4));
setBearerToken(token.c_str());
auto [statusCode, response] = setFlightPlan(dataString);
UTMSP_LOG_INFO() << "UTMSPFlightPlanManager: Register Response -->" << response;

if(statusCode == 201)
{
try {
json jsonData = json::parse(response);
json jsonData = json::parse(response.toStdString());
std::string flightID = jsonData["id"];

_flightResponseID = flightID;
_responseJSON = response;
_responseJSON = response.toStdString();
_responseStatus = true;
}
catch (const json::parse_error& e) {
Expand Down
34 changes: 12 additions & 22 deletions src/UTMSP/UTMSPNetworkRemoteIDManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ UTMSPNetworkRemoteIDManager::~UTMSPNetworkRemoteIDManager()

void UTMSPNetworkRemoteIDManager::getCapabilty(const std::string &token)
{
connectNetwork();
setBearerToken(token);
setBearerToken(token.c_str());
}

void UTMSPNetworkRemoteIDManager::startTelemetry(const double &latitude,
Expand Down Expand Up @@ -145,34 +144,25 @@ void UTMSPNetworkRemoteIDManager::startTelemetry(const double &latitude,
final_format["observations"] = final_array;
json data = final_format;

// Get the RID response
_dispatcher->add_task([data,this]() {
auto [statusCode, response] = requestTelemetry(data.dump(4));
_statusCode = statusCode;
_response = response;
});
auto [statusCode, response] = requestTelemetry( QString::fromStdString(data.dump(4)));
_statusCode = statusCode;
_response = response.toStdString();
UTMSP_LOG_DEBUG()<< "Status Code: " << _statusCode;

if(!_response.empty()){

if(_statusCode == 201)
{
try {
json responseJson = json::parse(_response);
_response.clear();
if (responseJson.contains("message")){
if(responseJson["message"] == "Telemetry data succesfully submitted"){

auto now = std::chrono::system_clock::now();
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
char buffer[20];
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", std::localtime(&now_c));
UTMSP_LOG_DEBUG() <<"The Telemetry RID data submitted at " << buffer;
UTMSP_LOG_DEBUG() << "--------------Telemetry Submitted Successfully---------------";
}
}
auto now = std::chrono::system_clock::now();
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
char buffer[20];
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", std::localtime(&now_c));
UTMSP_LOG_DEBUG() <<"The Telemetry RID data submitted at " << buffer;
UTMSP_LOG_DEBUG() << "--------------Telemetry Submitted Successfully---------------";
}
catch (const json::parse_error& e) {
UTMSP_LOG_ERROR() << "UTMSPNetworkRemoteManager: Error parsing the response: " << e.what();
UTMSP_LOG_ERROR() << "UTMSPNetworkRemoteManager: Error parsing the Telemetry response: " << e.what();
}
}
else
Expand Down
Loading

0 comments on commit 0924af2

Please sign in to comment.