Skip to content

Commit 3e0c8df

Browse files
committed
Replace CayenneValueArray with CayenneDataArray.
1 parent 4305ca9 commit 3e0c8df

File tree

4 files changed

+10
-193
lines changed

4 files changed

+10
-193
lines changed

src/CayenneArduinoMQTTClient.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class CayenneArduinoMQTTClient
156156
* @param values Array of values to be sent
157157
* @param type Measurement type
158158
*/
159-
void virtualWrite(unsigned int channel, const CayenneValueArray& values, const char* type)
159+
void virtualWrite(unsigned int channel, const CayenneDataArray& values, const char* type)
160160
{
161161
publishData(DATA_TOPIC, channel, values.getArray(), values.getCount(), type);
162162
}
@@ -168,7 +168,7 @@ class CayenneArduinoMQTTClient
168168
* @param values Array of values to be sent
169169
* @param type Measurement type
170170
*/
171-
void virtualWrite(unsigned int channel, const CayenneValueArray& values, const __FlashStringHelper* type)
171+
void virtualWrite(unsigned int channel, const CayenneDataArray& values, const __FlashStringHelper* type)
172172
{
173173
publishData(DATA_TOPIC, channel, values.getArray(), values.getCount(), type);
174174
}
@@ -319,8 +319,7 @@ class CayenneArduinoMQTTClient
319319
*/
320320
template <typename T>
321321
static void publishData(CayenneTopic topic, unsigned int channel, const T& data, const char* key = NULL, const char* subkey = NULL) {
322-
char buffer[64];
323-
CayenneValueArray values(buffer, sizeof(buffer));
322+
CayenneDataArray values;
324323
values.add(subkey, data);
325324
publishData(topic, channel, values.getArray(), values.getCount(), key);
326325
}
@@ -335,9 +334,8 @@ class CayenneArduinoMQTTClient
335334
*/
336335
template <typename T>
337336
static void publishData(CayenneTopic topic, unsigned int channel, const T& data, const __FlashStringHelper* key, const __FlashStringHelper* subkey = NULL) {
338-
char buffer[64];
339337
char keyBuffer[MAX_TYPE_LENGTH + 1];
340-
CayenneValueArray values(buffer, sizeof(buffer));
338+
CayenneDataArray values;
341339
values.add(subkey, data);
342340
CAYENNE_MEMCPY(keyBuffer, reinterpret_cast<const char *>(key), CAYENNE_STRLEN(reinterpret_cast<const char *>(key)) + 1);
343341
publishData(topic, channel, values.getArray(), values.getCount(), keyBuffer);

src/CayenneMessage.h

Lines changed: 0 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -33,191 +33,6 @@ Code adapted from Blynk library BlynkMessage.cpp. Copyright info below.
3333
#include <stdlib.h>
3434
#include "CayenneMQTTClient/CayenneMQTTClient.h"
3535

36-
class CayenneValueArray
37-
{
38-
public:
39-
CayenneValueArray(char* buffer, size_t size) : _buffer(buffer), _size(size), _index(0), _valueCount(0) {
40-
clear();
41-
}
42-
43-
void clear() {
44-
for (int i = 0; i < CAYENNE_MAX_MESSAGE_VALUES; ++i) {
45-
_values[i].unit = NULL;
46-
_values[i].value = NULL;
47-
}
48-
_valueCount = 0;
49-
_index = 0;
50-
}
51-
52-
void add(const char* unit, const char* value, bool unitInFlash = false, bool valueInFlash = false) {
53-
if (_valueCount >= CAYENNE_MAX_MESSAGE_VALUES)
54-
return;
55-
56-
size_t unitLength = 0;
57-
if (unit) {
58-
unitLength = (unitInFlash ? CAYENNE_STRLEN(unit) : strlen(unit)) + 1;
59-
}
60-
size_t valueLength = 0;
61-
if (value) {
62-
valueLength = (valueInFlash ? CAYENNE_STRLEN(value) : strlen(value)) + 1;
63-
}
64-
if (_index + unitLength + valueLength > _size)
65-
return;
66-
67-
if (unit) {
68-
unitInFlash ? CAYENNE_MEMCPY(_buffer + _index, unit, unitLength) : memcpy(_buffer + _index, unit, unitLength);
69-
_values[_valueCount].unit = _buffer + _index;
70-
_index += unitLength;
71-
}
72-
else {
73-
_values[_valueCount].unit = NULL;
74-
}
75-
76-
if (value) {
77-
valueInFlash ? CAYENNE_MEMCPY(_buffer + _index, value, valueLength) : memcpy(_buffer + _index, value, valueLength);
78-
_values[_valueCount].value = _buffer + _index;
79-
_index += valueLength;
80-
}
81-
else {
82-
_values[_valueCount].value = NULL;
83-
}
84-
85-
_valueCount++;
86-
}
87-
88-
void add(const char* unit, const __FlashStringHelper* value) {
89-
const char* valueString = reinterpret_cast<const char *>(value);
90-
add(unit, valueString, false, true);
91-
}
92-
93-
void add(const __FlashStringHelper* unit, const char* value) {
94-
const char* unitString = reinterpret_cast<const char *>(unit);
95-
add(unitString, value, true, false);
96-
}
97-
98-
void add(const __FlashStringHelper* unit, const __FlashStringHelper* value) {
99-
const char* unitString = reinterpret_cast<const char *>(unit);
100-
const char* valueString = reinterpret_cast<const char *>(value);
101-
add(unitString, valueString, true, true);
102-
}
103-
104-
inline void add(const char* unit, const int value) {
105-
char str[2 + 8 * sizeof(value)];
106-
itoa(value, str, 10);
107-
add(unit, str);
108-
}
109-
110-
inline void add(const char* unit, const unsigned int value) {
111-
char str[1 + 8 * sizeof(value)];
112-
utoa(value, str, 10);
113-
add(unit, str);
114-
}
115-
116-
inline void add(const char* unit, const long value) {
117-
char str[2 + 8 * sizeof(value)];
118-
ltoa(value, str, 10);
119-
add(unit, str);
120-
}
121-
122-
inline void add(const char* unit, const unsigned long value) {
123-
char str[1 + 8 * sizeof(value)];
124-
ultoa(value, str, 10);
125-
add(unit, str);
126-
}
127-
128-
#if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32)
129-
130-
inline void add(const char* unit, const float value) {
131-
char str[33];
132-
dtostrf(value, 5, 3, str);
133-
add(unit, str);
134-
}
135-
136-
inline void add(const char* unit, const double value) {
137-
char str[33];
138-
dtostrf(value, 5, 3, str);
139-
add(unit, str);
140-
}
141-
142-
#else
143-
144-
inline void add(const char* unit, const float value) {
145-
char str[33];
146-
snprintf(str, 33, "%2.3f", value);
147-
add(unit, str);
148-
}
149-
150-
inline void add(const char* unit, const double value) {
151-
char str[33];
152-
snprintf(str, 33, "%2.3f", value);
153-
add(unit, str);
154-
}
155-
156-
#endif
157-
158-
#ifdef CAYENNE_USING_PROGMEM
159-
160-
inline void add(const __FlashStringHelper* unit, const int value) {
161-
char str[2 + 8 * sizeof(value)];
162-
itoa(value, str, 10);
163-
add(unit, str);
164-
}
165-
166-
inline void add(const __FlashStringHelper* unit, const unsigned int value) {
167-
char str[1 + 8 * sizeof(value)];
168-
utoa(value, str, 10);
169-
add(unit, str);
170-
}
171-
172-
inline void add(const __FlashStringHelper* unit, const long value) {
173-
char str[2 + 8 * sizeof(value)];
174-
ltoa(value, str, 10);
175-
add(unit, str);
176-
}
177-
178-
inline void add(const __FlashStringHelper* unit, const unsigned long value) {
179-
char str[1 + 8 * sizeof(value)];
180-
ultoa(value, str, 10);
181-
add(unit, str);
182-
}
183-
184-
inline void add(const __FlashStringHelper* unit, const float value) {
185-
char str[33];
186-
#if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32)
187-
dtostrf(value, 5, 3, str);
188-
#else
189-
snprintf(str, 33, "%2.3f", value);
190-
#endif
191-
add(unit, str);
192-
}
193-
194-
inline void add(const __FlashStringHelper* unit, const double value) {
195-
char str[33];
196-
#if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32)
197-
dtostrf(value, 5, 3, str);
198-
#else
199-
snprintf(str, 33, "%2.3f", value);
200-
#endif
201-
add(unit, str);
202-
}
203-
204-
#endif
205-
206-
const CayenneValuePair* getArray() const {
207-
return _values;
208-
}
209-
210-
const size_t getCount() const {
211-
return _valueCount;
212-
}
213-
214-
private:
215-
CayenneValuePair _values[CAYENNE_MAX_MESSAGE_VALUES];
216-
size_t _valueCount;
217-
char* _buffer;
218-
size_t _size;
219-
size_t _index;
220-
};
22136

22237
class CayenneMessage
22338
{

src/CayenneUtils/CayenneDataArray.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace CayenneMQTT
3232
* @param BUFFER_SIZE Maximum buffer size to use for data array, in bytes.
3333
* @param MAX_VALUES Maximum number of unit/value pairs in the array.
3434
*/
35-
template<int BUFFER_SIZE = CAYENNE_MAX_MESSAGE_SIZE, int MAX_VALUES = CAYENNE_MAX_MESSAGE_VALUES>
35+
template<int BUFFER_SIZE = CAYENNE_MAX_PAYLOAD_SIZE, int MAX_VALUES = CAYENNE_MAX_MESSAGE_VALUES>
3636
class DataArray
3737
{
3838
public:
@@ -325,7 +325,7 @@ namespace CayenneMQTT
325325
* Get the number of items in the unit/value pair array.
326326
* @return Count of items.
327327
*/
328-
size_t getCount() const {
328+
const size_t getCount() const {
329329
return _valueCount;
330330
}
331331

src/CayenneUtils/CayenneDefines.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEAL
2727
#define CAYENNE_MAX_MESSAGE_SIZE 134 // Redefine this for different message size
2828
#endif
2929

30+
#ifndef CAYENNE_MAX_PAYLOAD_SIZE
31+
#define CAYENNE_MAX_PAYLOAD_SIZE 64 // Redefine this for different message size
32+
#endif
33+
3034
#ifndef CAYENNE_MAX_MESSAGE_HANDLERS
3135
#define CAYENNE_MAX_MESSAGE_HANDLERS 5 /* Redefine to change number of handlers */
3236
#endif

0 commit comments

Comments
 (0)