From 67602608db7b8038f0b86911674cf36f6cfe2d4d Mon Sep 17 00:00:00 2001 From: Zoe Pfister <zoe.pfister@uibk.ac.at> Date: Thu, 19 Jan 2023 13:51:24 +0100 Subject: [PATCH] Refactors of TimeManager.cpp, include backup option --- .../lib/TimeManager/TimeManager.cpp | 25 +++++++++++++++++-- .../lib/TimeManager/TimeManager.h | 5 +++- host/host_central_mast/src/main.cpp | 10 ++++---- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/host/host_central_mast/lib/TimeManager/TimeManager.cpp b/host/host_central_mast/lib/TimeManager/TimeManager.cpp index dce0ddd..4d5c5b5 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 967cc13..d970a29 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 4c24347..af901c1 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); // -- GitLab