Skip to content

Commit

Permalink
Add a function to auto lookup our timezone using our public ip
Browse files Browse the repository at this point in the history
autoset the time based on local timezone
  • Loading branch information
jgstroud committed Oct 28, 2024
1 parent 6e6aee8 commit 945f3e4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/ratgdo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,18 @@ void obstruction_timer()

void service_timer_loop()
{
static unsigned long tz_update = 10000;
loop_id = LOOP_TIMER;
// Service the Obstruction Timer
obstruction_timer();

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);
Expand Down
51 changes: 47 additions & 4 deletions src/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "LittleFS.h"
#include "comms.h"
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

#ifdef LEGACY_SETTINGS_MIGRATION
// Filenames for legacy user configuation, replaced by single file.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,6 @@ bool read_config_from_file();
void write_config_to_file();

void delete_file(const char *filename);
void update_timezone();

#endif

0 comments on commit 945f3e4

Please sign in to comment.