| 86 | } |
| 87 | |
| 88 | void updateTLEFile(std::string path) |
| 89 | { |
| 90 | try |
| 91 | { |
| 92 | if (!std::filesystem::exists(std::filesystem::path(path).parent_path())) |
| 93 | std::filesystem::create_directories(std::filesystem::path(path).parent_path()); |
| 94 | } |
| 95 | catch (std::exception &e) |
| 96 | { |
| 97 | logger->error("Cannot create directory for TLE file: %s", e.what()); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | std::vector<int> norads_to_fetch = config::main_cfg["tle_settings"]["tles_to_fetch"].get<std::vector<int>>(); |
| 102 | std::vector<std::string> urls_to_fetch = config::main_cfg["tle_settings"]["urls_to_fetch"].get<std::vector<std::string>>(); |
| 103 | bool success = true; |
| 104 | TLERegistry new_registry; |
| 105 | |
| 106 | for (auto &url_str : urls_to_fetch) |
| 107 | { |
| 108 | logger->info(url_str); |
| 109 | std::string result; |
| 110 | int http_res = 1, trials = 0; |
| 111 | while (http_res == 1 && trials < 10) |
| 112 | { |
| 113 | if ((http_res = perform_http_request(url_str, result)) != 1) |
| 114 | { |
| 115 | std::istringstream tle_stream(result); |
| 116 | success = parseTLEStream(tle_stream, new_registry) > 0; |
| 117 | } |
| 118 | else |
| 119 | success = false; |
| 120 | trials++; |
| 121 | if (!success) |
| 122 | { |
| 123 | std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 124 | logger->info("Failed getting TLEs. Retrying..."); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if (!success) |
| 129 | { |
| 130 | logger->warn("Failed to get TLE for %s", url_str.c_str()); |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if (!success) |
| 136 | { |
| 137 | logger->error("Error updating TLEs. Not updated."); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | for (int norad : norads_to_fetch) |
| 142 | { |
| 143 | std::string url_str = config::main_cfg["tle_settings"]["url_template"].get<std::string>(); |
| 144 | while (url_str.find("%NORAD%") != std::string::npos) |
| 145 | url_str.replace(url_str.find("%NORAD%"), 7, std::to_string(norad)); |
no test coverage detected