Skip to content

Commit

Permalink
add dds json struct to config file
Browse files Browse the repository at this point in the history
  • Loading branch information
Noy-Zini committed Jan 9, 2025
1 parent b03f190 commit 81fa5a9
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 15 deletions.
99 changes: 99 additions & 0 deletions common/rs-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,105 @@ namespace rs2

static config_file& instance();

// Retrieves a value from a nested JSON structure using dot notation
template< typename T >
T get_nested( const std::string & path ) const
{
std::istringstream ss( path );
std::string token;
const rsutils::json * current = &_j;

while( std::getline( ss, token, '.' ) )
{
if( ! current->contains( token ) )
{
return config_value( "" );
}
current = &( *current )[token]; // getting to the next level in the JSON structure
}
return current->get< T >();
}

// Sets a value in a nested JSON structure using dot notation
template< typename T >
void set_nested( const std::string & path, const T & val )
{
std::vector< std::string > keys;
std::istringstream ss( path );
std::string token;

while( std::getline( ss, token, '.' ) )
{
keys.push_back( token );
}
rsutils::json * current = &_j;

for( size_t i = 0; i < keys.size() - 1; ++i )
{
if( ! current->contains( keys[i] ) )
{
( *current )[keys[i]] = rsutils::json::object();
}
current = &( *current )[keys[i]];
}

( *current )[keys.back()] = val;
save();
}

// Sets a default value to the config and default map
template< typename T >
void set_nested_default( const std::string & path, const T & default_val )
{
std::vector< std::string > keys;
std::istringstream ss( path );
std::string token;

while( std::getline( ss, token, '.' ) )
{
keys.push_back( token );
}

rsutils::json * current = &_j;
bool exists = true;

for( const auto & key : keys )
{
if( ! current->contains( key ) )
{
exists = false;
break;
}
current = &( *current )[key];
}

// If it doesn't exist, set the default value in both JSON and defaults map
if( ! exists )
{
current = &_j;
for( size_t i = 0; i < keys.size() - 1; ++i )
{
if( ! current->contains( keys[i] ) )
{
( *current )[keys[i]] = rsutils::json::object();
}
current = &( *current )[keys[i]];
}
( *current )[keys.back()] = default_val;

std::stringstream val_ss;
val_ss << default_val;
_defaults[path] = val_ss.str();
save();
}
else
{
std::stringstream val_ss;
val_ss << ( *current );
_defaults[path] = val_ss.str();
}
}

private:
std::string get_default(const char* key, const char* def) const;

Expand Down
9 changes: 2 additions & 7 deletions common/ux-window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,8 @@ namespace rs2
config_file::instance().set_default(configurations::viewer::log_filename, path + "librealsense.log");
config_file::instance().set_default(configurations::record::default_path, path);

auto const filename
= rsutils::os::get_special_folder( rsutils::os::special_folder::app_data ) + RS2_CONFIG_FILENAME;
auto config = rsutils::json_config::load_from_file( filename );
bool enable_dds = config.nested( "context", "dds", "enabled" ).get_ex( enable_dds );
int domain_id = config.nested("context", "dds", "domain").get_ex(domain_id);
config_file::instance().set_default( configurations::dds::enable_dds, enable_dds );
config_file::instance().set_default( configurations::dds::domain_id, domain_id );
config_file::instance().set_nested_default(configurations::dds::enable_dds, false);
config_file::instance().set_nested_default(configurations::dds::domain_id, 0);
#ifdef __APPLE__

config_file::instance().set_default(configurations::performance::font_oversample, 2);
Expand Down
14 changes: 7 additions & 7 deletions common/viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2815,11 +2815,11 @@ namespace rs2
}

ImGui::Separator();
bool enable_dds = temp_cfg.get( configurations::dds::enable_dds );
int domain_id = temp_cfg.get( configurations::dds::domain_id );
bool enable_dds = temp_cfg.get_nested<bool>("context.dds.enabled");
int domain_id = temp_cfg.get_nested<int>("context.dds.domain");
if( ImGui::Checkbox( "Enable DDS", &enable_dds ) )
{
temp_cfg.set( configurations::dds::enable_dds, enable_dds );
temp_cfg.set_nested("context.dds.enabled", enable_dds);
}
if( enable_dds )
{
Expand All @@ -2834,12 +2834,12 @@ namespace rs2
domain_id = 0;
else if( domain_id > 232 )
domain_id = 232;
temp_cfg.set( configurations::dds::domain_id, domain_id );
temp_cfg.set_nested("context.dds.domain", domain_id);
}
}
ImGui::SameLine();
ImGui::SetCursorPosX( w - 700 );
ImGui::Text(u8"\uf071 Changes will take effect only after restarting the application ");
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.7f, 0.7f, 0.7f, 0.8f));
ImGui::Text(u8"\uf071 DDS changes will take effect only after restarting the application ");
ImGui::PopStyleColor();

}

Expand Down
2 changes: 1 addition & 1 deletion third-party/rsutils/include/rsutils/type/eth-config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ bool eth_config::operator==( eth_config const & other ) const noexcept
// Only compare those items that are configurable
return configured.ip == other.configured.ip && configured.netmask == other.configured.netmask
&& configured.gateway == other.configured.gateway && dds.domain_id == other.dds.domain_id
&& dhcp.on == other.dhcp.on && link.priority == other.link.priority && link.timeout == other.link.timeout && dhcp.timeout != other.dhcp.timeout;
&& dhcp.on == other.dhcp.on && link.priority == other.link.priority && link.timeout == other.link.timeout && dhcp.timeout == other.dhcp.timeout;
}


Expand Down

0 comments on commit 81fa5a9

Please sign in to comment.