Skip to content
Open
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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ list(APPEND trunk_recorder_sources
trunk-recorder/sources/iq_file_source.cc
trunk-recorder/csv_helper.cc
trunk-recorder/config.cc
trunk-recorder/config_service.cc
trunk-recorder/setup_systems.cc
trunk-recorder/monitor_systems.cc
trunk-recorder/talkgroup.cc
Expand Down
44 changes: 44 additions & 0 deletions trunk-recorder/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ bool load_config(string config_file, Config &config, gr::top_block_sptr &tb, std
<< "byte position of error: " << e.byte << std::endl;
}

// Store the raw parsed JSON so we can track which keys were explicitly set
config.loaded_json = data;

try {
// const std::string json_filename = "config.json";

Expand Down Expand Up @@ -246,11 +249,13 @@ bool load_config(string config_file, Config &config, gr::top_block_sptr &tb, std

BOOST_LOG_TRIVIAL(info) << "\n-------------------------------------\nSYSTEMS\n-------------------------------------\n";

int sys_json_index = 0;
for (json element : data["systems"]) {
bool system_enabled = element.value("enabled", true);
if (system_enabled) {
// each system should have a unique index value;
System *system = System::make(sys_count++);
system->set_config_index(sys_json_index);

std::stringstream default_script;
unsigned long sys_id;
Expand Down Expand Up @@ -460,9 +465,11 @@ bool load_config(string config_file, Config &config, gr::top_block_sptr &tb, std
systems.push_back(system);
BOOST_LOG_TRIVIAL(info);
}
sys_json_index++;
}

BOOST_LOG_TRIVIAL(info) << "\n\n-------------------------------------\nSOURCES\n-------------------------------------\n";
int source_json_index = 0;
for (json element : data["sources"]) {

bool source_enabled = element.value("enabled", true);
Expand Down Expand Up @@ -651,10 +658,12 @@ bool load_config(string config_file, Config &config, gr::top_block_sptr &tb, std
source->create_debug_recorder(tb, source_count);
}

source->set_config_index(source_json_index);
sources.push_back(source);
source_count++;
BOOST_LOG_TRIVIAL(info) << "\n-------------------------------------\n\n";
}
source_json_index++;
}

BOOST_LOG_TRIVIAL(info) << "\n\n-------------------------------------\nPLUGINS\n-------------------------------------\n";
Expand All @@ -679,3 +688,38 @@ bool load_config(string config_file, Config &config, gr::top_block_sptr &tb, std
BOOST_LOG_TRIVIAL(info) << "\n\n";
return true;
}

bool save_config(const Config &config) {
if (config.config_file.empty()) {
BOOST_LOG_TRIVIAL(error) << "save_config: No config file path set";
return false;
}

try {
// Create a backup of the current config file with a timestamp
if (boost::filesystem::exists(config.config_file)) {
auto now = std::chrono::system_clock::now();
auto time_t_now = std::chrono::system_clock::to_time_t(now);
std::tm tm_now;
localtime_r(&time_t_now, &tm_now);
char time_buf[32];
std::strftime(time_buf, sizeof(time_buf), "%Y%m%d_%H%M%S", &tm_now);

std::string backup_file = config.config_file + "." + time_buf + ".bak";
boost::filesystem::copy_file(config.config_file, backup_file);
BOOST_LOG_TRIVIAL(info) << "Configuration backup saved to: " << backup_file;
}

std::ofstream f(config.config_file);
if (!f.is_open()) {
BOOST_LOG_TRIVIAL(error) << "save_config: Could not open file for writing: " << config.config_file;
return false;
}
f << config.loaded_json.dump(4) << std::endl;
BOOST_LOG_TRIVIAL(info) << "Configuration saved to: " << config.config_file;
return true;
} catch (const std::exception &e) {
BOOST_LOG_TRIVIAL(error) << "save_config: Failed to save config: " << e.what();
return false;
}
}
1 change: 1 addition & 0 deletions trunk-recorder/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@
#include <json.hpp>

bool load_config(std::string config_file, Config &config, gr::top_block_sptr &tb, std::vector<Source *> &sources, std::vector<System *> &systems);
bool save_config(const Config &config);

#endif
Loading
Loading