Skip to content

Commit 0872e7e

Browse files
committed
Integrating new PropertyContainer with firmware
1 parent eaf28dc commit 0872e7e

6 files changed

+20
-131
lines changed

src/ArduinoIoTCloud.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ void ArduinoIoTCloudClass::addPropertyReal(ArduinoCloudProperty& property, Strin
4747
}
4848

4949
if (seconds == ON_CHANGE) {
50-
_thing.addPropertyReal(property, name, permission, tag).publishOnChange(minDelta, DEFAULT_MIN_TIME_BETWEEN_UPDATES_MILLIS).onUpdate(fn).onSync(synFn);
50+
_property_container.addPropertyReal(property, name, permission, tag).publishOnChange(minDelta, DEFAULT_MIN_TIME_BETWEEN_UPDATES_MILLIS).onUpdate(fn).onSync(synFn);
5151
} else {
52-
_thing.addPropertyReal(property, name, permission, tag).publishEvery(seconds).onUpdate(fn).onSync(synFn);
52+
_property_container.addPropertyReal(property, name, permission, tag).publishEvery(seconds).onUpdate(fn).onSync(synFn);
5353
}
5454
}
5555

src/ArduinoIoTCloud.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <Arduino_DebugUtils.h>
2929

3030
#include "cbor/ArduinoCloudThing.h"
31+
#include "property/PropertyContainer.h"
3132

3233
#include "cbor/types/CloudWrapperBool.h"
3334
#include "cbor/types/CloudWrapperFloat.h"
@@ -140,6 +141,7 @@ class ArduinoIoTCloudClass
140141

141142
ConnectionHandler * _connection = nullptr;
142143
ArduinoCloudThing _thing;
144+
PropertyContainer _property_container;
143145
ArduinoIoTConnectionStatus _iot_status = ArduinoIoTConnectionStatus::IDLE;
144146

145147
NetworkConnectionState checkPhyConnection();

src/ArduinoIoTCloudLPWAN.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ int ArduinoIoTCloudLPWAN::begin(ConnectionHandler& connection, bool retry)
5656
{
5757
_connection = &connection;
5858
_retryEnable = retry;
59-
_thing.begin();
59+
_thing.begin(&_property_container);
6060
return 1;
6161
}
6262

6363
void ArduinoIoTCloudLPWAN::update()
6464
{
6565
// Check if a primitive property wrapper is locally changed
66-
_thing.updateTimestampOnLocallyChangedProperties();
66+
_property_container.updateTimestampOnLocallyChangedProperties();
6767

6868
ArduinoIoTConnectionStatus next_iot_status = _iot_status;
6969

src/ArduinoIoTCloudTCP.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort)
134134
_dataTopicIn = getTopic_datain();
135135
_ota_topic_in = getTopic_ota_in();
136136

137-
_thing.begin();
138-
_thing.registerGetTimeCallbackFunc(getTime);
137+
_thing.begin(&_property_container);
138+
_property_container.begin(getTime);
139139

140140
printConnectionStatus(_iot_status);
141141

@@ -154,7 +154,7 @@ void ArduinoIoTCloudTCP::update()
154154
#endif /* OTA_ENABLED */
155155

156156
// Check if a primitive property wrapper is locally changed
157-
_thing.updateTimestampOnLocallyChangedProperties();
157+
_property_container.updateTimestampOnLocallyChangedProperties();
158158

159159
if(checkPhyConnection() != NetworkConnectionState::CONNECTED) return;
160160
if(checkCloudConnection() != ArduinoIoTConnectionStatus::CONNECTED) return;

src/cbor/ArduinoCloudThing.cpp

Lines changed: 8 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ void PrintFreeRam(void) {
4848
******************************************************************************/
4949

5050
ArduinoCloudThing::ArduinoCloudThing() :
51-
_get_time_func{nullptr},
52-
_numPrimitivesProperties(0),
53-
_numProperties(0),
51+
_property_container{nullptr},
5452
_isSyncMessage(false),
5553
_currentPropertyName(""),
5654
_currentPropertyBaseTime(0),
@@ -61,11 +59,9 @@ ArduinoCloudThing::ArduinoCloudThing() :
6159
PUBLIC MEMBER FUNCTIONS
6260
******************************************************************************/
6361

64-
void ArduinoCloudThing::begin() {
65-
}
66-
67-
void ArduinoCloudThing::registerGetTimeCallbackFunc(GetTimeCallbackFunc func) {
68-
_get_time_func = func;
62+
void ArduinoCloudThing::begin(PropertyContainer * property_container)
63+
{
64+
_property_container = property_container;
6965
}
7066

7167
int ArduinoCloudThing::encode(uint8_t * data, size_t const size, bool lightPayload) {
@@ -80,7 +76,7 @@ int ArduinoCloudThing::encode(uint8_t * data, size_t const size, bool lightPaylo
8076
return -1;
8177
}
8278

83-
if (appendChangedProperties(&arrayEncoder, lightPayload) < 1) {
79+
if (_property_container->appendChangedProperties(&arrayEncoder, lightPayload) < 1) {
8480
return -1;
8581
}
8682

@@ -95,21 +91,6 @@ int ArduinoCloudThing::encode(uint8_t * data, size_t const size, bool lightPaylo
9591
return bytes_encoded;
9692
}
9793

98-
ArduinoCloudProperty& ArduinoCloudThing::addPropertyReal(ArduinoCloudProperty & property, String const & name, Permission const permission, int propertyIdentifier) {
99-
property.init(name, permission, _get_time_func);
100-
if (isPropertyInContainer(name)) {
101-
return (*getProperty(name));
102-
} else {
103-
if (property.isPrimitive()) {
104-
_numPrimitivesProperties++;
105-
}
106-
_numProperties++;
107-
addProperty(&property, propertyIdentifier);
108-
return (property);
109-
}
110-
111-
}
112-
11394
void ArduinoCloudThing::decode(uint8_t const * const payload, size_t const length, bool isSyncMessage) {
11495
_isSyncMessage = isSyncMessage;
11596

@@ -161,81 +142,6 @@ void ArduinoCloudThing::decode(uint8_t const * const payload, size_t const lengt
161142
}
162143
}
163144

164-
bool ArduinoCloudThing::isPropertyInContainer(String const & name)
165-
{
166-
return (getProperty(name) != nullptr);
167-
}
168-
169-
int ArduinoCloudThing::appendChangedProperties(CborEncoder * arrayEncoder, bool lightPayload)
170-
{
171-
int appendedProperties = 0;
172-
std::for_each(_property_list.begin(),
173-
_property_list.end(),
174-
[arrayEncoder, lightPayload, &appendedProperties](ArduinoCloudProperty * p)
175-
{
176-
if (p->shouldBeUpdated() && p->isReadableByCloud())
177-
{
178-
p->append(arrayEncoder, lightPayload);
179-
appendedProperties++;
180-
}
181-
});
182-
return appendedProperties;
183-
}
184-
185-
//retrieve property by name
186-
ArduinoCloudProperty * ArduinoCloudThing::getProperty(String const & name)
187-
{
188-
std::list<ArduinoCloudProperty *>::iterator iter;
189-
190-
iter = std::find_if(_property_list.begin(),
191-
_property_list.end(),
192-
[name](ArduinoCloudProperty * p) -> bool
193-
{
194-
return (p->name() == name);
195-
});
196-
197-
if (iter == _property_list.end())
198-
return nullptr;
199-
else
200-
return (*iter);
201-
}
202-
203-
//retrieve property by identifier
204-
ArduinoCloudProperty * ArduinoCloudThing::getProperty(int const & pos)
205-
{
206-
std::list<ArduinoCloudProperty *>::iterator iter;
207-
208-
iter = std::find_if(_property_list.begin(),
209-
_property_list.end(),
210-
[pos](ArduinoCloudProperty * p) -> bool
211-
{
212-
return (p->identifier() == pos);
213-
});
214-
215-
if (iter == _property_list.end())
216-
return nullptr;
217-
else
218-
return (*iter);
219-
}
220-
221-
// this function updates the timestamps on the primitive properties that have been modified locally since last cloud synchronization
222-
void ArduinoCloudThing::updateTimestampOnLocallyChangedProperties()
223-
{
224-
if (_numPrimitivesProperties > 0)
225-
{
226-
std::for_each(_property_list.begin(),
227-
_property_list.end(),
228-
[](ArduinoCloudProperty * p)
229-
{
230-
CloudWrapperBase * pbase = reinterpret_cast<CloudWrapperBase *>(p);
231-
if (pbase->isPrimitive() && pbase->isChangedLocally() && pbase->isReadableByCloud())
232-
{
233-
p->updateLocalTimestamp();
234-
}
235-
});
236-
}
237-
}
238-
239145
/******************************************************************************
240146
PRIVATE MEMBER FUNCTIONS
241147
******************************************************************************/
@@ -511,7 +417,7 @@ void ArduinoCloudThing::freeMapDataList(std::list<CborMapData *> * map_data_list
511417
}
512418

513419
void ArduinoCloudThing::updateProperty(String propertyName, unsigned long cloudChangeEventTime) {
514-
ArduinoCloudProperty* property = getProperty(propertyName);
420+
ArduinoCloudProperty* property = _property_container->getProperty(propertyName);
515421
if (property && property->isWriteableByCloud()) {
516422
property->setLastCloudChangeTimestamp(cloudChangeEventTime);
517423
property->setAttributesFromCloud(&_map_data_list);
@@ -528,9 +434,9 @@ void ArduinoCloudThing::updateProperty(String propertyName, unsigned long cloudC
528434
String ArduinoCloudThing::getPropertyNameByIdentifier(int propertyIdentifier) {
529435
ArduinoCloudProperty* property;
530436
if (propertyIdentifier > 255) {
531-
property = getProperty(propertyIdentifier & 255);
437+
property = _property_container->getProperty(propertyIdentifier & 255);
532438
} else {
533-
property = getProperty(propertyIdentifier);
439+
property = _property_container->getProperty(propertyIdentifier);
534440
}
535441
return property->name();
536442
}

src/cbor/ArduinoCloudThing.h

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#undef min
2727
#include <list>
2828

29-
#include "ArduinoCloudProperty.h"
29+
#include "../property/PropertyContainer.h"
3030

3131
#include "types/CloudBool.h"
3232
#include "types/CloudFloat.h"
@@ -80,8 +80,7 @@ class ArduinoCloudThing {
8080
public:
8181
ArduinoCloudThing();
8282

83-
void begin();
84-
void registerGetTimeCallbackFunc(GetTimeCallbackFunc func);
83+
void begin(PropertyContainer * property_container);
8584
//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
8685
ArduinoCloudProperty & addPropertyReal(ArduinoCloudProperty & property, String const & name, Permission const permission, int propertyIdentifier = -1);
8786

@@ -91,18 +90,11 @@ class ArduinoCloudThing {
9190
/* decode a CBOR payload received from the cloud */
9291
void decode(uint8_t const * const payload, size_t const length, bool isSyncMessage = false);
9392

94-
bool isPropertyInContainer(String const & name);
95-
int appendChangedProperties(CborEncoder * arrayEncoder, bool lightPayload);
96-
void updateTimestampOnLocallyChangedProperties();
9793
void updateProperty(String propertyName, unsigned long cloudChangeEventTime);
9894
String getPropertyNameByIdentifier(int propertyIdentifier);
9995

10096
private:
101-
GetTimeCallbackFunc _get_time_func;
102-
std::list<ArduinoCloudProperty *> _property_list;
103-
/* Keep track of the number of primitive properties in the Thing. If 0 it allows the early exit in updateTimestampOnLocallyChangedProperties() */
104-
int _numPrimitivesProperties;
105-
int _numProperties;
97+
PropertyContainer * _property_container;
10698
/* Indicates the if the message received to be decoded is a response to the getLastValues inquiry */
10799
bool _isSyncMessage;
108100
/* List of map data that will hold all the attributes of a property */
@@ -145,17 +137,6 @@ class ArduinoCloudThing {
145137
static bool ifNumericConvertToDouble(CborValue * value_iter, double * numeric_val);
146138
static double convertCborHalfFloatToDouble(uint16_t const half_val);
147139
void freeMapDataList(std::list<CborMapData *> * map_data_list);
148-
inline void addProperty(ArduinoCloudProperty * property_obj, int propertyIdentifier) {
149-
if (propertyIdentifier != -1) {
150-
property_obj->setIdentifier(propertyIdentifier);
151-
} else {
152-
// if property identifier is -1, an incremental value will be assigned as identifier.
153-
property_obj->setIdentifier(_numProperties);
154-
}
155-
_property_list.push_back(property_obj);
156-
}
157-
ArduinoCloudProperty * getProperty(String const & name);
158-
ArduinoCloudProperty * getProperty(int const & identifier);
159140

160141
};
161142

0 commit comments

Comments
 (0)