Skip to content

Commit 0b83429

Browse files
committed
Fix getTime management
1 parent ddd80f7 commit 0b83429

File tree

5 files changed

+20
-13
lines changed

5 files changed

+20
-13
lines changed

src/ArduinoIoTCloud.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,7 @@ const static int thingIdSlot = 12;
3333
static ConnectionManager *getTimeConnection = NULL;
3434

3535
static unsigned long getTime() {
36-
if (!getTimeConnection) return 0;
37-
unsigned long time = getTimeConnection->getTime();
38-
if (!NTPUtils::isTimeValid(time)) {
39-
debugMessage("Bogus NTP time from API, fallback to UDP method", 0);
40-
time = NTPUtils(getTimeConnection->getUDP()).getTime();
41-
}
42-
return time;
36+
return getTimeConnection->getTime();
4337
}
4438

4539
static unsigned long getTimestamp() {

src/ConnectionManager.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ class ConnectionManager {
3838
public:
3939
virtual void init() = 0;
4040
virtual void check() = 0;
41-
virtual unsigned long getTime() = 0;
41+
virtual unsigned long getTime();
4242
virtual Client &getClient();
43-
virtual UDP &getUDP();
43+
virtual UDP &getUDP() = 0;
4444

4545
virtual NetworkConnectionState getStatus() { return netConnectionState; }
4646

@@ -96,6 +96,10 @@ inline void debugMessage(char *_msg, int _debugLevel, bool _timestamp = true, bo
9696
}
9797
}
9898

99+
inline unsigned long ConnectionManager::getTime() {
100+
return NTPUtils(this->getUDP()).getTime();
101+
}
102+
99103
inline void setDebugMessageLevel(int _debugLevel){
100104
debugMessageLevel = _debugLevel;
101105
}

src/GSMConnectionManager.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class GSMConnectionManager : public ConnectionManager {
3939
const int CHECK_INTERVAL_IDLE = 100;
4040
const int CHECK_INTERVAL_INIT = 100;
4141
const int CHECK_INTERVAL_CONNECTING = 500;
42-
const int CHECK_INTERVAL_GETTIME = 666;
42+
const int CHECK_INTERVAL_GETTIME = 10;
4343
const int CHECK_INTERVAL_CONNECTED = 10000;
4444
const int CHECK_INTERVAL_RETRYING = 5000;
4545
const int CHECK_INTERVAL_DISCONNECTED = 1000;
@@ -67,7 +67,12 @@ GSMConnectionManager::GSMConnectionManager(const char *pin, const char *apn, con
6767
}
6868

6969
unsigned long GSMConnectionManager::getTime() {
70-
return gsmAccess.getTime();
70+
unsigned long time = ConnectionManager::getTime() {
71+
if (!NTPUtils::isTimeValid(time)) {
72+
debugMessage("Bogus NTP time from API, fallback to UDP method", 0);
73+
time = gsmAccess.getTime();
74+
}
75+
return time;
7176
}
7277

7378
void GSMConnectionManager::init() {

src/WiFiConnectionManager.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ WiFiConnectionManager::WiFiConnectionManager(const char *ssid, const char *pass)
6363
}
6464

6565
unsigned long WiFiConnectionManager::getTime() {
66-
return WiFi.getTime();
66+
unsigned long time = ConnectionManager::getTime();
67+
if (!NTPUtils::isTimeValid(time)) {
68+
debugMessage("Bogus NTP time from API, fallback to UDP method", 0);
69+
time = WiFi.getTime();
70+
}
71+
return time;
6772
}
6873

6974
void WiFiConnectionManager::init() {

src/utility/NTPUtils.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ static time_t cvt_TIME(char const *time) {
2929
NTPUtils::NTPUtils(UDP& Udp) : Udp(Udp) {}
3030

3131
bool NTPUtils::isTimeValid(unsigned long time) {
32-
Serial.println("Compile time: " + String(cvt_TIME(__DATE__)));
3332
return (time > cvt_TIME(__DATE__));
3433
}
3534

0 commit comments

Comments
 (0)