Skip to content

Commit

Permalink
Remove public functions to handle timezone changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pennam committed Mar 28, 2024
1 parent af472ca commit 896249b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
2 changes: 0 additions & 2 deletions src/ArduinoIoTCloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ ArduinoIoTCloudClass::ArduinoIoTCloudClass()
: _connection{nullptr}
, _last_checked_property_index{0}
, _time_service(TimeService)
, _tz_offset{0}
, _tz_dst_until{0}
, _thing_id{""}
, _thing_id_property{nullptr}
, _lib_version{AIOT_CONFIG_LIB_VERSION}
Expand Down
3 changes: 0 additions & 3 deletions src/ArduinoIoTCloud.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ class ArduinoIoTCloudClass

inline unsigned long getInternalTime() { return _time_service.getTime(); }
inline unsigned long getLocalTime() { return _time_service.getLocalTime(); }
inline void updateInternalTimezoneInfo() { _time_service.setTimeZoneData(_tz_offset, _tz_dst_until); }

void addCallback(ArduinoIoTCloudEvent const event, OnCloudEventCallback callback);

Expand Down Expand Up @@ -151,8 +150,6 @@ class ArduinoIoTCloudClass
PropertyContainer _thing_property_container;
unsigned int _last_checked_property_index;
TimeServiceClass & _time_service;
int _tz_offset;
unsigned int _tz_dst_until;
String _thing_id;
Property * _thing_id_property;
String _lib_version;
Expand Down
28 changes: 19 additions & 9 deletions src/ArduinoIoTCloudTCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,16 @@ unsigned long getTime()
return ArduinoCloud.getInternalTime();
}

void updateTimezoneInfo()
{
ArduinoCloud.updateInternalTimezoneInfo();
}

/******************************************************************************
CTOR/DTOR
******************************************************************************/

ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
: _state{State::ConnectPhy}
, _tz_offset{0}
, _tz_offset_property{nullptr}
, _tz_dst_until{0}
, _tz_dst_until_property{nullptr}
, _next_connection_attempt_tick{0}
, _last_connection_attempt_cnt{0}
, _next_device_subscribe_attempt_tick{0}
Expand Down Expand Up @@ -214,9 +213,10 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
#endif /* OTA_ENABLED */
p = new CloudWrapperString(_thing_id);
_thing_id_property = &addPropertyToContainer(_device_property_container, *p, "thing_id", Permission::ReadWrite, -1).writeOnDemand();

addPropertyReal(_tz_offset, "tz_offset", Permission::ReadWrite).onSync(CLOUD_WINS).onUpdate(updateTimezoneInfo);
addPropertyReal(_tz_dst_until, "tz_dst_until", Permission::ReadWrite).onSync(CLOUD_WINS).onUpdate(updateTimezoneInfo);
p = new CloudWrapperInt(_tz_offset);
_tz_offset_property = &addPropertyToContainer(_thing_property_container, *p, "tz_offset", Permission::ReadWrite, -1).writeOnDemand();
p = new CloudWrapperUnsignedInt(_tz_dst_until);
_tz_dst_until_property = &addPropertyToContainer(_thing_property_container, *p, "tz_dst_until", Permission::ReadWrite, -1).writeOnDemand();

#if OTA_ENABLED
_ota_cap = OTA::isCapable();
Expand Down Expand Up @@ -584,13 +584,23 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
_mqtt_data_request_retransmit = false;
}

/* Configure Time service with timezone data:
* _tz_offset [offset + dst]
* _tz_dst_until [posix timestamp until _tz_offset is valid]
*/
if (_tz_offset_property->isDifferentFromCloud() || _tz_dst_until_property->isDifferentFromCloud()) {
_tz_offset_property->fromCloudToLocal();
_tz_dst_until_property->fromCloudToLocal();
_time_service.setTimeZoneData(_tz_offset, _tz_dst_until);
}

/* Check if any properties need encoding and send them to
* the cloud if necessary.
*/
sendThingPropertiesToCloud();

unsigned long const internal_posix_time = _time_service.getTime();
if(internal_posix_time < _tz_dst_until) {
if (internal_posix_time < _tz_dst_until) {
return State::Connected;
} else {
return State::RequestLastValues;
Expand Down
5 changes: 5 additions & 0 deletions src/ArduinoIoTCloudTCP.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass

State _state;

int _tz_offset;
Property * _tz_offset_property;
unsigned int _tz_dst_until;
Property * _tz_dst_until_property;

unsigned long _next_connection_attempt_tick;
unsigned int _last_connection_attempt_cnt;
unsigned long _next_device_subscribe_attempt_tick;
Expand Down

0 comments on commit 896249b

Please sign in to comment.