Skip to content

Commit dcbd179

Browse files
committed
Bugfix + test code - if a property is manipulated in its onChange callback function to its origin state the change is still propagated to the cloud
1 parent ee503e4 commit dcbd179

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

ArduinoCloudProperty.hpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class ArduinoCloudProperty {
7777
inline bool isReadableByCloud () const { return (_permission == Permission::Read ) || (_permission == Permission::ReadWrite); }
7878
inline bool isWriteableByCloud() const { return (_permission == Permission::Write) || (_permission == Permission::ReadWrite); }
7979

80-
bool shouldBeUpdated () const;
80+
bool shouldBeUpdated ();
8181
void execCallbackOnChange ();
8282

8383
void append (CborEncoder * encoder, CloudProtocol const cloud_protocol);
@@ -91,7 +91,8 @@ class ArduinoCloudProperty {
9191
UpdateCallbackFunc _update_callback_func;
9292

9393
UpdatePolicy _update_policy;
94-
bool _has_been_updated_once;
94+
bool _has_been_updated_once,
95+
_has_been_modified_in_callback;
9596
/* Variables used for UpdatePolicy::OnChange */
9697
T _min_delta_property;
9798
unsigned long _min_time_between_updates_millis;

ArduinoCloudProperty.ipp

+11-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ ArduinoCloudProperty<T>::ArduinoCloudProperty(T & property, String const & name,
1111
_update_callback_func(NULL),
1212
_update_policy(UpdatePolicy::OnChange),
1313
_has_been_updated_once(false),
14+
_has_been_modified_in_callback(false),
1415
_min_delta_property(getInitialMinDeltaPropertyValue()),
1516
_min_time_between_updates_millis(0),
1617
_last_updated_millis(0),
@@ -52,9 +53,14 @@ ArduinoCloudProperty<T> & ArduinoCloudProperty<T>::publishEvery(unsigned long co
5253
}
5354

5455
template <typename T>
55-
bool ArduinoCloudProperty<T>::shouldBeUpdated() const {
56+
bool ArduinoCloudProperty<T>::shouldBeUpdated() {
5657
if(!_has_been_updated_once) return true;
5758

59+
if(_has_been_modified_in_callback) {
60+
_has_been_modified_in_callback = false;
61+
return true;
62+
}
63+
5864
if (_update_policy == UpdatePolicy::OnChange) {
5965
return (isValueDifferent(_property, _shadow_property) && ((millis() - _last_updated_millis) >= (_min_time_between_updates_millis)));
6066
}
@@ -72,6 +78,10 @@ void ArduinoCloudProperty<T>::execCallbackOnChange() {
7278
if(_update_callback_func != NULL) {
7379
_update_callback_func();
7480
}
81+
82+
if(!isValueDifferent(_property, _shadow_property)) {
83+
_has_been_modified_in_callback = true;
84+
}
7585
}
7686
}
7787

0 commit comments

Comments
 (0)