Skip to content
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

Remove attributes macros #339

Merged
merged 8 commits into from
Dec 1, 2022
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