Skip to content

Change CloudTime from CloudFloat to CloudUnsignedInt #277

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 7 commits into from
Oct 5, 2021
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
18 changes: 18 additions & 0 deletions extras/test/src/test_decode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,24 @@ SCENARIO("Arduino Cloud Properties are decoded", "[ArduinoCloudThing::decode]")

/************************************************************************************/

WHEN("A Time property is changed via CBOR message")
{
PropertyContainer property_container;

CloudTime test;
test = 0;
addPropertyToContainer(property_container, test, "test", Permission::ReadWrite);

/* [{0: "test", 2: 4294967295}] = 81 A2 00 64 74 65 73 74 02 1A FF FF FF FF */
uint8_t const payload[] = {0x81, 0xA2, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x02, 0x1A, 0xFF, 0xFF, 0xFF, 0xFF};
int const payload_length = sizeof(payload) / sizeof(uint8_t);
CBORDecoder::decode(property_container, payload, payload_length);

REQUIRE(test == 4294967295);
}

/************************************************************************************/

WHEN("Multiple properties is changed via CBOR message")
{
WHEN("Multiple properties of different type are changed via CBOR message")
Expand Down
26 changes: 21 additions & 5 deletions extras/test/src/test_encode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ SCENARIO("Arduino Cloud Properties are encoded", "[ArduinoCloudThing::encode-1]"
CloudInt int_test = 123;
addPropertyToContainer(property_container, int_test, "test", Permission::ReadWrite);

/* [{0: "test", 3: 123}] = 9F A2 00 64 74 65 73 74 02 18 7B FF */
/* [{0: "test", 2: 123}] = 9F A2 00 64 74 65 73 74 02 18 7B FF */
std::vector<uint8_t> const expected = {0x9F, 0xA2, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x02, 0x18, 0x7B, 0xFF};
std::vector<uint8_t> const actual = cbor::encode(property_container);
REQUIRE(actual == expected);
Expand Down Expand Up @@ -114,7 +114,7 @@ SCENARIO("Arduino Cloud Properties are encoded", "[ArduinoCloudThing::encode-1]"
CloudLocation location_test = CloudLocation(2.0f, 3.0f);
addPropertyToContainer(property_container, location_test, "test", Permission::ReadWrite);

/* [{0: "test:lat", 3: 2},{0: "test:lon", 3: 3}] = 9F A2 00 68 74 65 73 74 3A 6C 61 74 02 FA 40 00 00 00 A2 00 68 74 65 73 74 3A 6C 6F 6E 02 FA 40 40 00 00 FF*/
/* [{0: "test:lat", 2: 2},{0: "test:lon", 2: 3}] = 9F A2 00 68 74 65 73 74 3A 6C 61 74 02 FA 40 00 00 00 A2 00 68 74 65 73 74 3A 6C 6F 6E 02 FA 40 40 00 00 FF*/
std::vector<uint8_t> const expected = { 0x9F, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x6C, 0x61, 0x74, 0x02, 0xFA, 0x40, 0x00, 0x00, 0x00, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x6C, 0x6F, 0x6E, 0x02, 0xFA, 0x40, 0x40, 0x00, 0x00, 0xFF };
std::vector<uint8_t> const actual = cbor::encode(property_container);
REQUIRE(actual == expected);
Expand Down Expand Up @@ -164,7 +164,7 @@ SCENARIO("Arduino Cloud Properties are encoded", "[ArduinoCloudThing::encode-1]"
CloudColoredLight color_test = CloudColoredLight(true, 2.0, 2.0, 2.0);
addPropertyToContainer(property_container, color_test, "test", Permission::ReadWrite);

/* [{0: "test:swi", 4: true},{0: "test:hue", 2: 2.0},{0: "test:sat", 2: 2.0},{0: "test:bri", 2: 2.0}] = 83 A2 00 68 74 65 73 74 3A 73 77 69 04 F5 //A2 00 68 74 65 73 74 3A 68 75 65 02 FA 40 00 00 00 A2 00 68 74 65 73 74 3A 73 61 74 02 FA 40 00 00 00 A2 00 68 74 65 73 74 3A 62 72 69 02 FA 40 00 00 00 FF*/
/* [{0: "test:swi", 4: true},{0: "test:hue", 2: 2.0},{0: "test:sat", 2: 2.0},{0: "test:bri", 2: 2.0}] = 9F A2 00 68 74 65 73 74 3A 73 77 69 04 F5 A2 00 68 74 65 73 74 3A 68 75 65 02 FA 40 00 00 00 A2 00 68 74 65 73 74 3A 73 61 74 02 FA 40 00 00 00 A2 00 68 74 65 73 74 3A 62 72 69 02 FA 40 00 00 00 FF*/
std::vector<uint8_t> const expected = {0x9F, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x73, 0x77, 0x69, 0x04, 0xF5, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x68, 0x75, 0x65, 0x02, 0xFA, 0x40, 0x00, 0x00, 0x00, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x73, 0x61, 0x74, 0x02, 0xFA, 0x40, 0x00, 0x00, 0x00, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x62, 0x72, 0x69, 0x02, 0xFA, 0x40, 0x00, 0x00, 0x00, 0xFF };
std::vector<uint8_t> const actual = cbor::encode(property_container);
REQUIRE(actual == expected);
Expand All @@ -180,7 +180,7 @@ SCENARIO("Arduino Cloud Properties are encoded", "[ArduinoCloudThing::encode-1]"
CloudTelevision tv_test = CloudTelevision(true, 50, false, PlaybackCommands::Play, InputValue::TV, 7);
addPropertyToContainer(property_container, tv_test, "test", Permission::ReadWrite);

/* [{0: "test:swi", 4: true},{0: "test:vol", 2: 50},{0: "test:mut", 2: false},{0: "test:pbc", 2: 3},{0: "test:inp", 2: 55},{0: "test:cha", 2: 7}] = 9F A2 00 68 74 65 73 74 3A 73 77 69 04 F5 A2 00 68 74 65 73 74 3A 76 6F 6C 02 18 32 A2 00 68 74 65 73 74 3A 6D 75 74 04 F4 A2 00 68 74 65 73 74 3A 70 62 63 02 03 A2 00 68 74 65 73 74 3A 69 6E 70 02 18 37 A2 00 68 74 65 73 74 3A 63 68 61 02 07 FF */
/* [{0: "test:swi", 4: true},{0: "test:vol", 2: 50},{0: "test:mut", 4: false},{0: "test:pbc", 2: 3},{0: "test:inp", 2: 55},{0: "test:cha", 2: 7}] = 9F A2 00 68 74 65 73 74 3A 73 77 69 04 F5 A2 00 68 74 65 73 74 3A 76 6F 6C 02 18 32 A2 00 68 74 65 73 74 3A 6D 75 74 04 F4 A2 00 68 74 65 73 74 3A 70 62 63 02 03 A2 00 68 74 65 73 74 3A 69 6E 70 02 18 37 A2 00 68 74 65 73 74 3A 63 68 61 02 07 FF */
std::vector<uint8_t> const expected = {0x9F, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x73, 0x77, 0x69, 0x04, 0xF5, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x76, 0x6F, 0x6C, 0x02, 0x18, 0x32, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x6D, 0x75, 0x74, 0x04, 0xF4, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x70, 0x62, 0x63, 0x02, 0x03, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x69, 0x6E, 0x70, 0x02, 0x18, 0x37, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x63, 0x68, 0x61, 0x02, 0x07, 0xFF};
std::vector<uint8_t> const actual = cbor::encode(property_container);
REQUIRE(actual == expected);
Expand All @@ -196,7 +196,7 @@ SCENARIO("Arduino Cloud Properties are encoded", "[ArduinoCloudThing::encode-1]"
CloudDimmedLight color_test = CloudDimmedLight(true, 2.0);
addPropertyToContainer(property_container, color_test, "test", Permission::ReadWrite);

/* [{0: "test:swi", 4: true},{0: "test:hue", 2: 0.0},{0: "test:sat", 2: 0.0},{0: "test:bri", 2: 2.0}] = 83 A2 00 68 74 65 73 74 3A 73 77 69 04 F5 //A2 00 68 74 65 73 74 3A 68 75 65 02 FA 00 00 00 00 A2 00 68 74 65 73 74 3A 73 61 74 02 FA 00 00 00 00 A2 00 68 74 65 73 74 3A 62 72 69 02 FA 40 00 00 00 FF*/
/* [{0: "test:swi", 4: true},{0: "test:hue", 2: 0.0},{0: "test:sat", 2: 0.0},{0: "test:bri", 2: 2.0}] = 9F A2 00 68 74 65 73 74 3A 73 77 69 04 F5 A2 00 68 74 65 73 74 3A 68 75 65 02 FA 00 00 00 00 A2 00 68 74 65 73 74 3A 73 61 74 02 FA 00 00 00 00 A2 00 68 74 65 73 74 3A 62 72 69 02 FA 40 00 00 00 FF*/
std::vector<uint8_t> const expected = {0x9F, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x73, 0x77, 0x69, 0x04, 0xF5, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x68, 0x75, 0x65, 0x02, 0xFA, 0x00, 0x00, 0x00, 0x00, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x73, 0x61, 0x74, 0x02, 0xFA, 0x00, 0x00, 0x00, 0x00, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x62, 0x72, 0x69, 0x02, 0xFA, 0x40, 0x00, 0x00, 0x00, 0xFF };
std::vector<uint8_t> const actual = cbor::encode(property_container);
REQUIRE(actual == expected);
Expand Down Expand Up @@ -306,6 +306,22 @@ SCENARIO("Arduino Cloud Properties are encoded", "[ArduinoCloudThing::encode-1]"

/************************************************************************************/

WHEN("A time property is added")
{
PropertyContainer property_container;
cbor::encode(property_container);

CloudTime test = 1633342784;
addPropertyToContainer(property_container, test, "test", Permission::ReadWrite);

/* [{0: "test", 2: 1633342784}] = 9F A2 00 64 74 65 73 74 02 1A 61 5A D5 40 FF*/
std::vector<uint8_t> const expected = {0x9F, 0xA2, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x02, 0x1A, 0x61, 0x5A, 0xD5, 0x40, 0xFF};
std::vector<uint8_t> const actual = cbor::encode(property_container);
REQUIRE(actual == expected);
}

/************************************************************************************/

WHEN("Multiple properties are added")
{
PropertyContainer property_container;
Expand Down
22 changes: 22 additions & 0 deletions src/ArduinoIoTCloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,28 @@ Property& ArduinoIoTCloudClass::addPropertyReal(int& property, String name, int
return addPropertyToContainer(_property_container, *p, name, permission, tag);
}

void ArduinoIoTCloudClass::addPropertyReal(unsigned int& property, String name, permissionType permission_type, long seconds, void(*fn)(void), float minDelta, void(*synFn)(Property & property))
{
addPropertyReal(property, name, -1, permission_type, seconds, fn, minDelta, synFn);
}

void ArduinoIoTCloudClass::addPropertyReal(unsigned int& property, String name, int tag, permissionType permission_type, long seconds, void(*fn)(void), float minDelta, void(*synFn)(Property & property))
{
Property* p = new CloudWrapperUnsignedInt(property);
addPropertyReal(*p, name, tag, permission_type, seconds, fn, minDelta, synFn);
}

Property& ArduinoIoTCloudClass::addPropertyReal(unsigned int& property, String name, Permission const permission)
{
return addPropertyReal(property, name, -1, permission);
}

Property& ArduinoIoTCloudClass::addPropertyReal(unsigned int& property, String name, int tag, Permission const permission)
{
Property* p = new CloudWrapperUnsignedInt(property);
return addPropertyToContainer(_property_container, *p, name, permission, tag);
}

void ArduinoIoTCloudClass::addPropertyReal(String& property, String name, permissionType permission_type, long seconds, void(*fn)(void), float minDelta, void(*synFn)(Property & property))
{
addPropertyReal(property, name, -1, permission_type, seconds, fn, minDelta, synFn);
Expand Down
5 changes: 5 additions & 0 deletions src/ArduinoIoTCloud.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "property/types/CloudWrapperBool.h"
#include "property/types/CloudWrapperFloat.h"
#include "property/types/CloudWrapperInt.h"
#include "property/types/CloudWrapperUnsignedInt.h"
#include "property/types/CloudWrapperString.h"

#include "utility/time/TimeService.h"
Expand Down Expand Up @@ -110,12 +111,14 @@ class ArduinoIoTCloudClass
void addPropertyReal(bool& property, String name, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0f, void(*synFn)(Property & property) = CLOUD_WINS) __attribute__((deprecated("Use addProperty(property, Permission::ReadWrite) instead.")));
void addPropertyReal(float& property, String name, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0f, void(*synFn)(Property & property) = CLOUD_WINS) __attribute__((deprecated("Use addProperty(property, Permission::ReadWrite) instead.")));
void addPropertyReal(int& property, String name, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0, void(*synFn)(Property & property) = CLOUD_WINS) __attribute__((deprecated("Use addProperty(property, Permission::ReadWrite) instead.")));
void addPropertyReal(unsigned int& property, String name, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0, void(*synFn)(Property & property) = CLOUD_WINS) __attribute__((deprecated("Use addProperty(property, Permission::ReadWrite) instead.")));
void addPropertyReal(String& property, String name, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0f, void(*synFn)(Property & property) = CLOUD_WINS) __attribute__((deprecated("Use addProperty(property, Permission::ReadWrite) instead.")));

Property& addPropertyReal(Property& property, String name, Permission const permission);
Property& addPropertyReal(bool& property, String name, Permission const permission);
Property& addPropertyReal(float& property, String name, Permission const permission);
Property& addPropertyReal(int& property, String name, Permission const permission);
Property& addPropertyReal(unsigned int& property, String name, Permission const permission);
Property& addPropertyReal(String& property, String name, Permission const permission);

/* The following methods are for MKR WAN 1300/1310 LoRa boards since
Expand All @@ -128,12 +131,14 @@ class ArduinoIoTCloudClass
void addPropertyReal(bool& property, String name, int tag, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0f, void(*synFn)(Property & property) = CLOUD_WINS) __attribute__((deprecated("Use addProperty(property, Permission::ReadWrite) instead.")));
void addPropertyReal(float& property, String name, int tag, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0f, void(*synFn)(Property & property) = CLOUD_WINS) __attribute__((deprecated("Use addProperty(property, Permission::ReadWrite) instead.")));
void addPropertyReal(int& property, String name, int tag, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0, void(*synFn)(Property & property) = CLOUD_WINS) __attribute__((deprecated("Use addProperty(property, Permission::ReadWrite) instead.")));
void addPropertyReal(unsigned int& property, String name, int tag, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0, void(*synFn)(Property & property) = CLOUD_WINS) __attribute__((deprecated("Use addProperty(property, Permission::ReadWrite) instead.")));
void addPropertyReal(String& property, String name, int tag, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0f, void(*synFn)(Property & property) = CLOUD_WINS) __attribute__((deprecated("Use addProperty(property, Permission::ReadWrite) instead.")));

Property& addPropertyReal(Property& property, String name, int tag, Permission const permission);
Property& addPropertyReal(bool& property, String name, int tag, Permission const permission);
Property& addPropertyReal(float& property, String name, int tag, Permission const permission);
Property& addPropertyReal(int& property, String name, int tag, Permission const permission);
Property& addPropertyReal(unsigned int& property, String name, int tag, Permission const permission);
Property& addPropertyReal(String& property, String name, int tag, Permission const permission);

protected:
Expand Down
15 changes: 15 additions & 0 deletions src/property/Property.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ CborError Property::appendAttributeReal(int value, String attributeName, CborEnc
}, encoder);
}

CborError Property::appendAttributeReal(unsigned int value, String attributeName, CborEncoder *encoder) {
return appendAttributeName(attributeName, [value](CborEncoder & mapEncoder)
{
CHECK_CBOR(cbor_encode_int(&mapEncoder, static_cast<int>(CborIntegerMapKey::Value)));
CHECK_CBOR(cbor_encode_int(&mapEncoder, value));
return CborNoError;
}, encoder);
}

CborError Property::appendAttributeReal(float value, String attributeName, CborEncoder *encoder) {
return appendAttributeName(attributeName, [value](CborEncoder & mapEncoder)
{
Expand Down Expand Up @@ -264,6 +273,12 @@ void Property::setAttributeReal(int& value, String attributeName) {
});
}

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

void Property::setAttributeReal(float& value, String attributeName) {
setAttributeReal(attributeName, [&value](CborMapData & md) {
value = md.val.get();
Expand Down
2 changes: 2 additions & 0 deletions src/property/Property.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ class Property
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);
#ifndef __AVR__
Expand All @@ -198,6 +199,7 @@ class Property
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);
Expand Down
3 changes: 2 additions & 1 deletion src/property/PropertyContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "types/CloudBool.h"
#include "types/CloudFloat.h"
#include "types/CloudInt.h"
#include "types/CloudUnsignedInt.h"
#include "types/CloudString.h"
#include "types/CloudLocation.h"
#include "types/CloudColor.h"
Expand Down Expand Up @@ -72,7 +73,7 @@ typedef CloudFloat CloudElectricCurrent;
typedef CloudFloat CloudElectricPotential;
typedef CloudFloat CloudElectricResistance;
typedef CloudFloat CloudCapacitance;
typedef CloudFloat CloudTime;
typedef CloudUnsignedInt CloudTime;
typedef CloudFloat CloudFrequency;
typedef CloudFloat CloudDataRate;
typedef CloudFloat CloudHeartRate;
Expand Down
Loading