|
1 | 1 | #include <SoapySDR/Logger.h> |
2 | | -#include <config.h> |
| 2 | +#include <application.h> |
3 | 3 | #include <logger.h> |
4 | | -#include <network/mqtt.h> |
5 | | -#include <network/remote_controller.h> |
6 | | -#include <scanner.h> |
7 | 4 | #include <signal.h> |
8 | | -#include <utils/file_utils.h> |
9 | 5 |
|
10 | 6 | #include <CLI/CLI.hpp> |
11 | 7 | #include <memory> |
@@ -42,71 +38,11 @@ int main(int argc, char** argv) { |
42 | 38 |
|
43 | 39 | try { |
44 | 40 | 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")); |
47 | 42 | nlohmann::json tmpJson; |
48 | 43 | 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()) { |
110 | 46 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
111 | 47 | } |
112 | 48 | } |
|
0 commit comments