Skip to content

Various minor refactor operations #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 3 additions & 28 deletions src/cbor/ArduinoCloudThing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);


Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -397,31 +397,6 @@ void ArduinoCloudThing::freeMapDataList(std::list<CborMapData *> * 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)) {
Expand Down
2 changes: 0 additions & 2 deletions src/cbor/ArduinoCloudThing.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions src/property/Property.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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()) {
Expand All @@ -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);
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/property/Property.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down
42 changes: 40 additions & 2 deletions src/property/PropertyContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<CborMapData *> * 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)
Expand Down
4 changes: 2 additions & 2 deletions src/property/PropertyContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<CborMapData *> * map_data_list);
String getPropertyNameByIdentifier(PropertyContainer & prop_cont, int propertyIdentifier);

#endif /* ARDUINO_PROPERTY_CONTAINER_H_ */