Fix RTC using manual time

This commit is contained in:
Theo Arends 2020-11-07 12:42:39 +01:00
parent e498b28864
commit e5dfbb1137
2 changed files with 47 additions and 39 deletions

View File

@ -371,42 +371,46 @@ uint32_t RuleToTime(TimeRule r, int yr)
void RtcSecond(void)
{
static uint32_t last_sync = 0;
static bool mutex = false;
if (mutex) { return; }
if (Rtc.time_synced) {
mutex = true;
Rtc.time_synced = false;
last_sync = Rtc.utc_time;
if (Rtc.restart_time == 0) {
Rtc.restart_time = Rtc.utc_time - TasmotaGlobal.uptime; // save first synced time as restart time
}
TIME_T tmpTime;
BreakTime(Rtc.utc_time, tmpTime);
RtcTime.year = tmpTime.year + 1970;
Rtc.daylight_saving_time = RuleToTime(Settings.tflag[1], RtcTime.year);
Rtc.standard_time = RuleToTime(Settings.tflag[0], RtcTime.year);
// Do not use AddLog_P( here (interrupt routine) if syslog or mqttlog is enabled. UDP/TCP will force exception 9
PrepLog_P(LOG_LEVEL_DEBUG, PSTR("RTC: " D_UTC_TIME " %s, " D_DST_TIME " %s, " D_STD_TIME " %s"),
GetDateAndTime(DT_UTC).c_str(), GetDateAndTime(DT_DST).c_str(), GetDateAndTime(DT_STD).c_str());
if (Rtc.local_time < START_VALID_TIME) { // 2016-01-01
TasmotaGlobal.rules_flag.time_init = 1;
} else {
TasmotaGlobal.rules_flag.time_set = 1;
}
} else {
Rtc.utc_time++; // Increment every second
}
Rtc.millis = millis();
if (!Rtc.user_time_entry) {
if (Rtc.time_synced) {
Rtc.time_synced = false;
last_sync = Rtc.utc_time;
if (Rtc.restart_time == 0) {
Rtc.restart_time = Rtc.utc_time - TasmotaGlobal.uptime; // save first synced time as restart time
}
TIME_T tmpTime;
BreakTime(Rtc.utc_time, tmpTime);
RtcTime.year = tmpTime.year + 1970;
Rtc.daylight_saving_time = RuleToTime(Settings.tflag[1], RtcTime.year);
Rtc.standard_time = RuleToTime(Settings.tflag[0], RtcTime.year);
// Do not use AddLog_P( here (interrupt routine) if syslog or mqttlog is enabled. UDP/TCP will force exception 9
PrepLog_P(LOG_LEVEL_DEBUG, PSTR("RTC: " D_UTC_TIME " %s, " D_DST_TIME " %s, " D_STD_TIME " %s"),
GetDateAndTime(DT_UTC).c_str(), GetDateAndTime(DT_DST).c_str(), GetDateAndTime(DT_STD).c_str());
if (Rtc.local_time < START_VALID_TIME) { // 2016-01-01
TasmotaGlobal.rules_flag.time_init = 1;
} else {
TasmotaGlobal.rules_flag.time_set = 1;
}
}
if ((Rtc.utc_time > (2 * 60 * 60)) && (last_sync < Rtc.utc_time - (2 * 60 * 60))) { // Every two hours a warning
// Do not use AddLog_P( here (interrupt routine) if syslog or mqttlog is enabled. UDP/TCP will force exception 9
PrepLog_P(LOG_LEVEL_DEBUG, PSTR("RTC: Not synced"));
last_sync = Rtc.utc_time;
}
if ((Rtc.utc_time > (2 * 60 * 60)) && (last_sync < Rtc.utc_time - (2 * 60 * 60))) { // Every two hours a warning
// Do not use AddLog_P( here (interrupt routine) if syslog or mqttlog is enabled. UDP/TCP will force exception 9
PrepLog_P(LOG_LEVEL_DEBUG, PSTR("RTC: Not synced"));
last_sync = Rtc.utc_time;
}
Rtc.utc_time++; // Increment every second
Rtc.local_time = Rtc.utc_time;
if (Rtc.local_time > START_VALID_TIME) { // 2016-01-01
int16_t timezone_minutes = Settings.timezone_minutes;
@ -453,10 +457,17 @@ void RtcSecond(void)
}
RtcTime.year += 1970;
mutex = false;
}
void RtcSetTime(uint32_t epoch)
{
void RtcSync(void) {
Rtc.time_synced = true;
RtcSecond();
// AddLog_P(LOG_LEVEL_DEBUG, PSTR("RTC: Synced"));
}
void RtcSetTime(uint32_t epoch) {
if (epoch < START_VALID_TIME) { // 2016-01-01
Rtc.user_time_entry = false;
TasmotaGlobal.ntp_force_sync = true;
@ -466,8 +477,7 @@ void RtcSetTime(uint32_t epoch)
}
}
void RtcInit(void)
{
void RtcInit(void) {
Rtc.utc_time = 0;
BreakTime(Rtc.utc_time, RtcTime);
TickerRtc.attach(1, RtcSecond);

View File

@ -705,7 +705,7 @@ void wifiKeepAlive(void) {
void WifiPollNtp() {
static uint8_t ntp_sync_minute = 0;
if (TasmotaGlobal.global_state.network_down) { return; }
if (TasmotaGlobal.global_state.network_down || Rtc.user_time_entry) { return; }
uint8_t uptime_minute = (TasmotaGlobal.uptime / 60) % 60; // 0 .. 59
if ((ntp_sync_minute > 59) && (uptime_minute > 2)) {
@ -722,9 +722,7 @@ void WifiPollNtp() {
if (ntp_time > START_VALID_TIME) {
Rtc.utc_time = ntp_time;
ntp_sync_minute = 60; // Sync so block further requests
Rtc.time_synced = true;
RtcSecond();
// AddLog_P(LOG_LEVEL_DEBUG, PSTR("NTP: Synced"));
RtcSync();
} else {
ntp_sync_minute++; // Try again in next minute
}