Skip to content

Commit eaf28dc

Browse files
committed
Adding new class PropertyContainer which replaces built-in contained functionality of ArduinoCloudThing
1 parent 03bc13e commit eaf28dc

File tree

2 files changed

+220
-0
lines changed

2 files changed

+220
-0
lines changed

src/property/PropertyContainer.cpp

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
This file is part of ArduinoIoTCloud.
3+
4+
Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
5+
6+
This software is released under the GNU General Public License version 3,
7+
which covers the main part of arduino-cli.
8+
The terms of this license can be found at:
9+
https://www.gnu.org/licenses/gpl-3.0.en.html
10+
11+
You can be released from the requirements of the above licenses by purchasing
12+
a commercial license. Buying such a license is mandatory if you want to modify or
13+
otherwise use the software for commercial activities involving the Arduino
14+
software without disclosing the source code of your own applications. To purchase
15+
a commercial license, send an email to license@arduino.cc.
16+
*/
17+
18+
/******************************************************************************
19+
INCLUDE
20+
******************************************************************************/
21+
22+
#include "PropertyContainer.h"
23+
24+
#include <algorithm>
25+
26+
#include "../cbor/types/CloudWrapperBase.h"
27+
28+
/******************************************************************************
29+
CTOR/DTOR
30+
******************************************************************************/
31+
32+
PropertyContainer::PropertyContainer()
33+
: _numProperties{0}
34+
, _numPrimitivesProperties{0}
35+
, _get_time_func{nullptr}
36+
{
37+
38+
}
39+
40+
/******************************************************************************
41+
PUBLIC MEMBER FUNCTIONS
42+
******************************************************************************/
43+
44+
void PropertyContainer::begin(GetTimeCallbackFunc func)
45+
{
46+
_get_time_func = func;
47+
}
48+
49+
ArduinoCloudProperty & PropertyContainer::addPropertyReal(ArduinoCloudProperty & property, String const & name, Permission const permission, int propertyIdentifier)
50+
{
51+
/* Check wether or not the property already has been added to the container */
52+
ArduinoCloudProperty * p = getProperty(name);
53+
if(p != nullptr) return (*p);
54+
55+
/* Initialize property and add it to the container */
56+
property.init(name, permission, _get_time_func);
57+
58+
if (property.isPrimitive()) { _numPrimitivesProperties++; }
59+
_numProperties++;
60+
addProperty(&property, propertyIdentifier);
61+
return property;
62+
}
63+
64+
65+
ArduinoCloudProperty * PropertyContainer::getProperty(String const & name)
66+
{
67+
std::list<ArduinoCloudProperty *>::iterator iter;
68+
69+
iter = std::find_if(_property_list.begin(),
70+
_property_list.end(),
71+
[name](ArduinoCloudProperty * p) -> bool
72+
{
73+
return (p->name() == name);
74+
});
75+
76+
if (iter == _property_list.end())
77+
return nullptr;
78+
else
79+
return (*iter);
80+
}
81+
82+
ArduinoCloudProperty * PropertyContainer::getProperty(int const identifier)
83+
{
84+
std::list<ArduinoCloudProperty *>::iterator iter;
85+
86+
iter = std::find_if(_property_list.begin(),
87+
_property_list.end(),
88+
[identifier](ArduinoCloudProperty * p) -> bool
89+
{
90+
return (p->identifier() == identifier);
91+
});
92+
93+
if (iter == _property_list.end())
94+
return nullptr;
95+
else
96+
return (*iter);
97+
}
98+
99+
int PropertyContainer::appendChangedProperties(CborEncoder * arrayEncoder, bool lightPayload)
100+
{
101+
int appendedProperties = 0;
102+
std::for_each(_property_list.begin(),
103+
_property_list.end(),
104+
[arrayEncoder, lightPayload, &appendedProperties](ArduinoCloudProperty * p)
105+
{
106+
if (p->shouldBeUpdated() && p->isReadableByCloud())
107+
{
108+
p->append(arrayEncoder, lightPayload);
109+
appendedProperties++;
110+
}
111+
});
112+
return appendedProperties;
113+
}
114+
115+
void PropertyContainer::updateTimestampOnLocallyChangedProperties()
116+
{
117+
/* This function updates the timestamps on the primitive properties
118+
* that have been modified locally since last cloud synchronization
119+
*/
120+
if (_numPrimitivesProperties > 0)
121+
{
122+
std::for_each(_property_list.begin(),
123+
_property_list.end(),
124+
[](ArduinoCloudProperty * p)
125+
{
126+
CloudWrapperBase * pbase = reinterpret_cast<CloudWrapperBase *>(p);
127+
if (pbase->isPrimitive() && pbase->isChangedLocally() && pbase->isReadableByCloud())
128+
{
129+
p->updateLocalTimestamp();
130+
}
131+
});
132+
}
133+
}
134+
135+
/******************************************************************************
136+
PRIVATE MEMBER FUNCTIONS
137+
******************************************************************************/
138+
139+
void PropertyContainer::addProperty(ArduinoCloudProperty * property_obj, int propertyIdentifier)
140+
{
141+
if (propertyIdentifier != -1)
142+
{
143+
property_obj->setIdentifier(propertyIdentifier);
144+
}
145+
/* If property identifier is -1, an incremental value will be assigned as identifier. */
146+
else
147+
{
148+
property_obj->setIdentifier(_numProperties);
149+
}
150+
_property_list.push_back(property_obj);
151+
}

src/property/PropertyContainer.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
This file is part of ArduinoIoTCloud.
3+
4+
Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
5+
6+
This software is released under the GNU General Public License version 3,
7+
which covers the main part of arduino-cli.
8+
The terms of this license can be found at:
9+
https://www.gnu.org/licenses/gpl-3.0.en.html
10+
11+
You can be released from the requirements of the above licenses by purchasing
12+
a commercial license. Buying such a license is mandatory if you want to modify or
13+
otherwise use the software for commercial activities involving the Arduino
14+
software without disclosing the source code of your own applications. To purchase
15+
a commercial license, send an email to license@arduino.cc.
16+
*/
17+
18+
#ifndef ARDUINO_PROPERTY_CONTAINER_H_
19+
#define ARDUINO_PROPERTY_CONTAINER_H_
20+
21+
/******************************************************************************
22+
INCLUDE
23+
******************************************************************************/
24+
25+
#include "../cbor/ArduinoCloudProperty.h"
26+
27+
#undef max
28+
#undef min
29+
#include <list>
30+
31+
/******************************************************************************
32+
CLASS DECLARATION
33+
******************************************************************************/
34+
35+
class PropertyContainer
36+
{
37+
38+
public:
39+
40+
PropertyContainer();
41+
42+
43+
void begin(GetTimeCallbackFunc func);
44+
45+
46+
ArduinoCloudProperty & addPropertyReal(ArduinoCloudProperty & property, String const & name, Permission const permission, int propertyIdentifier = -1);
47+
48+
49+
ArduinoCloudProperty * getProperty (String const & name);
50+
ArduinoCloudProperty * getProperty (int const identifier);
51+
52+
53+
int appendChangedProperties(CborEncoder * arrayEncoder, bool lightPayload);
54+
void updateTimestampOnLocallyChangedProperties();
55+
56+
57+
58+
private:
59+
60+
int _numProperties;
61+
int _numPrimitivesProperties;
62+
GetTimeCallbackFunc _get_time_func;
63+
std::list<ArduinoCloudProperty *> _property_list;
64+
65+
void addProperty(ArduinoCloudProperty * property_obj, int propertyIdentifier);
66+
67+
};
68+
69+
#endif /* ARDUINO_PROPERTY_CONTAINER_H_ */

0 commit comments

Comments
 (0)