Skip to content

Commit

Permalink
reafactor func
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-du-car committed Jul 26, 2023
1 parent 9ae8603 commit c1c334f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
15 changes: 9 additions & 6 deletions src/v2i-hub/CDASimAdapter/src/CDASimConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace CDASimAdapter{

bool CDASimConnection::connect() {
//Read local sensor file and populate the sensors JSON
populate_sensors_with_file(_sensor_json_file_path, _sensors_json_v);
_sensors_json_v = read_json_file(_sensor_json_file_path);
if(_sensors_json_v.empty())
{
PLOG(logWARNING) << "Sensors JSON is empty!" << std::endl;
Expand Down Expand Up @@ -224,14 +224,15 @@ namespace CDASimAdapter{
forward_message( msg , message_receiver_publisher );
}

void CDASimConnection::populate_sensors_with_file(const std::string& file_path, Json::Value& sensors_json_v){
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;
return sensors_json_v;
}
std::string line;
std::stringstream ss;
Expand All @@ -240,18 +241,20 @@ namespace CDASimAdapter{
}
in_strm.close();

populate_json_with_string(ss.str(), sensors_json_v);
return string_to_json(ss.str());
}

void CDASimConnection::populate_json_with_string(const std::string& json_str, Json::Value& json_v){
//Update JSON value with information from file
Json::Value CDASimConnection::string_to_json(const std::string& json_str) const{
//Update JSON value with information from string
Json::Value json_v;
Json::CharReaderBuilder builder;
std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
JSONCPP_STRING err;
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;
}
return json_v;
}

Json::Value CDASimConnection::get_sensor_by_id(std::string &sensor_id)
Expand Down
11 changes: 6 additions & 5 deletions src/v2i-hub/CDASimAdapter/src/include/CDASimConnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,15 @@ namespace CDASimAdapter {
/**
* @brief Read local file that has the sensor information in JSON format from disk. Populate global sensor json variable with the information.
* @param file_path A string of file location in the host machine.
* @param sensors_json_v A reference to the location where the sensors inforation is updated and stored.
* @return A reference to the location where the sensors inforation is updated and stored.
*/
void populate_sensors_with_file(const std::string& file_path, Json::Value& sensors_json_v);
Json::Value read_json_file(const std::string& file_path) const;
/**
* @brief Read local file that has the sensor information in JSON format from disk. Populate global sensor json variable with the information.
* @param json_str A JSON string.
* @param json_v A reference to JSON value.
* @return A reference to JSON value.
*/
void populate_json_with_string(const std::string &json_str, Json::Value& json_v);
Json::Value string_to_json(const std::string &json_str) const;

std::string _simulation_ip;
uint _simulation_registration_port;
Expand All @@ -175,7 +175,8 @@ namespace CDASimAdapter {
std::shared_ptr<tmx::utils::UdpServer> time_sync_listener;

FRIEND_TEST(TestCARMASimulationConnection, get_handshake_json);
FRIEND_TEST(TestCARMASimulationConnection, populate_sensors_with_file);
FRIEND_TEST(TestCARMASimulationConnection, read_json_file);
FRIEND_TEST(TestCARMASimulationConnection, string_to_json);
};

}
13 changes: 9 additions & 4 deletions src/v2i-hub/CDASimAdapter/test/TestCARMASimulationConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,25 @@ namespace CDASimAdapter {
ASSERT_TRUE(connection->connect());
}

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

TEST_F(TestCARMASimulationConnection, string_to_json)
{
auto sensorJsonV = connection->string_to_json("Invalid Json");
ASSERT_TRUE(sensorJsonV.empty());
}

TEST_F(TestCARMASimulationConnection, get_sensor_by_id)
{
Point location;
Expand Down

0 comments on commit c1c334f

Please sign in to comment.