2021-01-03 21:43:35 +00:00
|
|
|
#include <TimeLib.h>
|
2021-03-12 22:21:45 +00:00
|
|
|
#include <logger.h>
|
|
|
|
|
2021-01-04 22:10:23 +00:00
|
|
|
#include "Task.h"
|
2021-03-12 22:21:45 +00:00
|
|
|
#include "TaskNTP.h"
|
|
|
|
#include "project_configuration.h"
|
2021-01-03 21:43:35 +00:00
|
|
|
|
2021-03-12 22:21:45 +00:00
|
|
|
NTPTask::NTPTask() : Task(TASK_NTP, TaskNtp), _beginCalled(false) {
|
2021-01-03 21:43:35 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 22:21:45 +00:00
|
|
|
NTPTask::~NTPTask() {
|
2021-01-03 21:43:35 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 22:21:45 +00:00
|
|
|
bool NTPTask::setup(std::shared_ptr<Configuration> config, std::shared_ptr<BoardConfig> boardConfig) {
|
|
|
|
_ntpClient = std::shared_ptr<NTPClient>(new NTPClient(config->ntpServer.c_str()));
|
|
|
|
return true;
|
2021-01-03 21:43:35 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 22:21:45 +00:00
|
|
|
bool NTPTask::loop(std::shared_ptr<Configuration> config) {
|
|
|
|
if (!_beginCalled) {
|
|
|
|
_ntpClient->begin();
|
|
|
|
_beginCalled = true;
|
|
|
|
}
|
|
|
|
if (_ntpClient->update()) {
|
|
|
|
setTime(_ntpClient->getEpochTime());
|
|
|
|
logPrintI("Current time: ");
|
|
|
|
logPrintlnI(_ntpClient->getFormattedTime());
|
|
|
|
}
|
|
|
|
_stateInfo = _ntpClient->getFormattedTime();
|
|
|
|
_state = Okay;
|
|
|
|
return true;
|
2021-01-03 21:43:35 +00:00
|
|
|
}
|