From 577f23aa66e0e8b8029a5d6f477ae7d56f591684 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 3 Oct 2023 09:58:34 +0200 Subject: [PATCH 1/4] CloudString: add getValue() --- src/property/types/CloudString.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/property/types/CloudString.h b/src/property/types/CloudString.h index f884720c0..712c893b9 100644 --- a/src/property/types/CloudString.h +++ b/src/property/types/CloudString.h @@ -52,6 +52,9 @@ class CloudString : public Property { virtual bool isDifferentFromCloud() { return _value != _cloud_value; } + virtual String & getValue() { + return _value; + } virtual void fromCloudToLocal() { _value = _cloud_value; } From 19176392365e72c653c2292c6e3f2ce08bb84453 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 3 Oct 2023 10:03:18 +0200 Subject: [PATCH 2/4] Allow updating cloud value of read-only properties --- src/property/PropertyContainer.cpp | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/property/PropertyContainer.cpp b/src/property/PropertyContainer.cpp index 833250f02..22f4f198e 100644 --- a/src/property/PropertyContainer.cpp +++ b/src/property/PropertyContainer.cpp @@ -114,16 +114,22 @@ void updateProperty(PropertyContainer & prop_cont, String propertyName, unsigned { Property * property = getProperty(prop_cont, propertyName); - if (property && property->isWriteableByCloud()) + if (property ) { + /* Update _cloud_value */ property->setLastCloudChangeTimestamp(cloudChangeEventTime); property->setAttributesFromCloud(map_data_list); - if (is_sync_message) { - property->execCallbackOnSync(); - } else { - property->fromCloudToLocal(); - property->execCallbackOnChange(); - property->provideEcho(); + + /* Update _value */ + if (property->isWriteableByCloud()) + { + if (is_sync_message) { + property->execCallbackOnSync(); + } else { + property->fromCloudToLocal(); + property->execCallbackOnChange(); + property->provideEcho(); + } } } } From 6de5655a2d11224557dd9f890e3997211ce53d82 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 3 Oct 2023 10:07:31 +0200 Subject: [PATCH 3/4] Remove public functions to handle thing_id discovery protocol --- src/ArduinoIoTCloud.h | 10 ++-------- src/ArduinoIoTCloudTCP.cpp | 23 +++++++++-------------- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/src/ArduinoIoTCloud.h b/src/ArduinoIoTCloud.h index bce662eb6..0b125822b 100644 --- a/src/ArduinoIoTCloud.h +++ b/src/ArduinoIoTCloud.h @@ -92,16 +92,10 @@ class ArduinoIoTCloudClass bool setTimestamp(String const & prop_name, unsigned long const timestamp); inline void setThingId (String const thing_id) { _thing_id = thing_id; }; - inline String & getThingId () { return _thing_id; }; + inline String & getThingId () { return _thing_id.getValue(); }; inline void setDeviceId(String const device_id) { _device_id = device_id; }; inline String & getDeviceId() { return _device_id; }; - inline void setThingIdOutdatedFlag() { _thing_id_outdated = true ; } - inline void clrThingIdOutdatedFlag() { _thing_id_outdated = false ; } - inline bool getThingIdOutdatedFlag() { return _thing_id_outdated; } - - inline bool deviceNotAttached() { return _thing_id == ""; } - inline ConnectionHandler * getConnection() { return _connection; } inline unsigned long getInternalTime() { return _time_service.getTime(); } @@ -159,7 +153,7 @@ class ArduinoIoTCloudClass TimeServiceClass & _time_service; int _tz_offset; unsigned int _tz_dst_until; - String _thing_id; + CloudString _thing_id; String _lib_version; void execCloudEventCallback(ArduinoIoTCloudEvent const event); diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 1efbf3f33..f583703f7 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -65,11 +65,6 @@ void updateTimezoneInfo() ArduinoCloud.updateInternalTimezoneInfo(); } -void setThingIdOutdated() -{ - ArduinoCloud.setThingIdOutdatedFlag(); -} - /****************************************************************************** CTOR/DTOR ******************************************************************************/ @@ -201,8 +196,8 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress, p = new CloudWrapperBool(_ota_req); addPropertyToContainer(_device_property_container, *p, "OTA_REQ", Permission::ReadWrite, -1); #endif /* OTA_ENABLED */ - p = new CloudWrapperString(_thing_id); - addPropertyToContainer(_device_property_container, *p, "thing_id", Permission::ReadWrite, -1).onUpdate(setThingIdOutdated); + + addPropertyToContainer(_device_property_container, _thing_id, "thing_id", Permission::Read, -1); 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); @@ -386,7 +381,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_WaitDeviceConfig() return State::Disconnect; } - if (getThingIdOutdatedFlag()) + if(_thing_id.isDifferentFromCloud()) { return State::CheckDeviceConfig; } @@ -422,7 +417,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_CheckDeviceConfig() updateThingTopics(); - if (deviceNotAttached()) + if (_thing_id.getValue().length() == 0) { /* Configuration received but device not attached. Wait: 40s */ unsigned long attach_retry_delay = (1 << _last_device_attach_cnt) * AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms; @@ -445,7 +440,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeThingTopics() return State::Disconnect; } - if (getThingIdOutdatedFlag()) + if (_thing_id.isDifferentFromCloud()) { return State::CheckDeviceConfig; } @@ -501,7 +496,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues() return State::Disconnect; } - if (getThingIdOutdatedFlag()) + if (_thing_id.isDifferentFromCloud()) { return State::CheckDeviceConfig; } @@ -544,7 +539,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected() /* We are connected so let's to our stuff here. */ else { - if (getThingIdOutdatedFlag()) + if (_thing_id.isDifferentFromCloud()) { return State::CheckDeviceConfig; } @@ -738,12 +733,12 @@ int ArduinoIoTCloudTCP::write(String const topic, byte const data[], int const l void ArduinoIoTCloudTCP::updateThingTopics() { + _thing_id.fromCloudToLocal(); + _shadowTopicOut = getTopic_shadowout(); _shadowTopicIn = getTopic_shadowin(); _dataTopicOut = getTopic_dataout(); _dataTopicIn = getTopic_datain(); - - clrThingIdOutdatedFlag(); } /****************************************************************************** From c15c14590354664d860d5207d2ca843486981e13 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 3 Oct 2023 15:02:55 +0200 Subject: [PATCH 4/4] Remove public functions to handle timezone changes --- src/ArduinoIoTCloud.h | 4 ++-- src/ArduinoIoTCloudTCP.cpp | 25 ++++++++++++++++--------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/ArduinoIoTCloud.h b/src/ArduinoIoTCloud.h index 0b125822b..3fe2a8eaf 100644 --- a/src/ArduinoIoTCloud.h +++ b/src/ArduinoIoTCloud.h @@ -151,8 +151,8 @@ class ArduinoIoTCloudClass PropertyContainer _thing_property_container; unsigned int _last_checked_property_index; TimeServiceClass & _time_service; - int _tz_offset; - unsigned int _tz_dst_until; + CloudInt _tz_offset; + CloudUnsignedInt _tz_dst_until; CloudString _thing_id; String _lib_version; diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index f583703f7..ac2c1b9c4 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -60,11 +60,6 @@ unsigned long getTime() return ArduinoCloud.getInternalTime(); } -void updateTimezoneInfo() -{ - ArduinoCloud.updateInternalTimezoneInfo(); -} - /****************************************************************************** CTOR/DTOR ******************************************************************************/ @@ -199,8 +194,8 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress, addPropertyToContainer(_device_property_container, _thing_id, "thing_id", Permission::Read, -1); - 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); + addPropertyToContainer(_thing_property_container, _tz_offset, "tz_offset", Permission::Read, -1); + addPropertyToContainer(_thing_property_container, _tz_dst_until, "tz_dst_until", Permission::Read, -1); #if OTA_ENABLED _ota_cap = OTA::isCapable(); @@ -591,13 +586,26 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected() #endif /* OTA_ENABLED */ + /* Configure Time service with timezone data: + * _tz_offset [offset + dst] + * _tz_dst_until [posix timestamp until _tz_offset is valid] + */ + if (_tz_offset.isDifferentFromCloud() || _tz_dst_until.isDifferentFromCloud()) { + _tz_offset.fromCloudToLocal(); + _tz_dst_until.fromCloudToLocal(); + _time_service.setTimeZoneData(_tz_offset, _tz_dst_until); + } + /* Check if any properties need encoding and send them to * the cloud if necessary. */ sendThingPropertiesToCloud(); + /* Check if stored timezone data needs to be updated and + * if data is expired issue a LastValue request to the cloud. + */ 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; @@ -645,7 +653,6 @@ void ArduinoIoTCloudTCP::handleMessage(int length) { DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] last values received", __FUNCTION__, millis()); CBORDecoder::decode(_thing_property_container, (uint8_t*)bytes, length, true); - _time_service.setTimeZoneData(_tz_offset, _tz_dst_until); execCloudEventCallback(ArduinoIoTCloudEvent::SYNC); _last_sync_request_cnt = 0; _last_sync_request_tick = 0;