-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathArduinoCloudProperty.h
224 lines (192 loc) · 7.74 KB
/
ArduinoCloudProperty.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
//
// 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>
// in order to allow <functional> to define its own max and min functions
#undef max
#undef min
#include <list>
#include <functional>
#include "lib/tinycbor/cbor-lib.h"
#define appendAttributesToCloud() appendAttributesToCloudReal(CborEncoder *encoder)
#define appendAttribute(x) appendAttributeReal(x, getAttributeName(#x, '.'), encoder)
#define setAttribute(x) setAttributeReal(x, getAttributeName(#x, '.'))
/******************************************************************************
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<float> 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
};
typedef void(*UpdateCallbackFunc)(void);
typedef unsigned long(*GetTimeCallbackFunc)();
/******************************************************************************
CLASS DECLARATION
******************************************************************************/
class ArduinoCloudProperty {
typedef void(*SyncCallbackFunc)(ArduinoCloudProperty &property);
public:
ArduinoCloudProperty();
void init(String const name, Permission const permission, GetTimeCallbackFunc func);
/* Composable configuration of the ArduinoCloudProperty class */
ArduinoCloudProperty & onUpdate(UpdateCallbackFunc func);
ArduinoCloudProperty & onSync(SyncCallbackFunc func);
ArduinoCloudProperty & publishOnChange(float const min_delta_property, unsigned long const min_time_between_updates_millis = 0);
ArduinoCloudProperty & publishEvery(unsigned long const seconds);
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);
}
bool shouldBeUpdated();
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();
void append(CborEncoder * encoder, bool lightPayload);
void appendAttributeReal(bool value, String attributeName = "", CborEncoder *encoder = nullptr);
void appendAttributeReal(int value, String attributeName = "", CborEncoder *encoder = nullptr);
void appendAttributeReal(float value, String attributeName = "", CborEncoder *encoder = nullptr);
void appendAttributeReal(String value, String attributeName = "", CborEncoder *encoder = nullptr);
void appendAttributeName(String attributeName, std::function<void (CborEncoder& mapEncoder)>f, CborEncoder *encoder);
void setAttributesFromCloud(std::list<CborMapData *> * map_data_list);
void setAttributeReal(bool& value, String attributeName = "");
void setAttributeReal(int& value, String attributeName = "");
void setAttributeReal(float& value, String attributeName = "");
void setAttributeReal(String& value, String attributeName = "");
void setAttributeReal(String attributeName, std::function<void (CborMapData *md)>setValue);
String getAttributeName(String propertyName, char separator);
virtual bool isDifferentFromCloud() = 0;
virtual void fromCloudToLocal() = 0;
virtual void fromLocalToCloud() = 0;
virtual void appendAttributesToCloudReal(CborEncoder *encoder) = 0;
virtual void setAttributesFromCloud() = 0;
virtual bool isPrimitive() {
return false;
};
protected:
/* Variables used for UpdatePolicy::OnChange */
String _name;
float _min_delta_property;
unsigned long _min_time_between_updates_millis;
private:
Permission _permission;
GetTimeCallbackFunc _get_time_func;
UpdateCallbackFunc _update_callback_func;
void (*_sync_callback_func)(ArduinoCloudProperty &property);
UpdatePolicy _update_policy;
bool _has_been_updated_once,
_has_been_modified_in_callback;
/* 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;
};
/******************************************************************************
PROTOTYPE FREE FUNCTIONs
******************************************************************************/
inline bool operator == (ArduinoCloudProperty const & lhs, ArduinoCloudProperty const & rhs) {
return (lhs.name() == rhs.name());
}
#endif /* ARDUINO_CLOUD_PROPERTY_HPP_ */