Skip to content

Commit 967ae24

Browse files
committed
Extract both updateProperty and getPropertyNameByIdentifier from ArduinoCloudThing since those functions don't really concern themselves with cbor encoding/decoding
1 parent cd6901d commit 967ae24

File tree

4 files changed

+38
-30
lines changed

4 files changed

+38
-30
lines changed

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)) {

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;

src/property/PropertyContainer.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,38 @@ void updateTimestampOnLocallyChangedProperties(PropertyContainer & prop_cont)
120120
});
121121
}
122122

123+
void updateProperty(PropertyContainer & prop_cont, String propertyName, unsigned long cloudChangeEventTime, bool const is_sync_message, std::list<CborMapData *> * map_data_list)
124+
{
125+
Property * property = getProperty(prop_cont, propertyName);
126+
127+
if (property && property->isWriteableByCloud())
128+
{
129+
property->setLastCloudChangeTimestamp(cloudChangeEventTime);
130+
property->setAttributesFromCloud(map_data_list);
131+
if (is_sync_message) {
132+
property->execCallbackOnSync();
133+
} else {
134+
property->fromCloudToLocal();
135+
property->execCallbackOnChange();
136+
}
137+
}
138+
}
139+
140+
String getPropertyNameByIdentifier(PropertyContainer & prop_cont, int propertyIdentifier)
141+
{
142+
Property * property = nullptr;
143+
144+
if (propertyIdentifier > 255)
145+
property = getProperty(prop_cont, propertyIdentifier & 255);
146+
else
147+
property = getProperty(prop_cont, propertyIdentifier);
148+
149+
if (property)
150+
return property->name();
151+
else
152+
return String("");
153+
}
154+
123155
/******************************************************************************
124156
PRIVATE MEMBER FUNCTIONS
125157
******************************************************************************/

src/property/PropertyContainer.h

+3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ 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+
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);
68+
6669

6770
void addProperty(PropertyContainer & prop_cont, Property * property_obj, int propertyIdentifier);
6871

0 commit comments

Comments
 (0)