Skip to content

Commit ad5e61e

Browse files
committed
Created Application.
1 parent fc308e8 commit ad5e61e

File tree

7 files changed

+114
-73
lines changed

7 files changed

+114
-73
lines changed

sources/application.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include "application.h"
2+
3+
#include <logger.h>
4+
#include <utils/file_utils.h>
5+
6+
constexpr auto LABEL = "application";
7+
8+
Application::Application(nlohmann::json& tmpJson, const ArgConfig& argConfig)
9+
: m_reload(false),
10+
m_argConfig(argConfig),
11+
m_tmpJson(tmpJson),
12+
m_fileJson(m_tmpJson.empty() ? readFromFile(m_argConfig.configFile, static_cast<nlohmann::json>(FileConfig())) : m_tmpJson),
13+
m_fileConfig(FileConfig::fromJson(m_fileJson)),
14+
m_config(m_argConfig, m_fileConfig),
15+
m_mqtt(m_config),
16+
m_remoteController(m_config, m_mqtt) {
17+
Logger::configure(m_config.consoleLogLevel(), m_config.fileLogLevel(), m_argConfig.logFileName, m_argConfig.logFileSize, m_argConfig.logFileCount, m_config.isColorLogEnabled());
18+
Logger::info(LABEL, "{}", colored(GREEN, "{}", "started"));
19+
Logger::info(LABEL, "config: {}", colored(GREEN, "{}", FileConfig::toPrint(m_fileJson).dump()));
20+
Logger::info(LABEL, "mqtt: {}", colored(GREEN, "{}", m_config.mqtt()));
21+
22+
for (const auto& device : m_config.devices()) {
23+
try {
24+
if (!device.enabled) {
25+
Logger::info(LABEL, "device disabled, skipping: {}", colored(GREEN, "{}", device.getName()));
26+
} else {
27+
m_scanners.push_back(std::make_unique<Scanner>(m_config, device, m_mqtt, m_remoteController, m_config.recordersCount()));
28+
}
29+
} catch (const std::exception& exception) {
30+
Logger::error(LABEL, "can not open device: {}, exception: {}", colored(RED, "{}", device.getName()), exception.what());
31+
}
32+
}
33+
if (m_scanners.empty()) {
34+
Logger::warn(LABEL, "{}", colored(RED, "{}", "empty devices list"));
35+
}
36+
37+
m_remoteController.setConfigQuery([this](const nlohmann::json& json) {
38+
try {
39+
Logger::info(LABEL, "set config: {}", colored(GREEN, "{}", FileConfig::toPrint(json).dump()));
40+
saveToFile(m_argConfig.configFile, FileConfig::toSave(json));
41+
m_reload = true;
42+
m_tmpJson.clear();
43+
m_remoteController.setConfigResponse(true);
44+
} catch (const std::runtime_error& exception) {
45+
Logger::warn(LABEL, "set config exception: {}", exception.what());
46+
m_remoteController.setConfigResponse(false);
47+
}
48+
});
49+
50+
m_remoteController.resetTmpConfigQuery([this](const std::string&) {
51+
Logger::info(LABEL, "reset tmp config");
52+
m_reload = true;
53+
m_tmpJson.clear();
54+
m_remoteController.resetTmpConfigResponse(true);
55+
});
56+
57+
m_remoteController.setTmpConfigQuery([this](const nlohmann::json& json) {
58+
try {
59+
Logger::info(LABEL, "set tmp config: {}", colored(GREEN, "{}", FileConfig::toPrint(json).dump()));
60+
m_reload = true;
61+
m_tmpJson = json;
62+
m_remoteController.setConfigResponse(true);
63+
} catch (const std::runtime_error& exception) {
64+
Logger::warn(LABEL, "set tmp config exception: {}", exception.what());
65+
m_remoteController.setConfigResponse(false);
66+
}
67+
});
68+
69+
m_remoteController.getConfigQuery([this](const std::string&) {
70+
Logger::info(LABEL, "get config");
71+
m_remoteController.getConfigResponse(static_cast<nlohmann::json>(m_fileConfig).dump());
72+
});
73+
}
74+
75+
Application::~Application() { Logger::info(LABEL, "{}", colored(GREEN, "{}", "stopped")); }
76+
77+
bool Application::reload() const { return m_reload; }

sources/application.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#pragma once
2+
3+
#include <arg_config.h>
4+
#include <config.h>
5+
#include <file_config.h>
6+
#include <network/mqtt.h>
7+
#include <network/remote_controller.h>
8+
#include <scanner.h>
9+
10+
#include <nlohmann/json.hpp>
11+
12+
class Application {
13+
public:
14+
Application(nlohmann::json& tmpJson, const ArgConfig& argConfig);
15+
~Application();
16+
17+
bool reload() const;
18+
19+
private:
20+
bool m_reload;
21+
const ArgConfig& m_argConfig;
22+
nlohmann::json& m_tmpJson;
23+
const nlohmann::json m_fileJson;
24+
const FileConfig m_fileConfig;
25+
const Config m_config;
26+
27+
Mqtt m_mqtt;
28+
RemoteController m_remoteController;
29+
std::vector<std::unique_ptr<Scanner>> m_scanners;
30+
};

sources/config.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
#include <fstream>
99
#include <regex>
1010

11-
constexpr auto LABEL = "config";
12-
1311
spdlog::level::level_enum parseLogLevel(const std::string& level) {
1412
if (level == "trace")
1513
return spdlog::level::level_enum::trace;

sources/config_migrator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <logger.h>
44
#include <radio/help_structures.h>
55

6-
constexpr auto LABEL = "config";
6+
constexpr auto LABEL = "migrator";
77

88
void ConfigMigrator::update(nlohmann::json& config) {
99
const auto version = config.contains("version") ? config.at("version").get<int>() : 0;

sources/file_config.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include <regex>
88

9-
constexpr auto LABEL = "config";
9+
constexpr auto LABEL = "file_config";
1010

1111
FileConfig FileConfig::fromJson(nlohmann::json json) {
1212
ConfigMigrator::update(json);

sources/main.cpp

Lines changed: 4 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
#include <SoapySDR/Logger.h>
2-
#include <config.h>
2+
#include <application.h>
33
#include <logger.h>
4-
#include <network/mqtt.h>
5-
#include <network/remote_controller.h>
6-
#include <scanner.h>
74
#include <signal.h>
8-
#include <utils/file_utils.h>
95

106
#include <CLI/CLI.hpp>
117
#include <memory>
@@ -42,71 +38,11 @@ int main(int argc, char** argv) {
4238

4339
try {
4440
Logger::configure(spdlog::level::info, spdlog::level::info, argConfig.logFileName, argConfig.logFileSize, argConfig.logFileCount, true);
45-
Logger::info(LABEL, "{}", colored(GREEN, "{}", "starting"));
46-
41+
Logger::info(LABEL, "{}", colored(GREEN, "{}", "started"));
4742
nlohmann::json tmpJson;
4843
while (isRunning) {
49-
bool reload = false;
50-
const auto fileJson = tmpJson.empty() ? readFromFile(argConfig.configFile, static_cast<nlohmann::json>(FileConfig())) : tmpJson;
51-
const auto fileConfig = FileConfig::fromJson(fileJson);
52-
const Config config(argConfig, fileConfig);
53-
Logger::configure(config.consoleLogLevel(), config.fileLogLevel(), argConfig.logFileName, argConfig.logFileSize, argConfig.logFileCount, config.isColorLogEnabled());
54-
Logger::info(LABEL, "config: {}", colored(GREEN, "{}", FileConfig::toPrint(fileJson).dump()));
55-
Logger::info(LABEL, "mqtt: {}", colored(GREEN, "{}", config.mqtt()));
56-
57-
Mqtt mqtt(config);
58-
RemoteController remoteController(config, mqtt);
59-
remoteController.setConfigQuery([&reload, &argConfig, &remoteController, &tmpJson](const nlohmann::json& json) {
60-
try {
61-
Logger::info(LABEL, "set config: {}", colored(GREEN, "{}", FileConfig::toPrint(json).dump()));
62-
saveToFile(argConfig.configFile, FileConfig::toSave(json));
63-
reload = true;
64-
tmpJson.clear();
65-
remoteController.setConfigResponse(true);
66-
} catch (const std::runtime_error& exception) {
67-
Logger::warn(LABEL, "set config exception: {}", exception.what());
68-
remoteController.setConfigResponse(false);
69-
}
70-
});
71-
remoteController.resetTmpConfigQuery([&reload, &argConfig, &remoteController, &tmpJson](const std::string&) {
72-
Logger::info(LABEL, "reset tmp config");
73-
reload = true;
74-
tmpJson.clear();
75-
remoteController.resetTmpConfigResponse(true);
76-
});
77-
remoteController.setTmpConfigQuery([&reload, &argConfig, &remoteController, &tmpJson](const nlohmann::json& json) {
78-
try {
79-
Logger::info(LABEL, "set tmp config: {}", colored(GREEN, "{}", FileConfig::toPrint(json).dump()));
80-
reload = true;
81-
tmpJson = json;
82-
remoteController.setConfigResponse(true);
83-
} catch (const std::runtime_error& exception) {
84-
Logger::warn(LABEL, "set tmp config exception: {}", exception.what());
85-
remoteController.setConfigResponse(false);
86-
}
87-
});
88-
remoteController.getConfigQuery([&remoteController, &fileConfig](const std::string&) {
89-
Logger::info(LABEL, "get config");
90-
remoteController.getConfigResponse(static_cast<nlohmann::json>(fileConfig).dump());
91-
});
92-
std::vector<std::unique_ptr<Scanner>> scanners;
93-
for (const auto& device : config.devices()) {
94-
try {
95-
if (!device.enabled) {
96-
Logger::info(LABEL, "device disabled, skipping: {}", colored(GREEN, "{}", device.getName()));
97-
} else {
98-
scanners.push_back(std::make_unique<Scanner>(config, device, mqtt, remoteController, config.recordersCount()));
99-
}
100-
} catch (const std::exception& exception) {
101-
Logger::error(LABEL, "can not open device: {}, exception: {}", colored(RED, "{}", device.getName()), exception.what());
102-
}
103-
}
104-
if (scanners.empty()) {
105-
Logger::warn(LABEL, "{}", colored(RED, "{}", "empty devices list"));
106-
}
107-
108-
Logger::info(LABEL, "{}", colored(GREEN, "{}", "started"));
109-
while (isRunning && !reload) {
44+
Application application(tmpJson, argConfig);
45+
while (isRunning && !application.reload()) {
11046
std::this_thread::sleep_for(std::chrono::milliseconds(100));
11147
}
11248
}

sources/radio/sdr_device_reader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <logger.h>
55
#include <utils/utils.h>
66

7-
constexpr auto LABEL = "config";
7+
constexpr auto LABEL = "sdr_reader";
88

99
std::vector<Frequency> getSampleRates(SoapySDR::Device* sdr) {
1010
std::vector<Frequency> sampleRates;

0 commit comments

Comments
 (0)