-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathArduinoCloudThing.h
162 lines (138 loc) · 7.12 KB
/
ArduinoCloudThing.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
//
// 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_THING_H_
#define ARDUINO_CLOUD_THING_H_
/******************************************************************************
INCLUDE
******************************************************************************/
#undef max
#undef min
#include <list>
#include "ArduinoCloudProperty.h"
#include "types/CloudBool.h"
#include "types/CloudFloat.h"
#include "types/CloudInt.h"
#include "types/CloudString.h"
#include "types/CloudLocation.h"
#include "types/CloudColor.h"
#include "types/CloudWrapperBase.h"
#include "types/automation/CloudColoredLight.h"
#include "types/automation/CloudContactSensor.h"
#include "types/automation/CloudDimmedLight.h"
#include "types/automation/CloudLight.h"
#include "types/automation/CloudMotionSensor.h"
#include "types/automation/CloudSmartPlug.h"
#include "types/automation/CloudSwitch.h"
#include "types/automation/CloudTemperature.h"
#include "types/automation/CloudTelevision.h"
/******************************************************************************
CONSTANTS
******************************************************************************/
static bool const ON = true;
static bool const OFF = false;
static long const ON_CHANGE = -1;
static long const SECONDS = 1;
static long const MINUTES = 60;
static long const HOURS = 3600;
static long const DAYS = 86400;
/******************************************************************************
SYNCHRONIZATION CALLBACKS
******************************************************************************/
void onAutoSync(ArduinoCloudProperty & property);
#define MOST_RECENT_WINS onAutoSync
void onForceCloudSync(ArduinoCloudProperty & property);
#define CLOUD_WINS onForceCloudSync
void onForceDeviceSync(ArduinoCloudProperty & 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.
/******************************************************************************
CLASS DECLARATION
******************************************************************************/
class ArduinoCloudThing {
public:
ArduinoCloudThing();
void begin();
void registerGetTimeCallbackFunc(GetTimeCallbackFunc func);
//if propertyIdentifier is different from -1, an integer identifier is associated to the added property to be use instead of the property name when the parameter lightPayload is true in the encode method
ArduinoCloudProperty & addPropertyReal(ArduinoCloudProperty & property, String const & name, Permission const permission, int propertyIdentifier = -1);
/* encode return > 0 if a property has changed and encodes the changed properties in CBOR format into the provided buffer */
/* if lightPayload is true the integer identifier of the property will be encoded in the message instead of the property name in order to reduce the size of the message payload*/
int encode(uint8_t * data, size_t const size, bool lightPayload = false);
/* decode a CBOR payload received from the cloud */
void decode(uint8_t const * const payload, size_t const length, bool isSyncMessage = false);
bool isPropertyInContainer(String const & name);
int appendChangedProperties(CborEncoder * arrayEncoder, bool lightPayload);
void updateTimestampOnLocallyChangedProperties();
void updateProperty(String propertyName, unsigned long cloudChangeEventTime);
String getPropertyNameByIdentifier(int propertyIdentifier);
private:
GetTimeCallbackFunc _get_time_func;
std::list<ArduinoCloudProperty *> _property_list;
/* Keep track of the number of primitive properties in the Thing. If 0 it allows the early exit in updateTimestampOnLocallyChangedProperties() */
int _numPrimitivesProperties;
int _numProperties;
/* Indicates the if the message received to be decoded is a response to the getLastValues inquiry */
bool _isSyncMessage;
/* List of map data that will hold all the attributes of a property */
std::list<CborMapData *> _map_data_list;
/* Current property name during decoding: use to look for a new property in the senml value array */
String _currentPropertyName;
unsigned long _currentPropertyBaseTime,
_currentPropertyTime;
enum class MapParserState {
EnterMap,
MapKey,
UndefinedKey,
BaseVersion,
BaseName,
BaseTime,
Name,
Value,
StringValue,
BooleanValue,
Time,
LeaveMap,
Complete,
Error
};
MapParserState handle_EnterMap(CborValue * map_iter, CborValue * value_iter, CborMapData **map_data);
MapParserState handle_MapKey(CborValue * value_iter);
MapParserState handle_UndefinedKey(CborValue * value_iter);
MapParserState handle_BaseVersion(CborValue * value_iter, CborMapData * map_data);
MapParserState handle_BaseName(CborValue * value_iter, CborMapData * map_data);
MapParserState handle_BaseTime(CborValue * value_iter, CborMapData * map_data);
MapParserState handle_Name(CborValue * value_iter, CborMapData * map_data);
MapParserState handle_Value(CborValue * value_iter, CborMapData * map_data);
MapParserState handle_StringValue(CborValue * value_iter, CborMapData * map_data);
MapParserState handle_BooleanValue(CborValue * value_iter, CborMapData * map_data);
MapParserState handle_Time(CborValue * value_iter, CborMapData * map_data);
MapParserState handle_LeaveMap(CborValue * map_iter, CborValue * value_iter, CborMapData * map_data);
static bool ifNumericConvertToDouble(CborValue * value_iter, double * numeric_val);
static double convertCborHalfFloatToDouble(uint16_t const half_val);
void freeMapDataList(std::list<CborMapData *> * map_data_list);
inline void addProperty(ArduinoCloudProperty * property_obj, int propertyIdentifier) {
if (propertyIdentifier != -1) {
property_obj->setIdentifier(propertyIdentifier);
} else {
// if property identifier is -1, an incremental value will be assigned as identifier.
property_obj->setIdentifier(_numProperties);
}
_property_list.push_back(property_obj);
}
ArduinoCloudProperty * getProperty(String const & name);
ArduinoCloudProperty * getProperty(int const & identifier);
};
#endif /* ARDUINO_CLOUD_THING_H_ */