Skip to content

Commit 4959e2b

Browse files
committed
Implement feature for optional encoding of timestamps in CBOR map
1 parent deeb4bb commit 4959e2b

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

src/ArduinoIoTCloud.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ void ArduinoIoTCloudClass::push()
3030
requestUpdateForAllProperties(_property_container);
3131
}
3232

33+
bool ArduinoIoTCloudClass::setTimestamp(String const & prop_name, unsigned long const timestamp)
34+
{
35+
Property * p = getProperty(_property_container, prop_name);
36+
37+
if (p == nullptr)
38+
return false;
39+
40+
p->setTimestamp(timestamp);
41+
42+
return true;
43+
}
44+
3345
void ArduinoIoTCloudClass::addCallback(ArduinoIoTCloudEvent const event, OnCloudEventCallback callback)
3446
{
3547
_cloud_event_callback[static_cast<size_t>(event)] = callback;

src/ArduinoIoTCloud.h

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class ArduinoIoTCloudClass
8888
virtual void printDebugInfo() = 0;
8989

9090
void push();
91+
bool setTimestamp(String const & prop_name, unsigned long const timestamp);
9192

9293
inline void setThingId (String const thing_id) { _thing_id = thing_id; };
9394
inline String & getThingId () { return _thing_id; };

src/property/Property.cpp

+26-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ Property::Property()
4545
_identifier(0),
4646
_attributeIdentifier(0),
4747
_lightPayload(false),
48-
_update_requested(false) {
48+
_update_requested(false),
49+
_encode_timestamp(false),
50+
_timestamp(0)
51+
{
52+
4953
}
5054

5155
/******************************************************************************
@@ -85,6 +89,17 @@ Property & Property::publishOnDemand() {
8589
return (*this);
8690
}
8791

92+
Property & Property::encodeTimestamp()
93+
{
94+
_encode_timestamp = true;
95+
return (*this);
96+
}
97+
98+
void Property::setTimestamp(unsigned long const timestamp)
99+
{
100+
_timestamp = timestamp;
101+
}
102+
88103
bool Property::shouldBeUpdated() {
89104
if (!_has_been_updated_once) {
90105
return true;
@@ -170,7 +185,8 @@ void Property::appendAttributeName(String attributeName, std::function<void (Cbo
170185
_attributeIdentifier++;
171186
}
172187
CborEncoder mapEncoder;
173-
cbor_encoder_create_map(encoder, &mapEncoder, 2);
188+
unsigned int num_map_properties = _encode_timestamp ? 3 : 2;
189+
cbor_encoder_create_map(encoder, &mapEncoder, num_map_properties);
174190
cbor_encode_int(&mapEncoder, static_cast<int>(CborIntegerMapKey::Name));
175191

176192
// if _lightPayload is true, the property and attribute identifiers will be encoded instead of the property name
@@ -187,7 +203,15 @@ void Property::appendAttributeName(String attributeName, std::function<void (Cbo
187203
}
188204
cbor_encode_text_stringz(&mapEncoder, completeName.c_str());
189205
}
206+
/* Encode the value */
190207
appendValue(mapEncoder);
208+
/* Encode the timestamp if that has been required. */
209+
if(_encode_timestamp)
210+
{
211+
cbor_encode_int (&mapEncoder, static_cast<int>(CborIntegerMapKey::Time));
212+
cbor_encode_uint(&mapEncoder, _timestamp);
213+
}
214+
/* Close the container */
191215
cbor_encoder_close_container(encoder, &mapEncoder);
192216
}
193217

src/property/Property.h

+5
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ class Property {
139139
Property & publishOnChange(float const min_delta_property, unsigned long const min_time_between_updates_millis = 0);
140140
Property & publishEvery(unsigned long const seconds);
141141
Property & publishOnDemand();
142+
Property & encodeTimestamp();
142143

143144
inline String name() const {
144145
return _name;
@@ -153,6 +154,7 @@ class Property {
153154
return (_permission == Permission::Write) || (_permission == Permission::ReadWrite);
154155
}
155156

157+
void setTimestamp(unsigned long const timestamp);
156158
bool shouldBeUpdated();
157159
void requestUpdate();
158160
void execCallbackOnChange();
@@ -215,6 +217,9 @@ class Property {
215217
bool _lightPayload;
216218
/* Indicates whether a property update has been requested in case of the OnDemand update policy. */
217219
bool _update_requested;
220+
/* Indicates whether the timestamp shall be encoded in the property or not */
221+
bool _encode_timestamp;
222+
unsigned long _timestamp;
218223
};
219224

220225
/******************************************************************************

0 commit comments

Comments
 (0)