Skip to content

Commit 51981c1

Browse files
authored
Merge pull request #141 from arduino-libraries/refactor-1
Various minor refactor operations
2 parents cd6901d + 4d74549 commit 51981c1

File tree

6 files changed

+57
-44
lines changed

6 files changed

+57
-44
lines changed

Diff for: src/cbor/ArduinoCloudThing.cpp

+3-28
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ ArduinoCloudThing::MapParserState ArduinoCloudThing::handle_Name(CborValue * val
264264
map_data->name_identifier.set(val & 255);
265265
map_data->attribute_identifier.set(val >> 8);
266266
map_data->light_payload.set(true);
267-
String name = getPropertyNameByIdentifier(val);
267+
String name = getPropertyNameByIdentifier(*_property_container, val);
268268
map_data->name.set(name);
269269

270270

@@ -353,7 +353,7 @@ ArduinoCloudThing::MapParserState ArduinoCloudThing::handle_LeaveMap(CborValue *
353353

354354
if (_currentPropertyName != "" && propertyName != _currentPropertyName) {
355355
/* Update the property containers depending on the parsed data */
356-
updateProperty(_currentPropertyName, _currentPropertyBaseTime + _currentPropertyTime);
356+
updateProperty(*_property_container, _currentPropertyName, _currentPropertyBaseTime + _currentPropertyTime, _isSyncMessage, &_map_data_list);
357357
/* Reset current property data */
358358
freeMapDataList(&_map_data_list);
359359
_currentPropertyBaseTime = 0;
@@ -376,7 +376,7 @@ ArduinoCloudThing::MapParserState ArduinoCloudThing::handle_LeaveMap(CborValue *
376376
next_state = MapParserState::EnterMap;
377377
} else {
378378
/* Update the property containers depending on the parsed data */
379-
updateProperty(_currentPropertyName, _currentPropertyBaseTime + _currentPropertyTime);
379+
updateProperty(*_property_container, _currentPropertyName, _currentPropertyBaseTime + _currentPropertyTime, _isSyncMessage, &_map_data_list);
380380
/* Reset last property data */
381381
freeMapDataList(&_map_data_list);
382382
next_state = MapParserState::Complete;
@@ -397,31 +397,6 @@ void ArduinoCloudThing::freeMapDataList(std::list<CborMapData *> * map_data_list
397397
map_data_list->clear();
398398
}
399399

400-
void ArduinoCloudThing::updateProperty(String propertyName, unsigned long cloudChangeEventTime) {
401-
Property* property = getProperty(*_property_container, propertyName);
402-
if (property && property->isWriteableByCloud()) {
403-
property->setLastCloudChangeTimestamp(cloudChangeEventTime);
404-
property->setAttributesFromCloud(&_map_data_list);
405-
if (_isSyncMessage) {
406-
property->execCallbackOnSync();
407-
} else {
408-
property->fromCloudToLocal();
409-
property->execCallbackOnChange();
410-
}
411-
}
412-
}
413-
414-
// retrieve the property name by the identifier
415-
String ArduinoCloudThing::getPropertyNameByIdentifier(int propertyIdentifier) {
416-
Property* property;
417-
if (propertyIdentifier > 255) {
418-
property = getProperty(*_property_container, propertyIdentifier & 255);
419-
} else {
420-
property = getProperty(*_property_container, propertyIdentifier);
421-
}
422-
return property->name();
423-
}
424-
425400
bool ArduinoCloudThing::ifNumericConvertToDouble(CborValue * value_iter, double * numeric_val) {
426401

427402
if (cbor_value_is_integer(value_iter)) {

Diff for: src/cbor/ArduinoCloudThing.h

-2
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ class ArduinoCloudThing {
8787
/* decode a CBOR payload received from the cloud */
8888
void decode(uint8_t const * const payload, size_t const length, bool isSyncMessage = false);
8989

90-
void updateProperty(String propertyName, unsigned long cloudChangeEventTime);
91-
String getPropertyNameByIdentifier(int propertyIdentifier);
9290

9391
private:
9492
PropertyContainer * _property_container;

Diff for: src/property/Property.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Property::Property()
3535
, _permission{Permission::Read}
3636
, _get_time_func{nullptr}
3737
, _update_callback_func{nullptr}
38-
, _sync_callback_func{nullptr}
38+
, _on_sync_callback_func{nullptr}
3939
, _update_policy{UpdatePolicy::OnChange}
4040
, _has_been_updated_once{false}
4141
, _has_been_modified_in_callback{false}
@@ -67,8 +67,8 @@ Property & Property::onUpdate(UpdateCallbackFunc func) {
6767
return (*this);
6868
}
6969

70-
Property & Property::onSync(SyncCallbackFunc func) {
71-
_sync_callback_func = func;
70+
Property & Property::onSync(OnSyncCallbackFunc func) {
71+
_on_sync_callback_func = func;
7272
return (*this);
7373
}
7474

@@ -128,7 +128,7 @@ void Property::requestUpdate()
128128
}
129129

130130
void Property::execCallbackOnChange() {
131-
if (_update_callback_func != NULL) {
131+
if (_update_callback_func != nullptr) {
132132
_update_callback_func();
133133
}
134134
if (!isDifferentFromCloud()) {
@@ -137,8 +137,8 @@ void Property::execCallbackOnChange() {
137137
}
138138

139139
void Property::execCallbackOnSync() {
140-
if (_sync_callback_func != NULL) {
141-
_sync_callback_func(*this);
140+
if (_on_sync_callback_func != nullptr) {
141+
_on_sync_callback_func(*this);
142142
}
143143
}
144144

Diff for: src/property/Property.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -122,20 +122,22 @@ enum class UpdatePolicy {
122122

123123
typedef void(*UpdateCallbackFunc)(void);
124124
typedef unsigned long(*GetTimeCallbackFunc)();
125+
class Property;
126+
typedef void(*OnSyncCallbackFunc)(Property &);
125127

126128
/******************************************************************************
127129
CLASS DECLARATION
128130
******************************************************************************/
129131

130-
class Property {
131-
typedef void(*SyncCallbackFunc)(Property &property);
132+
class Property
133+
{
132134
public:
133135
Property();
134136
void init(String const name, Permission const permission, GetTimeCallbackFunc func);
135137

136138
/* Composable configuration of the Property class */
137139
Property & onUpdate(UpdateCallbackFunc func);
138-
Property & onSync(SyncCallbackFunc func);
140+
Property & onSync(OnSyncCallbackFunc func);
139141
Property & publishOnChange(float const min_delta_property, unsigned long const min_time_between_updates_millis = 0);
140142
Property & publishEvery(unsigned long const seconds);
141143
Property & publishOnDemand();
@@ -201,7 +203,7 @@ class Property {
201203
Permission _permission;
202204
GetTimeCallbackFunc _get_time_func;
203205
UpdateCallbackFunc _update_callback_func;
204-
void (*_sync_callback_func)(Property &property);
206+
OnSyncCallbackFunc _on_sync_callback_func;
205207

206208
UpdatePolicy _update_policy;
207209
bool _has_been_updated_once,

Diff for: src/property/PropertyContainer.cpp

+40-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@
2626
#include "types/CloudWrapperBase.h"
2727

2828
/******************************************************************************
29-
PUBLIC MEMBER FUNCTIONS
29+
INTERNAL FUNCTION DECLARATION
30+
******************************************************************************/
31+
32+
void addProperty(PropertyContainer & prop_cont, Property * property_obj, int propertyIdentifier);
33+
34+
/******************************************************************************
35+
PUBLIC FUNCTION DEFINITION
3036
******************************************************************************/
3137

3238
Property & addPropertyToContainer(PropertyContainer & prop_cont, Property & property, String const & name, Permission const permission, int propertyIdentifier, GetTimeCallbackFunc func)
@@ -120,8 +126,40 @@ void updateTimestampOnLocallyChangedProperties(PropertyContainer & prop_cont)
120126
});
121127
}
122128

129+
void updateProperty(PropertyContainer & prop_cont, String propertyName, unsigned long cloudChangeEventTime, bool const is_sync_message, std::list<CborMapData *> * map_data_list)
130+
{
131+
Property * property = getProperty(prop_cont, propertyName);
132+
133+
if (property && property->isWriteableByCloud())
134+
{
135+
property->setLastCloudChangeTimestamp(cloudChangeEventTime);
136+
property->setAttributesFromCloud(map_data_list);
137+
if (is_sync_message) {
138+
property->execCallbackOnSync();
139+
} else {
140+
property->fromCloudToLocal();
141+
property->execCallbackOnChange();
142+
}
143+
}
144+
}
145+
146+
String getPropertyNameByIdentifier(PropertyContainer & prop_cont, int propertyIdentifier)
147+
{
148+
Property * property = nullptr;
149+
150+
if (propertyIdentifier > 255)
151+
property = getProperty(prop_cont, propertyIdentifier & 255);
152+
else
153+
property = getProperty(prop_cont, propertyIdentifier);
154+
155+
if (property)
156+
return property->name();
157+
else
158+
return String("");
159+
}
160+
123161
/******************************************************************************
124-
PRIVATE MEMBER FUNCTIONS
162+
INTERNAL FUNCTION DEFINITION
125163
******************************************************************************/
126164

127165
void addProperty(PropertyContainer & prop_cont, Property * property_obj, int propertyIdentifier)

Diff for: src/property/PropertyContainer.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Property * getProperty(PropertyContainer & prop_cont, int const identifier);
6363
int appendChangedProperties(PropertyContainer & prop_cont, CborEncoder * arrayEncoder, bool lightPayload);
6464
void updateTimestampOnLocallyChangedProperties(PropertyContainer & prop_cont);
6565
void requestUpdateForAllProperties(PropertyContainer & prop_cont);
66-
67-
void addProperty(PropertyContainer & prop_cont, Property * property_obj, int propertyIdentifier);
66+
void updateProperty(PropertyContainer & prop_cont, String propertyName, unsigned long cloudChangeEventTime, bool const is_sync_message, std::list<CborMapData *> * map_data_list);
67+
String getPropertyNameByIdentifier(PropertyContainer & prop_cont, int propertyIdentifier);
6868

6969
#endif /* ARDUINO_PROPERTY_CONTAINER_H_ */

0 commit comments

Comments
 (0)