-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathPropertyContainer.cpp
164 lines (135 loc) · 5.38 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
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
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"
/******************************************************************************
INTERNAL FUNCTION DECLARATION
******************************************************************************/
void addProperty(PropertyContainer & prop_cont, Property * property_obj, int propertyIdentifier);
/******************************************************************************
PUBLIC FUNCTION DEFINITION
******************************************************************************/
Property & addPropertyToContainer(PropertyContainer & prop_cont, Property & property, String const & name, Permission const permission, int propertyIdentifier, GetTimeCallbackFunc func)
{
/* Check whether or not the property already has been added to the container */
Property * p = getProperty(prop_cont, name);
if(p != nullptr) return (*p);
/* Initialize property and add it to the container */
property.init(name, permission, func);
addProperty(prop_cont, &property, propertyIdentifier);
return property;
}
Property * getProperty(PropertyContainer & prop_cont, String const & name)
{
std::list<Property *>::iterator iter;
iter = std::find_if(prop_cont.begin(),
prop_cont.end(),
[name](Property * p) -> bool
{
return (p->name() == name);
});
if (iter == prop_cont.end())
return nullptr;
else
return (*iter);
}
Property * getProperty(PropertyContainer & prop_cont, int const identifier)
{
std::list<Property *>::iterator iter;
iter = std::find_if(prop_cont.begin(),
prop_cont.end(),
[identifier](Property * p) -> bool
{
return (p->identifier() == identifier);
});
if (iter == prop_cont.end())
return nullptr;
else
return (*iter);
}
void requestUpdateForAllProperties(PropertyContainer & prop_cont)
{
std::for_each(prop_cont.begin(),
prop_cont.end(),
[](Property * p)
{
p->requestUpdate();
});
}
void updateTimestampOnLocallyChangedProperties(PropertyContainer & prop_cont)
{
/* This function updates the timestamps on the primitive properties
* that have been modified locally since last cloud synchronization
*/
std::for_each(prop_cont.begin(),
prop_cont.end(),
[](Property * p)
{
CloudWrapperBase * pbase = reinterpret_cast<CloudWrapperBase *>(p);
if (pbase->isPrimitive() && pbase->isChangedLocally() && pbase->isReadableByCloud())
{
p->updateLocalTimestamp();
}
});
}
void updateProperty(PropertyContainer & prop_cont, String propertyName, unsigned long cloudChangeEventTime, bool const is_sync_message, std::list<CborMapData> * map_data_list)
{
Property * property = getProperty(prop_cont, propertyName);
if (property && property->isWriteableByCloud())
{
property->setLastCloudChangeTimestamp(cloudChangeEventTime);
property->setAttributesFromCloud(map_data_list);
if (is_sync_message) {
property->execCallbackOnSync();
} else {
if (property->isWritableOnChange()) {
property->fromCloudToLocal();
}
property->execCallbackOnChange();
property->provideEcho();
}
}
}
String getPropertyNameByIdentifier(PropertyContainer & prop_cont, int propertyIdentifier)
{
Property * property = nullptr;
if (propertyIdentifier > 255)
property = getProperty(prop_cont, propertyIdentifier & 255);
else
property = getProperty(prop_cont, propertyIdentifier);
if (property)
return property->name();
else
return String("");
}
/******************************************************************************
INTERNAL FUNCTION DEFINITION
******************************************************************************/
void addProperty(PropertyContainer & prop_cont, 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(prop_cont.size() + 1); /* This is in order to stay compatible to the old system of first increasing _numProperties and then assigning it here. */
}
prop_cont.push_back(property_obj);
}