Skip to content

Commit 32292b5

Browse files
committed
Add properties and logic to handle timezone and dst changes
1 parent a3b2b67 commit 32292b5

File tree

5 files changed

+41
-3
lines changed

5 files changed

+41
-3
lines changed

src/ArduinoIoTCloud.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ class ArduinoIoTCloudClass
9797

9898
inline ConnectionHandler * getConnection() { return _connection; }
9999

100-
inline unsigned long getInternalTime() { return _time_service.getTime(); }
100+
inline unsigned long getInternalTime() { return _time_service.getTime(); }
101+
inline unsigned long getLocalTime() { return _time_service.getLocalTime(); }
102+
inline void updateInternalTimezoneInfo() { _time_service.setTimeZoneData(_tz_offset, _tz_dst_until); }
101103

102104
void addCallback(ArduinoIoTCloudEvent const event, OnCloudEventCallback callback);
103105

@@ -146,6 +148,8 @@ class ArduinoIoTCloudClass
146148
ConnectionHandler * _connection = nullptr;
147149
PropertyContainer _property_container;
148150
TimeService _time_service;
151+
int _tz_offset = 0;
152+
unsigned int _tz_dst_until = 0;
149153

150154
void execCloudEventCallback(ArduinoIoTCloudEvent const event);
151155

src/ArduinoIoTCloudTCP.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ extern "C" unsigned long getTime()
6565
return ArduinoCloud.getInternalTime();
6666
}
6767

68+
extern "C" void updateTimezoneInfo()
69+
{
70+
ArduinoCloud.updateInternalTimezoneInfo();
71+
}
72+
6873
/******************************************************************************
6974
CTOR/DTOR
7075
******************************************************************************/
@@ -244,6 +249,9 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
244249
addPropertyReal(_ota_req, "OTA_REQ", Permission::ReadWrite).onSync(CLOUD_WINS);
245250
#endif /* OTA_ENABLED */
246251

252+
addPropertyReal(_tz_offset, "tz_offset", Permission::ReadWrite).onSync(CLOUD_WINS).onUpdate(updateTimezoneInfo);
253+
addPropertyReal(_tz_dst_until, "tz_dst_until", Permission::ReadWrite).onSync(CLOUD_WINS).onUpdate(updateTimezoneInfo);
254+
247255
#if OTA_STORAGE_PORTENTA_QSPI
248256
#define BOOTLOADER_ADDR (0x8000000)
249257
uint32_t bootloader_data_offset = 0x1F000;
@@ -528,7 +536,12 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
528536
*/
529537
sendPropertiesToCloud();
530538

531-
return State::Connected;
539+
unsigned long const internal_posix_time = _time_service.getTime();
540+
if(internal_posix_time < _tz_dst_until) {
541+
return State::Connected;
542+
} else {
543+
return State::RequestLastValues;
544+
}
532545
}
533546
}
534547

@@ -556,6 +569,7 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
556569
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] last values received", __FUNCTION__, millis());
557570
CBORDecoder::decode(_property_container, (uint8_t*)bytes, length, true);
558571
sendPropertiesToCloud();
572+
_time_service.setTimeZoneData(_tz_offset, _tz_dst_until);
559573
execCloudEventCallback(ArduinoIoTCloudEvent::SYNC);
560574
_last_sync_request_cnt = 0;
561575
_last_sync_request_tick = 0;

src/property/types/CloudSchedule.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class Schedule : public TimeService {
7474

7575
bool isActive() {
7676

77-
unsigned int now = getTime();
77+
unsigned int now = getLocalTime();
7878
if(checkSchedulePeriod(now, frm, to)) {
7979
/* We are in the schedule range */
8080

src/utility/time/TimeService.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ unsigned long TimeService::getTime()
9191
#endif
9292
}
9393

94+
void TimeService::setTimeZoneData(int offset, unsigned int dst_until)
95+
{
96+
_timezone_offset = offset;
97+
_timezone_dst_until = dst_until;
98+
}
99+
100+
unsigned long TimeService::getLocalTime()
101+
{
102+
unsigned long utc = getTime();
103+
return utc + _timezone_offset;
104+
}
105+
94106
/**************************************************************************************
95107
* PRIVATE MEMBER FUNCTIONS
96108
**************************************************************************************/

src/utility/time/TimeService.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include <Arduino_ConnectionHandler.h>
2626

27+
2728
#ifdef ARDUINO_ARCH_SAMD
2829
#include <RTCZero.h>
2930
#endif
@@ -46,6 +47,8 @@ class TimeService
4647

4748
void begin (ConnectionHandler * con_hdl);
4849
unsigned long getTime();
50+
unsigned long getLocalTime();
51+
void setTimeZoneData(int offset, unsigned int valid_until);
4952

5053
private:
5154

@@ -55,8 +58,13 @@ class TimeService
5558
#endif
5659

5760
unsigned long getRemoteTime();
61+
5862
static bool isTimeValid(unsigned long const time);
5963

64+
private:
65+
unsigned long _timezone_offset;
66+
unsigned long _timezone_dst_until;
67+
6068
};
6169

6270
#endif /* ARDUINO_IOT_CLOUD_TIME_SERVICE_H_ */

0 commit comments

Comments
 (0)