@@ -47,7 +47,7 @@ class SnapTime {
47
47
}
48
48
49
49
// / Record the last time difference between client and server
50
- void setTimeDifferenceClientServerMs (int diff) { time_diff = diff; }
50
+ void setTimeDifferenceClientServerMs (uint32_t diff) { time_diff = diff; }
51
51
52
52
// / Provides the current server time in ms
53
53
uint32_t serverMillis () {
@@ -70,13 +70,13 @@ class SnapTime {
70
70
return sec * 1000 + (usec / 1000 );
71
71
}
72
72
73
- bool printLocalTime () {
73
+ bool printLocalTime (const char * msg ) {
74
74
const timeval val = time ();
75
75
auto *tm_result = gmtime (&val.tv_sec );
76
76
77
77
char str[80 ];
78
78
strftime (str, 80 , " %d-%m-%Y %H-%M-%S" , tm_result);
79
- ESP_LOGI (TAG, " Time is %s" , str);
79
+ ESP_LOGI (TAG, " %s: Time is %s" , msg , str);
80
80
return true ;
81
81
}
82
82
@@ -89,12 +89,21 @@ class SnapTime {
89
89
// / updates the actual time
90
90
bool setTime (timeval time) {
91
91
ESP_LOGI (TAG, " epoch: %lu" , time.tv_sec );
92
- #if !CONFIG_SNAPCLIENT_SNTP_ENABLE && CONFIG_SNAPCLIENT_SETTIME_ALLOWD
92
+ #if CONFIG_SNAPCLIENT_SET_TIME_ALLOWD
93
+ // we do not allow the update when the time is managed via smtp
94
+ if (has_sntp_time){
95
+ ESP_LOGI (TAG," setTime not relevant because it is managed via sntp" );
96
+ return true ;
97
+ }
98
+ // update epoch
93
99
int rc = settimeofday (&time, NULL );
94
- printLocalTime ();
100
+ if (rc==0 ){
101
+ time_diff = time.tv_sec / 1000 ;
102
+ }
103
+ printLocalTime (" setTime" );
95
104
return rc == 0 ;
96
105
#else
97
- ESP_LOGI (TAG, " setTime now allowed/active" );
106
+ ESP_LOGI (TAG, " setTime not allowed/active" );
98
107
return false ;
99
108
#endif
100
109
}
@@ -113,11 +122,32 @@ class SnapTime {
113
122
return delay;
114
123
}
115
124
125
+ void setupSNTPTime () {
126
+ #if CONFIG_SNAPCLIENT_SNTP_ENABLE
127
+ ESP_LOGD (TAG, " start" );
128
+ const char *ntpServer = CONFIG_SNTP_SERVER;
129
+ const long gmtOffset_sec = 1 * 60 * 60 ;
130
+ const int daylightOffset_sec = 1 * 60 * 60 ;
131
+ for (int retry = 0 ; retry < 5 ; retry++) {
132
+ configTime (gmtOffset_sec, daylightOffset_sec, ntpServer);
133
+ tm time;
134
+ if (!getLocalTime (&time)){
135
+ continue ;
136
+ }
137
+ SnapTime::instance ().printLocalTime (" SNTP" );
138
+ has_sntp_time = true ;
139
+ break ;
140
+ }
141
+ #endif
142
+ }
143
+
144
+
116
145
protected:
117
- int64_t time_diff = 0 ;
146
+ uint32_t time_diff = 0 ;
118
147
const char *TAG = " SnapTime" ;
119
148
uint32_t server_ms = 0 ;
120
149
uint32_t local_ms;
121
150
uint16_t message_buffer_delay_ms = 0 ;
122
151
timeval server_time;
152
+ bool has_sntp_time = false ;
123
153
};
0 commit comments