-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathPropertyContainer.cpp
151 lines (125 loc) · 4.72 KB
/
PropertyContainer.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
/*
This file is part of ArduinoIoTCloud.
Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
This software is released under the GNU General Public License version 3,
which covers the main part of arduino-cli.
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
******************************************************************************/
#include "PropertyContainer.h"
#include <algorithm>
#include "types/CloudWrapperBase.h"
/******************************************************************************
CTOR/DTOR
******************************************************************************/
PropertyContainer::PropertyContainer()
: _numProperties{0}
, _numPrimitivesProperties{0}
, _get_time_func{nullptr}
{
}
/******************************************************************************
PUBLIC MEMBER FUNCTIONS
******************************************************************************/
void PropertyContainer::begin(GetTimeCallbackFunc func)
{
_get_time_func = func;
}
Property & PropertyContainer::addPropertyReal(Property & property, String const & name, Permission const permission, int propertyIdentifier)
{
/* Check whether or not the property already has been added to the container */
Property * p = getProperty(name);
if(p != nullptr) return (*p);
/* Initialize property and add it to the container */
property.init(name, permission, _get_time_func);
if (property.isPrimitive()) { _numPrimitivesProperties++; }
_numProperties++;
addProperty(&property, propertyIdentifier);
return property;
}
Property * PropertyContainer::getProperty(String const & name)
{
std::list<Property *>::iterator iter;
iter = std::find_if(_property_list.begin(),
_property_list.end(),
[name](Property * p) -> bool
{
return (p->name() == name);
});
if (iter == _property_list.end())
return nullptr;
else
return (*iter);
}
Property * PropertyContainer::getProperty(int const identifier)
{
std::list<Property *>::iterator iter;
iter = std::find_if(_property_list.begin(),
_property_list.end(),
[identifier](Property * p) -> bool
{
return (p->identifier() == identifier);
});
if (iter == _property_list.end())
return nullptr;
else
return (*iter);
}
int PropertyContainer::appendChangedProperties(CborEncoder * arrayEncoder, bool lightPayload)
{
int appendedProperties = 0;
std::for_each(_property_list.begin(),
_property_list.end(),
[arrayEncoder, lightPayload, &appendedProperties](Property * p)
{
if (p->shouldBeUpdated() && p->isReadableByCloud())
{
p->append(arrayEncoder, lightPayload);
appendedProperties++;
}
});
return appendedProperties;
}
void PropertyContainer::updateTimestampOnLocallyChangedProperties()
{
/* This function updates the timestamps on the primitive properties
* that have been modified locally since last cloud synchronization
*/
if (_numPrimitivesProperties > 0)
{
std::for_each(_property_list.begin(),
_property_list.end(),
[](Property * p)
{
CloudWrapperBase * pbase = reinterpret_cast<CloudWrapperBase *>(p);
if (pbase->isPrimitive() && pbase->isChangedLocally() && pbase->isReadableByCloud())
{
p->updateLocalTimestamp();
}
});
}
}
/******************************************************************************
PRIVATE MEMBER FUNCTIONS
******************************************************************************/
void PropertyContainer::addProperty(Property * property_obj, int propertyIdentifier)
{
if (propertyIdentifier != -1)
{
property_obj->setIdentifier(propertyIdentifier);
}
/* If property identifier is -1, an incremental value will be assigned as identifier. */
else
{
property_obj->setIdentifier(_numProperties);
}
_property_list.push_back(property_obj);
}