diff --git a/host/host_central_mast/lib/TimeManager/TimeManager.cpp b/host/host_central_mast/lib/TimeManager/TimeManager.cpp index dce0ddd69ff8d2bcdde5d8bdc503eb49491191b8..4d5c5b5fdf75b8616b4dcb56a65c63f4689ffc59 100644 --- a/host/host_central_mast/lib/TimeManager/TimeManager.cpp +++ b/host/host_central_mast/lib/TimeManager/TimeManager.cpp @@ -6,11 +6,32 @@ TimeManager::TimeManager(TinyGsmSim7000 &modem) : modem(modem) {} -void TimeManager::syncNTP(const std::string &ntpServer) { +void TimeManager::syncNTP(const std::string &ntpServer, const std::string &backupNtpServer) { + // create list of ntp servers + std::vector<std::string> ntpServers; + ntpServers.push_back(ntpServer); + ntpServers.push_back(backupNtpServer); + tryNtpServerSync(backupNtpServer, ntpServers); +} +void TimeManager::tryNtpServerSync(const std::string &backupNtpServer, + std::vector<std::string> &ntpServers) { // try to sync ntp with each server + for (const auto &server : ntpServers) { + try { + synchronizeNTPWithModem(server); + return; + } catch (NTPException &e) { + if (server == backupNtpServer) { + esp_log_write(ESP_LOG_ERROR, TAG, "Failed to sync NTP with %s (%s)\n", server.c_str(), e.what()); + } + esp_log_write(ESP_LOG_ERROR, TAG, "Failed to sync NTP with %s (%s). Retrying with backup...\n", + server.c_str(), e.what()); + } + } +} +void TimeManager::synchronizeNTPWithModem(const std::string &ntpServer) { esp_log_write(ESP_LOG_DEBUG, TAG, "NTP Server Syncing...\n"); long error = modem.NTPServerSync(ntpServer.c_str(), timeZone); - esp_log_write(ESP_LOG_DEBUG, TAG, "error code: %ld\n", error); /** According to TinGsmNTP.tpp, the error codes are: case 1: "Network time synchronization is successful"; diff --git a/host/host_central_mast/lib/TimeManager/TimeManager.h b/host/host_central_mast/lib/TimeManager/TimeManager.h index 967cc132c83283ee10ea8490a8572386d9e5e7fe..d970a292d388ae4b6cc0bce139d0c9196b3bd147 100644 --- a/host/host_central_mast/lib/TimeManager/TimeManager.h +++ b/host/host_central_mast/lib/TimeManager/TimeManager.h @@ -29,9 +29,12 @@ class TimeManager { // convert time to unix epoch seconds static time_t timeToUnixEpochSeconds(const std::string &time, const std::string &format = "%y/%m/%d,%T+00"); + void synchronizeNTPWithModem(const std::string &ntpServer); + void tryNtpServerSync(const std::string &backupNtpServer, std::vector<std::string> &ntpServers); + public: // sync ntp - void syncNTP(const std::string &ntpServer = "time1.uibk.ac.at"); + void syncNTP(const std::string &ntpServer, const std::string &backupNtpServer); // write modem time to rtc void writeModemTimeToRTC(); diff --git a/host/host_central_mast/src/main.cpp b/host/host_central_mast/src/main.cpp index 4c24347bb572835eb569cdfb38520cb9564b8589..af901c1e4402190a5f7260bc8faaecd9175b514d 100644 --- a/host/host_central_mast/src/main.cpp +++ b/host/host_central_mast/src/main.cpp @@ -462,11 +462,7 @@ void loop() { int csq = modem.getSignalQuality(); esp_log_write(ESP_LOG_DEBUG, TAG_GSM.c_str(), "Signal quality: %d\n", csq); - try { - timeManager.syncNTP("at.pool.ntp.org"); - } catch (const std::exception &e) { - esp_log_write(ESP_LOG_ERROR, TAG_GSM.c_str(), "Error syncing time: %s\n", e.what()); - } + timeManager.syncNTP("pool.ntp.org", "at.pool.ntp.org"); try { timeManager.writeModemTimeToRTC(); @@ -489,6 +485,10 @@ void loop() { connectionManager.modemPowerOff(); + // put readings into array + // protocol address + // hardware name != sensor name + // // connectionManager.setBand(ConnectionManager::BAND_CAT_M, 20); //