From 945f3e474f9f1baeb60494eb3d6e602a7d55dd78 Mon Sep 17 00:00:00 2001 From: jgstroud Date: Sun, 27 Oct 2024 22:08:22 -0500 Subject: [PATCH] Add a function to auto lookup our timezone using our public ip autoset the time based on local timezone --- src/ratgdo.cpp | 5 +++++ src/utilities.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++---- src/utilities.h | 1 + 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/ratgdo.cpp b/src/ratgdo.cpp index a319001..41a8954 100644 --- a/src/ratgdo.cpp +++ b/src/ratgdo.cpp @@ -249,6 +249,7 @@ void obstruction_timer() void service_timer_loop() { + static unsigned long tz_update = 10000; loop_id = LOOP_TIMER; // Service the Obstruction Timer obstruction_timer(); @@ -256,6 +257,10 @@ void service_timer_loop() unsigned long current_millis = millis(); #ifdef NTP_CLIENT + if (enableNTP && !clockSet && (current_millis > tz_update)) { + tz_update = current_millis + 10000; + update_timezone(); + } if (enableNTP && clockSet && lastRebootAt == 0) { lastRebootAt = time(NULL) - (current_millis / 1000); diff --git a/src/utilities.cpp b/src/utilities.cpp index 5245dc8..9c80379 100644 --- a/src/utilities.cpp +++ b/src/utilities.cpp @@ -6,6 +6,7 @@ #include "LittleFS.h" #include "comms.h" #include +#include #ifdef LEGACY_SETTINGS_MIGRATION // Filenames for legacy user configuation, replaced by single file. @@ -51,6 +52,51 @@ bool enableNTP = false; unsigned long lastRebootAt = 0; int32_t savedDoorUpdateAt = 0; +bool get_tz() +{ + WiFiClient client; + HTTPClient http; + bool success = false; + + if (http.begin(client, "http://ip-api.com/csv/?fields=timezone,offset")) + { // HTTP + + // start connection and send HTTP header + int httpCode = http.GET(); + + // httpCode will be negative on error + if (httpCode > 0) + { + if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) + { + String payload = http.getString(); + char * timezone = (char*)payload.c_str(); + char * ch = strchr(timezone, ','); + *ch = '\0'; + ch++; + int offset = atoi(ch); + offset /= -3600; + sprintf(userConfig->timeZone, "%s;UTC%d", timezone, offset); + RINFO("Setting timezone to %s", userConfig->timeZone); + success = true; + } + } + http.end(); + } else { + success = false; + } + return success; +} + +void update_timezone() +{ + if (get_tz()) { + char *tz = strchr(userConfig->timeZone, ';'); + // semicolon may separate continent/city from posix TZ string + configTime((tz) ? tz + 1 : userConfig->timeZone, NTP_SERVER); + } +} + void time_is_set(bool from_sntp) { RINFO("Clock set from NTP server: %d", from_sntp ? 1 : 0); @@ -93,7 +139,7 @@ char *timeString(time_t reqTime, bool syslog) else { // Print format example: 27-Oct-2024 11:16:18 EDT - strftime(tBuffer, sizeof(tBuffer), "%d-%b-%Y %H:%M:%S %Z", &tmTime); + strftime(tBuffer, sizeof(tBuffer), "%d-%b-%Y %H:%M:%S", &tmTime); } } return tBuffer; @@ -239,9 +285,6 @@ void load_all_config_settings() if (enableNTP) { settimeofday_cb(time_is_set); - char *tz = strchr(userConfig->timeZone, ';'); - // semicolon may separate continent/city from posix TZ string - configTime((tz) ? tz + 1 : userConfig->timeZone, NTP_SERVER); } #endif syslogEn = userConfig->syslogEn; diff --git a/src/utilities.h b/src/utilities.h index 84f510b..b63a2ad 100644 --- a/src/utilities.h +++ b/src/utilities.h @@ -117,5 +117,6 @@ bool read_config_from_file(); void write_config_to_file(); void delete_file(const char *filename); +void update_timezone(); #endif