Skip to content

Commit

Permalink
fix(aquamqtt): fix broken ntp update
Browse files Browse the repository at this point in the history
  • Loading branch information
tspopp committed Oct 14, 2024
1 parent 21807f8 commit 1b5235d
Showing 1 changed file with 37 additions and 29 deletions.
66 changes: 37 additions & 29 deletions AquaMQTT/src/handler/RTC.cpp
Original file line number Diff line number Diff line change
@@ -21,43 +21,51 @@ void RTCHandler::setup()

void RTCHandler::loop()
{
if (timeStatus() != timeSet)
// do a ntp update if case time is not set or last ntp update is an hour ago
if (timeStatus() != timeSet || (millis() - mLastNTPUpdate) >= 3600000)
{
if ((millis() - mLastNTPUpdate) >= 3600000)
mLastNTPUpdate = millis();
struct tm timeinfo
{
mLastNTPUpdate = millis();
struct tm timeinfo
{
};
if (getLocalTime(&timeinfo, 5000))
{
setTime(timeinfo.tm_hour,
timeinfo.tm_min,
timeinfo.tm_sec,
timeinfo.tm_mday,
timeinfo.tm_mon + 1,
timeinfo.tm_year + 1900);
if (mFoundRTC)
{
mRTC.adjust(DateTime(now()));
}
}
else if (mFoundRTC)
};

// success getting time via ntp
if (getLocalTime(&timeinfo, 5000))
{
// set local time in lib (PaulStoffregen/Time)
setTime(timeinfo.tm_hour,
timeinfo.tm_min,
timeinfo.tm_sec,
timeinfo.tm_mday,
timeinfo.tm_mon + 1,
timeinfo.tm_year + 1900);

// store the new time to the rtc
if (mFoundRTC)
{
setTime(mRTC.now().unixtime());
mRTC.adjust(DateTime(now()));
}
}
// ntp update failed, but we have a hardware rtc
else if (mFoundRTC)
{
// set local time in lib from RTC
setTime(mRTC.now().unixtime());
}
}
else if (config::OVERRIDE_TIME_AND_DATE_IN_MITM && (millis() - mLastStateUpdate) > 500)

// if time is known (either from RTC or NTP), then update the state
if (timeStatus() == timeSet && config::OVERRIDE_TIME_AND_DATE_IN_MITM && ((millis() - mLastStateUpdate) > 1000))
{
mLastStateUpdate = now();
mLastStateUpdate = millis();
time_t timeUpdate = now();
aquamqtt::HMIStateProxy::getInstance().updateTime(
second(mLastStateUpdate),
minute(mLastStateUpdate),
hour(mLastStateUpdate),
day(mLastStateUpdate),
month(mLastStateUpdate),
year(mLastStateUpdate));
second(timeUpdate),
minute(timeUpdate),
hour(timeUpdate),
day(timeUpdate),
month(timeUpdate),
year(timeUpdate));
}
}

0 comments on commit 1b5235d

Please sign in to comment.