Skip to content
Snippets Groups Projects
Verified Commit 67602608 authored by Zoe Michaela Dietmar Pfister's avatar Zoe Michaela Dietmar Pfister :gay_pride_flag:
Browse files

Refactors of TimeManager.cpp, include backup option

parent cf134c9e
No related branches found
No related tags found
2 merge requests!39Merge Develop into Main,!23NTP Refactor into dev
......@@ -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";
......
......@@ -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();
......
......@@ -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);
//
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment