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

WIP: TimeManager

parent 8859a166
No related branches found
No related tags found
2 merge requests!39Merge Develop into Main,!23NTP Refactor into dev
//
// Created by zoe on 12/19/22.
//
#ifndef HOST_CENTRAL_MAST_NTPEXCEPTION_HPP
#define HOST_CENTRAL_MAST_NTPEXCEPTION_HPP
#include <exception>
#include <string>
class NTPException : public std::exception {
public:
explicit NTPException(const std::string &message) : message(message) {}
const char *what() const noexcept override {
return message.c_str();
}
private:
std::string message;
};
#endif //HOST_CENTRAL_MAST_NTPEXCEPTION_HPP
//
// Created by zoe on 12/19/22.
//
#ifndef HOST_CENTRAL_MAST_STRINGTOTIMECONVERSIONEXCEPTION_HPP
#define HOST_CENTRAL_MAST_STRINGTOTIMECONVERSIONEXCEPTION_HPP
#include <exception>
#include <string>
#include "TimeManager.h"
class StringToTimeConversionException : public std::exception {
public:
explicit StringToTimeConversionException(const std::string &message) : message(message) {}
const char *what() const noexcept override {
esp_log_write(ESP_LOG_ERROR, "TimeManager", "Error converting time to epoch: %s",
message.c_str());
return message.c_str();
}
private:
std::string message;
};
#endif //HOST_CENTRAL_MAST_STRINGTOTIMECONVERSIONEXCEPTION_HPP
//
// Created by zoe on 12/19/22.
//
#include "TimeManager.h"
TimeManager::TimeManager(TinyGsmSim7000 &modem) : modem(modem) {
}
void TimeManager::syncNTP(const std::string &ntpServer) {
esp_log_write(ESP_LOG_DEBUG, TAG, "NTP Server Syncing...\n");
auto error = modem.NTPServerSync(ntpServer.c_str(), timeZone);
/**
According to TinGsmNTP.tpp, the error codes are:
case 1: "Network time synchronization is successful";
case 61: "Network error";
case 62: "DNS resolution error";
case 63: "Connection error";
case 64: "Service response error";
case 65: "Service response timeout";
default: "Unknown error: " + String(error);
*/
if (error != 1) {
throw NTPException(modem.ShowNTPError(error).c_str());
} else {
esp_log_write(ESP_LOG_DEBUG, TAG, "NTP: %s", modem.ShowNTPError(error).c_str());
}
}
time_t TimeManager::timeToUnixEpochSeconds(const std::string &time) {
// 22/10/27,10:16:20+00
struct tm tm{};
time_t dateInEpoch = 0;
std::stringstream stringStream(time);
try {
stringStream >> std::get_time(&tm, "%y/%m/%d,%H:%M:%S%z");
} catch (std::exception &e) {
throw StringToTimeConversionException(("Error converting time to epoch %s", e.what()));
}
return dateInEpoch;
}
void TimeManager::writeModemTimeToRTC() {
auto gsmDateTimeString = modem.getGSMDateTime(DATE_FULL);
esp_log_write(ESP_LOG_DEBUG, TAG, "GSM DateTime: %s\n", gsmDateTimeString.c_str());
time_t time = timeToUnixEpochSeconds(gsmDateTimeString.c_str());
rtc.setTime(time);
esp_log_write(ESP_LOG_INFO, TAG, "Time set to EPOCH: %s\n",
String(rtc.getEpoch()).c_str());
}
//
// Created by zoe on 12/19/22.
//
#ifndef HOST_CENTRAL_MAST_TIMEMANAGER_H
#define HOST_CENTRAL_MAST_TIMEMANAGER_H
#define TINY_GSM_MODEM_SIM7000
#include <TinyGsmClientSIM7000.h>
#include <string>
#include "NTPException.hpp"
#include "StringToTimeConversionException.hpp"
#include "ESP32Time.h"
#include <iomanip>
class TimeManager {
public:
explicit TimeManager(TinyGsmSim7000 &modem);
private:
constexpr static char *TAG = "GSM";
ESP32Time rtc;
int timeZone = 0;
// I would like this to be a const reference but the functions used by the modem are not const
TinyGsmSim7000 &modem;
// sync ntp
void syncNTP(const std::string &ntpServer = "time1.uibk.ac.at");
// convert time to unix epoch seconds
time_t timeToUnixEpochSeconds(const std::string &time);
// write modem time to rtc
void writeModemTimeToRTC();
};
#endif //HOST_CENTRAL_MAST_TIMEMANAGER_H
......@@ -13,6 +13,7 @@ platform = espressif32
board = esp-wrover-kit
framework = arduino
monitor_speed = 115200
lib_ldf_mode = deep
monitor_port = /dev/ttyACM0
upload_port = /dev/ttyACM0
build_flags =
......
This diff is collapsed.
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