11#include < ESP8266HTTPClient.h>
2- #include < WiFiClientSecureBearSSL.h>
32#include < AceTime.h>
43#include " ntp_client_plus.h"
54#include " udplogger.h"
65
7- // URL for timezone API
8- const char *url = " https://js.maxmind.com/geoip/v2.1/city/me?referrer=https%3A%2F%2Fwww.maxmind.com" ;
9-
106using namespace ace_time ;
117
128static const int CACHE_SIZE = 1 ;
@@ -17,74 +13,62 @@ static ExtendedZoneManager manager(
1713 zoneProcessorCache);
1814
1915/* *
20- * @brief Get the Timezone as string from the MaxMind API
16+ * @brief Request the timezone as string from the IP- API
2117 *
2218 * @param logger UDPLogger object to log messages
2319 * @return String timezone string
2420 */
25- String getTimezoneString (UDPLogger &logger) {
26- std::unique_ptr<BearSSL::WiFiClientSecure> client (new BearSSL::WiFiClientSecure);
27- client->setInsecure ();
28- HTTPClient https;
29- logger.logString (" [HTTPS] Requesting timezone from MaxMind API" );
30- if (https.begin (*client, url)) {
31- int httpCode = https.GET ();
21+ String getTimeZoneString (UDPLogger &logger) {
22+ WiFiClient client;
23+ HTTPClient http;
24+ logger.logString (" [HTTP] Requesting timezone from IP-API" );
25+ if (http.begin (client, " http://ip-api.com/json/" )) {
26+ int httpCode = http.GET ();
3227
3328 if (httpCode > 0 ) {
34- if (httpCode == HTTP_CODE_OK) {
35- String payload = https.getString ();
36- logger.logString (" [HTTPS] Response received, length: " + String (payload.length ()));
29+ if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
30+ String payload = http.getString ();
3731
38- // Search for "time_zone" in the response
39- int index = payload.indexOf (" time_zone" );
40- if (index != -1 ) {
41- int startIndex = payload.indexOf (' "' , index + 10 ) + 1 ; // Find the first '"' after "time_zone"
42- int endIndex = payload.indexOf (' "' , startIndex); // Find the closing '"'
43- if (startIndex != -1 && endIndex != -1 ) {
44- String timeZone = payload.substring (startIndex, endIndex);
45- logger.logString (" [HTTPS] Extracted time_zone: " + timeZone);
46- return timeZone;
47- }
48- else {
49- logger.logString (" [HTTPS] Error: Could not extract time_zone value." );
50- }
51- }
52- else {
53- logger.logString (" [HTTPS] Error: time_zone not found in response." );
32+ int tzIndex = payload.indexOf (" \" timezone\" :\" " );
33+ if (tzIndex != -1 ) {
34+ int tzStart = tzIndex + 12 ;
35+ int tzEnd = payload.indexOf (" \" " , tzStart);
36+ String timezone = payload.substring (tzStart, tzEnd);
37+ logger.logString (" [HTTP] Received timezone: " + timezone);
38+ return timezone;
5439 }
5540 }
56- }
41+ }
5742 else {
58- logger. logString (" [HTTPS] request failed, error: " + https .errorToString (httpCode));
43+ Serial. printf (" [HTTP] GET... failed, error: %s \n " , http .errorToString (httpCode). c_str ( ));
5944 }
60- https .end (); // Close connection
45+ http .end (); // Close connection
6146 }
6247 else {
63- logger.logString (" [HTTPS ] Unable to connect" );
48+ logger.logString (" [HTTP ] Unable to connect" );
6449 }
65-
6650 return " " ;
6751}
6852
6953/* *
70- * @brief Update the UTC offset from the timezone string obtained from the MaxMind API
54+ * @brief Update the UTC offset from the timezone string obtained from the IP- API
7155 *
7256 * @param logger UDPLogger object to log messages
7357 * @param ntp NTPClientPlus object to set the UTC offset
7458 * @return int
7559 */
7660void updateUTCOffsetFromTimezoneAPI (UDPLogger &logger, NTPClientPlus &ntp) {
77- String timezone = getTimezoneString (logger);
61+ String timezone = getTimeZoneString (logger);
7862 if (timezone.length () > 0 ) {
7963 uint16_t zone_index = manager.indexForZoneName (timezone.c_str ());
8064 if (zone_index != ZoneManager::kInvalidIndex ) {
8165 ExtendedZone zone = manager.getZoneForIndex (zone_index);
8266 int offset = zone.stdOffset ().toMinutes ();
83- logger.logString (" [HTTPS ] Timezone offset: " + String (offset));
67+ logger.logString (" [ZoneManager ] Timezone offset (min) : " + String (offset));
8468 ntp.setUTCOffset (offset);
8569 }
8670 else {
87- logger.logString (" [HTTPS ] Error: Could not find time_zone value in DB. Use hardcoded UTC offset." );
71+ logger.logString (" [ZoneManager ] Error: Could not find time_zone value in DB. Use hardcoded UTC offset." );
8872 }
8973 }
9074}
0 commit comments