From a30135f3a23f6343bdb0e656ef837882d20ac0e7 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Mon, 8 Jun 2020 12:28:00 +0200 Subject: [PATCH 1/2] Add UpdatePolicy::OnDemand which allows to transmit properties only when the tranmission is explicitly requested --- src/property/Property.cpp | 16 +++++++++++++++- src/property/Property.h | 6 +++++- src/property/PropertyContainer.cpp | 10 ++++++++++ src/property/PropertyContainer.h | 1 + 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/property/Property.cpp b/src/property/Property.cpp index 52a1d2a58..72fdddf2f 100644 --- a/src/property/Property.cpp +++ b/src/property/Property.cpp @@ -44,7 +44,8 @@ Property::Property() _last_cloud_change_timestamp(0), _identifier(0), _attributeIdentifier(0), - _lightPayload(false) { + _lightPayload(false), + _update_requested(false) { } /****************************************************************************** @@ -79,6 +80,11 @@ Property & Property::publishEvery(unsigned long const seconds) { return (*this); } +Property & Property::publishOnDemand() { + _update_policy = UpdatePolicy::OnDemand; + return (*this); +} + bool Property::shouldBeUpdated() { if (!_has_been_updated_once) { return true; @@ -93,11 +99,18 @@ bool Property::shouldBeUpdated() { return (isDifferentFromCloud() && ((millis() - _last_updated_millis) >= (_min_time_between_updates_millis))); } else if (_update_policy == UpdatePolicy::TimeInterval) { return ((millis() - _last_updated_millis) >= _update_interval_millis); + } else if (_update_policy == UpdatePolicy::OnDemand) { + return _update_requested; } else { return false; } } +void Property::requestUpdate() +{ + _update_requested = true; +} + void Property::execCallbackOnChange() { if (_update_callback_func != NULL) { _update_callback_func(); @@ -119,6 +132,7 @@ void Property::append(CborEncoder *encoder, bool lightPayload) { appendAttributesToCloudReal(encoder); fromLocalToCloud(); _has_been_updated_once = true; + _update_requested = false; _last_updated_millis = millis(); } diff --git a/src/property/Property.h b/src/property/Property.h index c9a936b99..a5da5a1f1 100644 --- a/src/property/Property.h +++ b/src/property/Property.h @@ -117,7 +117,7 @@ enum class Type { }; enum class UpdatePolicy { - OnChange, TimeInterval + OnChange, TimeInterval, OnDemand }; typedef void(*UpdateCallbackFunc)(void); @@ -138,6 +138,7 @@ class Property { Property & onSync(SyncCallbackFunc func); Property & publishOnChange(float const min_delta_property, unsigned long const min_time_between_updates_millis = 0); Property & publishEvery(unsigned long const seconds); + Property & publishOnDemand(); inline String name() const { return _name; @@ -153,6 +154,7 @@ class Property { } bool shouldBeUpdated(); + void requestUpdate(); void execCallbackOnChange(); void execCallbackOnSync(); void setLastCloudChangeTimestamp(unsigned long cloudChangeTime); @@ -211,6 +213,8 @@ class Property { int _attributeIdentifier; /* Indicates if the property shall be encoded using the identifier instead of the name */ bool _lightPayload; + /* Indicates whether a property update has been requested in case of the OnDemand update policy. */ + bool _update_requested; }; /****************************************************************************** diff --git a/src/property/PropertyContainer.cpp b/src/property/PropertyContainer.cpp index 4067ee920..17b1fbfb3 100644 --- a/src/property/PropertyContainer.cpp +++ b/src/property/PropertyContainer.cpp @@ -112,6 +112,16 @@ int PropertyContainer::appendChangedProperties(CborEncoder * arrayEncoder, bool return appendedProperties; } +void PropertyContainer::requestUpdateForAllProperties() +{ + std::for_each(_property_list.begin(), + _property_list.end(), + [](Property * p) + { + p->requestUpdate(); + }); +} + void PropertyContainer::updateTimestampOnLocallyChangedProperties() { /* This function updates the timestamps on the primitive properties diff --git a/src/property/PropertyContainer.h b/src/property/PropertyContainer.h index 2e23f5ff6..ae3aaa4b2 100644 --- a/src/property/PropertyContainer.h +++ b/src/property/PropertyContainer.h @@ -52,6 +52,7 @@ class PropertyContainer int appendChangedProperties(CborEncoder * arrayEncoder, bool lightPayload); void updateTimestampOnLocallyChangedProperties(); + void requestUpdateForAllProperties(); From 5b00945615dc5ff6fb830988c1fc1da2f8f81af4 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Mon, 8 Jun 2020 12:29:07 +0200 Subject: [PATCH 2/2] When calling ArduinoCloud.push() then the _update_requested flag of all properties is set leading to encoding and a transmission to the cloud on the next call to update --- src/ArduinoIoTCloud.cpp | 5 +++++ src/ArduinoIoTCloud.h | 2 ++ 2 files changed, 7 insertions(+) diff --git a/src/ArduinoIoTCloud.cpp b/src/ArduinoIoTCloud.cpp index f1aaf91e9..41bdfbc32 100644 --- a/src/ArduinoIoTCloud.cpp +++ b/src/ArduinoIoTCloud.cpp @@ -25,6 +25,11 @@ * PUBLIC MEMBER FUNCTIONS ******************************************************************************/ +void ArduinoIoTCloudClass::push() +{ + _property_container.requestUpdateForAllProperties(); +} + void ArduinoIoTCloudClass::addCallback(ArduinoIoTCloudEvent const event, OnCloudEventCallback callback) { _cloud_event_callback[static_cast(event)] = callback; diff --git a/src/ArduinoIoTCloud.h b/src/ArduinoIoTCloud.h index 2f480d5df..c0ca4e5ed 100644 --- a/src/ArduinoIoTCloud.h +++ b/src/ArduinoIoTCloud.h @@ -87,6 +87,8 @@ class ArduinoIoTCloudClass virtual int connected () = 0; virtual void printDebugInfo() = 0; + void push(); + inline void setThingId (String const thing_id) { _thing_id = thing_id; }; inline String & getThingId () { return _thing_id; }; inline void setDeviceId(String const device_id) { _device_id = device_id; };