Skip to content

Commit 21bbe9f

Browse files
committed
Time Synch
1 parent 3fc846d commit 21bbe9f

File tree

5 files changed

+51
-32
lines changed

5 files changed

+51
-32
lines changed

src/SnapClient.h

+2-19
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ class SnapClient {
8686

8787
setupNVS();
8888

89-
setupSNTPTime();
90-
9189
setupMDNS();
9290

9391
setupPSRAM();
9492

93+
SnapTime::instance().setupSNTPTime();
94+
9595
// start tasks
9696
return p_snapprocessor->begin();
9797
}
@@ -135,23 +135,6 @@ class SnapClient {
135135
#endif
136136
SnapProcessor *p_snapprocessor = &default_processor;
137137

138-
void setupSNTPTime() {
139-
#if CONFIG_SNAPCLIENT_SNTP_ENABLE
140-
ESP_LOGD(TAG, "start");
141-
const char *ntpServer = CONFIG_SNTP_SERVER;
142-
const long gmtOffset_sec = 1 * 60 * 60;
143-
const int daylightOffset_sec = 1 * 60 * 60;
144-
for (int retry = 0; retry < 5; retry++) {
145-
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
146-
if(!SnapTime().instance().printLocalTime()){
147-
continue;
148-
}
149-
checkHeap();
150-
SnapTime::instance().printLocalTime();
151-
break;
152-
}
153-
#endif
154-
}
155138

156139
void setupMDNS() {
157140
#if CONFIG_SNAPCLIENT_USE_MDNS

src/SnapConfig.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
#ifndef CONFIG_SNAPCLIENT_SNTP_ENABLE
1414
# define CONFIG_SNAPCLIENT_SNTP_ENABLE true
1515
#endif
16-
#ifndef CONFIG_SNAPCLIENT_SETTIME_ALLOWD
17-
# define CONFIG_SNAPCLIENT_SETTIME_ALLOWD false
16+
#ifndef CONFIG_SNAPCLIENT_SET_TIME_ALLOWD
17+
# define CONFIG_SNAPCLIENT_SET_TIME_ALLOWD true
1818
#endif
1919
#ifndef CONFIG_SNAPCLIENT_USE_MDNS
2020
# define CONFIG_SNAPCLIENT_USE_MDNS true

src/api/SnapOutputSimple.h

+9-3
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,15 @@ class SnapOutputSimple : public SnapOutput {
6363
// wait for the audio to become valid
6464
int diff_ms = msg_time - server_time;
6565
int delay_ms = diff_ms + SnapTime::instance().getStartDelay();
66-
ESP_LOGI(TAG, "starting after %d ms", delay_ms);
67-
delay(delay_ms);
68-
is_sync_active = false;
66+
if (delay_ms < 100000){
67+
ESP_LOGI(TAG, "starting after %d ms", delay_ms);
68+
delay(delay_ms);
69+
is_sync_active = false;
70+
} else {
71+
ESP_LOGI(TAG, "invalid delay: %d ms - starting anyhow...", delay_ms);
72+
delay(SnapTime::instance().getStartDelay());
73+
is_sync_active = false;
74+
}
6975
}
7076
}
7177
return true;

src/api/SnapProcessor.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ class SnapProcessor {
460460
is_time_set = true;
461461
} else {
462462
int64_t time_diff = snap_time.timeDifferenceMs(trx, ttx);
463-
int time_diff_int = time_diff;
463+
uint32_t time_diff_int = time_diff;
464464
assert(time_diff_int==time_diff);
465465
ESP_LOGI(TAG, "Time Difference to Server: %ld ms", time_diff);
466466
snap_time.setTimeDifferenceClientServerMs(time_diff);

src/api/SnapTime.h

+37-7
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class SnapTime {
4747
}
4848

4949
/// 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; }
5151

5252
/// Provides the current server time in ms
5353
uint32_t serverMillis() {
@@ -70,13 +70,13 @@ class SnapTime {
7070
return sec * 1000 + (usec / 1000);
7171
}
7272

73-
bool printLocalTime() {
73+
bool printLocalTime(const char* msg) {
7474
const timeval val = time();
7575
auto *tm_result = gmtime(&val.tv_sec);
7676

7777
char str[80];
7878
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);
8080
return true;
8181
}
8282

@@ -89,12 +89,21 @@ class SnapTime {
8989
/// updates the actual time
9090
bool setTime(timeval time) {
9191
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
9399
int rc = settimeofday(&time, NULL);
94-
printLocalTime();
100+
if (rc==0){
101+
time_diff = time.tv_sec / 1000;
102+
}
103+
printLocalTime("setTime");
95104
return rc == 0;
96105
#else
97-
ESP_LOGI(TAG, "setTime now allowed/active");
106+
ESP_LOGI(TAG, "setTime not allowed/active");
98107
return false;
99108
#endif
100109
}
@@ -113,11 +122,32 @@ class SnapTime {
113122
return delay;
114123
}
115124

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+
116145
protected:
117-
int64_t time_diff = 0;
146+
uint32_t time_diff = 0;
118147
const char *TAG = "SnapTime";
119148
uint32_t server_ms = 0;
120149
uint32_t local_ms;
121150
uint16_t message_buffer_delay_ms = 0;
122151
timeval server_time;
152+
bool has_sntp_time = false;
123153
};

0 commit comments

Comments
 (0)