Skip to content

Commit

Permalink
Merge pull request #339 from pennam/attributes-fix
Browse files Browse the repository at this point in the history
Remove attributes macros
  • Loading branch information
pennam authored Dec 1, 2022
2 parents 9828845 + 7d5221c commit 27c05bc
Show file tree
Hide file tree
Showing 18 changed files with 111 additions and 127 deletions.
43 changes: 18 additions & 25 deletions src/property/Property.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ void Property::execCallbackOnSync() {
CborError Property::append(CborEncoder *encoder, bool lightPayload) {
_lightPayload = lightPayload;
_attributeIdentifier = 0;
CHECK_CBOR(appendAttributesToCloudReal(encoder));
CHECK_CBOR(appendAttributesToCloud(encoder));
fromLocalToCloud();
_has_been_updated_once = true;
_has_been_modified_in_callback = false;
Expand All @@ -181,7 +181,7 @@ CborError Property::append(CborEncoder *encoder, bool lightPayload) {
return CborNoError;
}

CborError Property::appendAttributeReal(bool value, String attributeName, CborEncoder *encoder) {
CborError Property::appendAttribute(bool value, String attributeName, CborEncoder *encoder) {
return appendAttributeName(attributeName, [value](CborEncoder & mapEncoder)
{
CHECK_CBOR(cbor_encode_int(&mapEncoder, static_cast<int>(CborIntegerMapKey::BooleanValue)));
Expand All @@ -190,7 +190,7 @@ CborError Property::appendAttributeReal(bool value, String attributeName, CborEn
}, encoder);
}

CborError Property::appendAttributeReal(int value, String attributeName, CborEncoder *encoder) {
CborError Property::appendAttribute(int value, String attributeName, CborEncoder *encoder) {
return appendAttributeName(attributeName, [value](CborEncoder & mapEncoder)
{
CHECK_CBOR(cbor_encode_int(&mapEncoder, static_cast<int>(CborIntegerMapKey::Value)));
Expand All @@ -199,7 +199,7 @@ CborError Property::appendAttributeReal(int value, String attributeName, CborEnc
}, encoder);
}

CborError Property::appendAttributeReal(unsigned int value, String attributeName, CborEncoder *encoder) {
CborError Property::appendAttribute(unsigned int value, String attributeName, CborEncoder *encoder) {
return appendAttributeName(attributeName, [value](CborEncoder & mapEncoder)
{
CHECK_CBOR(cbor_encode_int(&mapEncoder, static_cast<int>(CborIntegerMapKey::Value)));
Expand All @@ -208,7 +208,7 @@ CborError Property::appendAttributeReal(unsigned int value, String attributeName
}, encoder);
}

CborError Property::appendAttributeReal(float value, String attributeName, CborEncoder *encoder) {
CborError Property::appendAttribute(float value, String attributeName, CborEncoder *encoder) {
return appendAttributeName(attributeName, [value](CborEncoder & mapEncoder)
{
CHECK_CBOR(cbor_encode_int(&mapEncoder, static_cast<int>(CborIntegerMapKey::Value)));
Expand All @@ -217,7 +217,7 @@ CborError Property::appendAttributeReal(float value, String attributeName, CborE
}, encoder);
}

CborError Property::appendAttributeReal(String value, String attributeName, CborEncoder *encoder) {
CborError Property::appendAttribute(String value, String attributeName, CborEncoder *encoder) {
return appendAttributeName(attributeName, [value](CborEncoder & mapEncoder)
{
CHECK_CBOR(cbor_encode_int(&mapEncoder, static_cast<int>(CborIntegerMapKey::StringValue)));
Expand Down Expand Up @@ -278,8 +278,8 @@ void Property::setAttributesFromCloud(std::list<CborMapData> * map_data_list) {
setAttributesFromCloud();
}

void Property::setAttributeReal(bool& value, String attributeName) {
setAttributeReal(attributeName, [&value](CborMapData & md) {
void Property::setAttribute(bool& value, String attributeName) {
setAttribute(attributeName, [&value](CborMapData & md) {
// Manage the case to have boolean values received as integers 0/1
if (md.bool_val.isSet()) {
value = md.bool_val.get();
Expand All @@ -295,34 +295,34 @@ void Property::setAttributeReal(bool& value, String attributeName) {
});
}

void Property::setAttributeReal(int& value, String attributeName) {
setAttributeReal(attributeName, [&value](CborMapData & md) {
void Property::setAttribute(int& value, String attributeName) {
setAttribute(attributeName, [&value](CborMapData & md) {
value = md.val.get();
});
}

void Property::setAttributeReal(unsigned int& value, String attributeName) {
setAttributeReal(attributeName, [&value](CborMapData & md) {
void Property::setAttribute(unsigned int& value, String attributeName) {
setAttribute(attributeName, [&value](CborMapData & md) {
value = md.val.get();
});
}

void Property::setAttributeReal(float& value, String attributeName) {
setAttributeReal(attributeName, [&value](CborMapData & md) {
void Property::setAttribute(float& value, String attributeName) {
setAttribute(attributeName, [&value](CborMapData & md) {
value = md.val.get();
});
}

void Property::setAttributeReal(String& value, String attributeName) {
setAttributeReal(attributeName, [&value](CborMapData & md) {
void Property::setAttribute(String& value, String attributeName) {
setAttribute(attributeName, [&value](CborMapData & md) {
value = md.str_val.get();
});
}

#ifdef __AVR__
void Property::setAttributeReal(String attributeName, nonstd::function<void (CborMapData & md)>setValue)
void Property::setAttribute(String attributeName, nonstd::function<void (CborMapData & md)>setValue)
#else
void Property::setAttributeReal(String attributeName, std::function<void (CborMapData & md)>setValue)
void Property::setAttribute(String attributeName, std::function<void (CborMapData & md)>setValue)
#endif
{
if (attributeName != "") {
Expand Down Expand Up @@ -354,13 +354,6 @@ void Property::setAttributeReal(String attributeName, std::function<void (CborMa
});
}

String Property::getAttributeName(String propertyName, char separator) {
int colonPos;
String attributeName = "";
(colonPos = propertyName.indexOf(separator)) != -1 ? attributeName = propertyName.substring(colonPos + 1) : "";
return attributeName;
}

void Property::updateLocalTimestamp() {
if (isReadableByCloud()) {
if (_get_time_func) {
Expand Down
35 changes: 13 additions & 22 deletions src/property/Property.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@

#include "../cbor/lib/tinycbor/cbor-lib.h"

/******************************************************************************
DEFINE
******************************************************************************/

#define appendAttributesToCloud() appendAttributesToCloudReal(CborEncoder *encoder)
#define appendAttribute(x) appendAttributeReal(x, getAttributeName(#x, '.'), encoder)
#define setAttribute(x) setAttributeReal(x, getAttributeName(#x, '.'))

/******************************************************************************
CONST
******************************************************************************/
Expand Down Expand Up @@ -194,30 +186,29 @@ class Property

void updateLocalTimestamp();
CborError append(CborEncoder * encoder, bool lightPayload);
CborError appendAttributeReal(bool value, String attributeName = "", CborEncoder *encoder = nullptr);
CborError appendAttributeReal(int value, String attributeName = "", CborEncoder *encoder = nullptr);
CborError appendAttributeReal(unsigned int value, String attributeName = "", CborEncoder *encoder = nullptr);
CborError appendAttributeReal(float value, String attributeName = "", CborEncoder *encoder = nullptr);
CborError appendAttributeReal(String value, String attributeName = "", CborEncoder *encoder = nullptr);
CborError appendAttribute(bool value, String attributeName = "", CborEncoder *encoder = nullptr);
CborError appendAttribute(int value, String attributeName = "", CborEncoder *encoder = nullptr);
CborError appendAttribute(unsigned int value, String attributeName = "", CborEncoder *encoder = nullptr);
CborError appendAttribute(float value, String attributeName = "", CborEncoder *encoder = nullptr);
CborError appendAttribute(String value, String attributeName = "", CborEncoder *encoder = nullptr);
#ifndef __AVR__
CborError appendAttributeName(String attributeName, std::function<CborError (CborEncoder& mapEncoder)>f, CborEncoder *encoder);
void setAttributeReal(String attributeName, std::function<void (CborMapData & md)>setValue);
void setAttribute(String attributeName, std::function<void (CborMapData & md)>setValue);
#else
CborError appendAttributeName(String attributeName, nonstd::function<CborError (CborEncoder& mapEncoder)>f, CborEncoder *encoder);
void setAttributeReal(String attributeName, nonstd::function<void (CborMapData & md)>setValue);
void setAttribute(String attributeName, nonstd::function<void (CborMapData & md)>setValue);
#endif
void setAttributesFromCloud(std::list<CborMapData> * map_data_list);
void setAttributeReal(bool& value, String attributeName = "");
void setAttributeReal(int& value, String attributeName = "");
void setAttributeReal(unsigned int& value, String attributeName = "");
void setAttributeReal(float& value, String attributeName = "");
void setAttributeReal(String& value, String attributeName = "");
String getAttributeName(String propertyName, char separator);
void setAttribute(bool& value, String attributeName = "");
void setAttribute(int& value, String attributeName = "");
void setAttribute(unsigned int& value, String attributeName = "");
void setAttribute(float& value, String attributeName = "");
void setAttribute(String& value, String attributeName = "");

virtual bool isDifferentFromCloud() = 0;
virtual void fromCloudToLocal() = 0;
virtual void fromLocalToCloud() = 0;
virtual CborError appendAttributesToCloudReal(CborEncoder *encoder) = 0;
virtual CborError appendAttributesToCloud(CborEncoder *encoder) = 0;
virtual void setAttributesFromCloud() = 0;
virtual bool isPrimitive() {
return false;
Expand Down
6 changes: 3 additions & 3 deletions src/property/types/CloudBool.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ class CloudBool : public Property {
virtual void fromLocalToCloud() {
_cloud_value = _value;
}
virtual CborError appendAttributesToCloud() {
return appendAttribute(_value);
virtual CborError appendAttributesToCloud(CborEncoder *encoder) {
return appendAttribute(_value, "", encoder);
}
virtual void setAttributesFromCloud() {
setAttribute(_cloud_value);
setAttribute(_cloud_value, "");
}
//modifiers
CloudBool& operator=(bool v) {
Expand Down
14 changes: 7 additions & 7 deletions src/property/types/CloudColor.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,16 +188,16 @@ class CloudColor : public Property {
virtual void fromLocalToCloud() {
_cloud_value = _value;
}
virtual CborError appendAttributesToCloud() {
CHECK_CBOR_MULTI(appendAttribute(_value.hue));
CHECK_CBOR_MULTI(appendAttribute(_value.sat));
CHECK_CBOR_MULTI(appendAttribute(_value.bri));
virtual CborError appendAttributesToCloud(CborEncoder *encoder) {
CHECK_CBOR_MULTI(appendAttribute(_value.hue, "hue", encoder));
CHECK_CBOR_MULTI(appendAttribute(_value.sat, "sat", encoder));
CHECK_CBOR_MULTI(appendAttribute(_value.bri, "bri", encoder));
return CborNoError;
}
virtual void setAttributesFromCloud() {
setAttribute(_cloud_value.hue);
setAttribute(_cloud_value.sat);
setAttribute(_cloud_value.bri);
setAttribute(_cloud_value.hue, "hue");
setAttribute(_cloud_value.sat, "sat");
setAttribute(_cloud_value.bri, "bri");
}
};

Expand Down
6 changes: 3 additions & 3 deletions src/property/types/CloudFloat.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ class CloudFloat : public Property {
virtual void fromLocalToCloud() {
_cloud_value = _value;
}
virtual CborError appendAttributesToCloud() {
return appendAttribute(_value);
virtual CborError appendAttributesToCloud(CborEncoder *encoder) {
return appendAttribute(_value, "", encoder);
}
virtual void setAttributesFromCloud() {
setAttribute(_cloud_value);
setAttribute(_cloud_value, "");
}
//modifiers
CloudFloat& operator=(float v) {
Expand Down
6 changes: 3 additions & 3 deletions src/property/types/CloudInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ class CloudInt : public Property {
virtual void fromLocalToCloud() {
_cloud_value = _value;
}
virtual CborError appendAttributesToCloud() {
return appendAttribute(_value);
virtual CborError appendAttributesToCloud(CborEncoder *encoder) {
return appendAttribute(_value, "", encoder);
}
virtual void setAttributesFromCloud() {
setAttribute(_cloud_value);
setAttribute(_cloud_value, "");
}
//modifiers
CloudInt& operator=(int v) {
Expand Down
10 changes: 5 additions & 5 deletions src/property/types/CloudLocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ class CloudLocation : public Property {
virtual void fromLocalToCloud() {
_cloud_value = _value;
}
virtual CborError appendAttributesToCloud() {
CHECK_CBOR_MULTI(appendAttribute(_value.lat));
CHECK_CBOR_MULTI(appendAttribute(_value.lon));
virtual CborError appendAttributesToCloud(CborEncoder *encoder) {
CHECK_CBOR_MULTI(appendAttribute(_value.lat, "lat", encoder));
CHECK_CBOR_MULTI(appendAttribute(_value.lon, "lon", encoder));
return CborNoError;
}
virtual void setAttributesFromCloud() {
setAttribute(_cloud_value.lat);
setAttribute(_cloud_value.lon);
setAttribute(_cloud_value.lat, "lat");
setAttribute(_cloud_value.lon, "lon");
}
};

Expand Down
18 changes: 9 additions & 9 deletions src/property/types/CloudSchedule.h
Original file line number Diff line number Diff line change
Expand Up @@ -417,18 +417,18 @@ class CloudSchedule : public Property {
virtual void fromLocalToCloud() {
_cloud_value = _value;
}
virtual CborError appendAttributesToCloud() {
CHECK_CBOR_MULTI(appendAttribute(_value.frm));
CHECK_CBOR_MULTI(appendAttribute(_value.to));
CHECK_CBOR_MULTI(appendAttribute(_value.len));
CHECK_CBOR_MULTI(appendAttribute(_value.msk));
virtual CborError appendAttributesToCloud(CborEncoder *encoder) {
CHECK_CBOR_MULTI(appendAttribute(_value.frm, "frm", encoder));
CHECK_CBOR_MULTI(appendAttribute(_value.to, "to", encoder));
CHECK_CBOR_MULTI(appendAttribute(_value.len, "len", encoder));
CHECK_CBOR_MULTI(appendAttribute(_value.msk, "msk", encoder));
return CborNoError;
}
virtual void setAttributesFromCloud() {
setAttribute(_cloud_value.frm);
setAttribute(_cloud_value.to);
setAttribute(_cloud_value.len);
setAttribute(_cloud_value.msk);
setAttribute(_cloud_value.frm, "frm");
setAttribute(_cloud_value.to, "to");
setAttribute(_cloud_value.len, "len");
setAttribute(_cloud_value.msk, "msk");
}
};

Expand Down
6 changes: 3 additions & 3 deletions src/property/types/CloudString.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ class CloudString : public Property {
virtual void fromLocalToCloud() {
_cloud_value = _value;
}
virtual CborError appendAttributesToCloud() {
return appendAttribute(_value);
virtual CborError appendAttributesToCloud(CborEncoder *encoder) {
return appendAttribute(_value, "", encoder);
}
virtual void setAttributesFromCloud() {
setAttribute(_cloud_value);
setAttribute(_cloud_value, "");
}
//modifiers
CloudString& operator=(String v) {
Expand Down
6 changes: 3 additions & 3 deletions src/property/types/CloudUnsignedInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ class CloudUnsignedInt : public Property {
virtual void fromLocalToCloud() {
_cloud_value = _value;
}
virtual CborError appendAttributesToCloud() {
return appendAttribute(_value);
virtual CborError appendAttributesToCloud(CborEncoder *encoder) {
return appendAttribute(_value, "", encoder);
}
virtual void setAttributesFromCloud() {
setAttribute(_cloud_value);
setAttribute(_cloud_value, "");
}
//modifiers
CloudUnsignedInt& operator=(unsigned int v) {
Expand Down
6 changes: 3 additions & 3 deletions src/property/types/CloudWrapperBool.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ class CloudWrapperBool : public CloudWrapperBase {
virtual void fromLocalToCloud() {
_cloud_value = _primitive_value;
}
virtual CborError appendAttributesToCloud() {
return appendAttribute(_primitive_value);
virtual CborError appendAttributesToCloud(CborEncoder *encoder) {
return appendAttribute(_primitive_value, "", encoder);
}
virtual void setAttributesFromCloud() {
setAttribute(_cloud_value);
setAttribute(_cloud_value, "");
}
virtual bool isPrimitive() {
return true;
Expand Down
6 changes: 3 additions & 3 deletions src/property/types/CloudWrapperFloat.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ class CloudWrapperFloat : public CloudWrapperBase {
virtual void fromLocalToCloud() {
_cloud_value = _primitive_value;
}
virtual CborError appendAttributesToCloud() {
return appendAttribute(_primitive_value);
virtual CborError appendAttributesToCloud(CborEncoder *encoder) {
return appendAttribute(_primitive_value, "", encoder);
}
virtual void setAttributesFromCloud() {
setAttribute(_cloud_value);
setAttribute(_cloud_value, "");
}
virtual bool isPrimitive() {
return true;
Expand Down
6 changes: 3 additions & 3 deletions src/property/types/CloudWrapperInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ class CloudWrapperInt : public CloudWrapperBase {
virtual void fromLocalToCloud() {
_cloud_value = _primitive_value;
}
virtual CborError appendAttributesToCloud() {
return appendAttribute(_primitive_value);
virtual CborError appendAttributesToCloud(CborEncoder *encoder) {
return appendAttribute(_primitive_value, "", encoder);
}
virtual void setAttributesFromCloud() {
setAttribute(_cloud_value);
setAttribute(_cloud_value, "");
}
virtual bool isPrimitive() {
return true;
Expand Down
6 changes: 3 additions & 3 deletions src/property/types/CloudWrapperString.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ class CloudWrapperString : public CloudWrapperBase {
virtual void fromLocalToCloud() {
_cloud_value = _primitive_value;
}
virtual CborError appendAttributesToCloud() {
return appendAttribute(_primitive_value);
virtual CborError appendAttributesToCloud(CborEncoder *encoder) {
return appendAttribute(_primitive_value, "", encoder);
}
virtual void setAttributesFromCloud() {
setAttribute(_cloud_value);
setAttribute(_cloud_value, "");
}
virtual bool isPrimitive() {
return true;
Expand Down
Loading

0 comments on commit 27c05bc

Please sign in to comment.