Skip to content

Commit

Permalink
geolocation timezone works.
Browse files Browse the repository at this point in the history
  • Loading branch information
merlokk committed May 17, 2017
1 parent f222cb7 commit 70af813
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
23 changes: 15 additions & 8 deletions lib/autotimezone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ void AutoTimeZone::begin(xLogger *_logger) {
state = tzWait;
}

// 2017-05-17T14:32:25Z
String GetJSDate(time_t dt) {
String s = IntToStr(year(dt), 4) + '-' + IntToStr(month(dt)) + '-' + IntToStr(day(dt)) + 'T' +
IntToStr(hour(dt)) + ':' + IntToStr(minute(dt)) + ':' + IntToStr(second(dt)) + 'Z';
return s;
}

void AutoTimeZone::handle() {
if (state == tzInit)
return;
Expand All @@ -32,7 +39,7 @@ void AutoTimeZone::handle() {
// file found at server
if(httpCode == HTTP_CODE_OK) {
if (ProcessStage1(http.getString())) {
state = tzStage2Send;
state = tzWaitStage2;
} else {
ttimer.Reset(TID_ERROR_TIMEOUT);
state = tzWait;
Expand All @@ -47,11 +54,7 @@ void AutoTimeZone::handle() {
}

case tzWaitStage2:
if (timeStatus() != timeSet) {
break;
}

if (ttimer.isArmed(TID_ERROR_TIMEOUT)) {
if (timeStatus() == timeSet && ttimer.isArmed(TID_ERROR_TIMEOUT)) {
state = tzStage2Send;
}

Expand All @@ -60,9 +63,9 @@ void AutoTimeZone::handle() {
case tzStage2Send:{
HTTPClient http;
String ia = IanaTimezone;
DEBUG_PRINTLN(SF("TZ:HTTP GET stage 2. iana:") + ia);
DEBUG_PRINTLN(SF("TZ:HTTP GET stage 2. iana:") + ia + SF(" now:") + GetJSDate(now()));
ia.replace("/", "%2F");
http.begin(SF("http://api.teleport.org/api/timezones/iana:") + ia + SF("/offsets/?date=2017-05-17T14:32:25Z"));
http.begin(SF("http://api.teleport.org/api/timezones/iana:") + ia + SF("/offsets/?date=") + GetJSDate(now()));
int httpCode = http.GET();
if(httpCode > 0) {
DEBUG_PRINTLN(SF("TZ:HTTP GET ok: ") + String(httpCode));
Expand Down Expand Up @@ -114,6 +117,10 @@ void AutoTimeZone::handle() {
String s;
GetStr(s);
DEBUG_PRINTLN(SF("TZ: ok. ") + s);

// all is OK
GotData = true;

state = tzSleep;
break;
}
Expand Down
1 change: 1 addition & 0 deletions lib/autotimezone.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <NtpClientLib.h> // https://github.com/gmag11/NtpClient
#include <xlogger.h> // logger https://github.com/merlokk/xlogger
#include <pitimer.h>
#include <etools.h>

// stage 1
// http://ip-api.com/json
Expand Down
7 changes: 5 additions & 2 deletions lib/az7798.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,15 @@ void az7798::SendCommand(AZProcessCommands cmd) {
case acSetDateTime:
// number of seconds from 1 jan 2000.
// ">"
if (timeStatus() == timeSet) {
#ifdef AUTO_TIMEZONE
if (timeStatus() == timeSet && atz.GotData) {
time_t dt = now();

#ifdef AUTO_TIMEZONE
dt = dt + (atz.getCurrentOffset() * 60); // offset in minutes
#else
if (timeStatus() == timeSet) {
time_t dt = now();

dt = myTZ.toLocal(dt);
// dt = dt + (TIMEZONE * 60 * 60);
#endif
Expand Down
9 changes: 9 additions & 0 deletions lib/etools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ void BufferToString(String & str, const char* buf, int len) {
}
}

String IntToStr(int i, int digits) {
String s = String(i);
if (s.length() < digits) {
for(int k = 0; k < digits - s.length(); k++)
s = '0' + s;
}
return s;
}

// https://www.speedguide.net/faq/how-does-rssi-dbm-relate-to-signal-quality-percent-439
// rssi -100 = 0%; -50=100%
int RSSItoQuality(const int RSSI) {
Expand Down
1 change: 1 addition & 0 deletions lib/etools.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ union dataDouble

// String
void BufferToString(String & str, const char* buf, int len);
String IntToStr(int i, int digits = 2);

// tools
int RSSItoQuality(const int RSSI);
Expand Down

0 comments on commit 70af813

Please sign in to comment.