Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
# Changelog
## unreleased
### Features
- [174](https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino/pull/174) - All API methods with a string param allow specifying string by all basic types of :
- Arduino `String` class
- C `char *` or `char[]`
- Flash string using `F`,`PSTR` or `FPSTR` macros

## 3.10.0 [2022-01-20]
### Features
- [167](https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino/pull/167) - Added `InfluxDBClient::writeRecord(const char *record)`.
Expand Down
10 changes: 5 additions & 5 deletions src/InfluxDbClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

static const char TooEarlyMessage[] PROGMEM = "Cannot send request yet because of applied retry strategy. Remaining ";

static String escapeJSONString(String &value);
static String escapeJSONString(const String &value);
static String precisionToString(WritePrecision precision, uint8_t version = 2) {
switch(precision) {
case WritePrecision::US:
Expand Down Expand Up @@ -352,7 +352,7 @@ char * InfluxDBClient::Batch::createData() {
return buff;
}

bool InfluxDBClient::writeRecord(String &record) {
bool InfluxDBClient::writeRecord(const String &record) {
return writeRecord(record.c_str());
}

Expand Down Expand Up @@ -577,11 +577,11 @@ static const char QueryDialect[] PROGMEM = "\
static const char Params[] PROGMEM = ",\
\"params\": {";

FluxQueryResult InfluxDBClient::query(String fluxQuery) {
FluxQueryResult InfluxDBClient::query(const String &fluxQuery) {
return query(fluxQuery, QueryParams());
}

FluxQueryResult InfluxDBClient::query(String fluxQuery, QueryParams params) {
FluxQueryResult InfluxDBClient::query(const String &fluxQuery, QueryParams params) {
uint32_t rwt = getRemainingRetryTime();
if(rwt > 0) {
INFLUXDB_CLIENT_DEBUG("[W] Cannot query yet, pause %ds, %ds yet\n", _retryTime, rwt);
Expand Down Expand Up @@ -638,7 +638,7 @@ FluxQueryResult InfluxDBClient::query(String fluxQuery, QueryParams params) {
}


static String escapeJSONString(String &value) {
static String escapeJSONString(const String &value) {
String ret;
int d = 0;
int i,from = 0;
Expand Down
6 changes: 3 additions & 3 deletions src/InfluxDbClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,19 @@ class InfluxDBClient {
bool validateConnection();
// Writes record in InfluxDB line protocol format to write buffer.
// Returns true if successful, false in case of any error
bool writeRecord(String &record);
bool writeRecord(const String &record);
bool writeRecord(const char *record);
// Writes record represented by Point to buffer
// Returns true if successful, false in case of any error
bool writePoint(Point& point);
// Sends Flux query and returns FluxQueryResult object for subsequently reading flux query response.
// Use FluxQueryResult::next() method to iterate over lines of the query result.
// Always call of FluxQueryResult::close() when reading is finished. Check FluxQueryResult doc for more info.
FluxQueryResult query(String fluxQuery);
FluxQueryResult query(const String &fluxQuery);
// Sends Flux query with params and returns FluxQueryResult object for subsequently reading flux query response.
// Use FluxQueryResult::next() method to iterate over lines of the query result.
// Always call of FluxQueryResult::close() when reading is finished. Check FluxQueryResult doc for more info.
FluxQueryResult query(String fluxQuery, QueryParams params);
FluxQueryResult query(const String &fluxQuery, QueryParams params);
// Forces writing of all points in buffer, even the batch is not full.
// Returns true if successful, false in case of any error
bool flushBuffer();
Expand Down
2 changes: 1 addition & 1 deletion src/Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "Options.h"
#include "util/helpers.h"

WriteOptions& WriteOptions::addDefaultTag(String name, String value) {
WriteOptions& WriteOptions::addDefaultTag(const String &name, const String &value) {
if(_defaultTags.length() > 0) {
_defaultTags += ',';
}
Expand Down
2 changes: 1 addition & 1 deletion src/Options.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class WriteOptions {
WriteOptions& retryInterval(uint16_t retryIntervalSec) { _retryInterval = retryIntervalSec; return *this; }
WriteOptions& maxRetryInterval(uint16_t maxRetryIntervalSec) { _maxRetryInterval = maxRetryIntervalSec; return *this; }
WriteOptions& maxRetryAttempts(uint16_t maxRetryAttempts) { _maxRetryAttempts = maxRetryAttempts; return *this; }
WriteOptions& addDefaultTag(String name, String value);
WriteOptions& addDefaultTag(const String &name, const String &value);
WriteOptions& clearDefaultTags() { _defaultTags = (char *)nullptr; return *this; }
};

Expand Down
64 changes: 54 additions & 10 deletions src/Point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "Point.h"
#include "util/helpers.h"

Point::Point(String measurement):
Point::Point(const String & measurement):
_measurement(escapeKey(measurement, false)),
_tags(""),
_fields(""),
Expand All @@ -37,7 +37,7 @@ Point::Point(String measurement):

}

void Point::addTag(String name, String value) {
void Point::addTag(const String &name, String value) {
if(_tags.length() > 0) {
_tags += ',';
}
Expand All @@ -46,23 +46,67 @@ void Point::addTag(String name, String value) {
_tags += escapeKey(value);
}

void Point::addField(String name, long long value) {
void Point::addField(const String &name, long long value) {
char buff[50];
snprintf(buff, 50, "%lld", value);
putField(name, String(buff)+"i");
}

void Point::addField(String name, unsigned long long value) {
void Point::addField(const String &name, unsigned long long value) {
char buff[50];
snprintf(buff, 50, "%llu", value);
putField(name, String(buff)+"i");
}

void Point::addField(String name, const char *value) {
putField(name, "\"" + escapeValue(value) + "\"");
void Point::addField(const String &name, const char *value) {
putField(name, escapeValue(value));
}

void Point::putField(String name, String value) {
void Point::addField(const String &name, const __FlashStringHelper *pstr) {
addField(name, String(pstr));
}

void Point::addField(const String &name, float value, int decimalPlaces) {
if(!isnan(value)) putField(name, String(value, decimalPlaces));
}

void Point::addField(const String &name, double value, int decimalPlaces) {
if(!isnan(value)) putField(name, String(value, decimalPlaces));
}

void Point::addField(const String &name, char value) {
addField(name, String(value).c_str());
}

void Point::addField(const String &name, unsigned char value) {
putField(name, String(value)+"i");
}

void Point::addField(const String &name, int value) {
putField(name, String(value)+"i");
}

void Point::addField(const String &name, unsigned int value) {
putField(name, String(value)+"i");
}

void Point::addField(const String &name, long value) {
putField(name, String(value)+"i");
}

void Point::addField(const String &name, unsigned long value) {
putField(name, String(value)+"i");
}

void Point::addField(const String &name, bool value) {
putField(name, bool2string(value));
}

void Point::addField(const String &name, const String &value) {
addField(name, value.c_str());
}

void Point::putField(const String &name, const String &value) {
if(_fields.length() > 0) {
_fields += ',';
}
Expand All @@ -71,11 +115,11 @@ void Point::putField(String name, String value) {
_fields += value;
}

String Point::toLineProtocol(String includeTags) const {
String Point::toLineProtocol(const String &includeTags) const {
return createLineProtocol(includeTags);
}

String Point::createLineProtocol(String &incTags) const {
String Point::createLineProtocol(const String &incTags) const {
String line;
line.reserve(_measurement.length() + 1 + incTags.length() + 1 + _tags.length() + 1 + _fields.length() + 1 + _timestamp.length());
line += _measurement;
Expand Down Expand Up @@ -125,7 +169,7 @@ void Point::setTime(unsigned long long timestamp) {
_timestamp = timeStampToString(timestamp);
}

void Point::setTime(String timestamp) {
void Point::setTime(const String &timestamp) {
_timestamp = timestamp;
}

Expand Down
39 changes: 20 additions & 19 deletions src/Point.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,30 @@
class Point {
friend class InfluxDBClient;
public:
Point(String measurement);
Point(const String &measurement);
// Adds string tag
void addTag(String name, String value);
void addTag(const String &name, String value);
// Add field with various types
void addField(String name, float value, int decimalPlaces = 2) { if(!isnan(value)) putField(name, String(value, decimalPlaces)); }
void addField(String name, double value, int decimalPlaces = 2) { if(!isnan(value)) putField(name, String(value, decimalPlaces)); }
void addField(String name, char value) { addField(name, String(value).c_str()); }
void addField(String name, unsigned char value) { putField(name, String(value)+"i"); }
void addField(String name, int value) { putField(name, String(value)+"i"); }
void addField(String name, unsigned int value) { putField(name, String(value)+"i"); }
void addField(String name, long value) { putField(name, String(value)+"i"); }
void addField(String name, unsigned long value) { putField(name, String(value)+"i"); }
void addField(String name, bool value) { putField(name, bool2string(value)); }
void addField(String name, String value) { addField(name, value.c_str()); }
void addField(String name, long long value);
void addField(String name, unsigned long long value);
void addField(String name, const char *value);
void addField(const String &name, float value, int decimalPlaces = 2);
void addField(const String &name, double value, int decimalPlaces = 2);
void addField(const String &name, char value);
void addField(const String &name, unsigned char value);
void addField(const String &name, int value);
void addField(const String &name, unsigned int value);
void addField(const String &name, long value);
void addField(const String &name, unsigned long value);
void addField(const String &name, bool value);
void addField(const String &name, const String &value);
void addField(const String &name, const __FlashStringHelper *pstr);
void addField(const String &name, long long value);
void addField(const String &name, unsigned long long value);
void addField(const String &name, const char *value);
// Set timestamp to `now()` and store it in specified precision, nanoseconds by default. Date and time must be already set. See `configTime` in the device API
void setTime(WritePrecision writePrecision = WritePrecision::NS);
// Set timestamp in offset since epoch (1.1.1970). Correct precision must be set InfluxDBClient::setWriteOptions.
void setTime(unsigned long long timestamp);
// Set timestamp in offset since epoch (1.1.1970 00:00:00). Correct precision must be set InfluxDBClient::setWriteOptions.
void setTime(String timestamp);
void setTime(const String &timestamp);
// Clear all fields. Usefull for reusing point
void clearFields();
// Clear tags
Expand All @@ -72,7 +73,7 @@ friend class InfluxDBClient;
// True if a point contains timestamp
bool hasTime() const { return _timestamp.length() > 0; }
// Creates line protocol with optionally added tags
String toLineProtocol(String includeTags = "") const;
String toLineProtocol(const String &includeTags = "") const;
// returns current timestamp
String getTime() const { return _timestamp; }
protected:
Expand All @@ -82,8 +83,8 @@ friend class InfluxDBClient;
String _timestamp;
protected:
// method for formating field into line protocol
void putField(String name, String value);
void putField(const String &name, const String &value);
// Creates line protocol string
String createLineProtocol(String &incTags) const;
String createLineProtocol(const String &incTags) const;
};
#endif //_POINT_H_
6 changes: 3 additions & 3 deletions src/query/FluxParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ FluxQueryResult::FluxQueryResult(CsvReader *reader) {
_data = std::make_shared<Data>(reader);
}

FluxQueryResult::FluxQueryResult(String error):FluxQueryResult((CsvReader *)nullptr) {
FluxQueryResult::FluxQueryResult(const String &error):FluxQueryResult((CsvReader *)nullptr) {
_data->_error = error;
}

Expand All @@ -51,7 +51,7 @@ FluxQueryResult &FluxQueryResult::operator=(const FluxQueryResult &other) {
FluxQueryResult::~FluxQueryResult() {
}

int FluxQueryResult::getColumnIndex(String columnName) {
int FluxQueryResult::getColumnIndex(const String &columnName) {
int i = -1;
std::vector<String>::iterator it = find(_data->_columnNames.begin(), _data->_columnNames.end(), columnName);
if (it != _data->_columnNames.end()) {
Expand All @@ -68,7 +68,7 @@ FluxValue FluxQueryResult::getValueByIndex(int index) {
return ret;
}

FluxValue FluxQueryResult::getValueByName(String columnName) {
FluxValue FluxQueryResult::getValueByName(const String &columnName) {
FluxValue ret;
int i = getColumnIndex(columnName);
if(i > -1) {
Expand Down
6 changes: 3 additions & 3 deletions src/query/FluxParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class FluxQueryResult {
// Constructor for reading result
FluxQueryResult(CsvReader *reader);
// Constructor for error result
FluxQueryResult(String error);
FluxQueryResult(const String &error);
// Copy constructor
FluxQueryResult(const FluxQueryResult &other);
// Assignment operator
Expand All @@ -64,11 +64,11 @@ class FluxQueryResult {
// or an error. Call getError() and check non empty value
bool next();
// Returns index of the column, or -1 if not found
int getColumnIndex(String columnName);
int getColumnIndex(const String &columnName);
// Returns a converted value by index, or nullptr in case of missing value or wrong index
FluxValue getValueByIndex(int index);
// Returns a result value by column name, or nullptr in case of missing value or wrong column name
FluxValue getValueByName(String columnName);
FluxValue getValueByName(const String &columnName);
// Returns flux datatypes of all columns
std::vector<String> getColumnsDatatype() { return _data->_columnDatatypes; }
// Returns names of all columns
Expand Down
Loading