Skip to content

Commit 989693e

Browse files
committed
add timestamps for getlocation getweather synctime
1 parent 928ed6e commit 989693e

File tree

5 files changed

+25
-25
lines changed

5 files changed

+25
-25
lines changed

src/GetLocation.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#pragma once
22

3-
#include <IPAddress.h>
4-
3+
#include <time.h>
54
namespace Watchy_GetLocation {
65

76
typedef struct {
@@ -12,6 +11,8 @@ typedef struct {
1211
} location;
1312

1413
extern location currentLocation;
14+
extern time_t lastGetLocationTS; // timestamp of last successful getLocation
1515

16+
// returns nullptr on failure
1617
extern const location* getLocation();
1718
} // namespace Watchy_GetLocation

src/GetWeather.cpp

+14-13
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
namespace Watchy_GetWeather {
1111
RTC_DATA_ATTR weatherData currentWeather = {.temperature = 22,
1212
.weatherConditionCode = 800};
13-
RTC_DATA_ATTR unsigned long lastWeatherTS = 0;
13+
RTC_DATA_ATTR time_t lastGetWeatherTS = 0;
1414

1515
weatherData getWeather() {
1616
// only update if WEATHER_UPDATE_INTERVAL has elapsed i.e. 30 minutes
17-
if (now() - lastWeatherTS < WEATHER_UPDATE_INTERVAL) {
17+
if (lastGetWeatherTS &&
18+
(now() - lastGetWeatherTS < WEATHER_UPDATE_INTERVAL)) {
1819
// too soon to update, just re-use existing values. Not an error
1920
Watchy::err = Watchy::RATE_LIMITED;
2021
return currentWeather;
@@ -48,22 +49,22 @@ weatherData getWeather() {
4849
Watchy::err = Watchy::REQUEST_FAILED;
4950
LOGE("http.begin failed");
5051
} else {
51-
int httpResponseCode = http.GET();
52-
if (httpResponseCode == 200) {
53-
String payload = http.getString();
54-
JSONVar responseObject = JSON.parse(payload);
55-
currentWeather.temperature = int(responseObject["main"]["temp"]);
56-
currentWeather.weatherConditionCode =
57-
int(responseObject["weather"][0]["id"]);
52+
int httpResponseCode = http.GET();
53+
if (httpResponseCode == 200) {
54+
String payload = http.getString();
55+
JSONVar responseObject = JSON.parse(payload);
56+
currentWeather.temperature = int(responseObject["main"]["temp"]);
57+
currentWeather.weatherConditionCode =
58+
int(responseObject["weather"][0]["id"]);
5859
strncpy(currentWeather.weatherCity, loc->city,
5960
sizeof(currentWeather.weatherCity));
6061
lastGetWeatherTS = now();
6162
Watchy::err = Watchy::OK;
62-
} else {
63+
} else {
6364
Watchy::err = Watchy::REQUEST_FAILED;
64-
LOGE("http response %d", httpResponseCode);
65-
}
66-
http.end();
65+
LOGE("http response %d", httpResponseCode);
66+
}
67+
http.end();
6768
}
6869
// turn off radios
6970
WiFi.mode(WIFI_OFF);

src/GetWeather.h

+1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ typedef struct weatherData {
1818
} weatherData;
1919

2020
extern weatherData getWeather();
21+
extern time_t lastGetWeatherTS; // timestamp of last successful getWeather call
2122

2223
} // namespace Watchy_GetWeather

src/SyncTime.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Watchy_SyncTime {
1010

1111
RTC_DATA_ATTR const char *ntpServer = NTP_SERVER;
12+
RTC_DATA_ATTR time_t lastSyncTimeTS = 0;
1213

1314
// RTC does not know about TZ
1415
// so DST has to be in app code
@@ -22,6 +23,8 @@ void timeSyncCallback(struct timeval *tv) {
2223
setTime(tv->tv_sec); // set system time
2324
settimeofday(tv, nullptr); // set posix
2425
sntp_set_sync_status(SNTP_SYNC_STATUS_COMPLETED);
26+
lastSyncTimeTS = tv->tv_sec;
27+
LOGD("lastSyncTimeTS %ld", lastSyncTimeTS);
2528
}
2629

2730
void syncTime(const char* timezone) {

src/SyncTime.h

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include <time.h>
4+
35
namespace Watchy_SyncTime {
46

57
// ntpServer defaults to NTP_SERVER from config.h
@@ -8,16 +10,8 @@ extern const char* ntpServer;
810

911
constexpr const char *DEFAULT_TIMEZONE = "UTC0"; // posix format
1012

11-
typedef enum {
12-
ready,
13-
waiting,
14-
wifiFailed,
15-
timeout,
16-
success,
17-
numSyncStates
18-
} SyncResult;
19-
13+
extern time_t lastSyncTimeTS; // timestamp of last successful syncTime
2014
// sets RTC, now() and time() to UTC
2115
// sets current timezone so that localtime works
22-
extern SyncResult syncTime(const char* timezone = DEFAULT_TIMEZONE);
16+
extern void syncTime(const char* timezone = DEFAULT_TIMEZONE);
2317
}; // namespace Watchy_SyncTime

0 commit comments

Comments
 (0)