Skip to content

Commit 419fd32

Browse files
authored
Merge pull request #135 from arduino-libraries/update-on-demand
Additional update policy "OnDemand"
2 parents 39c88ab + 5b00945 commit 419fd32

File tree

6 files changed

+38
-2
lines changed

6 files changed

+38
-2
lines changed

src/ArduinoIoTCloud.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
* PUBLIC MEMBER FUNCTIONS
2626
******************************************************************************/
2727

28+
void ArduinoIoTCloudClass::push()
29+
{
30+
_property_container.requestUpdateForAllProperties();
31+
}
32+
2833
void ArduinoIoTCloudClass::addCallback(ArduinoIoTCloudEvent const event, OnCloudEventCallback callback)
2934
{
3035
_cloud_event_callback[static_cast<size_t>(event)] = callback;

src/ArduinoIoTCloud.h

+2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ class ArduinoIoTCloudClass
8787
virtual int connected () = 0;
8888
virtual void printDebugInfo() = 0;
8989

90+
void push();
91+
9092
inline void setThingId (String const thing_id) { _thing_id = thing_id; };
9193
inline String & getThingId () { return _thing_id; };
9294
inline void setDeviceId(String const device_id) { _device_id = device_id; };

src/property/Property.cpp

+15-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ Property::Property()
4444
_last_cloud_change_timestamp(0),
4545
_identifier(0),
4646
_attributeIdentifier(0),
47-
_lightPayload(false) {
47+
_lightPayload(false),
48+
_update_requested(false) {
4849
}
4950

5051
/******************************************************************************
@@ -79,6 +80,11 @@ Property & Property::publishEvery(unsigned long const seconds) {
7980
return (*this);
8081
}
8182

83+
Property & Property::publishOnDemand() {
84+
_update_policy = UpdatePolicy::OnDemand;
85+
return (*this);
86+
}
87+
8288
bool Property::shouldBeUpdated() {
8389
if (!_has_been_updated_once) {
8490
return true;
@@ -93,11 +99,18 @@ bool Property::shouldBeUpdated() {
9399
return (isDifferentFromCloud() && ((millis() - _last_updated_millis) >= (_min_time_between_updates_millis)));
94100
} else if (_update_policy == UpdatePolicy::TimeInterval) {
95101
return ((millis() - _last_updated_millis) >= _update_interval_millis);
102+
} else if (_update_policy == UpdatePolicy::OnDemand) {
103+
return _update_requested;
96104
} else {
97105
return false;
98106
}
99107
}
100108

109+
void Property::requestUpdate()
110+
{
111+
_update_requested = true;
112+
}
113+
101114
void Property::execCallbackOnChange() {
102115
if (_update_callback_func != NULL) {
103116
_update_callback_func();
@@ -119,6 +132,7 @@ void Property::append(CborEncoder *encoder, bool lightPayload) {
119132
appendAttributesToCloudReal(encoder);
120133
fromLocalToCloud();
121134
_has_been_updated_once = true;
135+
_update_requested = false;
122136
_last_updated_millis = millis();
123137
}
124138

src/property/Property.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ enum class Type {
117117
};
118118

119119
enum class UpdatePolicy {
120-
OnChange, TimeInterval
120+
OnChange, TimeInterval, OnDemand
121121
};
122122

123123
typedef void(*UpdateCallbackFunc)(void);
@@ -138,6 +138,7 @@ class Property {
138138
Property & onSync(SyncCallbackFunc func);
139139
Property & publishOnChange(float const min_delta_property, unsigned long const min_time_between_updates_millis = 0);
140140
Property & publishEvery(unsigned long const seconds);
141+
Property & publishOnDemand();
141142

142143
inline String name() const {
143144
return _name;
@@ -153,6 +154,7 @@ class Property {
153154
}
154155

155156
bool shouldBeUpdated();
157+
void requestUpdate();
156158
void execCallbackOnChange();
157159
void execCallbackOnSync();
158160
void setLastCloudChangeTimestamp(unsigned long cloudChangeTime);
@@ -211,6 +213,8 @@ class Property {
211213
int _attributeIdentifier;
212214
/* Indicates if the property shall be encoded using the identifier instead of the name */
213215
bool _lightPayload;
216+
/* Indicates whether a property update has been requested in case of the OnDemand update policy. */
217+
bool _update_requested;
214218
};
215219

216220
/******************************************************************************

src/property/PropertyContainer.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@ int PropertyContainer::appendChangedProperties(CborEncoder * arrayEncoder, bool
112112
return appendedProperties;
113113
}
114114

115+
void PropertyContainer::requestUpdateForAllProperties()
116+
{
117+
std::for_each(_property_list.begin(),
118+
_property_list.end(),
119+
[](Property * p)
120+
{
121+
p->requestUpdate();
122+
});
123+
}
124+
115125
void PropertyContainer::updateTimestampOnLocallyChangedProperties()
116126
{
117127
/* This function updates the timestamps on the primitive properties

src/property/PropertyContainer.h

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class PropertyContainer
5252

5353
int appendChangedProperties(CborEncoder * arrayEncoder, bool lightPayload);
5454
void updateTimestampOnLocallyChangedProperties();
55+
void requestUpdateForAllProperties();
5556

5657

5758

0 commit comments

Comments
 (0)