Skip to content

Commit 2c7ba81

Browse files
committed
Add Property write policy to manually handle local values update
1 parent 48df22a commit 2c7ba81

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/property/Property.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Property::Property()
2929
, _min_delta_property{0.0f}
3030
, _min_time_between_updates_millis{DEFAULT_MIN_TIME_BETWEEN_UPDATES_MILLIS}
3131
, _permission{Permission::Read}
32+
, _write_policy{WritePolicy::Auto}
3233
, _get_time_func{nullptr}
3334
, _update_callback_func{nullptr}
3435
, _on_sync_callback_func{nullptr}
@@ -102,6 +103,18 @@ Property & Property::encodeTimestamp()
102103
return (*this);
103104
}
104105

106+
Property & Property::writeOnChange()
107+
{
108+
_write_policy = WritePolicy::Auto;
109+
return (*this);
110+
}
111+
112+
Property & Property::writeOnDemand()
113+
{
114+
_write_policy = WritePolicy::Manual;
115+
return (*this);
116+
}
117+
105118
void Property::setTimestamp(unsigned long const timestamp)
106119
{
107120
_timestamp = timestamp;

src/property/Property.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ enum class UpdatePolicy {
125125
OnChange, TimeInterval, OnDemand
126126
};
127127

128+
enum class WritePolicy {
129+
Auto, Manual
130+
};
131+
128132
typedef void(*UpdateCallbackFunc)(void);
129133
typedef unsigned long(*GetTimeCallbackFunc)();
130134
class Property;
@@ -147,6 +151,8 @@ class Property
147151
Property & publishEvery(unsigned long const seconds);
148152
Property & publishOnDemand();
149153
Property & encodeTimestamp();
154+
Property & writeOnChange();
155+
Property & writeOnDemand();
150156

151157
inline String name() const {
152158
return _name;
@@ -160,6 +166,9 @@ class Property
160166
inline bool isWriteableByCloud() const {
161167
return (_permission == Permission::Write) || (_permission == Permission::ReadWrite);
162168
}
169+
inline bool isWritableOnChange() const {
170+
return _write_policy == WritePolicy::Auto;
171+
}
163172

164173
void setTimestamp(unsigned long const timestamp);
165174
bool shouldBeUpdated();
@@ -209,6 +218,7 @@ class Property
209218

210219
private:
211220
Permission _permission;
221+
WritePolicy _write_policy;
212222
GetTimeCallbackFunc _get_time_func;
213223
UpdateCallbackFunc _update_callback_func;
214224
OnSyncCallbackFunc _on_sync_callback_func;
@@ -219,7 +229,7 @@ class Property
219229
_has_been_appended_but_not_sended;
220230
/* Variables used for UpdatePolicy::TimeInterval */
221231
unsigned long _last_updated_millis,
222-
_update_interval_millis;
232+
_update_interval_millis;
223233
/* Variables used for reconnection sync*/
224234
unsigned long _last_local_change_timestamp;
225235
unsigned long _last_cloud_change_timestamp;

src/property/PropertyContainer.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ void updateProperty(PropertyContainer & prop_cont, String propertyName, unsigned
121121
if (is_sync_message) {
122122
property->execCallbackOnSync();
123123
} else {
124-
property->fromCloudToLocal();
124+
if (property->isWritableOnChange()) {
125+
property->fromCloudToLocal();
126+
}
125127
property->execCallbackOnChange();
126128
property->provideEcho();
127129
}

0 commit comments

Comments
 (0)