Refactor RTC saving code space

Refactor RTC saving code space
This commit is contained in:
Theo Arends 2019-08-17 16:49:17 +02:00
parent ef63883a1c
commit c107864b92
4 changed files with 100 additions and 96 deletions

View File

@ -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 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"; static const char kMonthNamesEnglish[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
uint32_t utc_time = 0; struct RTC {
uint32_t local_time = 0; uint32_t utc_time = 0;
uint32_t daylight_saving_time = 0; uint32_t local_time = 0;
uint32_t standard_time = 0; uint32_t daylight_saving_time = 0;
uint32_t ntp_time = 0; uint32_t standard_time = 0;
uint32_t midnight = 0; uint32_t ntp_time = 0;
uint32_t restart_time = 0; uint32_t midnight = 0;
int32_t drift_time = 0; uint32_t restart_time = 0;
int32_t time_timezone = 0; int32_t drift_time = 0;
uint8_t midnight_now = 0; int32_t time_timezone = 0;
uint8_t ntp_sync_minute = 0; uint8_t ntp_sync_minute = 0;
bool user_time_entry = false; // Override NTP by user setting 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) 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) String GetBuildDateAndTime(void)
@ -90,7 +116,7 @@ String GetTimeZone(void)
{ {
char tz[7]; 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 return String(tz); // -03:45
} }
@ -140,20 +166,20 @@ String GetDT(uint32_t time)
String GetDateAndTime(uint8_t time_type) String GetDateAndTime(uint8_t time_type)
{ {
// "2017-03-07T11:08:02-07:00" - ISO8601:2004 // "2017-03-07T11:08:02-07:00" - ISO8601:2004
uint32_t time = local_time; uint32_t time = Rtc.local_time;
switch (time_type) { switch (time_type) {
case DT_ENERGY: case DT_ENERGY:
time = Settings.energy_kWhtotal_time; time = Settings.energy_kWhtotal_time;
break; break;
case DT_UTC: case DT_UTC:
time = utc_time; time = Rtc.utc_time;
break; break;
case DT_RESTART: case DT_RESTART:
if (restart_time == 0) { if (Rtc.restart_time == 0) {
return ""; return "";
} }
time = restart_time; time = Rtc.restart_time;
break; break;
} }
String dt = GetDT(time); // 2017-03-07T11:08:02 String dt = GetDT(time); // 2017-03-07T11:08:02
@ -171,10 +197,10 @@ String GetTime(int type)
*/ */
char stime[25]; // Skip newline char stime[25]; // Skip newline
uint32_t time = utc_time; uint32_t time = Rtc.utc_time;
if (1 == type) time = local_time; if (1 == type) time = Rtc.local_time;
if (2 == type) time = daylight_saving_time; if (2 == type) time = Rtc.daylight_saving_time;
if (3 == type) time = standard_time; if (3 == type) time = Rtc.standard_time;
snprintf_P(stime, sizeof(stime), sntp_get_real_time(time)); snprintf_P(stime, sizeof(stime), sntp_get_real_time(time));
return String(stime); // Thu Nov 01 11:41:02 2018 return String(stime); // Thu Nov 01 11:41:02 2018
@ -182,8 +208,8 @@ String GetTime(int type)
uint32_t UpTime(void) uint32_t UpTime(void)
{ {
if (restart_time) { if (Rtc.restart_time) {
return utc_time - restart_time; return Rtc.utc_time - Rtc.restart_time;
} else { } else {
return uptime; return uptime;
} }
@ -331,102 +357,80 @@ uint32_t RuleToTime(TimeRule r, int yr)
return t; 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) void RtcSecond(void)
{ {
TIME_T tmpTime; TIME_T tmpTime;
if (!user_time_entry) { if (!Rtc.user_time_entry) {
if ((ntp_sync_minute > 59) && (RtcTime.minute > 2)) ntp_sync_minute = 1; // If sync prepare for a new cycle 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 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)) { if (!global_state.wifi_down && (((offset == RtcTime.second) && ((RtcTime.year < 2016) || (Rtc.ntp_sync_minute == RtcTime.minute))) || ntp_force_sync)) {
ntp_time = sntp_get_current_timestamp(); Rtc.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 (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; ntp_force_sync = false;
if (utc_time > 1451602800) { drift_time = ntp_time - utc_time; } if (Rtc.utc_time > 1451602800) { Rtc.drift_time = Rtc.ntp_time - Rtc.utc_time; }
utc_time = ntp_time; Rtc.utc_time = Rtc.ntp_time;
ntp_sync_minute = 60; // Sync so block further requests Rtc.ntp_sync_minute = 60; // Sync so block further requests
if (restart_time == 0) { if (Rtc.restart_time == 0) {
restart_time = utc_time - uptime; // save first ntp time as restart time 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; RtcTime.year = tmpTime.year + 1970;
daylight_saving_time = RuleToTime(Settings.tflag[1], RtcTime.year); Rtc.daylight_saving_time = RuleToTime(Settings.tflag[1], RtcTime.year);
standard_time = RuleToTime(Settings.tflag[0], 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 // 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()); // 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; ntp_synced_message = true;
if (local_time < 1451602800) { // 2016-01-01 if (Rtc.local_time < 1451602800) { // 2016-01-01
rules_flag.time_init = 1; rules_flag.time_init = 1;
} else { } else {
rules_flag.time_set = 1; rules_flag.time_set = 1;
} }
} else { } else {
ntp_sync_minute++; // Try again in next minute Rtc.ntp_sync_minute++; // Try again in next minute
} }
} }
} }
utc_time++; Rtc.utc_time++;
local_time = utc_time; Rtc.local_time = Rtc.utc_time;
if (local_time > 1451602800) { // 2016-01-01 if (Rtc.local_time > 1451602800) { // 2016-01-01
int16_t timezone_minutes = Settings.timezone_minutes; int16_t timezone_minutes = Settings.timezone_minutes;
if (Settings.timezone < 0) { timezone_minutes *= -1; } 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) { if (99 == Settings.timezone) {
int32_t dstoffset = Settings.toffset[1] * SECS_PER_MIN; int32_t dstoffset = Settings.toffset[1] * SECS_PER_MIN;
int32_t stdoffset = Settings.toffset[0] * SECS_PER_MIN; int32_t stdoffset = Settings.toffset[0] * SECS_PER_MIN;
if (Settings.tflag[1].hemis) { if (Settings.tflag[1].hemis) {
// Southern hemisphere // Southern hemisphere
if ((utc_time >= (standard_time - dstoffset)) && (utc_time < (daylight_saving_time - stdoffset))) { if ((Rtc.utc_time >= (Rtc.standard_time - dstoffset)) && (Rtc.utc_time < (Rtc.daylight_saving_time - stdoffset))) {
time_timezone = stdoffset; // Standard Time Rtc.time_timezone = stdoffset; // Standard Time
} else { } else {
time_timezone = dstoffset; // Daylight Saving Time Rtc.time_timezone = dstoffset; // Daylight Saving Time
} }
} else { } else {
// Northern hemisphere // Northern hemisphere
if ((utc_time >= (daylight_saving_time - stdoffset)) && (utc_time < (standard_time - dstoffset))) { if ((Rtc.utc_time >= (Rtc.daylight_saving_time - stdoffset)) && (Rtc.utc_time < (Rtc.standard_time - dstoffset))) {
time_timezone = dstoffset; // Daylight Saving Time Rtc.time_timezone = dstoffset; // Daylight Saving Time
} else { } else {
time_timezone = stdoffset; // Standard Time Rtc.time_timezone = stdoffset; // Standard Time
} }
} }
} }
local_time += time_timezone; Rtc.local_time += Rtc.time_timezone;
time_timezone /= 60; Rtc.time_timezone /= 60;
if (!Settings.energy_kWhtotal_time) { Settings.energy_kWhtotal_time = local_time; } 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 (RtcTime.valid) {
if (!midnight) { if (!Rtc.midnight) {
midnight = local_time - (RtcTime.hour * 3600) - (RtcTime.minute * 60) - RtcTime.second; Rtc.midnight = Rtc.local_time - (RtcTime.hour * 3600) - (RtcTime.minute * 60) - RtcTime.second;
} }
if (!RtcTime.hour && !RtcTime.minute && !RtcTime.second) { if (!RtcTime.hour && !RtcTime.minute && !RtcTime.second) {
midnight = local_time; Rtc.midnight = Rtc.local_time;
midnight_now = 1; Rtc.midnight_now = true;
} }
} }
@ -436,11 +440,11 @@ void RtcSecond(void)
void RtcSetTime(uint32_t epoch) void RtcSetTime(uint32_t epoch)
{ {
if (epoch < 1451602800) { // 2016-01-01 if (epoch < 1451602800) { // 2016-01-01
user_time_entry = false; Rtc.user_time_entry = false;
ntp_force_sync = true; ntp_force_sync = true;
} else { } else {
user_time_entry = true; Rtc.user_time_entry = true;
utc_time = epoch -1; Rtc.utc_time = epoch -1; // Will be corrected by RtcSecond
} }
RtcSecond(); RtcSecond();
} }
@ -453,7 +457,7 @@ void RtcInit(void)
sntp_stop(); sntp_stop();
sntp_set_timezone(0); // UTC time sntp_set_timezone(0); // UTC time
sntp_init(); sntp_init();
utc_time = 0; Rtc.utc_time = 0;
BreakTime(utc_time, RtcTime); BreakTime(Rtc.utc_time, RtcTime);
TickerRtc.attach(1, RtcSecond); TickerRtc.attach(1, RtcSecond);
} }

View File

@ -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 = 0; //Weltzeit
// double Zeitzone = 1; //Winterzeit // double Zeitzone = 1; //Winterzeit
// double Zeitzone = 2.0; //Sommerzeit // double Zeitzone = 2.0; //Sommerzeit
float Zeitzone = ((float)time_timezone) / 60; float Zeitzone = ((float)Rtc.time_timezone) / 60;
float Zeitgleichung = BerechneZeitgleichung(&DK, T); float Zeitgleichung = BerechneZeitgleichung(&DK, T);
float Zeitdifferenz = 12.0f*acosf((sinf(h) - sinf(B)*sinf(DK)) / (cosf(B)*cosf(DK)))/pi; float Zeitdifferenz = 12.0f*acosf((sinf(h) - sinf(B)*sinf(DK)) / (cosf(B)*cosf(DK)))/pi;
float AufgangOrtszeit = 12.0f - Zeitdifferenz - Zeitgleichung; float AufgangOrtszeit = 12.0f - Zeitdifferenz - Zeitgleichung;

View File

@ -142,30 +142,30 @@ bool Xsns33(uint8_t function)
break; break;
case FUNC_EVERY_SECOND: case FUNC_EVERY_SECOND:
TIME_T tmpTime; 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 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 // 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. // We need it to set rules etc.
BreakTime(utc_time, tmpTime); BreakTime(Rtc.utc_time, tmpTime);
if (utc_time < 1451602800 ) { if (Rtc.utc_time < 1451602800 ) {
ds3231ReadStatus = true; //if time in DS3231 is valid, do not update again ds3231ReadStatus = true; //if time in DS3231 is valid, do not update again
} }
RtcTime.year = tmpTime.year + 1970; RtcTime.year = tmpTime.year + 1970;
daylight_saving_time = RuleToTime(Settings.tflag[1], RtcTime.year); Rtc.daylight_saving_time = RuleToTime(Settings.tflag[1], RtcTime.year);
standard_time = RuleToTime(Settings.tflag[0], 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"), 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()); 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; rules_flag.time_init = 1;
} else { } else {
rules_flag.time_set = 1; 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"), 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()); 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; ds3231WriteStatus = true;
} }
break; break;

View File

@ -241,9 +241,9 @@ void AzEverySecond(void)
} }
// update the clock from network time // 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]; 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); AzSerial->write(tmpString);
// discard the response // discard the response
do { do {