Update Time using NTP

This commit is contained in:
iranl
2024-05-22 22:04:22 +02:00
parent 1623cc07c3
commit 941dd51fd4
8 changed files with 120 additions and 7 deletions

View File

@@ -6,6 +6,8 @@
#include <NukiLockUtils.h>
#include "Config.h"
#include "hal/wdt_hal.h"
#include <time.h>
#include "esp_sntp.h"
NukiWrapper* nukiInst = nullptr;
@@ -19,6 +21,7 @@ NukiWrapper::NukiWrapper(const std::string& deviceName, NukiDeviceId* deviceId,
_gpio(gpio),
_preferences(preferences)
{
Log->print("Device id lock: ");
Log->println(_deviceId->get());
@@ -368,6 +371,11 @@ void NukiWrapper::update()
_nextKeypadUpdateTs = ts + _intervalKeypad * 1000;
updateKeypad(false);
}
if(_preferences->getBool(preference_update_time, false) && ts > (120 * 1000) && ts > _nextTimeUpdateTs)
{
_nextTimeUpdateTs = ts + (12 * 60 * 60 * 1000);
updateTime();
}
}
if(_clearAuthData)
{
@@ -4195,3 +4203,39 @@ void NukiWrapper::updateGpioOutputs()
}
}
}
void NukiWrapper::updateTime()
{
if(!isPinValid())
{
Log->println(F("No valid PIN set"));
return;
}
time_t now;
tm tm;
time(&now);
localtime_r(&now, &tm);
if (int(tm.tm_year + 1900) < int(2025))
{
Log->println(F("NTP Time not valid, not updating Nuki device"));
return;
}
Nuki::TimeValue nukiTime;
nukiTime.year = tm.tm_year + 1900;
nukiTime.month = tm.tm_mon + 1;
nukiTime.day = tm.tm_mday;
nukiTime.hour = tm.tm_hour;
nukiTime.minute = tm.tm_min;
nukiTime.second = tm.tm_sec;
Nuki::CmdResult cmdResult = _nukiLock.updateTime(nukiTime);
char resultStr[15] = {0};
NukiLock::cmdResultToString(cmdResult, resultStr);
Log->print(F("Lock time update result: "));
Log->println(resultStr);
}