Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbourelly999 committed Sep 3, 2024
1 parent 1a42b0e commit acc6a1b
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 85 deletions.
74 changes: 58 additions & 16 deletions src/v2i-hub/CDASimAdapter/src/CDASimConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@ namespace CDASimAdapter{
//Sample sensors.json: https://raw.githubusercontent.com/usdot-fhwa-OPS/V2X-Hub/develop/src/v2i-hub/CDASimAdapter/test/sensors.json
// Sensor configuration is an optional part of registration message.
if ( !_sensor_json_file_path.empty() ) {
auto sensors_json_v = read_json_file(_sensor_json_file_path);
if(sensors_json_v.empty())
{
PLOG(logWARNING) << "Sensors JSON is empty!" << std::endl;
}
message["sensors"] = sensors_json_v;
try {
auto sensors_json_v = read_sensor_configuration_file(_sensor_json_file_path);
message["sensors"] = sensors_json_v;
}
catch(const std::invalid_argument &e) {
PLOG(tmx::utils::logWARNING) << e.what();
}
}
else {
PLOG(logWARNING) << "No sensors where configured for this V2X-Hub instance.";
Expand Down Expand Up @@ -234,14 +235,12 @@ namespace CDASimAdapter{
}

Json::Value CDASimConnection::read_json_file(const std::string& file_path) const{
Json::Value sensors_json_v;
//Read file from disk
std::ifstream in_strm;
in_strm.open(file_path, std::ifstream::binary);
if(!in_strm.is_open())
{
PLOG(logERROR) << "File cannot be opened. File path: " << file_path <<std::endl;
return sensors_json_v;
throw std::invalid_argument("File cannot be opened. File path: " + file_path);
}
std::string line;
std::stringstream ss;
Expand All @@ -262,21 +261,64 @@ namespace CDASimAdapter{
if(!reader->parse(json_str.c_str(), json_str.c_str() + json_str.length(), &json_v, &err))
{
PLOG(logERROR) << "Error parsing sensors from string: " << json_str << std::endl;
throw std::invalid_argument("Error parsing JSON from string: " + json_str);

}
return json_v;
}

Json::Value CDASimConnection::read_sensor_configuration_file(const std::string &file_path) const {
// Sensor Configuration File JSON format
/* {
"sensorId": "sensor_1",
"type": "SemanticLidar",
"ref": {
"type": "CARTESIAN",
"location": {
"x": 1.0,
"y": 2.0,
"z": -3.2
},
"orientation": {
"yaw": 0.0,
"pitch": 0.0,
"roll": 0.0
}
}
}
] */
auto sensor_configuration = read_json_file(file_path);
Json::Value sensors_registration;
for ( int index = 0; index < json_v.size(); ++index ) {
for ( int index = 0; index < sensor_configuration.size(); ++index ) {
Json::Value sensor;
sensor["sensorId"] = json_v[index]["sensorId"];
sensor["type"] = json_v[index]["type"];
if ( json_v[index]["ref"]["type"] != "CARTESIAN" ){
sensor["sensorId"] = sensor_configuration[index]["sensorId"];
sensor["type"] = sensor_configuration[index]["type"];
if ( sensor_configuration[index]["ref"]["type"] != "CARTESIAN" ){
PLOG(logWARNING) << "Skipping sensor configuration for " + sensor["sensorId"].asString() + ". Invalid ref type! Only CARTESIAN ref type currently supported for CDASim!" ;
continue;
}
sensor["location"] = json_v[index]["ref"]["location"];
sensor["orientation"] =json_v[index]["ref"]["orientation"];
sensor["location"] = sensor_configuration[index]["ref"]["location"];
sensor["orientation"] =sensor_configuration[index]["ref"]["orientation"];
sensors_registration[index] = sensor;
}

/** Sensor Registration JSON Format
[
{
"sensorId": "SomeID",
"type": "SemanticLidar",
"location": {
"x": 1.0,
"y": 2.0,
"z": -3.2
},
"orientation": {
"yaw": 0.0,
"pitch": 0.0,
"roll": 0.0
}
}
]
*/
return sensors_registration;
}
}
23 changes: 16 additions & 7 deletions src/v2i-hub/CDASimAdapter/src/include/CDASimConnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,26 @@ namespace CDASimAdapter {
const tmx::utils::Point &location) const;

/**
* @brief Read Json file specified by the file path from disk, and convert the json into Json::Value object.
* @param file_path A string of file path in the host machine.
* @brief Helper method to read JSON file and return resulting Json::Value.
* @param file_path A string of file path in the host machine to the JSON file.
* @return A Json::Value object.
* @throw std::invalid_argument exception if no file is found or file is not readable for provided path.
*/
Json::Value read_json_file(const std::string& file_path) const;
/**
* @brief Convert the Json string into Json::Value object.
* @param json_str A JSON string.
* @return A Json::Value object.
*/
/**
* @brief Helper method to convert string JSON content to JSON::Value.
* @param json_str A JSON string content of Sensor Configuration JSON file
* @return A Json::Value object parsed from string.
* @throw std::invalid_argument exception if provided string is not valid JSON
*/
Json::Value string_to_json(const std::string &json_str) const;
/**
* @brief Method to read Sensor Configuration JSON file and return Sensor Registration information
* required for Infrastructure Registration to CDASim.
* @param file_path Path to Sensor Configuration file.
* @return Json::Value correspoding to Sensor Registration information for Infrastructure Registration to CDASim.
*/
Json::Value read_sensor_configuration_file(const std::string &file_path) const;

std::string _simulation_ip;
uint _simulation_registration_port;
Expand Down
10 changes: 4 additions & 6 deletions src/v2i-hub/CDASimAdapter/test/TestCDASimConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace CDASimAdapter {
}
public:
std::shared_ptr<CDASimConnection> connection;
std::string sensors_file_path = "../../CDASimAdapter/test/sensors_new.json";
std::string sensors_file_path = "../../CDASimAdapter/test/sensors.json";


};
Expand Down Expand Up @@ -101,20 +101,18 @@ namespace CDASimAdapter {

TEST_F(TestCDASimConnection, read_json_file)
{
auto sensorJsonV = connection->read_json_file("Invalid_file_path" );
ASSERT_TRUE(sensorJsonV.empty());
EXPECT_THROW(connection->read_json_file("Invalid_file_path" ), std::invalid_argument);
std::ifstream in_strm;
in_strm.open(sensors_file_path, std::ifstream::binary);
if(in_strm.is_open())
{
sensorJsonV = connection->read_json_file(sensors_file_path );
auto sensorJsonV = connection->read_json_file(sensors_file_path );
ASSERT_FALSE(sensorJsonV.empty());
}
}

TEST_F(TestCDASimConnection, string_to_json)
{
auto sensorJsonV = connection->string_to_json("Invalid Json");
ASSERT_TRUE(sensorJsonV.empty());
EXPECT_THROW(connection->string_to_json("Invalid Json"), std::invalid_argument);
}
}
46 changes: 26 additions & 20 deletions src/v2i-hub/CDASimAdapter/test/sensors.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,35 @@
{
"sensorId": "SomeID",
"type": "SemanticLidar",
"location": {
"x": 0.0,
"y": 0.0,
"z": 0.0
},
"orientation": {
"yaw": 0.0,
"pitch": 0.0,
"roll": 0.0
}
"ref": {
"type": "CARTESIAN",
"location": {
"x": 0.0,
"y": 0.0,
"z": 0.0
},
"orientation": {
"yaw": 0.0,
"pitch": 0.0,
"roll": 0.0
}
}
},
{
"sensorId": "SomeID2",
"type": "SemanticLidar",
"location": {
"x": 1.0,
"y": 2.0,
"z": 0.0
},
"orientation": {
"yaw": 23.0,
"pitch": 0.0,
"roll": 0.0
}
"ref": {
"type": "CARTESIAN",
"location": {
"x": 1.0,
"y": 2.0,
"z": 0.0
},
"orientation": {
"yaw": 23.0,
"pitch": 0.0,
"roll": 0.0
}
}
}
]
36 changes: 0 additions & 36 deletions src/v2i-hub/CDASimAdapter/test/sensors_new.json

This file was deleted.

0 comments on commit acc6a1b

Please sign in to comment.