Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional update policy "OnDemand" #135

Merged
merged 2 commits into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/ArduinoIoTCloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<size_t>(event)] = callback;
Expand Down
2 changes: 2 additions & 0 deletions src/ArduinoIoTCloud.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; };
Expand Down
16 changes: 15 additions & 1 deletion src/property/Property.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ Property::Property()
_last_cloud_change_timestamp(0),
_identifier(0),
_attributeIdentifier(0),
_lightPayload(false) {
_lightPayload(false),
_update_requested(false) {
}

/******************************************************************************
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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();
}

Expand Down
6 changes: 5 additions & 1 deletion src/property/Property.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ enum class Type {
};

enum class UpdatePolicy {
OnChange, TimeInterval
OnChange, TimeInterval, OnDemand
};

typedef void(*UpdateCallbackFunc)(void);
Expand All @@ -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;
Expand All @@ -153,6 +154,7 @@ class Property {
}

bool shouldBeUpdated();
void requestUpdate();
void execCallbackOnChange();
void execCallbackOnSync();
void setLastCloudChangeTimestamp(unsigned long cloudChangeTime);
Expand Down Expand Up @@ -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;
};

/******************************************************************************
Expand Down
10 changes: 10 additions & 0 deletions src/property/PropertyContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/property/PropertyContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class PropertyContainer

int appendChangedProperties(CborEncoder * arrayEncoder, bool lightPayload);
void updateTimestampOnLocallyChangedProperties();
void requestUpdateForAllProperties();



Expand Down