forked from arduino-libraries/ArduinoIoTCloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProperty.cpp
403 lines (352 loc) · 12.4 KB
/
Property.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//
// This file is part of ArduinoCloudThing
//
// Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of ArduinoCloudThing.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to modify or
// otherwise use the software for commercial activities involving the Arduino
// software without disclosing the source code of your own applications. To purchase
// a commercial license, send an email to [email protected].
//
#include "Property.h"
#undef max
#undef min
#include <algorithm>
/******************************************************************************
CTOR/DTOR
******************************************************************************/
Property::Property()
: _name{""}
, _min_delta_property{0.0f}
, _min_time_between_updates_millis{DEFAULT_MIN_TIME_BETWEEN_UPDATES_MILLIS}
, _permission{Permission::Read}
, _get_time_func{nullptr}
, _update_callback_func{nullptr}
, _on_sync_callback_func{nullptr}
, _update_policy{UpdatePolicy::OnChange}
, _has_been_updated_once{false}
, _has_been_modified_in_callback{false}
, _has_been_appended_but_not_sended{false}
, _last_updated_millis{0}
, _update_interval_millis{0}
, _last_local_change_timestamp{0}
, _last_cloud_change_timestamp{0}
, _identifier{0}
, _attributeIdentifier{0}
, _lightPayload{false}
, _update_requested{false}
, _encode_timestamp{false}
, _echo_requested{false}
, _timestamp{0}
{
}
/******************************************************************************
CONST
******************************************************************************/
namespace PropertyActions
{
const String CLEAR = "\x1b";
}
/******************************************************************************
PUBLIC MEMBER FUNCTIONS
******************************************************************************/
void Property::init(String const name, Permission const permission, GetTimeCallbackFunc func) {
_name = name;
_permission = permission;
_get_time_func = func;
}
Property & Property::onUpdate(UpdateCallbackFunc func) {
_update_callback_func = func;
return (*this);
}
Property & Property::onSync(OnSyncCallbackFunc func) {
_on_sync_callback_func = func;
return (*this);
}
Property & Property::publishOnChange(float const min_delta_property, unsigned long const min_time_between_updates_millis) {
_update_policy = UpdatePolicy::OnChange;
_min_delta_property = min_delta_property;
_min_time_between_updates_millis = min_time_between_updates_millis;
return (*this);
}
Property & Property::publishEvery(unsigned long const seconds) {
_update_policy = UpdatePolicy::TimeInterval;
_update_interval_millis = (seconds * 1000);
return (*this);
}
Property & Property::publishOnDemand() {
_update_policy = UpdatePolicy::OnDemand;
return (*this);
}
Property & Property::encodeTimestamp()
{
_encode_timestamp = true;
return (*this);
}
void Property::setTimestamp(unsigned long const timestamp)
{
_timestamp = timestamp;
}
bool Property::shouldBeUpdated() {
if (!_has_been_updated_once) {
return true;
}
if (_has_been_appended_but_not_sended) {
return true;
}
if (_has_been_modified_in_callback) {
return true;
}
if (_echo_requested) {
return true;
}
if (_update_policy == UpdatePolicy::OnChange) {
return (isDifferentFromCloud() && ((millis() - _last_updated_millis) >= (_min_time_between_updates_millis)));
} else if (_update_policy == UpdatePolicy::TimeInterval) {
return ((millis() - _last_updated_millis) >= _update_interval_millis);
} else if (_update_policy == UpdatePolicy::OnDemand) {
return _update_requested;
} else {
return false;
}
}
void Property::requestUpdate()
{
_update_requested = true;
}
void Property::provideEcho()
{
_echo_requested = true;
}
void Property::appendCompleted()
{
if (_has_been_appended_but_not_sended) {
_has_been_appended_but_not_sended = false;
}
}
void Property::execCallbackOnChange() {
if (_update_callback_func != nullptr) {
_update_callback_func();
}
if (isDifferentFromCloud()) {
_has_been_modified_in_callback = true;
}
}
void Property::execCallbackOnSync() {
if (_on_sync_callback_func != nullptr) {
_on_sync_callback_func(*this);
}
}
CborError Property::append(CborEncoder *encoder, bool lightPayload) {
_lightPayload = lightPayload;
_attributeIdentifier = 0;
CHECK_CBOR(appendAttributesToCloud(encoder));
fromLocalToCloud();
_has_been_updated_once = true;
_has_been_modified_in_callback = false;
_update_requested = false;
_echo_requested = false;
_has_been_appended_but_not_sended = true;
_last_updated_millis = millis();
return CborNoError;
}
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)));
CHECK_CBOR(cbor_encode_boolean(&mapEncoder, value));
return CborNoError;
}, 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)));
CHECK_CBOR(cbor_encode_int(&mapEncoder, value));
return CborNoError;
}, 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)));
CHECK_CBOR(cbor_encode_int(&mapEncoder, value));
return CborNoError;
}, 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)));
CHECK_CBOR(cbor_encode_float(&mapEncoder, value));
return CborNoError;
}, 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)));
CHECK_CBOR(cbor_encode_text_stringz(&mapEncoder, value.c_str()));
return CborNoError;
}, encoder);
}
#ifdef __AVR__
CborError Property::appendAttributeName(String attributeName, nonstd::function<CborError (CborEncoder& mapEncoder)>appendValue, CborEncoder *encoder)
#else
CborError Property::appendAttributeName(String attributeName, std::function<CborError (CborEncoder& mapEncoder)>appendValue, CborEncoder *encoder)
#endif
{
if (attributeName != "") {
// when the attribute name string is not empty, the attribute identifier is incremented in order to be encoded in the message if the _lightPayload flag is set
_attributeIdentifier++;
}
CborEncoder mapEncoder;
unsigned int num_map_properties = _encode_timestamp ? 3 : 2;
CHECK_CBOR(cbor_encoder_create_map(encoder, &mapEncoder, num_map_properties));
CHECK_CBOR(cbor_encode_int(&mapEncoder, static_cast<int>(CborIntegerMapKey::Name)));
// if _lightPayload is true, the property and attribute identifiers will be encoded instead of the property name
if (_lightPayload)
{
// the most significant byte of the identifier to be encoded represent the property identifier
int completeIdentifier = _attributeIdentifier * 256;
// the least significant byte of the identifier to be encoded represent the attribute identifier
completeIdentifier += _identifier;
CHECK_CBOR(cbor_encode_int(&mapEncoder, completeIdentifier));
}
else
{
String completeName = _name;
if (attributeName != "") {
completeName += ":" + attributeName;
}
CHECK_CBOR(cbor_encode_text_stringz(&mapEncoder, completeName.c_str()));
}
/* Encode the value */
CHECK_CBOR(appendValue(mapEncoder));
/* Encode the timestamp if that has been required. */
if(_encode_timestamp)
{
CHECK_CBOR(cbor_encode_int (&mapEncoder, static_cast<int>(CborIntegerMapKey::Time)));
CHECK_CBOR(cbor_encode_uint(&mapEncoder, _timestamp));
}
/* Close the container */
CHECK_CBOR(cbor_encoder_close_container(encoder, &mapEncoder));
return CborNoError;
}
void Property::setAttributesFromCloud(std::list<CborMapData> * map_data_list) {
_map_data_list = map_data_list;
_attributeIdentifier = 0;
setAttributesFromCloud();
}
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();
} else if (md.val.isSet()) {
if (md.val.get() == 0) {
value = false;
} else if (md.val.get() == 1) {
value = true;
} else {
/* This should not happen. Leave the previous value */
}
}
});
}
void Property::setAttribute(int& value, String attributeName) {
setAttribute(attributeName, [&value](CborMapData & md) {
value = md.val.get();
});
}
void Property::setAttribute(unsigned int& value, String attributeName) {
setAttribute(attributeName, [&value](CborMapData & md) {
value = md.val.get();
});
}
void Property::setAttribute(float& value, String attributeName) {
setAttribute(attributeName, [&value](CborMapData & md) {
value = md.val.get();
});
}
void Property::setAttribute(String& value, String attributeName) {
setAttribute(attributeName, [&value](CborMapData & md) {
value = md.str_val.get();
});
}
#ifdef __AVR__
void Property::setAttribute(String attributeName, nonstd::function<void (CborMapData & md)>setValue)
#else
void Property::setAttribute(String attributeName, std::function<void (CborMapData & md)>setValue)
#endif
{
if (attributeName != "") {
_attributeIdentifier++;
}
std::for_each(_map_data_list->begin(),
_map_data_list->end(),
[this, attributeName, setValue](CborMapData & map)
{
if (map.light_payload.isSet() && map.light_payload.get())
{
// if a light payload is detected, the attribute identifier is retrieved from the cbor map and the corresponding attribute is updated
int attid = map.attribute_identifier.get();
if (attid == _attributeIdentifier) {
setValue(map);
return;
}
}
else
{
// if a normal payload is detected, the name of the attribute to be updated is extracted directly from the cbor map
String an = map.attribute_name.get();
if (an == attributeName) {
setValue(map);
return;
}
}
});
}
void Property::updateLocalTimestamp() {
if (isReadableByCloud()) {
if (_get_time_func) {
_last_local_change_timestamp = _get_time_func();
}
}
}
void Property::setLastCloudChangeTimestamp(unsigned long cloudChangeEventTime) {
_last_cloud_change_timestamp = cloudChangeEventTime;
}
void Property::setLastLocalChangeTimestamp(unsigned long localChangeTime) {
_last_local_change_timestamp = localChangeTime;
}
unsigned long Property::getLastCloudChangeTimestamp() {
return _last_cloud_change_timestamp;
}
unsigned long Property::getLastLocalChangeTimestamp() {
return _last_local_change_timestamp;
}
void Property::setIdentifier(int identifier) {
_identifier = identifier;
}
/******************************************************************************
SYNCHRONIZATION CALLBACKS
******************************************************************************/
void onAutoSync(Property & property) {
if (property.getLastCloudChangeTimestamp() > property.getLastLocalChangeTimestamp()) {
property.fromCloudToLocal();
property.execCallbackOnChange();
}
}
void onForceCloudSync(Property & property) {
property.fromCloudToLocal();
property.execCallbackOnChange();
}
void onForceDeviceSync(Property & /* property */) {
}