@@ -15,7 +15,6 @@ static void utox8(uint32_t val, char* s) {
15
15
for (int i = 0 ; i < 8 ; i++) {
16
16
int d = val & 0XF ;
17
17
val = (val >> 4 );
18
-
19
18
s[7 - i] = d > 9 ? ' A' + d - 10 : ' 0' + d;
20
19
}
21
20
}
@@ -42,27 +41,31 @@ ArduinoCloudThing::ArduinoCloudThing() {
42
41
43
42
44
43
void ArduinoCloudThing::begin () {
45
-
46
44
status = ON;
47
45
addPropertyReal (status, " status" , READ);
48
46
}
49
47
48
+
50
49
int ArduinoCloudThing::poll (uint8_t * data, size_t size) {
51
50
52
51
// check if backing storage and cloud has diverged
52
+ // time interval may be elapsed or property may be changed
53
53
int diff = 0 ;
54
54
55
+ // are there some changed prperies???
55
56
diff = checkNewData ();
56
57
if (diff > 0 ) {
57
58
CborError err;
58
59
CborEncoder encoder, arrayEncoder;
60
+
59
61
cbor_encoder_init (&encoder, data, size, 0 );
60
62
// 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 );
62
64
if (err) {
63
65
Serial.println (cbor_error_string (err));
64
66
return -1 ;
65
67
}
68
+
66
69
for (int i = 0 ; i < list.size (); i++) {
67
70
ArduinoCloudPropertyGeneric *p = list.get (i);
68
71
// 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) {
73
76
}
74
77
75
78
err = cbor_encoder_close_container (&encoder, &arrayEncoder);
76
- // update properties shadow values, in order to check if variable has changed since last publish
77
79
80
+ // update properties shadow values, in order to check if variable has changed since last publish
78
81
for (int i = 0 ; i < list.size (); i++) {
79
82
ArduinoCloudPropertyGeneric *p = list.get (i);
80
83
p->updateShadow ();
81
84
}
82
- // return the number of byte of the CBOR encoded array
85
+
86
+ // return the number of byte of the CBOR encoded array
83
87
return cbor_encoder_get_buffer_size (&encoder, data);
84
88
}
85
89
86
90
#if defined(DEBUG_MEMORY) && defined(ARDUINO_ARCH_SAMD)
87
91
PrintFreeRam ();
88
92
#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
90
94
return diff;
91
95
}
92
96
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
-
105
97
int ArduinoCloudThing::checkNewData () {
106
98
int counter = 0 ;
107
99
for (int i = 0 ; i < list.size (); i++) {
@@ -110,79 +102,100 @@ int ArduinoCloudThing::checkNewData() {
110
102
counter++;
111
103
}
112
104
}
105
+ // return number of props that has to be updated
113
106
return counter;
114
107
}
115
108
116
109
ArduinoCloudPropertyGeneric* ArduinoCloudThing::exists (String &name) {
117
110
for (int i = 0 ; i < list.size (); i++) {
118
111
ArduinoCloudPropertyGeneric *p = list.get (i);
112
+ // Check the property existance just comparing its name with existent ones
119
113
if (p->getName () == name) {
120
114
return p;
121
115
}
122
116
}
123
117
return NULL ;
124
118
}
125
119
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
+
126
132
ArduinoCloudPropertyGeneric& ArduinoCloudThing::addPropertyReal (int & property, String name, permissionType _permission, long seconds, void (*fn)(void ), int minDelta) {
127
133
if (ArduinoCloudPropertyGeneric* p = exists (name)) {
128
134
return *p;
129
135
}
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));
139
148
}
140
149
141
150
ArduinoCloudPropertyGeneric& ArduinoCloudThing::addPropertyReal (bool & property, String name, permissionType _permission, long seconds, void (*fn)(void ), bool minDelta) {
142
151
if (ArduinoCloudPropertyGeneric* p = exists (name)) {
143
152
return *p;
144
153
}
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 ));
152
161
}
153
162
154
163
ArduinoCloudPropertyGeneric& ArduinoCloudThing::addPropertyReal (float & property, String name, permissionType _permission, long seconds, void (*fn)(void ), float minDelta) {
155
164
if (ArduinoCloudPropertyGeneric* p = exists (name)) {
156
165
return *p;
157
166
}
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 ));
166
175
}
167
176
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 ) {
169
178
if (ArduinoCloudPropertyGeneric* p = exists (name)) {
170
179
return *p;
171
180
}
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 ));
179
188
}
180
189
181
- void ArduinoCloudThing::decode (uint8_t * payload, size_t length) {
190
+
191
+ void ArduinoCloudThing::decode (uint8_t *payload, size_t length) {
192
+
182
193
CborError err;
183
194
CborParser parser;
184
195
CborValue recursedMap, propValue, dataArray;
185
- int propId; String propType, propName;
196
+ int propId;
197
+ String propName;
198
+ propertyType propType;
186
199
187
200
err = cbor_parser_init (payload, length, 0 , &parser, &dataArray);
188
201
if (err) {
@@ -250,15 +263,15 @@ void ArduinoCloudThing::decode(uint8_t * payload, size_t length) {
250
263
// Check for the property type, write method internally check for the permission
251
264
propType = property->getType ();
252
265
253
- if (propType == " FLOAT" && !cbor_value_map_find_value (&recursedMap, " v" , &propValue)) {
266
+ if (propType == FLOAT && !cbor_value_map_find_value (&recursedMap, " v" , &propValue)) {
254
267
if (propValue.type == CborDoubleType) {
255
268
double val;
256
269
// get the value of the property as a double
257
270
cbor_value_get_double (&propValue, &val);
258
271
ArduinoCloudProperty<float >* p = (ArduinoCloudProperty<float >*) property;
259
272
p->write ((float )val);
260
273
}
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)) {
262
275
// if no key proper key was found, do nothing
263
276
if (propValue.type == CborIntegerType) {
264
277
int val;
@@ -272,14 +285,14 @@ void ArduinoCloudThing::decode(uint8_t * payload, size_t length) {
272
285
ArduinoCloudProperty<int >* p = (ArduinoCloudProperty<int >*) property;
273
286
p->write ((int )val);
274
287
}
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)) {
276
289
if (propValue.type == CborBooleanType) {
277
290
bool val;
278
291
cbor_value_get_boolean (&propValue, &val);
279
292
ArduinoCloudProperty<bool >* p = (ArduinoCloudProperty<bool >*) property;
280
293
p->write (val);
281
294
}
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)){
283
296
if (propValue.type == CborTextStringType) {
284
297
char *val; size_t valSize;
285
298
err = cbor_value_dup_text_string (&propValue, &val, &valSize, &propValue);
0 commit comments