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

Replace protobuf with JSON #19

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
676f5bc
added option to send messages as json
MarioIvancik Jul 2, 2024
908cf59
Merge branch 'master' into BAF-874/skip_protobuf_option
MarioIvancik Jul 9, 2024
d9a3919
added flag to use raw json instead of protobuf
MarioIvancik Jul 11, 2024
c4d1353
dump json instead of to_string
MarioIvancik Aug 2, 2024
7d9fe2b
use protobuf only for message validation
MarioIvancik Aug 5, 2024
3c47f5f
print received command
MarioIvancik Aug 5, 2024
616f0d3
generate first command manually
MarioIvancik Aug 5, 2024
dc4e25b
use to_string on json
MarioIvancik Aug 5, 2024
36fc047
code improvements
MarioIvancik Aug 7, 2024
28b3eb0
config is not iterable
MarioIvancik Aug 7, 2024
d452390
add debug prints to command validation
MarioIvancik Aug 8, 2024
ef57529
use .at when addressing json keys
MarioIvancik Aug 8, 2024
095b7e7
debug logging
MarioIvancik Aug 12, 2024
39f03d0
validate a command when validating a command
MarioIvancik Aug 14, 2024
f998d24
remove debug logging
MarioIvancik Aug 14, 2024
9127756
update version tag
MarioIvancik Aug 14, 2024
7707c2f
formatting fixes
MarioIvancik Aug 14, 2024
4d5572e
Merge branch 'master' into BAF-874/skip_protobuf_option
MarioIvancik Aug 14, 2024
05cc21e
fix cmakelists
MarioIvancik Aug 14, 2024
7bf4394
sonar cloud fixes
MarioIvancik Aug 15, 2024
18b96e9
catch specific exceptions
MarioIvancik Aug 15, 2024
db41bef
revert to general exceptions
MarioIvancik Aug 15, 2024
f7b61a6
Merge branch 'master' of github.com:bringauto/mission-module into BAF…
MarioIvancik Oct 16, 2024
c47cfd6
use from_chars to convert strings
MarioIvancik Oct 16, 2024
b01f4eb
update fleet protocol http client version
MarioIvancik Oct 23, 2024
1270015
fix config value checking
MarioIvancik Oct 23, 2024
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: 8 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ INCLUDE(CheckPIESupported)
CHECK_PIE_SUPPORTED()
SET(CMAKE_POSITION_INDEPENDENT_CODE ON)

SET(MISSION_MODULE_VERSION 1.2.8)
SET(MISSION_MODULE_VERSION 2.0.0)
MarioIvancik marked this conversation as resolved.
Show resolved Hide resolved

OPTION(BRINGAUTO_INSTALL "Configure install" OFF)
OPTION(BRINGAUTO_PACKAGE "Configure package creation" OFF)
Expand Down Expand Up @@ -54,6 +54,7 @@ IF (NOT BRINGAUTO_SYSTEM_DEP)
ENDIF ()
ENDIF ()

FIND_PACKAGE(nlohmann_json 3.10.5 REQUIRED)
FIND_PACKAGE(Protobuf 3.21.12 REQUIRED)
FIND_PACKAGE(fleet-protocol-interface 2.0.0 REQUIRED)
FIND_PACKAGE(fleet-protocol-cxx-helpers-static 1.1.1 REQUIRED)
Expand All @@ -72,18 +73,18 @@ CMDEF_ADD_LIBRARY(
VERSION ${MISSION_MODULE_VERSION}
)

# Protobuf mission payload
# Protobuf, JSON mission payload
SET(Protobuf_USE_STATIC_LIBS ON)
FILE(GLOB_RECURSE protobuf_mission_cpp_files "lib/protobuf-mission-module/*")
ADD_LIBRARY(protobuf_mission_lib STATIC ${protobuf_mission_cpp_files})
TARGET_INCLUDE_DIRECTORIES(protobuf_mission_lib PUBLIC "lib/protobuf-mission-module/")
TARGET_LINK_LIBRARIES(protobuf_mission_lib PUBLIC protobuf::libprotobuf)
ADD_LIBRARY(message_handler_lib STATIC ${protobuf_mission_cpp_files})
TARGET_INCLUDE_DIRECTORIES(message_handler_lib PUBLIC "lib/protobuf-mission-module/")
TARGET_LINK_LIBRARIES(message_handler_lib PUBLIC protobuf::libprotobuf nlohmann_json::nlohmann_json)

FILE(GLOB_RECURSE HEADERS "include/*")

ADD_LIBRARY(mission_module_hpp INTERFACE ${HEADERS})
TARGET_INCLUDE_DIRECTORIES(mission_module_hpp INTERFACE "include/")
TARGET_LINK_LIBRARIES(mission_module_hpp INTERFACE protobuf_mission_lib)
TARGET_LINK_LIBRARIES(mission_module_hpp INTERFACE message_handler_lib)

FILE(GLOB_RECURSE SOURCES "source/bringauto/*")

Expand All @@ -93,7 +94,7 @@ TARGET_LINK_LIBRARIES(mission_module_sources PUBLIC
mission_module_hpp
fleet-protocol-interface::common-headers-interface
fleet-protocol-cxx-helpers-static::fleet-protocol-cxx-helpers-static
protobuf_mission_lib
message_handler_lib
)

IF (FLEET_PROTOCOL_BUILD_MODULE_GATEWAY)
Expand Down
17 changes: 9 additions & 8 deletions cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
SET(CMAKE_FIND_USE_CMAKE_SYSTEM_PATH FALSE)

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)
BA_PACKAGE_LIBRARY(protobuf v4.21.12)
BA_PACKAGE_LIBRARY(zlib v1.2.11)
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)
BA_PACKAGE_LIBRARY(zlib v1.2.11)
BA_PACKAGE_LIBRARY(nlohmann-json v3.10.5 PLATFORM_STRING_MODE any_machine NO_DEBUG ON)
BA_PACKAGE_LIBRARY(protobuf v4.21.12)

IF (FLEET_PROTOCOL_BUILD_EXTERNAL_SERVER)
BA_PACKAGE_LIBRARY(fleet-http-client-shared v1.3.0)
BA_PACKAGE_LIBRARY(boost v1.78.0)
BA_PACKAGE_LIBRARY(cpprestsdk v2.10.20)
BA_PACKAGE_LIBRARY(ba-logger v1.2.0)
BA_PACKAGE_LIBRARY(fleet-http-client-shared v1.3.0)
BA_PACKAGE_LIBRARY(boost v1.78.0)
BA_PACKAGE_LIBRARY(cpprestsdk v2.10.20)
BA_PACKAGE_LIBRARY(ba-logger v1.2.0)
ENDIF ()
26 changes: 26 additions & 0 deletions include/bringauto/ba_json/JsonHelper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <fleet_protocol/common_headers/memory_management.h>

#include <MissionModule.pb.h>

#include <nlohmann/json.hpp>

namespace bringauto::ba_json {

using json = nlohmann::ordered_json;

class JsonHelper {
public:
static int bufferToJson(json& json, const buffer& buffer);

static int jsonToBuffer(buffer* buffer, const json& json);

static MissionModule::AutonomyStatus_State stringToAutonomyState(const std::string_view &state);

static std::string autonomyStateToString(MissionModule::AutonomyStatus_State state);

static std::string autonomyActionToString(MissionModule::AutonomyCommand_Action action);

};
}
4 changes: 2 additions & 2 deletions include/bringauto/modules/mission_module/Constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Constants {
/**
* @brief Minimum time between two status messages being sent when speed is above the threshold
*/
static constexpr std::chrono::milliseconds status_sending_period = std::chrono::milliseconds(2900);
static constexpr auto status_sending_period = std::chrono::milliseconds(2900);
};

}
}
5 changes: 2 additions & 3 deletions include/bringauto/modules/mission_module/Context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@

#include <vector>
#include <condition_variable>
#include <thread>

namespace bringauto::modules::mission_module {

struct Context {
std::shared_ptr<bringauto::fleet_protocol::http_client::FleetApiClient> fleet_api_client;
std::shared_ptr<fleet_protocol::http_client::FleetApiClient> fleet_api_client;
std::vector<device_identification> devices;
std::vector<std::pair<std::string, bringauto::fleet_protocol::cxx::DeviceID>> command_vector;
std::vector<std::pair<std::string, fleet_protocol::cxx::DeviceID>> command_vector;
std::mutex mutex;
std::condition_variable con_variable;
long last_command_timestamp;
Expand Down
6 changes: 3 additions & 3 deletions include/bringauto/modules/mission_module/MissionModule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


namespace bringauto::modules::mission_module {
static const int MISSION_MODULE_NUMBER = 1;
static constexpr int MISSION_MODULE_NUMBER = 1;

static const int AUTONOMY_DEVICE_TYPE = 1;
}
static constexpr int AUTONOMY_DEVICE_TYPE = 1;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include <MissionModule.pb.h>
#include <fleet_protocol/common_headers/memory_management.h>

#include <chrono>
#include <map>

namespace bringauto::modules::mission_module::devices {
Expand Down Expand Up @@ -90,13 +90,10 @@ class AutonomyDevice {
*/
static int command_data_valid(const struct buffer command);
private:
static MissionModule::AutonomyCommand generateCommand(std::vector<MissionModule::Station> stops, std::string route,
MissionModule::AutonomyCommand::Action action);

/**
* @brief Map of last sent status timestamps for each device type
*/
static std::map<unsigned int, std::chrono::milliseconds> last_sent_status_timestamps_;

};
}
}
8 changes: 4 additions & 4 deletions include/bringauto/protobuf/ProtobufHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ class ProtobufHelper {
public:
static int serializeProtobufMessageToBuffer(struct buffer* message, const google::protobuf::Message &protobufMessage);

static MissionModule::AutonomyStatus parseAutonomyStatus(struct buffer status);
static int validateAutonomyStatus(const std::string &status);

static MissionModule::AutonomyCommand parseAutonomyCommand(struct buffer command);
static int validateAutonomyCommand(const std::string &command);

static MissionModule::AutonomyError parseAutonomyError(struct buffer errorMessage);
static int validateAutonomyError(const std::string &errorMessage);


};
}
}
70 changes: 70 additions & 0 deletions source/bringauto/ba_json/JsonHelper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <fleet_protocol/common_headers/general_error_codes.h>

#include <bringauto/ba_json/JsonHelper.hpp>


namespace bringauto::ba_json {

using json = nlohmann::ordered_json;

int JsonHelper::bufferToJson(json& json, const buffer& buffer) {
const auto buffer_data = static_cast<char*> (buffer.data);
try {
json = json::parse(buffer_data, buffer_data + buffer.size_in_bytes);
} catch (json::parse_error &) {
return NOT_OK;
}
return OK;
}

int JsonHelper::jsonToBuffer(buffer *buffer, const json& json) {
const std::string tmp = nlohmann::to_string(json);
if (allocate(buffer, tmp.size()) == NOT_OK) {
return NOT_OK;
}
std::memcpy(buffer->data, tmp.c_str(), tmp.size());
return OK;
}

MissionModule::AutonomyStatus_State JsonHelper::stringToAutonomyState(const std::string_view &state) {
if (state == "IDLE") {
return MissionModule::AutonomyStatus_State::AutonomyStatus_State_IDLE;
} else if (state == "DRIVE") {
return MissionModule::AutonomyStatus_State::AutonomyStatus_State_DRIVE;
} else if (state == "IN_STOP") {
return MissionModule::AutonomyStatus_State::AutonomyStatus_State_IN_STOP;
} else if (state == "OBSTACLE") {
return MissionModule::AutonomyStatus_State::AutonomyStatus_State_OBSTACLE;
} else if (state == "ERROR") {
return MissionModule::AutonomyStatus_State::AutonomyStatus_State_ERROR;
}
return MissionModule::AutonomyStatus_State::AutonomyStatus_State_ERROR;
}

std::string JsonHelper::autonomyStateToString(const MissionModule::AutonomyStatus_State state) {
switch (state) {
case MissionModule::AutonomyStatus_State::AutonomyStatus_State_IDLE:
return "IDLE";
case MissionModule::AutonomyStatus_State::AutonomyStatus_State_DRIVE:
return "DRIVE";
case MissionModule::AutonomyStatus_State::AutonomyStatus_State_IN_STOP:
return "IN_STOP";
case MissionModule::AutonomyStatus_State::AutonomyStatus_State_OBSTACLE:
return "OBSTACLE";
default:
return "ERROR";
}
}

std::string JsonHelper::autonomyActionToString(const MissionModule::AutonomyCommand_Action action) {
switch (action) {
case MissionModule::AutonomyCommand_Action::AutonomyCommand_Action_STOP:
return "STOP";
case MissionModule::AutonomyCommand_Action::AutonomyCommand_Action_START:
return "DRIVE";
default:
return "NO_ACTION";
}
}

}
Loading