Skip to content

Commit 5080d9d

Browse files
committed
Updated poll and addPropertyReal
1 parent 873f91d commit 5080d9d

File tree

2 files changed

+70
-58
lines changed

2 files changed

+70
-58
lines changed

ArduinoCloudThing.cpp

Lines changed: 69 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ static void utox8(uint32_t val, char* s) {
1515
for (int i = 0; i < 8; i++) {
1616
int d = val & 0XF;
1717
val = (val >> 4);
18-
1918
s[7 - i] = d > 9 ? 'A' + d - 10 : '0' + d;
2019
}
2120
}
@@ -42,27 +41,31 @@ ArduinoCloudThing::ArduinoCloudThing() {
4241

4342

4443
void ArduinoCloudThing::begin() {
45-
4644
status = ON;
4745
addPropertyReal(status, "status", READ);
4846
}
4947

48+
5049
int ArduinoCloudThing::poll(uint8_t* data, size_t size) {
5150

5251
// check if backing storage and cloud has diverged
52+
// time interval may be elapsed or property may be changed
5353
int diff = 0;
5454

55+
// are there some changed prperies???
5556
diff = checkNewData();
5657
if (diff > 0) {
5758
CborError err;
5859
CborEncoder encoder, arrayEncoder;
60+
5961
cbor_encoder_init(&encoder, data, size, 0);
6062
// create a cbor array containing the property that should be updated.
61-
err = cbor_encoder_create_array(&encoder, &arrayEncoder, diff);
63+
err = cbor_encoder_create_array(&encoder, &arrayEncoder, CborIndefiniteLength);
6264
if (err) {
6365
Serial.println(cbor_error_string(err));
6466
return -1;
6567
}
68+
6669
for (int i = 0; i < list.size(); i++) {
6770
ArduinoCloudPropertyGeneric *p = list.get(i);
6871
// If a property should be updated and has read permission from the Cloud point of view
@@ -73,35 +76,24 @@ int ArduinoCloudThing::poll(uint8_t* data, size_t size) {
7376
}
7477

7578
err = cbor_encoder_close_container(&encoder, &arrayEncoder);
76-
// update properties shadow values, in order to check if variable has changed since last publish
7779

80+
// update properties shadow values, in order to check if variable has changed since last publish
7881
for (int i = 0; i < list.size(); i++) {
7982
ArduinoCloudPropertyGeneric *p = list.get(i);
8083
p->updateShadow();
8184
}
82-
// return the number of byte of the CBOR encoded array
85+
86+
// return the number of byte of the CBOR encoded array
8387
return cbor_encoder_get_buffer_size(&encoder, data);
8488
}
8589

8690
#if defined(DEBUG_MEMORY) && defined(ARDUINO_ARCH_SAMD)
8791
PrintFreeRam();
8892
#endif
89-
// If nothing has to be sent, return diff, that is 0 in this case
93+
// If nothing has to be sent, return diff, that is 0 in this case
9094
return diff;
9195
}
9296

93-
// It return the index of the property, inside the local array, with the name passed as parameter. (-1 if it does not exist.)
94-
int ArduinoCloudThing::findPropertyByName(String &name) {
95-
for (int i = 0; i < list.size(); i++) {
96-
ArduinoCloudPropertyGeneric *p = list.get(i);
97-
// Check the property existance just comparing its name with existent ones
98-
if (p->getName() == name) {
99-
return i;
100-
}
101-
}
102-
return -1;
103-
}
104-
10597
int ArduinoCloudThing::checkNewData() {
10698
int counter = 0;
10799
for (int i = 0; i < list.size(); i++) {
@@ -110,79 +102,100 @@ int ArduinoCloudThing::checkNewData() {
110102
counter++;
111103
}
112104
}
105+
// return number of props that has to be updated
113106
return counter;
114107
}
115108

116109
ArduinoCloudPropertyGeneric* ArduinoCloudThing::exists(String &name) {
117110
for (int i = 0; i < list.size(); i++) {
118111
ArduinoCloudPropertyGeneric *p = list.get(i);
112+
// Check the property existance just comparing its name with existent ones
119113
if (p->getName() == name) {
120114
return p;
121115
}
122116
}
123117
return NULL;
124118
}
125119

120+
// It return the index of the property, inside the local array, with the name passed as parameter. (-1 if it does not exist.)
121+
int ArduinoCloudThing::findPropertyByName(String &name) {
122+
for (int i = 0; i < list.size(); i++) {
123+
ArduinoCloudPropertyGeneric *p = list.get(i);
124+
// Check the property existance just comparing its name with existent ones
125+
if (p->getName() == name) {
126+
return i;
127+
}
128+
}
129+
return -1;
130+
}
131+
126132
ArduinoCloudPropertyGeneric& ArduinoCloudThing::addPropertyReal(int& property, String name, permissionType _permission, long seconds, void(*fn)(void), int minDelta) {
127133
if (ArduinoCloudPropertyGeneric* p = exists(name)) {
128134
return *p;
129135
}
130-
ArduinoCloudProperty<int> *thing = new ArduinoCloudProperty<int>(property, name);
131-
list.add(thing);
132-
thing->shadow_property = -1;
133-
thing->minDelta = minDelta;
134-
thing->permission = _permission;
135-
thing->minDelta = minDelta;
136-
thing->updatePolicy = seconds;
137-
thing->callback = fn;
138-
return *(reinterpret_cast<ArduinoCloudPropertyGeneric*>(thing));
136+
// If a property with ythis name does not exist, create it into thing
137+
ArduinoCloudProperty<int> *propertyObj = new ArduinoCloudProperty<int>(property, name);
138+
// Initialize property data members, this is a friend class of ArduinoCloudProperty
139+
propertyObj->shadow_property = -1;
140+
propertyObj->minDelta = minDelta;
141+
propertyObj->permission = _permission;
142+
propertyObj->updatePolicy = seconds;
143+
propertyObj->callback = fn;
144+
// Add the new property to the thin properties list
145+
list.add(propertyObj);
146+
// Return the new property as a generic one
147+
return *(reinterpret_cast<ArduinoCloudPropertyGeneric*>(propertyObj));
139148
}
140149

141150
ArduinoCloudPropertyGeneric& ArduinoCloudThing::addPropertyReal(bool& property, String name, permissionType _permission, long seconds, void(*fn)(void), bool minDelta) {
142151
if (ArduinoCloudPropertyGeneric* p = exists(name)) {
143152
return *p;
144153
}
145-
ArduinoCloudProperty<bool> *thing = new ArduinoCloudProperty<bool>(property, name);
146-
list.add(thing);
147-
thing->shadow_property = !property;
148-
thing->permission = _permission;
149-
thing->updatePolicy = seconds;
150-
thing->callback = fn;
151-
return *(reinterpret_cast<ArduinoCloudPropertyGeneric*>(thing));
154+
ArduinoCloudProperty<bool> *propertyObj = new ArduinoCloudProperty<bool>(property, name);
155+
propertyObj->shadow_property = !property;
156+
propertyObj->permission = _permission;
157+
propertyObj->updatePolicy = seconds;
158+
propertyObj->callback = fn;
159+
list.add(propertyObj);
160+
return *(reinterpret_cast<ArduinoCloudPropertyGeneric*>(propertyObj));
152161
}
153162

154163
ArduinoCloudPropertyGeneric& ArduinoCloudThing::addPropertyReal(float& property, String name, permissionType _permission, long seconds, void(*fn)(void), float minDelta) {
155164
if (ArduinoCloudPropertyGeneric* p = exists(name)) {
156165
return *p;
157166
}
158-
ArduinoCloudProperty<float> *thing = new ArduinoCloudProperty<float>(property, name);
159-
list.add(thing);
160-
thing->permission = _permission;
161-
thing->minDelta = minDelta;
162-
thing->updatePolicy = seconds;
163-
thing->shadow_property = property - 1.0f;
164-
thing->callback = fn;
165-
return *(reinterpret_cast<ArduinoCloudPropertyGeneric*>(thing));
167+
ArduinoCloudProperty<float> *propertyObj = new ArduinoCloudProperty<float>(property, name);
168+
propertyObj->shadow_property = property + 0.5f;
169+
propertyObj->permission = _permission;
170+
propertyObj->minDelta = minDelta;
171+
propertyObj->updatePolicy = seconds;
172+
propertyObj->callback = fn;
173+
list.add(propertyObj);
174+
return *(reinterpret_cast<ArduinoCloudPropertyGeneric*>(propertyObj));
166175
}
167176

168-
ArduinoCloudPropertyGeneric& ArduinoCloudThing::addPropertyReal(String& property, String name, permissionType _permission, long seconds, void(*fn)(void), String mindelta) {
177+
ArduinoCloudPropertyGeneric& ArduinoCloudThing::addPropertyReal(String& property, String name, permissionType _permission, long seconds, void(*fn)(void), String minDelta) {
169178
if (ArduinoCloudPropertyGeneric* p = exists(name)) {
170179
return *p;
171180
}
172-
ArduinoCloudProperty<String> *thing = new ArduinoCloudProperty<String>(property, name);
173-
list.add(thing);
174-
thing->shadow_property = "";
175-
thing->permission = _permission;
176-
thing->updatePolicy = seconds;
177-
thing->callback = fn;
178-
return *(reinterpret_cast<ArduinoCloudPropertyGeneric*>(thing));
181+
ArduinoCloudProperty<String> *propertyObj = new ArduinoCloudProperty<String>(property, name);
182+
propertyObj->shadow_property = property + "x";
183+
propertyObj->permission = _permission;
184+
propertyObj->updatePolicy = seconds;
185+
propertyObj->callback = fn;
186+
list.add(propertyObj);
187+
return *(reinterpret_cast<ArduinoCloudPropertyGeneric*>(propertyObj));
179188
}
180189

181-
void ArduinoCloudThing::decode(uint8_t * payload, size_t length) {
190+
191+
void ArduinoCloudThing::decode(uint8_t *payload, size_t length) {
192+
182193
CborError err;
183194
CborParser parser;
184195
CborValue recursedMap, propValue, dataArray;
185-
int propId; String propType, propName;
196+
int propId;
197+
String propName;
198+
propertyType propType;
186199

187200
err = cbor_parser_init(payload, length, 0, &parser, &dataArray);
188201
if(err) {
@@ -250,15 +263,15 @@ void ArduinoCloudThing::decode(uint8_t * payload, size_t length) {
250263
// Check for the property type, write method internally check for the permission
251264
propType = property->getType();
252265

253-
if (propType == "FLOAT" && !cbor_value_map_find_value(&recursedMap, "v", &propValue)) {
266+
if (propType == FLOAT && !cbor_value_map_find_value(&recursedMap, "v", &propValue)) {
254267
if (propValue.type == CborDoubleType) {
255268
double val;
256269
// get the value of the property as a double
257270
cbor_value_get_double(&propValue, &val);
258271
ArduinoCloudProperty<float>* p = (ArduinoCloudProperty<float>*) property;
259272
p->write((float)val);
260273
}
261-
} else if (propType == "INT" && !cbor_value_map_find_value(&recursedMap, "v", &propValue)) {
274+
} else if (propType == INT && !cbor_value_map_find_value(&recursedMap, "v", &propValue)) {
262275
// if no key proper key was found, do nothing
263276
if (propValue.type == CborIntegerType) {
264277
int val;
@@ -272,14 +285,14 @@ void ArduinoCloudThing::decode(uint8_t * payload, size_t length) {
272285
ArduinoCloudProperty<int>* p = (ArduinoCloudProperty<int>*) property;
273286
p->write((int)val);
274287
}
275-
} else if (propType == "BOOL" && !cbor_value_map_find_value(&recursedMap, "vb", &propValue)) {
288+
} else if (propType == BOOL && !cbor_value_map_find_value(&recursedMap, "vb", &propValue)) {
276289
if (propValue.type == CborBooleanType) {
277290
bool val;
278291
cbor_value_get_boolean(&propValue, &val);
279292
ArduinoCloudProperty<bool>* p = (ArduinoCloudProperty<bool>*) property;
280293
p->write(val);
281294
}
282-
} else if (propType == "STRING" && !cbor_value_map_find_value(&recursedMap, "vs", &propValue)){
295+
} else if (propType == STRING && !cbor_value_map_find_value(&recursedMap, "vs", &propValue)){
283296
if (propValue.type == CborTextStringType) {
284297
char *val; size_t valSize;
285298
err = cbor_value_dup_text_string(&propValue, &val, &valSize, &propValue);

ArduinoCloudThing.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class ArduinoCloudThing {
5454
public:
5555
ArduinoCloudThing();
5656
void begin();
57-
// overload for different type of properties
57+
// overload, with default arguments, of different type of properties
5858
ArduinoCloudPropertyGeneric& addPropertyReal(int& property, String name, permissionType _permission = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, int minDelta = 0);
5959
ArduinoCloudPropertyGeneric& addPropertyReal(bool& property, String name, permissionType _permission = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, bool minDelta = false);
6060
ArduinoCloudPropertyGeneric& addPropertyReal(float& property, String name, permissionType _permission = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0f);
@@ -63,7 +63,6 @@ class ArduinoCloudThing {
6363
int poll(uint8_t* data, size_t size);
6464
// decode a CBOR payload received from the Cloud.
6565
void decode(uint8_t * payload, size_t length);
66-
bool hasAllReadProperties();
6766

6867
private:
6968
void update();

0 commit comments

Comments
 (0)