diff --git a/src/cbor/ArduinoCloudThing.cpp b/src/cbor/ArduinoCloudThing.cpp index dc92c66b5..419c3de90 100644 --- a/src/cbor/ArduinoCloudThing.cpp +++ b/src/cbor/ArduinoCloudThing.cpp @@ -264,7 +264,7 @@ ArduinoCloudThing::MapParserState ArduinoCloudThing::handle_Name(CborValue * val map_data->name_identifier.set(val & 255); map_data->attribute_identifier.set(val >> 8); map_data->light_payload.set(true); - String name = getPropertyNameByIdentifier(val); + String name = getPropertyNameByIdentifier(*_property_container, val); map_data->name.set(name); @@ -353,7 +353,7 @@ ArduinoCloudThing::MapParserState ArduinoCloudThing::handle_LeaveMap(CborValue * if (_currentPropertyName != "" && propertyName != _currentPropertyName) { /* Update the property containers depending on the parsed data */ - updateProperty(_currentPropertyName, _currentPropertyBaseTime + _currentPropertyTime); + updateProperty(*_property_container, _currentPropertyName, _currentPropertyBaseTime + _currentPropertyTime, _isSyncMessage, &_map_data_list); /* Reset current property data */ freeMapDataList(&_map_data_list); _currentPropertyBaseTime = 0; @@ -376,7 +376,7 @@ ArduinoCloudThing::MapParserState ArduinoCloudThing::handle_LeaveMap(CborValue * next_state = MapParserState::EnterMap; } else { /* Update the property containers depending on the parsed data */ - updateProperty(_currentPropertyName, _currentPropertyBaseTime + _currentPropertyTime); + updateProperty(*_property_container, _currentPropertyName, _currentPropertyBaseTime + _currentPropertyTime, _isSyncMessage, &_map_data_list); /* Reset last property data */ freeMapDataList(&_map_data_list); next_state = MapParserState::Complete; @@ -397,31 +397,6 @@ void ArduinoCloudThing::freeMapDataList(std::list * map_data_list map_data_list->clear(); } -void ArduinoCloudThing::updateProperty(String propertyName, unsigned long cloudChangeEventTime) { - Property* property = getProperty(*_property_container, propertyName); - if (property && property->isWriteableByCloud()) { - property->setLastCloudChangeTimestamp(cloudChangeEventTime); - property->setAttributesFromCloud(&_map_data_list); - if (_isSyncMessage) { - property->execCallbackOnSync(); - } else { - property->fromCloudToLocal(); - property->execCallbackOnChange(); - } - } -} - -// retrieve the property name by the identifier -String ArduinoCloudThing::getPropertyNameByIdentifier(int propertyIdentifier) { - Property* property; - if (propertyIdentifier > 255) { - property = getProperty(*_property_container, propertyIdentifier & 255); - } else { - property = getProperty(*_property_container, propertyIdentifier); - } - return property->name(); -} - bool ArduinoCloudThing::ifNumericConvertToDouble(CborValue * value_iter, double * numeric_val) { if (cbor_value_is_integer(value_iter)) { diff --git a/src/cbor/ArduinoCloudThing.h b/src/cbor/ArduinoCloudThing.h index 40cfdb58d..5ef9b0ae2 100644 --- a/src/cbor/ArduinoCloudThing.h +++ b/src/cbor/ArduinoCloudThing.h @@ -87,8 +87,6 @@ class ArduinoCloudThing { /* decode a CBOR payload received from the cloud */ void decode(uint8_t const * const payload, size_t const length, bool isSyncMessage = false); - void updateProperty(String propertyName, unsigned long cloudChangeEventTime); - String getPropertyNameByIdentifier(int propertyIdentifier); private: PropertyContainer * _property_container; diff --git a/src/property/Property.cpp b/src/property/Property.cpp index 062e352fe..7870534bf 100644 --- a/src/property/Property.cpp +++ b/src/property/Property.cpp @@ -35,7 +35,7 @@ Property::Property() , _permission{Permission::Read} , _get_time_func{nullptr} , _update_callback_func{nullptr} -, _sync_callback_func{nullptr} +, _on_sync_callback_func{nullptr} , _update_policy{UpdatePolicy::OnChange} , _has_been_updated_once{false} , _has_been_modified_in_callback{false} @@ -67,8 +67,8 @@ Property & Property::onUpdate(UpdateCallbackFunc func) { return (*this); } -Property & Property::onSync(SyncCallbackFunc func) { - _sync_callback_func = func; +Property & Property::onSync(OnSyncCallbackFunc func) { + _on_sync_callback_func = func; return (*this); } @@ -128,7 +128,7 @@ void Property::requestUpdate() } void Property::execCallbackOnChange() { - if (_update_callback_func != NULL) { + if (_update_callback_func != nullptr) { _update_callback_func(); } if (!isDifferentFromCloud()) { @@ -137,8 +137,8 @@ void Property::execCallbackOnChange() { } void Property::execCallbackOnSync() { - if (_sync_callback_func != NULL) { - _sync_callback_func(*this); + if (_on_sync_callback_func != nullptr) { + _on_sync_callback_func(*this); } } diff --git a/src/property/Property.h b/src/property/Property.h index 48fe45357..e89d543ba 100644 --- a/src/property/Property.h +++ b/src/property/Property.h @@ -122,20 +122,22 @@ enum class UpdatePolicy { typedef void(*UpdateCallbackFunc)(void); typedef unsigned long(*GetTimeCallbackFunc)(); +class Property; +typedef void(*OnSyncCallbackFunc)(Property &); /****************************************************************************** CLASS DECLARATION ******************************************************************************/ -class Property { - typedef void(*SyncCallbackFunc)(Property &property); +class Property +{ public: Property(); void init(String const name, Permission const permission, GetTimeCallbackFunc func); /* Composable configuration of the Property class */ Property & onUpdate(UpdateCallbackFunc func); - Property & onSync(SyncCallbackFunc func); + Property & onSync(OnSyncCallbackFunc func); Property & publishOnChange(float const min_delta_property, unsigned long const min_time_between_updates_millis = 0); Property & publishEvery(unsigned long const seconds); Property & publishOnDemand(); @@ -201,7 +203,7 @@ class Property { Permission _permission; GetTimeCallbackFunc _get_time_func; UpdateCallbackFunc _update_callback_func; - void (*_sync_callback_func)(Property &property); + OnSyncCallbackFunc _on_sync_callback_func; UpdatePolicy _update_policy; bool _has_been_updated_once, diff --git a/src/property/PropertyContainer.cpp b/src/property/PropertyContainer.cpp index f0ef98838..c8ebb793c 100644 --- a/src/property/PropertyContainer.cpp +++ b/src/property/PropertyContainer.cpp @@ -26,7 +26,13 @@ #include "types/CloudWrapperBase.h" /****************************************************************************** - PUBLIC MEMBER FUNCTIONS + INTERNAL FUNCTION DECLARATION + ******************************************************************************/ + +void addProperty(PropertyContainer & prop_cont, Property * property_obj, int propertyIdentifier); + +/****************************************************************************** + PUBLIC FUNCTION DEFINITION ******************************************************************************/ 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) }); } +void updateProperty(PropertyContainer & prop_cont, String propertyName, unsigned long cloudChangeEventTime, bool const is_sync_message, std::list * map_data_list) +{ + Property * property = getProperty(prop_cont, propertyName); + + if (property && property->isWriteableByCloud()) + { + property->setLastCloudChangeTimestamp(cloudChangeEventTime); + property->setAttributesFromCloud(map_data_list); + if (is_sync_message) { + property->execCallbackOnSync(); + } else { + property->fromCloudToLocal(); + property->execCallbackOnChange(); + } + } +} + +String getPropertyNameByIdentifier(PropertyContainer & prop_cont, int propertyIdentifier) +{ + Property * property = nullptr; + + if (propertyIdentifier > 255) + property = getProperty(prop_cont, propertyIdentifier & 255); + else + property = getProperty(prop_cont, propertyIdentifier); + + if (property) + return property->name(); + else + return String(""); +} + /****************************************************************************** - PRIVATE MEMBER FUNCTIONS + INTERNAL FUNCTION DEFINITION ******************************************************************************/ void addProperty(PropertyContainer & prop_cont, Property * property_obj, int propertyIdentifier) diff --git a/src/property/PropertyContainer.h b/src/property/PropertyContainer.h index d7852dd6e..7047bd0ba 100644 --- a/src/property/PropertyContainer.h +++ b/src/property/PropertyContainer.h @@ -63,7 +63,7 @@ Property * getProperty(PropertyContainer & prop_cont, int const identifier); int appendChangedProperties(PropertyContainer & prop_cont, CborEncoder * arrayEncoder, bool lightPayload); void updateTimestampOnLocallyChangedProperties(PropertyContainer & prop_cont); void requestUpdateForAllProperties(PropertyContainer & prop_cont); - -void addProperty(PropertyContainer & prop_cont, Property * property_obj, int propertyIdentifier); +void updateProperty(PropertyContainer & prop_cont, String propertyName, unsigned long cloudChangeEventTime, bool const is_sync_message, std::list * map_data_list); +String getPropertyNameByIdentifier(PropertyContainer & prop_cont, int propertyIdentifier); #endif /* ARDUINO_PROPERTY_CONTAINER_H_ */ \ No newline at end of file