diff --git a/sonoff/support_rtc.ino b/sonoff/support_rtc.ino index 102266eae..9efe92e70 100644 --- a/sonoff/support_rtc.ino +++ b/sonoff/support_rtc.ino @@ -39,22 +39,48 @@ Ticker TickerRtc; static const uint8_t kDaysInMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // API starts months from 1, this array starts from 0 static const char kMonthNamesEnglish[] = "JanFebMarAprMayJunJulAugSepOctNovDec"; -uint32_t utc_time = 0; -uint32_t local_time = 0; -uint32_t daylight_saving_time = 0; -uint32_t standard_time = 0; -uint32_t ntp_time = 0; -uint32_t midnight = 0; -uint32_t restart_time = 0; -int32_t drift_time = 0; -int32_t time_timezone = 0; -uint8_t midnight_now = 0; -uint8_t ntp_sync_minute = 0; -bool user_time_entry = false; // Override NTP by user setting +struct RTC { + uint32_t utc_time = 0; + uint32_t local_time = 0; + uint32_t daylight_saving_time = 0; + uint32_t standard_time = 0; + uint32_t ntp_time = 0; + uint32_t midnight = 0; + uint32_t restart_time = 0; + int32_t drift_time = 0; + int32_t time_timezone = 0; + uint8_t ntp_sync_minute = 0; + bool midnight_now = false; + bool user_time_entry = false; // Override NTP by user setting +} Rtc; + +uint32_t UtcTime(void) +{ + return Rtc.utc_time; +} + +uint32_t LocalTime(void) +{ + return Rtc.local_time; +} int32_t DriftTime(void) { - return drift_time; + return Rtc.drift_time; +} + +uint32_t Midnight(void) +{ + return Rtc.midnight; +} + +bool MidnightNow(void) +{ + if (Rtc.midnight_now) { + Rtc.midnight_now = false; + return true; + } + return false; } String GetBuildDateAndTime(void) @@ -90,7 +116,7 @@ String GetTimeZone(void) { char tz[7]; - snprintf_P(tz, sizeof(tz), PSTR("%+03d:%02d"), time_timezone / 60, abs(time_timezone % 60)); + snprintf_P(tz, sizeof(tz), PSTR("%+03d:%02d"), Rtc.time_timezone / 60, abs(Rtc.time_timezone % 60)); return String(tz); // -03:45 } @@ -140,20 +166,20 @@ String GetDT(uint32_t time) String GetDateAndTime(uint8_t time_type) { // "2017-03-07T11:08:02-07:00" - ISO8601:2004 - uint32_t time = local_time; + uint32_t time = Rtc.local_time; switch (time_type) { case DT_ENERGY: time = Settings.energy_kWhtotal_time; break; case DT_UTC: - time = utc_time; + time = Rtc.utc_time; break; case DT_RESTART: - if (restart_time == 0) { + if (Rtc.restart_time == 0) { return ""; } - time = restart_time; + time = Rtc.restart_time; break; } String dt = GetDT(time); // 2017-03-07T11:08:02 @@ -171,10 +197,10 @@ String GetTime(int type) */ char stime[25]; // Skip newline - uint32_t time = utc_time; - if (1 == type) time = local_time; - if (2 == type) time = daylight_saving_time; - if (3 == type) time = standard_time; + uint32_t time = Rtc.utc_time; + if (1 == type) time = Rtc.local_time; + if (2 == type) time = Rtc.daylight_saving_time; + if (3 == type) time = Rtc.standard_time; snprintf_P(stime, sizeof(stime), sntp_get_real_time(time)); return String(stime); // Thu Nov 01 11:41:02 2018 @@ -182,8 +208,8 @@ String GetTime(int type) uint32_t UpTime(void) { - if (restart_time) { - return utc_time - restart_time; + if (Rtc.restart_time) { + return Rtc.utc_time - Rtc.restart_time; } else { return uptime; } @@ -331,102 +357,80 @@ uint32_t RuleToTime(TimeRule r, int yr) return t; } -uint32_t UtcTime(void) -{ - return utc_time; -} - -uint32_t LocalTime(void) -{ - return local_time; -} - -uint32_t Midnight(void) -{ - return midnight; -} - -bool MidnightNow(void) -{ - bool mnflg = midnight_now; - if (mnflg) midnight_now = 0; - return mnflg; -} - void RtcSecond(void) { TIME_T tmpTime; - if (!user_time_entry) { - if ((ntp_sync_minute > 59) && (RtcTime.minute > 2)) ntp_sync_minute = 1; // If sync prepare for a new cycle + if (!Rtc.user_time_entry) { + if ((Rtc.ntp_sync_minute > 59) && (RtcTime.minute > 2)) Rtc.ntp_sync_minute = 1; // If sync prepare for a new cycle uint8_t offset = (uptime < 30) ? RtcTime.second : (((ESP.getChipId() & 0xF) * 3) + 3) ; // First try ASAP to sync. If fails try once every 60 seconds based on chip id - if (!global_state.wifi_down && (((offset == RtcTime.second) && ((RtcTime.year < 2016) || (ntp_sync_minute == RtcTime.minute))) || ntp_force_sync)) { - ntp_time = sntp_get_current_timestamp(); - if (ntp_time > 1451602800) { // Fix NTP bug in core 2.4.1/SDK 2.2.1 (returns Thu Jan 01 08:00:10 1970 after power on) + if (!global_state.wifi_down && (((offset == RtcTime.second) && ((RtcTime.year < 2016) || (Rtc.ntp_sync_minute == RtcTime.minute))) || ntp_force_sync)) { + Rtc.ntp_time = sntp_get_current_timestamp(); + if (Rtc.ntp_time > 1451602800) { // Fix NTP bug in core 2.4.1/SDK 2.2.1 (returns Thu Jan 01 08:00:10 1970 after power on) ntp_force_sync = false; - if (utc_time > 1451602800) { drift_time = ntp_time - utc_time; } - utc_time = ntp_time; - ntp_sync_minute = 60; // Sync so block further requests - if (restart_time == 0) { - restart_time = utc_time - uptime; // save first ntp time as restart time + if (Rtc.utc_time > 1451602800) { Rtc.drift_time = Rtc.ntp_time - Rtc.utc_time; } + Rtc.utc_time = Rtc.ntp_time; + Rtc.ntp_sync_minute = 60; // Sync so block further requests + if (Rtc.restart_time == 0) { + Rtc.restart_time = Rtc.utc_time - uptime; // save first ntp time as restart time } - BreakTime(utc_time, tmpTime); + BreakTime(Rtc.utc_time, tmpTime); RtcTime.year = tmpTime.year + 1970; - daylight_saving_time = RuleToTime(Settings.tflag[1], RtcTime.year); - standard_time = RuleToTime(Settings.tflag[0], RtcTime.year); + Rtc.daylight_saving_time = RuleToTime(Settings.tflag[1], RtcTime.year); + Rtc.standard_time = RuleToTime(Settings.tflag[0], RtcTime.year); // Do not use AddLog here if syslog is enabled. UDP will force exception 9 // AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION "(" D_UTC_TIME ") %s, (" D_DST_TIME ") %s, (" D_STD_TIME ") %s"), GetTime(0).c_str(), GetTime(2).c_str(), GetTime(3).c_str()); ntp_synced_message = true; - if (local_time < 1451602800) { // 2016-01-01 + if (Rtc.local_time < 1451602800) { // 2016-01-01 rules_flag.time_init = 1; } else { rules_flag.time_set = 1; } } else { - ntp_sync_minute++; // Try again in next minute + Rtc.ntp_sync_minute++; // Try again in next minute } } } - utc_time++; - local_time = utc_time; - if (local_time > 1451602800) { // 2016-01-01 + Rtc.utc_time++; + Rtc.local_time = Rtc.utc_time; + if (Rtc.local_time > 1451602800) { // 2016-01-01 int16_t timezone_minutes = Settings.timezone_minutes; if (Settings.timezone < 0) { timezone_minutes *= -1; } - time_timezone = (Settings.timezone * SECS_PER_HOUR) + (timezone_minutes * SECS_PER_MIN); + Rtc.time_timezone = (Settings.timezone * SECS_PER_HOUR) + (timezone_minutes * SECS_PER_MIN); if (99 == Settings.timezone) { int32_t dstoffset = Settings.toffset[1] * SECS_PER_MIN; int32_t stdoffset = Settings.toffset[0] * SECS_PER_MIN; if (Settings.tflag[1].hemis) { // Southern hemisphere - if ((utc_time >= (standard_time - dstoffset)) && (utc_time < (daylight_saving_time - stdoffset))) { - time_timezone = stdoffset; // Standard Time + if ((Rtc.utc_time >= (Rtc.standard_time - dstoffset)) && (Rtc.utc_time < (Rtc.daylight_saving_time - stdoffset))) { + Rtc.time_timezone = stdoffset; // Standard Time } else { - time_timezone = dstoffset; // Daylight Saving Time + Rtc.time_timezone = dstoffset; // Daylight Saving Time } } else { // Northern hemisphere - if ((utc_time >= (daylight_saving_time - stdoffset)) && (utc_time < (standard_time - dstoffset))) { - time_timezone = dstoffset; // Daylight Saving Time + if ((Rtc.utc_time >= (Rtc.daylight_saving_time - stdoffset)) && (Rtc.utc_time < (Rtc.standard_time - dstoffset))) { + Rtc.time_timezone = dstoffset; // Daylight Saving Time } else { - time_timezone = stdoffset; // Standard Time + Rtc.time_timezone = stdoffset; // Standard Time } } } - local_time += time_timezone; - time_timezone /= 60; - if (!Settings.energy_kWhtotal_time) { Settings.energy_kWhtotal_time = local_time; } + Rtc.local_time += Rtc.time_timezone; + Rtc.time_timezone /= 60; + if (!Settings.energy_kWhtotal_time) { Settings.energy_kWhtotal_time = Rtc.local_time; } } - BreakTime(local_time, RtcTime); + BreakTime(Rtc.local_time, RtcTime); if (RtcTime.valid) { - if (!midnight) { - midnight = local_time - (RtcTime.hour * 3600) - (RtcTime.minute * 60) - RtcTime.second; + if (!Rtc.midnight) { + Rtc.midnight = Rtc.local_time - (RtcTime.hour * 3600) - (RtcTime.minute * 60) - RtcTime.second; } if (!RtcTime.hour && !RtcTime.minute && !RtcTime.second) { - midnight = local_time; - midnight_now = 1; + Rtc.midnight = Rtc.local_time; + Rtc.midnight_now = true; } } @@ -436,11 +440,11 @@ void RtcSecond(void) void RtcSetTime(uint32_t epoch) { if (epoch < 1451602800) { // 2016-01-01 - user_time_entry = false; + Rtc.user_time_entry = false; ntp_force_sync = true; } else { - user_time_entry = true; - utc_time = epoch -1; + Rtc.user_time_entry = true; + Rtc.utc_time = epoch -1; // Will be corrected by RtcSecond } RtcSecond(); } @@ -453,7 +457,7 @@ void RtcInit(void) sntp_stop(); sntp_set_timezone(0); // UTC time sntp_init(); - utc_time = 0; - BreakTime(utc_time, RtcTime); + Rtc.utc_time = 0; + BreakTime(Rtc.utc_time, RtcTime); TickerRtc.attach(1, RtcSecond); } diff --git a/sonoff/xdrv_09_timers.ino b/sonoff/xdrv_09_timers.ino index 49da66331..674cbfb9b 100644 --- a/sonoff/xdrv_09_timers.ino +++ b/sonoff/xdrv_09_timers.ino @@ -137,7 +137,7 @@ void DuskTillDawn(uint8_t *hour_up,uint8_t *minute_up, uint8_t *hour_down, uint8 // double Zeitzone = 0; //Weltzeit // double Zeitzone = 1; //Winterzeit // double Zeitzone = 2.0; //Sommerzeit - float Zeitzone = ((float)time_timezone) / 60; + float Zeitzone = ((float)Rtc.time_timezone) / 60; float Zeitgleichung = BerechneZeitgleichung(&DK, T); float Zeitdifferenz = 12.0f*acosf((sinf(h) - sinf(B)*sinf(DK)) / (cosf(B)*cosf(DK)))/pi; float AufgangOrtszeit = 12.0f - Zeitdifferenz - Zeitgleichung; diff --git a/sonoff/xsns_33_ds3231.ino b/sonoff/xsns_33_ds3231.ino index 98afa0ab0..f1243a3a4 100644 --- a/sonoff/xsns_33_ds3231.ino +++ b/sonoff/xsns_33_ds3231.ino @@ -142,30 +142,30 @@ bool Xsns33(uint8_t function) break; case FUNC_EVERY_SECOND: TIME_T tmpTime; - if (!ds3231ReadStatus && DS3231chipDetected && utc_time < 1451602800 ) { // We still did not sync with NTP (time not valid) , so, read time from DS3231 + if (!ds3231ReadStatus && DS3231chipDetected && Rtc.utc_time < 1451602800 ) { // We still did not sync with NTP (time not valid) , so, read time from DS3231 ntp_force_sync = true; //force to sync with ntp - utc_time = ReadFromDS3231(); //we read UTC TIME from DS3231 + Rtc.utc_time = ReadFromDS3231(); //we read UTC TIME from DS3231 // from this line, we just copy the function from "void RtcSecond()" at the support.ino ,line 2143 and above // We need it to set rules etc. - BreakTime(utc_time, tmpTime); - if (utc_time < 1451602800 ) { + BreakTime(Rtc.utc_time, tmpTime); + if (Rtc.utc_time < 1451602800 ) { ds3231ReadStatus = true; //if time in DS3231 is valid, do not update again } RtcTime.year = tmpTime.year + 1970; - daylight_saving_time = RuleToTime(Settings.tflag[1], RtcTime.year); - standard_time = RuleToTime(Settings.tflag[0], RtcTime.year); + Rtc.daylight_saving_time = RuleToTime(Settings.tflag[1], RtcTime.year); + Rtc.standard_time = RuleToTime(Settings.tflag[0], RtcTime.year); AddLog_P2(LOG_LEVEL_INFO, PSTR("Set time from DS3231 to RTC (" D_UTC_TIME ") %s, (" D_DST_TIME ") %s, (" D_STD_TIME ") %s"), GetTime(0).c_str(), GetTime(2).c_str(), GetTime(3).c_str()); - if (local_time < 1451602800) { // 2016-01-01 + if (Rtc.local_time < 1451602800) { // 2016-01-01 rules_flag.time_init = 1; } else { rules_flag.time_set = 1; } } - else if (!ds3231WriteStatus && DS3231chipDetected && utc_time > 1451602800 && abs(utc_time - ReadFromDS3231()) > 60) {//if time is valid and is drift from RTC in more that 60 second + else if (!ds3231WriteStatus && DS3231chipDetected && Rtc.utc_time > 1451602800 && abs(Rtc.utc_time - ReadFromDS3231()) > 60) {//if time is valid and is drift from RTC in more that 60 second AddLog_P2(LOG_LEVEL_INFO, PSTR("Write Time TO DS3231 from NTP (" D_UTC_TIME ") %s, (" D_DST_TIME ") %s, (" D_STD_TIME ") %s"), GetTime(0).c_str(), GetTime(2).c_str(), GetTime(3).c_str()); - SetDS3231Time (utc_time); //update the DS3231 time + SetDS3231Time (Rtc.utc_time); //update the DS3231 time ds3231WriteStatus = true; } break; diff --git a/sonoff/xsns_38_az7798.ino b/sonoff/xsns_38_az7798.ino index 3a264c5f0..e27a44bad 100644 --- a/sonoff/xsns_38_az7798.ino +++ b/sonoff/xsns_38_az7798.ino @@ -241,9 +241,9 @@ void AzEverySecond(void) } // update the clock from network time - if ((az_clock_update == 0) && (local_time > AZ_EPOCH)) { + if ((az_clock_update == 0) && (LocalTime() > AZ_EPOCH)) { char tmpString[16]; - sprintf(tmpString, "C %d\r", (int)(local_time - AZ_EPOCH)); + sprintf(tmpString, "C %d\r", (int)(LocalTime() - AZ_EPOCH)); AzSerial->write(tmpString); // discard the response do {