-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathProperty.h
270 lines (231 loc) · 9.28 KB
/
Property.h
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
//
// 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].
//
#ifndef ARDUINO_CLOUD_PROPERTY_HPP_
#define ARDUINO_CLOUD_PROPERTY_HPP_
#ifdef HOST
#define substring(...) substr(__VA_ARGS__)
#define indexOf(x) find(x)
#endif
/******************************************************************************
INCLUDE
******************************************************************************/
#include <Arduino.h>
#undef max
#undef min
# include <functional>
#include <list>
#include "../cbor/lib/tinycbor/cbor-lib.h"
/******************************************************************************
CONST
******************************************************************************/
namespace PropertyActions
{
extern const String CLEAR;
}
/******************************************************************************
ENUM
******************************************************************************/
/* Source: https://tools.ietf.org/html/rfc8428#section-6 */
enum class CborIntegerMapKey : int {
BaseVersion = -1, /* bver */
BaseName = -2, /* bn */
BaseTime = -3, /* bt */
BaseUnit = -4, /* bu */
BaseValue = -5, /* bv */
BaseSum = -6, /* bs */
Name = 0, /* n */
Unit = 1, /* u */
Value = 2, /* v */
StringValue = 3, /* vs */
BooleanValue = 4, /* vb */
Sum = 5, /* s */
Time = 6, /* t */
UpdateTime = 7, /* ut */
DataValue = 8 /* vd */
};
template <typename T>
class MapEntry {
public:
MapEntry() : _is_set(false) { }
inline void set(T const & entry) {
_entry = entry;
_is_set = true;
}
inline T const get() const {
return _entry;
}
inline bool isSet() const {
return _is_set;
}
inline void reset() {
_is_set = false;
}
private:
T _entry;
bool _is_set;
};
class CborMapData {
public:
MapEntry<int> base_version;
MapEntry<String> base_name;
MapEntry<double> base_time;
MapEntry<String> name;
MapEntry<int> name_identifier;
MapEntry<bool> light_payload;
MapEntry<String> attribute_name;
MapEntry<int> attribute_identifier;
MapEntry<int> property_identifier;
MapEntry<double> val;
MapEntry<String> str_val;
MapEntry<bool> bool_val;
MapEntry<double> time;
};
enum class Permission {
Read, Write, ReadWrite
};
enum class Type {
Bool, Int, Float, String
};
enum class UpdatePolicy {
OnChange, TimeInterval, OnDemand
};
enum class WritePolicy {
Auto, Manual
};
typedef void(*UpdateCallbackFunc)(void);
typedef unsigned long(*GetTimeCallbackFunc)();
class Property;
typedef void(*OnSyncCallbackFunc)(Property &);
/******************************************************************************
CLASS DECLARATION
******************************************************************************/
class Property
{
public:
Property();
void init(String const name, Permission const permission, GetTimeCallbackFunc func);
/* Composable configuration of the Property class */
Property & onUpdate(UpdateCallbackFunc func);
Property & onSync(OnSyncCallbackFunc func);
Property & publishOnChange(float const min_delta_property, unsigned long const min_time_between_updates_millis = DEFAULT_MIN_TIME_BETWEEN_UPDATES_MILLIS);
Property & publishEvery(unsigned long const seconds);
Property & publishOnDemand();
Property & encodeTimestamp();
Property & writeOnChange();
Property & writeOnDemand();
inline String name() const {
return _name;
}
inline int identifier() const {
return _identifier;
}
inline bool isReadableByCloud() const {
return (_permission == Permission::Read) || (_permission == Permission::ReadWrite);
}
inline bool isWriteableByCloud() const {
return (_permission == Permission::Write) || (_permission == Permission::ReadWrite);
}
inline bool isWritableOnChange() const {
return _write_policy == WritePolicy::Auto;
}
void setTimestamp(unsigned long const timestamp);
bool shouldBeUpdated();
void requestUpdate();
void appendCompleted();
void provideEcho();
void execCallbackOnChange();
void execCallbackOnSync();
void setLastCloudChangeTimestamp(unsigned long cloudChangeTime);
void setLastLocalChangeTimestamp(unsigned long localChangeTime);
unsigned long getLastCloudChangeTimestamp();
unsigned long getLastLocalChangeTimestamp();
void setIdentifier(int identifier);
void updateLocalTimestamp();
CborError append(CborEncoder * encoder, bool lightPayload);
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);
CborError appendAttributeName(String attributeName, std::function<CborError (CborEncoder& mapEncoder)>f, CborEncoder *encoder);
void setAttribute(String attributeName, std::function<void (CborMapData & md)>setValue);
void setAttributesFromCloud(std::list<CborMapData> * map_data_list);
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 appendAttributesToCloud(CborEncoder *encoder) = 0;
virtual void setAttributesFromCloud() = 0;
virtual bool isPrimitive() {
return false;
};
static unsigned long const DEFAULT_MIN_TIME_BETWEEN_UPDATES_MILLIS = 500; /* Data rate throttled to 2 Hz */
protected:
/* Variables used for UpdatePolicy::OnChange */
String _name;
float _min_delta_property;
unsigned long _min_time_between_updates_millis;
private:
Permission _permission;
WritePolicy _write_policy;
GetTimeCallbackFunc _get_time_func;
UpdateCallbackFunc _update_callback_func;
OnSyncCallbackFunc _on_sync_callback_func;
UpdatePolicy _update_policy;
bool _has_been_updated_once,
_has_been_modified_in_callback,
_has_been_appended_but_not_sended;
/* Variables used for UpdatePolicy::TimeInterval */
unsigned long _last_updated_millis,
_update_interval_millis;
/* Variables used for reconnection sync*/
unsigned long _last_local_change_timestamp;
unsigned long _last_cloud_change_timestamp;
std::list<CborMapData> * _map_data_list;
/* Store the identifier of the property in the array list */
int _identifier;
int _attributeIdentifier;
/* Indicates if the property shall be encoded using the identifier instead of the name */
bool _lightPayload;
/* Indicates whether a property update has been requested in case of the OnDemand update policy. */
bool _update_requested;
/* Indicates whether the timestamp shall be encoded in the property or not */
bool _encode_timestamp;
/* Indicates if the property shall be echoed back to the cloud even if unchanged */
bool _echo_requested;
unsigned long _timestamp;
};
/******************************************************************************
PROTOTYPE FREE FUNCTIONs
******************************************************************************/
inline bool operator == (Property const & lhs, Property const & rhs) {
return (lhs.name() == rhs.name());
}
/******************************************************************************
SYNCHRONIZATION CALLBACKS
******************************************************************************/
void onAutoSync(Property & property);
#define MOST_RECENT_WINS onAutoSync
void onForceCloudSync(Property & property);
#define CLOUD_WINS onForceCloudSync
void onForceDeviceSync(Property & property);
#define DEVICE_WINS onForceDeviceSync // The device property value is already the correct one. The cloud property value will be synchronized at the next update cycle.
#endif /* ARDUINO_CLOUD_PROPERTY_HPP_ */