Skip to content

Commit a38d356

Browse files
committed
Added id argument.
1 parent da487fa commit a38d356

File tree

5 files changed

+15
-12
lines changed

5 files changed

+15
-12
lines changed

sources/config.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ std::vector<FrequencyRange> readIgnoredRanges(const nlohmann::json& json) {
5858
Config::Config(const nlohmann::json& json, const ArgConfig& argConfig)
5959
: m_json(json),
6060
m_argConfig(argConfig),
61+
m_id(!argConfig.id.empty() ? argConfig.id : generateRandomHash()),
6162
m_devices(SdrDeviceReader::readDevices(json)),
6263
m_isColorLogEnabled(readKey<bool>(json, {"output", "color_log_enabled"})),
6364
m_consoleLogLevel(parseLogLevel(readKey<std::string>(json, {"output", "console_log_level"}))),
@@ -109,6 +110,7 @@ void Config::saveToFile(const std::string& path, const nlohmann::json& json) {
109110
nlohmann::json Config::json() const { return m_json; }
110111
std::string Config::mqtt() const { return fmt::format("{}@{}", m_argConfig.mqttUser, m_argConfig.mqttUrl); };
111112

113+
std::string Config::getId() const { return m_id; }
112114
std::vector<Device> Config::devices() const { return m_devices; }
113115

114116
bool Config::isColorLogEnabled() const { return m_isColorLogEnabled; }

sources/config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ constexpr auto SPECTROGRAM_SEND_INTERVAL = std::chrono::milliseconds(1000); //
3636

3737
struct ArgConfig {
3838
std::string configFile;
39+
std::string id;
3940
std::string logFileName = "sdr_scanner.log"; // default log filename
4041
int logFileCount = 9; // default keep last n log files
4142
int logFileSize = 10 * 1024 * 1024; // default single log file max size
@@ -51,6 +52,7 @@ class Config {
5152
nlohmann::json json() const;
5253
std::string mqtt() const;
5354

55+
std::string getId() const;
5456
std::vector<Device> devices() const;
5557

5658
bool isColorLogEnabled() const;
@@ -74,6 +76,7 @@ class Config {
7476
const nlohmann::json m_json;
7577
const ArgConfig& m_argConfig;
7678

79+
const std::string m_id;
7780
const std::vector<Device> m_devices;
7881

7982
const bool m_isColorLogEnabled;

sources/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ int main(int argc, char** argv) {
2525

2626
ArgConfig argConfig;
2727
app.add_option("--config", argConfig.configFile, "config file")->required()->check(CLI::ExistingFile);
28+
app.add_option("--id", argConfig.id);
2829
app.add_option("--log-file", argConfig.logFileName, "log file");
2930
app.add_option("--log-file-count", argConfig.logFileCount, "log file count")->check(CLI::PositiveNumber);
3031
app.add_option("--log-file-size", argConfig.logFileSize, "log file size")->check(CLI::PositiveNumber);
@@ -42,7 +43,6 @@ int main(int argc, char** argv) {
4243
Logger::configure(spdlog::level::info, spdlog::level::info, argConfig.logFileName, argConfig.logFileSize, argConfig.logFileCount, true);
4344
Logger::info(LABEL, "{}", colored(GREEN, "{}", "starting"));
4445

45-
const auto id = generateRandomHash();
4646
while (isRunning) {
4747
bool reload = false;
4848
const Config config = Config::loadFromFile(argConfig.configFile, argConfig);
@@ -51,7 +51,7 @@ int main(int argc, char** argv) {
5151
Logger::info(LABEL, "mqtt: {}", colored(GREEN, "{}", config.mqtt()));
5252

5353
Mqtt mqtt(config);
54-
RemoteController remoteController(config, id, mqtt, [&reload, &argConfig](const nlohmann::json& json) {
54+
RemoteController remoteController(config, mqtt, [&reload, &argConfig](const nlohmann::json& json) {
5555
Logger::info(LABEL, "reload config: {}", colored(GREEN, "{}", json.dump()));
5656
Config::saveToFile(argConfig.configFile, json);
5757
reload = true;

sources/network/remote_controller.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,28 @@ constexpr auto LABEL = "remote";
1515

1616
using namespace std::placeholders;
1717

18-
RemoteController::RemoteController(const Config& config, const std::string& id, Mqtt& mqtt, std::function<void(const nlohmann::json&)> configCallback)
19-
: m_config(config), m_id(id), m_mqtt(mqtt), m_configCallback(configCallback) {
18+
RemoteController::RemoteController(const Config& config, Mqtt& mqtt, std::function<void(const nlohmann::json&)> configCallback) : m_config(config), m_mqtt(mqtt), m_configCallback(configCallback) {
2019
mqtt.setMessageCallback(fmt::format("sdr/{}", LIST), std::bind(&RemoteController::listCallback, this, _1));
21-
mqtt.setMessageCallback(fmt::format("sdr/{}/{}", CONFIG, m_id), std::bind(&RemoteController::configCallback, this, _1));
20+
mqtt.setMessageCallback(fmt::format("sdr/{}/{}", CONFIG, m_config.getId()), std::bind(&RemoteController::configCallback, this, _1));
2221
mqtt.setMessageCallback(fmt::format("sdr/{}", MANUAL_RECORDING), std::bind(&RemoteController::manualRecordingCallback, this, _1));
23-
mqtt.setMessageCallback(fmt::format("sdr/{}/{}", RESTART, m_id), std::bind(&RemoteController::restartCallback, this, _1));
24-
Logger::info(LABEL, "started, id: {}", colored(GREEN, "{}", m_id));
22+
mqtt.setMessageCallback(fmt::format("sdr/{}/{}", RESTART, m_config.getId()), std::bind(&RemoteController::restartCallback, this, _1));
23+
Logger::info(LABEL, "started, id: {}", colored(GREEN, "{}", m_config.getId()));
2524
}
2625

2726
void RemoteController::listCallback(const std::string&) {
2827
Logger::info(LABEL, "received list");
29-
m_mqtt.publish(fmt::format("sdr/{}/{}", STATUS, m_id), m_config.json().dump(), 2);
28+
m_mqtt.publish(fmt::format("sdr/{}/{}", STATUS, m_config.getId()), m_config.json().dump(), 2);
3029
}
3130

3231
void RemoteController::configCallback(const std::string& data) {
3332
Logger::info(LABEL, "received config");
3433
try {
3534
const auto json = nlohmann::json::parse(data);
3635
m_configCallback(json);
37-
m_mqtt.publish(fmt::format("sdr/{}/{}/{}", CONFIG, m_id, SUCCESS), "", 2);
36+
m_mqtt.publish(fmt::format("sdr/{}/{}/{}", CONFIG, m_config.getId(), SUCCESS), "", 2);
3837
} catch (const std::exception& e) {
3938
Logger::warn(LABEL, "invalid config");
40-
m_mqtt.publish(fmt::format("sdr/{}/{}/{}", CONFIG, m_id, FAILED), "", 2);
39+
m_mqtt.publish(fmt::format("sdr/{}/{}/{}", CONFIG, m_config.getId(), FAILED), "", 2);
4140
}
4241
}
4342

sources/network/remote_controller.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class RemoteController {
1010
public:
11-
RemoteController(const Config& config, const std::string& id, Mqtt& mqtt, std::function<void(const nlohmann::json&)> configCallback);
11+
RemoteController(const Config& config, Mqtt& mqtt, std::function<void(const nlohmann::json&)> configCallback);
1212

1313
private:
1414
void listCallback(const std::string& data);
@@ -17,7 +17,6 @@ class RemoteController {
1717
void restartCallback(const std::string& data);
1818

1919
const Config& m_config;
20-
const std::string m_id;
2120
Mqtt& m_mqtt;
2221
std::function<void(const nlohmann::json&)> m_configCallback;
2322
};

0 commit comments

Comments
 (0)