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

Remove ba logger #12

Merged
merged 4 commits into from
Sep 2, 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
4 changes: 1 addition & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ENDIF ()
SET(CMAKE_BUILD_RPATH_USE_ORIGIN ON)
SET(CMAKE_INSTALL_RPATH "$ORIGIN")
SET(CMAKE_CXX_STANDARD 20)
SET(FLEET_HTTP_CLIENT_VERSION 1.3.0)
SET(FLEET_HTTP_CLIENT_VERSION 1.5.0)

INCLUDE(CheckPIESupported)
CHECK_PIE_SUPPORTED()
Expand Down Expand Up @@ -37,7 +37,6 @@ IF(NOT BRINGAUTO_SYSTEM_DEP)
INCLUDE(cmake/Dependencies.cmake)
ENDIF()

FIND_PACKAGE(libbringauto_logger 1.2.0 REQUIRED)
FIND_PACKAGE(fleet-protocol-interface 2.0.0 REQUIRED)
FIND_PACKAGE(fleet-protocol-cxx-helpers-static 1.1.1 REQUIRED)

Expand All @@ -64,7 +63,6 @@ TARGET_LINK_LIBRARIES(fleet-http-client-shared
fleet-protocol-interface::common-headers-interface
fleet-protocol-cxx-helpers-static::fleet-protocol-cxx-helpers-static
bringauto-fleet-http-client-generated-shared
bringauto_logger::bringauto_logger
)

IF(BRINGAUTO_TESTS)
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ This has to be done because cpprest is obsolete.
### Requirements

- cpprestsdk
- bringauto-logger
- Boost

All requirements can be added by bringauto Packager
Expand Down
1 change: 0 additions & 1 deletion cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
SET(CMAKE_FIND_USE_CMAKE_SYSTEM_PATH OFF CACHE BOOL "Prevent using system paths for finding libraries")

BA_PACKAGE_LIBRARY(zlib v1.2.11)
BA_PACKAGE_LIBRARY(ba-logger v1.2.0)
BA_PACKAGE_LIBRARY(boost v1.86.0)
BA_PACKAGE_LIBRARY(fleet-protocol-interface v2.0.0 PLATFORM_STRING_MODE any_machine NO_DEBUG ON)
BA_PACKAGE_LIBRARY(fleet-protocol-cxx-helpers-static v1.1.1)
Expand Down
20 changes: 6 additions & 14 deletions example/main.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
#include <bringauto/fleet_protocol/http_client/FleetApiClient.hpp>
#include <bringauto/logging/ConsoleSink.hpp>
#include <bringauto/logging/Logger.hpp>
#include <bringauto/fleet_protocol/cxx/DeviceID.hpp>


using namespace bringauto::fleet_protocol::http_client;
using namespace bringauto::logging;

std::unique_ptr<FleetApiClient> fleetApiClient;

void createFleetApiClient() {
FleetApiClient::FleetApiClientConfig facConfig {
.apiUrl = "http://localhost:8080",
.apiKey = "StaticAccessKeyToBeUsedByDevelopersOnEtna",
.apiKey = "ProtocolStaticAccessKey",
.companyName = "bringauto",
.carName = "virtual_vehicle"
};
Expand All @@ -33,7 +30,7 @@ void getCars() {
auto cars = fleetApiClient->getCars();

std::cout << "Cars:" << std::endl;
for(auto &car: cars) {
for(const auto &car: cars.first) {
std::cout << " " << car->getCompanyName() << " / " << car->getCarName() << std::endl;
}
} catch(const std::exception &e) {
Expand All @@ -45,7 +42,7 @@ void getCommands() {
auto commands = fleetApiClient->getCommands();

std::cout << "Commands:" << std::endl;
for(auto command: commands) {
for(const auto& command: commands.first) {
std::cout << " Timestamp: " << command->getTimestamp() << std::endl;
std::cout << " Payload: " << command->getPayload()->getData()->getJson().serialize() << std::endl;
}
Expand All @@ -55,7 +52,7 @@ void getStatuses() {
auto statuses = fleetApiClient->getStatuses();

std::cout << "Statuses:" << std::endl;
for(auto status: statuses) {
for(const auto& status: statuses.first) {
std::cout << " Timestamp: " << status->getTimestamp() << std::endl;
std::cout << " Payload: " << status->getPayload()->getData()->getJson().serialize() << std::endl;
}
Expand All @@ -82,7 +79,7 @@ void getAvailableDevices() {
auto availableDevices = fleetApiClient->getAvailableDevices(1);

std::cout << "All module's devices:" << std::endl;
for(auto device: availableDevices->getDeviceList()) {
for(const auto& device: availableDevices->getDeviceList()) {
std::cout << " " << device->getName() << std::endl;
}
} catch(const std::exception &e) {
Expand All @@ -91,16 +88,11 @@ void getAvailableDevices() {
}

int main(int argc, char **argv) {
Logger::addSink<ConsoleSink>();
Logger::LoggerSettings params {"FleetHttpClientExample",
Logger::Verbosity::Debug};
Logger::init(params);

createFleetApiClient();
getCars();
getCommands();
getStatuses();
sendCommand();
sendStatus();
getAvailableDevices();
}
}
40 changes: 27 additions & 13 deletions include/bringauto/fleet_protocol/http_client/FleetApiClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ class FleetApiClient {
STATUS_ERROR
};

/**
* @brief Enum class for return codes
*/
enum class ReturnCode {
// request was successful without any delays
OK,
// request was delayed due to request rate threshold being reached
DELAYED
};

/**
* @brief Configuration struct for the FleetApiClient
*/
Expand Down Expand Up @@ -49,56 +59,60 @@ class FleetApiClient {
* @brief Sets the DeviceId parameters, required for sendCommand and sendStatuse functions
* @param deviceId DeviceId struct containing the device identification parameters
*/
void setDeviceIdentification(const fleet_protocol::cxx::DeviceID &deviceId);
void setDeviceIdentification(const cxx::DeviceID &deviceId) const;

/**
* @brief Calls the GET function on /cars of Fleet v2 HTTP API
* @param since optional, minimal timestamp of cars to look for
* @param wait optional, if true, waits for a predefined period until any car is found, will check request rate and possibly delay them
* @return Vector of shared pointers to the Car model
* @return Vector of shared pointers to the Car model and a return code
*/
std::vector<std::shared_ptr<org::openapitools::client::model::Car>> getCars(std::optional<int64_t> since = std::nullopt,
std::optional<bool> wait = std::nullopt);
[[nodiscard]] std::pair<std::vector<std::shared_ptr<org::openapitools::client::model::Car>>, ReturnCode> getCars(
std::optional<int64_t> since = std::nullopt,
std::optional<bool> wait = std::nullopt) const;

/**
* @brief Calls the GET function on /command/{company_name}/{car_name} of Fleet v2 HTTP API
* @param since optional, minimal timestamp of commands to look for
* @param wait optional, if true, waits for a predefined period until any command is found, will check request rate and possibly delay them
* @return Vector of shared pointers to the Message model containing commands in payload data
* @return Vector of shared pointers to the Message model containing commands in payload data and a return code
*/
std::vector<std::shared_ptr<org::openapitools::client::model::Message>> getCommands(std::optional<int64_t> since = std::nullopt,
std::optional<bool> wait = std::nullopt);
[[nodiscard]] std::pair<std::vector<std::shared_ptr<org::openapitools::client::model::Message>>, ReturnCode> getCommands(
std::optional<int64_t> since = std::nullopt,
std::optional<bool> wait = std::nullopt) const;

/**
* @brief Calls the GET function on /status/{company_name}/{car_name} of Fleet v2 HTTP API
* @param since optional, minimal timestamp of statuses to look for
* @param wait optional, if true, waits for a predefined period until any status is found, will check request rate and possibly delay them
* @return Vector of shared pointers to the Message model containing statuses in payload data
* @return Vector of shared pointers to the Message model containing statuses in payload data and a return code
*/
std::vector<std::shared_ptr<org::openapitools::client::model::Message>> getStatuses(std::optional<int64_t> since = std::nullopt,
std::optional<bool> wait = std::nullopt);
[[nodiscard]] std::pair<std::vector<std::shared_ptr<org::openapitools::client::model::Message>>, ReturnCode> getStatuses(
std::optional<int64_t> since = std::nullopt,
const std::optional<bool>& wait = std::nullopt) const;

/**
* @brief Calls the POST function on /command/{company_name}/{car_name} of Fleet v2 HTTP API.
* setDeviceIdentification needs to be used beforehand to set DeviceId, otherwise placeholder values will be used.
* @param commandJson payload data of a command represented by a json as a string
*/
void sendCommand(const std::string &commandJson);
void sendCommand(const std::string &commandJson) const;

/**
* @brief Calls the POST function on /status/{company_name}/{car_name} of Fleet v2 HTTP API.
* setDeviceIdentification needs to be used beforehand to set DeviceId, otherwise placeholder values will be used.
* @param statusJson payload data of a status represented by a json as a string
* @param statusType optional, type of the status, default is STATUS
*/
void sendStatus(const std::string &statusJson, StatusType statusType = StatusType::STATUS);
void sendStatus(const std::string &statusJson, StatusType statusType = StatusType::STATUS) const;

/**
* @brief Calls the GET function on /available-devices/{company_name}/{car_name} of Fleet v2 HTTP API
* @param moduleId optional, filters returned devices to only those with matching module Id
* @return Shared pointer to the AvailableDevices model
*/
std::shared_ptr<org::openapitools::client::model::AvailableDevices> getAvailableDevices(std::optional<int32_t> moduleId = std::nullopt);
[[nodiscard]] std::shared_ptr<org::openapitools::client::model::AvailableDevices> getAvailableDevices(
std::optional<int32_t> moduleId = std::nullopt) const;

private:
std::unique_ptr<RequestFrequencyGuard> requestFrequencyGuard_ {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ class RequestFrequencyGuard {
/**
* @brief Determines how long the next request should be delayed when the max request rate is reached
* Saves the current timestamp and calculates the delay based on timestamps of previous requests
* @return true if threshold was reached and long delay was applied
*/
void handleDelays();
bool handleDelays();

private:
uint32_t maxRequestsThresholdCount_ {};
Expand All @@ -54,4 +55,4 @@ class RequestFrequencyGuard {
bool isOverThreshold();
};

}// namespace bringauto::fleet_protocol::http_client
}// namespace bringauto::fleet_protocol::http_client
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ class Constants {
inline static const std::string STATUS_ERROR_MESSAGE_TYPE {"STATUS_ERROR"};
};

}// namespace bringauto::fleet_protocol::http_client::settings
}// namespace bringauto::fleet_protocol::http_client::settings
64 changes: 36 additions & 28 deletions src/bringauto/fleet_protocol/http_client/FleetApiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ FleetApiClient::FleetApiClient(
companyName_(facConfig.companyName),
carName_(facConfig.carName)
{
auto apiConfigPtr = std::make_shared<api::ApiConfiguration>();
const auto apiConfigPtr = std::make_shared<api::ApiConfiguration>();
apiConfigPtr->setBaseUrl(facConfig.apiUrl);
apiConfigPtr->setApiKey(settings::Constants::API_KEY_HEADER_KEY, facConfig.apiKey);
apiClientPtr_ = std::make_shared<api::ApiClient>();
Expand All @@ -28,7 +28,7 @@ FleetApiClient::FleetApiClient(
payloadPtr_ = std::make_shared<model::Payload>();
payloadDataPtr_ = std::make_shared<model::Payload_data>();

setDeviceIdentification(fleet_protocol::cxx::DeviceID(0, 0, 0,
setDeviceIdentification(cxx::DeviceID(0, 0, 0,
settings::Constants::DEFAULT_DEVICE_ROLE, settings::Constants::DEFAULT_DEVICE_NAME
));

Expand All @@ -42,77 +42,85 @@ FleetApiClient::FleetApiClient(
}


void FleetApiClient::setDeviceIdentification(const fleet_protocol::cxx::DeviceID &deviceId) {
void FleetApiClient::setDeviceIdentification(const cxx::DeviceID &deviceId) const {
auto deviceIdentification = deviceId.getDeviceId();
deviceIdPtr_->setModuleId(deviceIdentification.module);
deviceIdPtr_->setType(deviceIdentification.device_type);
deviceIdPtr_->setRole(std::string(static_cast<char*>(deviceIdentification.device_role.data)));
deviceIdPtr_->setName(std::string(static_cast<char*>(deviceIdentification.device_name.data)));
}


std::vector<std::shared_ptr<model::Car>> FleetApiClient::getCars(std::optional<int64_t> since, std::optional<bool> wait) {
auto carsRequest = carApi_->availableCars(wait.value_or(false), since.value_or(0));
std::pair<std::vector<std::shared_ptr<model::Car>>, FleetApiClient::ReturnCode> FleetApiClient::getCars(
const std::optional<int64_t> since, const std::optional<bool> wait) const
{
const auto carsRequest = carApi_->availableCars(wait.value_or(false), since.value_or(0));
std::vector<std::shared_ptr<model::Car>> cars {};
auto rc = ReturnCode::OK;

try {
cars = carsRequest.get();
} catch(std::exception &e) {
} catch(std::exception &) {
}

if(wait) {
requestFrequencyGuard_->handleDelays();
if(wait && requestFrequencyGuard_->handleDelays()) {
rc = ReturnCode::DELAYED;
}
return cars;
return {cars, rc};
}


std::vector<std::shared_ptr<model::Message>> FleetApiClient::getCommands(std::optional<int64_t> since, std::optional<bool> wait) {
auto commandsRequest = deviceApi_->listCommands(companyName_, carName_, since.value_or(0), wait.value_or(false));
std::pair<std::vector<std::shared_ptr<model::Message>>, FleetApiClient::ReturnCode> FleetApiClient::getCommands(
const std::optional<int64_t> since, const std::optional<bool> wait) const
{
const auto commandsRequest = deviceApi_->listCommands(companyName_, carName_, since.value_or(0), wait.value_or(false));
std::vector<std::shared_ptr<model::Message>> commands {};
auto rc = ReturnCode::OK;

try {
commands = commandsRequest.get();
} catch(std::exception &e) {
} catch(std::exception &) {
}

if(wait) {
requestFrequencyGuard_->handleDelays();
if(wait && requestFrequencyGuard_->handleDelays()) {
rc = ReturnCode::DELAYED;
}
return commands;
return {commands, rc};
}


std::vector<std::shared_ptr<model::Message>> FleetApiClient::getStatuses(std::optional<int64_t> since, std::optional<bool> wait) {
auto statusesRequest = deviceApi_->listStatuses(companyName_, carName_, since.value_or(0), wait.value_or(false));
std::pair<std::vector<std::shared_ptr<model::Message>>, FleetApiClient::ReturnCode> FleetApiClient::getStatuses(
const std::optional<int64_t> since, const std::optional<bool>& wait) const
{
const auto statusesRequest = deviceApi_->listStatuses(companyName_, carName_, since.value_or(0), wait.value_or(false));
std::vector<std::shared_ptr<model::Message>> statuses {};
auto rc = ReturnCode::OK;

try {
statuses = statusesRequest.get();
} catch(std::exception &e) {
} catch(std::exception &) {
}

if(wait) {
requestFrequencyGuard_->handleDelays();
if(wait && requestFrequencyGuard_->handleDelays()) {
rc = ReturnCode::DELAYED;
}
return statuses;
return {statuses, rc};
}


void FleetApiClient::sendCommand(const std::string &commandJson) {
void FleetApiClient::sendCommand(const std::string &commandJson) const {
payloadPtr_->setMessageType(settings::Constants::COMMAND_MESSAGE_TYPE);
payloadDataPtr_->setJson(web::json::value::parse(commandJson));
messagePtr_->setTimestamp(utility::datetime::utc_timestamp());

std::vector<std::shared_ptr<model::Message>> commands;
commands.push_back(messagePtr_);

auto commandsRequest = deviceApi_->sendCommands(companyName_, carName_, commands);
const auto commandsRequest = deviceApi_->sendCommands(companyName_, carName_, commands);
commandsRequest.wait();
}


void FleetApiClient::sendStatus(const std::string &statusJson, StatusType statusType) {
void FleetApiClient::sendStatus(const std::string &statusJson, const StatusType statusType) const {
switch(statusType) {
case StatusType::STATUS:
payloadPtr_->setMessageType(settings::Constants::STATUS_MESSAGE_TYPE);
Expand All @@ -129,18 +137,18 @@ void FleetApiClient::sendStatus(const std::string &statusJson, StatusType status
std::vector<std::shared_ptr<model::Message>> statuses;
statuses.push_back(messagePtr_);

auto statusesRequest = deviceApi_->sendStatuses(companyName_, carName_, statuses);
const auto statusesRequest = deviceApi_->sendStatuses(companyName_, carName_, statuses);
statusesRequest.wait();
}


std::shared_ptr<model::AvailableDevices> FleetApiClient::getAvailableDevices(std::optional<int32_t> moduleId) {
std::shared_ptr<model::AvailableDevices> FleetApiClient::getAvailableDevices(std::optional<int32_t> moduleId) const {
boost::optional<int32_t> moduleIdBoost = boost::none;
if(moduleId.has_value()) {
moduleIdBoost = moduleId.value();
}

auto availableDevicesRequest = moduleApi_->availableDevices(companyName_, carName_, moduleIdBoost);
const auto availableDevicesRequest = moduleApi_->availableDevices(companyName_, carName_, moduleIdBoost);
return availableDevicesRequest.get();
}

Expand Down
Loading