This repository was archived by the owner on Aug 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathArduinoCloudProperty.hpp
120 lines (91 loc) · 3.99 KB
/
ArduinoCloudProperty.hpp
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
#ifndef ARDUINO_CLOUD_PROPERTY_HPP_
#define ARDUINO_CLOUD_PROPERTY_HPP_
/******************************************************************************
* INCLUDE
******************************************************************************/
#include <Arduino.h>
#include "lib/tinycbor/cbor-lib.h"
/******************************************************************************
* TYPEDEF
******************************************************************************/
/******************************************************************************
* TYPEDEF
******************************************************************************/
enum class CloudProtocol {
V1, /* [{"n": "test", "vb": true}] */
V2 /* [{0: "test", 4: true}] */
};
enum class Permission {
Read, Write, ReadWrite
};
enum class Type {
Bool, Int, Float, String
};
enum class UpdatePolicy {
OnChange, TimeInterval
};
/* 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 */
};
typedef void(*UpdateCallbackFunc)(void);
/******************************************************************************
* CLASS DECLARATION
******************************************************************************/
template <typename T>
class ArduinoCloudProperty {
public:
ArduinoCloudProperty(T & property, String const & name, Permission const permission);
void writeByCloud(T const val);
/* Composable configuration of the ArduinoCloudProperty class */
ArduinoCloudProperty<T> & onUpdate (UpdateCallbackFunc func);
ArduinoCloudProperty<T> & publishOnChange(T const min_delta_property, unsigned long const min_time_between_updates_millis = 0);
ArduinoCloudProperty<T> & publishEvery (unsigned long const seconds);
inline String name () const { return _name; }
inline bool isReadableByCloud () const { return (_permission == Permission::Read ) || (_permission == Permission::ReadWrite); }
inline bool isWriteableByCloud() const { return (_permission == Permission::Write) || (_permission == Permission::ReadWrite); }
bool shouldBeUpdated () const;
void execCallbackOnChange ();
void append (CborEncoder * encoder, CloudProtocol const cloud_protocol);
private:
T & _property,
_shadow_property;
String _name;
Permission _permission;
UpdateCallbackFunc _update_callback_func;
UpdatePolicy _update_policy;
bool _has_been_updated_once;
/* Variables used for UpdatePolicy::OnChange */
T _min_delta_property;
unsigned long _min_time_between_updates_millis;
/* Variables used for UpdatePolicy::TimeInterval */
unsigned long _last_updated_millis,
_update_interval_millis;
void appendValue(CborEncoder * mapEncoder, CloudProtocol const cloud_protocol) const;
bool isValueDifferent(T const lhs, T const rhs) const;
};
/******************************************************************************
* PROTOTYPE FREE FUNCTIONs
******************************************************************************/
template <typename T>
inline bool operator == (ArduinoCloudProperty<T> const & lhs, ArduinoCloudProperty<T> const & rhs) { return (lhs.name() == rhs.name()); }
/******************************************************************************
* TEMPLATE IMPLEMENTATION
******************************************************************************/
#include "ArduinoCloudProperty.ipp"
#endif /* ARDUINO_CLOUD_PROPERTY_HPP_ */