parent
739f27c0b8
commit
159f3481d8
@ -1,245 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* BucketsClient.cpp: InfluxDB Buckets Client
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020 InfluxData
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#include "BucketsClient.h"
|
||||
#include "util/helpers.h"
|
||||
|
||||
//#define INFLUXDB_CLIENT_DEBUG_ENABLE
|
||||
#include "util/debug.h"
|
||||
|
||||
static const char *propTemplate PROGMEM = "\"%s\":";
|
||||
// Finds first id property from JSON response
|
||||
enum class PropType {
|
||||
String,
|
||||
Number
|
||||
};
|
||||
|
||||
static String findProperty(const char *prop,const String &json, PropType type = PropType::String);
|
||||
|
||||
static String findProperty(const char *prop,const String &json, PropType type) {
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Searching for %s in %s\n", prop, json.c_str());
|
||||
int propLen = strlen_P(propTemplate)+strlen(prop)-2;
|
||||
char *propSearch = new char[propLen+1];
|
||||
sprintf_P(propSearch, propTemplate, prop);
|
||||
int i = json.indexOf(propSearch);
|
||||
delete [] propSearch;
|
||||
if(i>-1) {
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Found at %d\n", i);
|
||||
switch(type) {
|
||||
case PropType::String:
|
||||
i = json.indexOf("\"", i+propLen);
|
||||
if(i>-1) {
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Found starting \" at %d\n", i);
|
||||
int e = json.indexOf("\"", i+1);
|
||||
if(e>-1) {
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Found ending \" at %d\n", e);
|
||||
return json.substring(i+1, e);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case PropType::Number:
|
||||
i = i+propLen;
|
||||
while(json[i] == ' ') {
|
||||
i++;
|
||||
}
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Found beginning of number at %d\n", i);
|
||||
int e = json.indexOf(",", i+1);
|
||||
if(e>-1) {
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Found , at %d\n", e);
|
||||
return json.substring(i, e);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
char *copyChars(const char *str) {
|
||||
char *ret = new char[strlen(str)+1];
|
||||
strcpy(ret, str);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Bucket::Bucket():_data(nullptr) {
|
||||
}
|
||||
|
||||
Bucket::Bucket(const char *id, const char *name, const uint32_t expire) {
|
||||
_data = std::make_shared<Data>(id, name, expire);
|
||||
}
|
||||
|
||||
Bucket::Bucket(const Bucket &other) {
|
||||
_data = other._data;
|
||||
}
|
||||
|
||||
Bucket& Bucket::operator=(const Bucket& other) {
|
||||
if(this != &other) {
|
||||
_data = other._data;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Bucket::~Bucket() {
|
||||
}
|
||||
|
||||
|
||||
Bucket::Data::Data(const char *id, const char *name, const uint32_t expire) {
|
||||
this->id = copyChars(id);
|
||||
this->name = copyChars(name);
|
||||
this->expire = expire;
|
||||
}
|
||||
|
||||
Bucket::Data::~Data() {
|
||||
delete [] id;
|
||||
delete [] name;
|
||||
}
|
||||
|
||||
|
||||
const char *toStringTmplt PROGMEM = "Bucket: ID %s, Name %s, expire %u";
|
||||
String Bucket::toString() const {
|
||||
int len = strlen_P(toStringTmplt) + (_data?strlen(_data->name):0) + (_data?strlen(_data->id):0) + 10 + 1; //10 is maximum length of string representation of expire
|
||||
char *buff = new char[len];
|
||||
sprintf_P(buff, toStringTmplt, getID(), getName(), getExpire());
|
||||
String ret = buff;
|
||||
return ret;
|
||||
}
|
||||
|
||||
BucketsClient::BucketsClient() {
|
||||
_data = nullptr;
|
||||
}
|
||||
|
||||
BucketsClient::BucketsClient(ConnectionInfo *pConnInfo, HTTPService *service) {
|
||||
_data = std::make_shared<Data>(pConnInfo, service);
|
||||
}
|
||||
|
||||
BucketsClient::BucketsClient(const BucketsClient &other) {
|
||||
_data = other._data;
|
||||
}
|
||||
|
||||
BucketsClient &BucketsClient::operator=(const BucketsClient &other) {
|
||||
if(this != &other) {
|
||||
_data = other._data;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
BucketsClient &BucketsClient::operator=(std::nullptr_t) {
|
||||
_data = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
String BucketsClient::getOrgID(const char *org) {
|
||||
if(!_data) {
|
||||
return "";
|
||||
}
|
||||
if(isValidID(org)) {
|
||||
return org;
|
||||
}
|
||||
String url = _data->pService->getServerAPIURL();
|
||||
url += "orgs?org=";
|
||||
url += urlEncode(org);
|
||||
String id;
|
||||
INFLUXDB_CLIENT_DEBUG("[D] getOrgID: url %s\n", url.c_str());
|
||||
_data->pService->doGET(url.c_str(), 200, [&id](HTTPClient *client){
|
||||
id = findProperty("id",client->getString());
|
||||
return true;
|
||||
});
|
||||
return id;
|
||||
}
|
||||
|
||||
bool BucketsClient::checkBucketExists(const char *bucketName) {
|
||||
Bucket b = findBucket(bucketName);
|
||||
return !b.isNull();
|
||||
}
|
||||
|
||||
static const char *CreateBucketTemplate PROGMEM = "{\"name\":\"%s\",\"orgID\":\"%s\",\"retentionRules\":[{\"everySeconds\":%u}]}";
|
||||
|
||||
Bucket BucketsClient::createBucket(const char *bucketName, uint32_t expiresSec) {
|
||||
Bucket b;
|
||||
if(_data) {
|
||||
String orgID = getOrgID(_data->pConnInfo->org.c_str());
|
||||
|
||||
if(!orgID.length()) {
|
||||
return b;
|
||||
}
|
||||
int expireLen = 0;
|
||||
uint32_t e = expiresSec;
|
||||
do {
|
||||
expireLen++;
|
||||
e /=10;
|
||||
} while(e > 0);
|
||||
int len = strlen_P(CreateBucketTemplate) + strlen(bucketName) + orgID.length() + expireLen+1;
|
||||
char *body = new char[len];
|
||||
sprintf_P(body, CreateBucketTemplate, bucketName, orgID.c_str(), expiresSec);
|
||||
String url = _data->pService->getServerAPIURL();
|
||||
url += "buckets";
|
||||
INFLUXDB_CLIENT_DEBUG("[D] CreateBucket: url %s, body %s\n", url.c_str(), body);
|
||||
_data->pService->doPOST(url.c_str(), body, "application/json", 201, [&b](HTTPClient *client){
|
||||
String resp = client->getString();
|
||||
String id = findProperty("id", resp);
|
||||
String name = findProperty("name", resp);
|
||||
String expireStr = findProperty("everySeconds", resp, PropType::Number);
|
||||
uint32_t expire = strtoul(expireStr.c_str(), nullptr, 10);
|
||||
b = Bucket(id.c_str(), name.c_str(), expire);
|
||||
return true;
|
||||
});
|
||||
delete [] body;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
bool BucketsClient::deleteBucket(const char *id) {
|
||||
if(!_data) {
|
||||
|
||||
return false;
|
||||
}
|
||||
String url = _data->pService->getServerAPIURL();
|
||||
url += "buckets/";
|
||||
url += id;
|
||||
INFLUXDB_CLIENT_DEBUG("[D] deleteBucket: url %s\n", url.c_str());
|
||||
return _data->pService->doDELETE(url.c_str(), 204, nullptr);
|
||||
}
|
||||
|
||||
Bucket BucketsClient::findBucket(const char *bucketName) {
|
||||
Bucket b;
|
||||
if(_data) {
|
||||
String url = _data->pService->getServerAPIURL();
|
||||
url += "buckets?name=";
|
||||
url += urlEncode(bucketName);
|
||||
INFLUXDB_CLIENT_DEBUG("[D] findBucket: url %s\n", url.c_str());
|
||||
_data->pService->doGET(url.c_str(), 200, [&b](HTTPClient *client){
|
||||
String resp = client->getString();
|
||||
String id = findProperty("id", resp);
|
||||
if(id.length()) {
|
||||
String name = findProperty("name", resp);
|
||||
String expireStr = findProperty("everySeconds", resp, PropType::Number);
|
||||
uint32_t expire = strtoul(expireStr.c_str(), nullptr, 10);
|
||||
b = Bucket(id.c_str(), name.c_str(), expire);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
return b;
|
||||
}
|
@ -1,124 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* BucketsClient.h: InfluxDB Buckets Client
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020 InfluxData
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#ifndef _BUCKETS_CLIENT_H_
|
||||
#define _BUCKETS_CLIENT_H_
|
||||
|
||||
#include <HTTPService.h>
|
||||
#include <memory>
|
||||
|
||||
class BucketsClient;
|
||||
class Test;
|
||||
/**
|
||||
* Bucket represents a bucket in the InfluxDB 2 server
|
||||
**/
|
||||
class Bucket {
|
||||
friend class BucketsClient;
|
||||
friend class Test;
|
||||
public:
|
||||
// Create empty, invalid, bucket instance
|
||||
Bucket();
|
||||
// Create a bucket instance
|
||||
Bucket(const char *id, const char *name, const uint32_t expire);
|
||||
// Copy constructor
|
||||
Bucket(const Bucket &other);
|
||||
// Assignment operator
|
||||
Bucket &operator=(const Bucket &other);
|
||||
// for testing validity
|
||||
operator bool() const { return !isNull(); }
|
||||
// Clean bucket
|
||||
~Bucket();
|
||||
// Returns Bucket ID
|
||||
const char *getID() const { return _data?_data->id:nullptr; }
|
||||
// Retuns bucket name
|
||||
const char *getName() const { return _data?_data->name:nullptr; }
|
||||
// Retention policy in sec, 0 - inifinite
|
||||
uint32_t getExpire() const { return _data?_data->expire:0; }
|
||||
// Checks if it is null instance
|
||||
bool isNull() const { return _data == nullptr; }
|
||||
// String representation
|
||||
String toString() const;
|
||||
private:
|
||||
class Data {
|
||||
public:
|
||||
Data(const char *id, const char *name, const uint32_t expire);
|
||||
~Data();
|
||||
char *id;
|
||||
char *name;
|
||||
uint32_t expire;
|
||||
};
|
||||
std::shared_ptr<Data> _data;
|
||||
};
|
||||
|
||||
class InfluxDBClient;
|
||||
class E2ETest;
|
||||
|
||||
/**
|
||||
* BucketsClient is a client for managing buckets in the InfluxDB 2 server
|
||||
* A new bucket can be created, or a bucket can be checked for existence by its name.
|
||||
* A bucket can be also deleted.
|
||||
**/
|
||||
class BucketsClient {
|
||||
friend class InfluxDBClient;
|
||||
friend class Test;
|
||||
friend class E2ETest;
|
||||
public:
|
||||
// Copy contructor
|
||||
BucketsClient(const BucketsClient &other);
|
||||
// Assignment operator
|
||||
BucketsClient &operator=(const BucketsClient &other);
|
||||
// nullptr assignment for clearing
|
||||
BucketsClient &operator=(std::nullptr_t);
|
||||
// for testing validity
|
||||
operator bool() const { return !isNull(); }
|
||||
// Returns true if a bucket exists
|
||||
bool checkBucketExists(const char *bucketName);
|
||||
// Returns a Bucket instance if a bucket is found.
|
||||
// Returned instance must be manually deleted at the end of usage.
|
||||
Bucket findBucket(const char *bucketName);
|
||||
// Creates a bucket with given name and optional retention policy. 0 means infinite.
|
||||
// Returned instance must be manually deleted at the end of usage.
|
||||
Bucket createBucket(const char *bucketName, uint32_t expiresSec = 0);
|
||||
// Delete a bucket with given id. Use findBucket to get a bucket with id.
|
||||
bool deleteBucket(const char *id);
|
||||
// Returns last error message
|
||||
String getLastErrorMessage() { return _data?_data->pConnInfo->lastError:""; }
|
||||
// check validity
|
||||
bool isNull() const { return _data == nullptr; }
|
||||
protected:
|
||||
BucketsClient();
|
||||
BucketsClient(ConnectionInfo *pConnInfo, HTTPService *service);
|
||||
String getOrgID(const char *org);
|
||||
private:
|
||||
class Data {
|
||||
public:
|
||||
Data(ConnectionInfo *pConnInfo, HTTPService *pService):pConnInfo(pConnInfo),pService(pService) {};
|
||||
ConnectionInfo *pConnInfo;
|
||||
HTTPService *pService;
|
||||
};
|
||||
std::shared_ptr<Data> _data;
|
||||
};
|
||||
#endif
|
@ -1,210 +0,0 @@
|
||||
|
||||
#include "HTTPService.h"
|
||||
#include "Platform.h"
|
||||
#include "Version.h"
|
||||
|
||||
// Uncomment bellow in case of a problem and rebuild sketch
|
||||
//#define INFLUXDB_CLIENT_DEBUG_ENABLE
|
||||
#include "util/debug.h"
|
||||
|
||||
static const char UserAgent[] PROGMEM = "influxdb-client-arduino/" INFLUXDB_CLIENT_VERSION " (" INFLUXDB_CLIENT_PLATFORM " " INFLUXDB_CLIENT_PLATFORM_VERSION ")";
|
||||
|
||||
#if defined(ESP8266)
|
||||
bool checkMFLN(BearSSL::WiFiClientSecure *client, String url);
|
||||
#endif
|
||||
|
||||
// This cannot be put to PROGMEM due to the way how it is used
|
||||
static const char *RetryAfter = "Retry-After";
|
||||
const char *TransferEncoding = "Transfer-Encoding";
|
||||
|
||||
HTTPService::HTTPService(ConnectionInfo *pConnInfo):_pConnInfo(pConnInfo) {
|
||||
_apiURL = pConnInfo->serverUrl;
|
||||
_apiURL += "/api/v2/";
|
||||
bool https = pConnInfo->serverUrl.startsWith("https");
|
||||
if(https) {
|
||||
#if defined(ESP8266)
|
||||
BearSSL::WiFiClientSecure *wifiClientSec = new BearSSL::WiFiClientSecure;
|
||||
if (pConnInfo->insecure) {
|
||||
wifiClientSec->setInsecure();
|
||||
} else if(pConnInfo->certInfo && strlen_P(pConnInfo->certInfo) > 0) {
|
||||
if(strlen_P(pConnInfo->certInfo) > 60 ) { //differentiate fingerprint and cert
|
||||
_cert = new BearSSL::X509List(pConnInfo->certInfo);
|
||||
wifiClientSec->setTrustAnchors(_cert);
|
||||
} else {
|
||||
wifiClientSec->setFingerprint(pConnInfo->certInfo);
|
||||
}
|
||||
}
|
||||
checkMFLN(wifiClientSec, pConnInfo->serverUrl);
|
||||
#elif defined(ESP32)
|
||||
WiFiClientSecure *wifiClientSec = new WiFiClientSecure;
|
||||
if (pConnInfo->insecure) {
|
||||
#ifndef ARDUINO_ESP32_RELEASE_1_0_4
|
||||
// This works only in ESP32 SDK 1.0.5 and higher
|
||||
wifiClientSec->setInsecure();
|
||||
#endif
|
||||
} else if(pConnInfo->certInfo && strlen_P(pConnInfo->certInfo) > 0) {
|
||||
wifiClientSec->setCACert(pConnInfo->certInfo);
|
||||
}
|
||||
#endif
|
||||
_wifiClient = wifiClientSec;
|
||||
} else {
|
||||
_wifiClient = new WiFiClient;
|
||||
}
|
||||
if(!_httpClient) {
|
||||
_httpClient = new HTTPClient;
|
||||
}
|
||||
_httpClient->setReuse(_httpOptions._connectionReuse);
|
||||
|
||||
_httpClient->setUserAgent(FPSTR(UserAgent));
|
||||
};
|
||||
|
||||
HTTPService::~HTTPService() {
|
||||
if(_httpClient) {
|
||||
delete _httpClient;
|
||||
_httpClient = nullptr;
|
||||
}
|
||||
if(_wifiClient) {
|
||||
delete _wifiClient;
|
||||
_wifiClient = nullptr;
|
||||
}
|
||||
#if defined(ESP8266)
|
||||
if(_cert) {
|
||||
delete _cert;
|
||||
_cert = nullptr;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void HTTPService::setHTTPOptions(const HTTPOptions & httpOptions) {
|
||||
_httpOptions = httpOptions;
|
||||
if(!_httpClient) {
|
||||
_httpClient = new HTTPClient;
|
||||
}
|
||||
_httpClient->setReuse(_httpOptions._connectionReuse);
|
||||
_httpClient->setTimeout(_httpOptions._httpReadTimeout);
|
||||
#if defined(ESP32)
|
||||
_httpClient->setConnectTimeout(_httpOptions._httpReadTimeout);
|
||||
#endif
|
||||
}
|
||||
|
||||
// parse URL for host and port and call probeMaxFragmentLength
|
||||
#if defined(ESP8266)
|
||||
bool checkMFLN(BearSSL::WiFiClientSecure *client, String url) {
|
||||
int index = url.indexOf(':');
|
||||
if(index < 0) {
|
||||
return false;
|
||||
}
|
||||
String protocol = url.substring(0, index);
|
||||
int port = -1;
|
||||
url.remove(0, (index + 3)); // remove http:// or https://
|
||||
|
||||
if (protocol == "http") {
|
||||
// set default port for 'http'
|
||||
port = 80;
|
||||
} else if (protocol == "https") {
|
||||
// set default port for 'https'
|
||||
port = 443;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
index = url.indexOf('/');
|
||||
String host = url.substring(0, index);
|
||||
url.remove(0, index); // remove host
|
||||
// check Authorization
|
||||
index = host.indexOf('@');
|
||||
if(index >= 0) {
|
||||
host.remove(0, index + 1); // remove auth part including @
|
||||
}
|
||||
// get port
|
||||
index = host.indexOf(':');
|
||||
if(index >= 0) {
|
||||
String portS = host;
|
||||
host = host.substring(0, index); // hostname
|
||||
portS.remove(0, (index + 1)); // remove hostname + :
|
||||
port = portS.toInt(); // get port
|
||||
}
|
||||
INFLUXDB_CLIENT_DEBUG("[D] probeMaxFragmentLength to %s:%d\n", host.c_str(), port);
|
||||
bool mfln = client->probeMaxFragmentLength(host, port, 1024);
|
||||
INFLUXDB_CLIENT_DEBUG("[D] MFLN:%s\n", mfln ? "yes" : "no");
|
||||
if (mfln) {
|
||||
client->setBufferSizes(1024, 1024);
|
||||
}
|
||||
return mfln;
|
||||
}
|
||||
#endif //ESP8266
|
||||
|
||||
bool HTTPService::beforeRequest(const char *url) {
|
||||
if(!_httpClient->begin(*_wifiClient, url)) {
|
||||
_pConnInfo->lastError = F("begin failed");
|
||||
return false;
|
||||
}
|
||||
if(_pConnInfo->authToken.length() > 0) {
|
||||
_httpClient->addHeader(F("Authorization"), "Token " + _pConnInfo->authToken);
|
||||
}
|
||||
const char * headerKeys[] = {RetryAfter, TransferEncoding} ;
|
||||
_httpClient->collectHeaders(headerKeys, 2);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool HTTPService::doPOST(const char *url, const char *data, const char *contentType, int expectedCode, httpResponseCallback cb) {
|
||||
INFLUXDB_CLIENT_DEBUG("[D] POST request - %s, data: %dbytes, type %s\n", url, strlen(data), contentType);
|
||||
if(!beforeRequest(url)) {
|
||||
return false;
|
||||
}
|
||||
if(contentType) {
|
||||
_httpClient->addHeader(F("Content-Type"), FPSTR(contentType));
|
||||
}
|
||||
_lastStatusCode = _httpClient->POST((uint8_t *) data, strlen(data));
|
||||
return afterRequest(expectedCode, cb);
|
||||
}
|
||||
|
||||
bool HTTPService::doGET(const char *url, int expectedCode, httpResponseCallback cb) {
|
||||
INFLUXDB_CLIENT_DEBUG("[D] GET request - %s\n", url);
|
||||
if(!beforeRequest(url)) {
|
||||
return false;
|
||||
}
|
||||
_lastStatusCode = _httpClient->GET();
|
||||
return afterRequest(expectedCode, cb, false);
|
||||
}
|
||||
|
||||
bool HTTPService::doDELETE(const char *url, int expectedCode, httpResponseCallback cb) {
|
||||
INFLUXDB_CLIENT_DEBUG("[D] DELETE - %s\n", url);
|
||||
if(!beforeRequest(url)) {
|
||||
return false;
|
||||
}
|
||||
_lastStatusCode = _httpClient->sendRequest("DELETE");
|
||||
return afterRequest(expectedCode, cb, false);
|
||||
}
|
||||
|
||||
bool HTTPService::afterRequest(int expectedStatusCode, httpResponseCallback cb, bool modifyLastConnStatus) {
|
||||
if(modifyLastConnStatus) {
|
||||
_lastRequestTime = millis();
|
||||
INFLUXDB_CLIENT_DEBUG("[D] HTTP status code - %d\n", _lastStatusCode);
|
||||
_lastRetryAfter = 0;
|
||||
if(_lastStatusCode >= 429) { //retryable server errors
|
||||
if(_httpClient->hasHeader(RetryAfter)) {
|
||||
_lastRetryAfter = _httpClient->header(RetryAfter).toInt();
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Reply after - %d\n", _lastRetryAfter);
|
||||
}
|
||||
}
|
||||
}
|
||||
_pConnInfo->lastError = (char *)nullptr;
|
||||
bool ret = _lastStatusCode == expectedStatusCode;
|
||||
bool endConnection = true;
|
||||
if(!ret) {
|
||||
if(_lastStatusCode > 0) {
|
||||
_pConnInfo->lastError = _httpClient->getString();
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Response:\n%s\n", _pConnInfo->lastError.c_str());
|
||||
} else {
|
||||
_pConnInfo->lastError = _httpClient->errorToString(_lastStatusCode);
|
||||
INFLUXDB_CLIENT_DEBUG("[E] Error - %s\n", _pConnInfo->lastError.c_str());
|
||||
}
|
||||
} else if(cb){
|
||||
endConnection = cb(_httpClient);
|
||||
}
|
||||
if(endConnection) {
|
||||
_httpClient->end();
|
||||
}
|
||||
return ret;
|
||||
}
|
@ -1,132 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* HTTPService.h: HTTP Service
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020 InfluxData
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#ifndef _HTTP_SERVICE_H_
|
||||
#define _HTTP_SERVICE_H_
|
||||
|
||||
#include <Arduino.h>
|
||||
#if defined(ESP8266)
|
||||
# include <WiFiClientSecureBearSSL.h>
|
||||
# include <ESP8266HTTPClient.h>
|
||||
#elif defined(ESP32)
|
||||
# include <HTTPClient.h>
|
||||
#else
|
||||
# error "This library currently supports only ESP8266 and ESP32."
|
||||
#endif
|
||||
#include "Options.h"
|
||||
|
||||
|
||||
class Test;
|
||||
typedef std::function<bool(HTTPClient *client)> httpResponseCallback;
|
||||
extern const char *TransferEncoding;
|
||||
|
||||
struct ConnectionInfo {
|
||||
// Connection info
|
||||
String serverUrl;
|
||||
// Write & query targets
|
||||
String bucket;
|
||||
String org;
|
||||
// v2 authetication token
|
||||
String authToken;
|
||||
// Version of InfluxDB 1 or 2
|
||||
uint8_t dbVersion;
|
||||
// V1 user authetication
|
||||
String user;
|
||||
String password;
|
||||
// Certificate info
|
||||
const char *certInfo;
|
||||
// flag if https should ignore cert validation
|
||||
bool insecure;
|
||||
// Error message of last failed operation
|
||||
String lastError;
|
||||
};
|
||||
|
||||
/**
|
||||
* HTTPService provides HTTP methods for communicating with InfluxDBServer,
|
||||
* while taking care of Authorization and error handling
|
||||
**/
|
||||
class HTTPService {
|
||||
friend class Test;
|
||||
private:
|
||||
// Connection info data
|
||||
ConnectionInfo *_pConnInfo;
|
||||
// Server API URL
|
||||
String _apiURL;
|
||||
// Last time in ms we made are a request to server
|
||||
uint32_t _lastRequestTime = 0;
|
||||
// HTTP status code of last request to server
|
||||
int _lastStatusCode = 0;
|
||||
// Underlying HTTPClient instance
|
||||
HTTPClient *_httpClient = nullptr;
|
||||
// Underlying connection object
|
||||
WiFiClient *_wifiClient = nullptr;
|
||||
#ifdef ESP8266
|
||||
// Trusted cert chain
|
||||
BearSSL::X509List *_cert = nullptr;
|
||||
#endif
|
||||
// Store retry timeout suggested by server after last request
|
||||
int _lastRetryAfter = 0;
|
||||
// HTTP options
|
||||
HTTPOptions _httpOptions;
|
||||
protected:
|
||||
// Sets request params
|
||||
bool beforeRequest(const char *url);
|
||||
// Handles response
|
||||
bool afterRequest(int expectedStatusCode, httpResponseCallback cb, bool modifyLastConnStatus = true);
|
||||
public:
|
||||
// Creates HTTPService instance
|
||||
// serverUrl - url of the InfluxDB 2 server (e.g. http://localhost:8086)
|
||||
// authToken - InfluxDB 2 authorization token
|
||||
// certInfo - InfluxDB 2 server trusted certificate (or CA certificate) or certificate SHA1 fingerprint. Should be stored in PROGMEM.
|
||||
HTTPService(ConnectionInfo *pConnInfo);
|
||||
// Clean instance on deletion
|
||||
~HTTPService();
|
||||
// Sets custom HTTP options. See HTTPOptions doc for more info.
|
||||
// Must be called before calling any method initiating a connection to server.
|
||||
// Example:
|
||||
// service.setHTTPOptions(HTTPOptions().httpReadTimeout(20000)).
|
||||
void setHTTPOptions(const HTTPOptions &httpOptions);
|
||||
// Returns current HTTPOption
|
||||
HTTPOptions &getHTTPOptions() { return _httpOptions; }
|
||||
// Performs HTTP POST by sending data. On success calls response call back
|
||||
bool doPOST(const char *url, const char *data, const char *contentType, int expectedCode, httpResponseCallback cb);
|
||||
// Performs HTTP GET. On success calls response call back
|
||||
bool doGET(const char *url, int expectedCode, httpResponseCallback cb);
|
||||
// Performs HTTP DELETE. On success calls response call back
|
||||
bool doDELETE(const char *url, int expectedCode, httpResponseCallback cb);
|
||||
// Returns InfluxDBServer API URL
|
||||
String getServerAPIURL() const { return _apiURL; }
|
||||
// Returns value of the Retry-After HTTP header from recent call. 0 if it was missing.
|
||||
int getLastRetryAfter() const { return _lastRetryAfter; }
|
||||
// Returns HTTP status code of recent call.
|
||||
int getLastStatusCode() const { return _lastStatusCode; }
|
||||
// Returns time of recent call successful call.
|
||||
uint32_t getLastRequestTime() const { return _lastRequestTime; }
|
||||
// Returns response of last failed call.
|
||||
String getLastErrorMessage() const { return _pConnInfo->lastError; }
|
||||
};
|
||||
|
||||
#endif //_HTTP_SERVICE_H_
|
@ -1,38 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* InfluxData.cpp: InfluxDB Client for Arduino
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2018-2020 Tobias Schürg
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#include "InfluxData.h"
|
||||
#include "util/helpers.h"
|
||||
|
||||
void InfluxData::setTimestamp(long int seconds)
|
||||
{
|
||||
_timestamp = timeStampToString(seconds) + "000000000";
|
||||
}
|
||||
|
||||
String InfluxData::toString() const {
|
||||
String t;
|
||||
return createLineProtocol(t);
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* InfluxData.h: InfluxDB Client for Arduino
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2018-2020 Tobias Schürg
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#include "InfluxDbClient.h"
|
||||
|
||||
class InfluxData : public Point {
|
||||
public:
|
||||
InfluxData(String measurement) : Point(measurement) {}
|
||||
|
||||
void addValue(String key, float value) { addField(key, value); }
|
||||
void addValueString(String key, String value) { addField(key, value); }
|
||||
void setTimestamp(long int seconds);
|
||||
String toString() const;
|
||||
};
|
@ -1,157 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* InfluxDb.cpp: InfluxDB Client for Arduino
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2018-2020 Tobias Schürg
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#include "InfluxDb.h"
|
||||
#include "Arduino.h"
|
||||
|
||||
/**
|
||||
* Construct an InfluxDb instance.
|
||||
* @param host the InfluxDb host
|
||||
* @param port the InfluxDb port
|
||||
*/
|
||||
Influxdb::Influxdb(String host, uint16_t port) {
|
||||
if(port == 443) {
|
||||
// this happens usualy when influxdb is behind fw/proxy. Mostly, when influxdb is switched to https, the port remains the same (8086)
|
||||
// port number shouldn't be qualificator for secure connection, either scheme or a flag
|
||||
_connInfo.serverUrl = "https://";
|
||||
} else {
|
||||
_connInfo.serverUrl = "http://";
|
||||
}
|
||||
_connInfo.serverUrl += host + ":" + String(port);
|
||||
_connInfo.dbVersion = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the database to be used.
|
||||
* @param db the Influx Database to be written to.
|
||||
*/
|
||||
void Influxdb::setDb(String db) {
|
||||
_connInfo.bucket = db;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the database to be used with authentication.
|
||||
*/
|
||||
void Influxdb::setDbAuth(String db, String user, String pass) {
|
||||
_connInfo.bucket = db;
|
||||
_connInfo.user = user;
|
||||
_connInfo.password = pass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Bucket to be used v2.0 ONLY.
|
||||
* @param bucket the InfluxDB Bucket which must already exist
|
||||
*/
|
||||
void Influxdb::setBucket(String bucket) {
|
||||
_connInfo.bucket = bucket;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the influxDB port.
|
||||
* @param port both v1.x and v3 use 8086
|
||||
*/
|
||||
void Influxdb::setPort(uint16_t port){
|
||||
int b = _connInfo.serverUrl.indexOf(":",5);
|
||||
if(b > 0) {
|
||||
_connInfo.serverUrl = _connInfo.serverUrl.substring(0, b+1) + String(port);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Set the Organization to be used v2.0 ONLY
|
||||
* @param org the Name of the organization unit to use which must already exist
|
||||
*/
|
||||
void Influxdb::setOrg(String org){
|
||||
_connInfo.org = org;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the authorization token v2.0 ONLY
|
||||
* @param token the Auth Token from InfluxDBv2 *required*
|
||||
*/
|
||||
void Influxdb::setToken(String token){
|
||||
_connInfo.authToken = token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the version of InfluxDB to write to
|
||||
* @param version accepts 1 for version 1.x or 2 for version 2.x
|
||||
*/
|
||||
void Influxdb::setVersion(uint16_t version){
|
||||
_connInfo.dbVersion = version;
|
||||
}
|
||||
|
||||
#if defined(ESP8266)
|
||||
/**
|
||||
* Set server certificate finger print
|
||||
* @param fingerPrint server certificate finger print
|
||||
*/
|
||||
void Influxdb::setFingerPrint(const char *fingerPrint){
|
||||
_connInfo.certInfo = fingerPrint;
|
||||
}
|
||||
#endif
|
||||
|
||||
void Influxdb::begin() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a measurement to be sent.
|
||||
*/
|
||||
void Influxdb::prepare(InfluxData data) {
|
||||
++_preparedPoints;
|
||||
if(_writeOptions._batchSize <= _preparedPoints) {
|
||||
// for preparation, batchsize must be greater than number of prepared points, or it will send data right away
|
||||
_writeOptions._batchSize = _preparedPoints+1;
|
||||
reserveBuffer(2*_writeOptions._batchSize);
|
||||
}
|
||||
write(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write all prepared measurements into the db.
|
||||
*/
|
||||
boolean Influxdb::write() {
|
||||
_preparedPoints = 0;
|
||||
return flushBuffer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a single measurement into the db.
|
||||
*/
|
||||
boolean Influxdb::write(InfluxData data) {
|
||||
return write(pointToLineProtocol(data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Send raw data to InfluxDb.
|
||||
*
|
||||
* @see
|
||||
* https://github.com/esp8266/Arduino/blob/cc0bfa04d401810ed3f5d7d01be6e88b9011997f/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h#L44-L55
|
||||
* for a list of error codes.
|
||||
*/
|
||||
boolean Influxdb::write(String data) {
|
||||
return writeRecord(data);
|
||||
}
|
@ -1,59 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* InfluxDb.h: InfluxDB Client for Arduino
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2018-2020 Tobias Schürg
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#ifndef _INFLUXDB_H_
|
||||
#define _INFLUXDB_H
|
||||
|
||||
#include "InfluxData.h"
|
||||
|
||||
class Influxdb : public InfluxDBClient {
|
||||
public:
|
||||
Influxdb(String host, uint16_t port = 8086);
|
||||
|
||||
void setDb(String db);
|
||||
void setDbAuth(String db, String user, String pass);
|
||||
|
||||
void setVersion(uint16_t version);
|
||||
void setBucket(String bucket);
|
||||
void setOrg(String org);
|
||||
void setToken(String token);
|
||||
void setPort(uint16_t port);
|
||||
#if defined(ESP8266)
|
||||
void setFingerPrint(const char *fingerPrint);
|
||||
#endif
|
||||
|
||||
void prepare(InfluxData data);
|
||||
boolean write();
|
||||
|
||||
boolean write(InfluxData data);
|
||||
boolean write(String data);
|
||||
|
||||
private:
|
||||
uint16_t _preparedPoints;
|
||||
|
||||
void begin();
|
||||
};
|
||||
#endif
|
@ -1,596 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* InfluxDBClient.cpp: InfluxDB Client for Arduino
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020 InfluxData
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#include "InfluxDbClient.h"
|
||||
#include "Platform.h"
|
||||
#include "Version.h"
|
||||
|
||||
// Uncomment bellow in case of a problem and rebuild sketch
|
||||
//#define INFLUXDB_CLIENT_DEBUG_ENABLE
|
||||
#include "util/debug.h"
|
||||
|
||||
static const char TooEarlyMessage[] PROGMEM = "Cannot send request yet because of applied retry strategy. Remaining ";
|
||||
|
||||
static String escapeJSONString(String &value);
|
||||
static String precisionToString(WritePrecision precision, uint8_t version = 2) {
|
||||
switch(precision) {
|
||||
case WritePrecision::US:
|
||||
return version==1?"u":"us";
|
||||
case WritePrecision::MS:
|
||||
return "ms";
|
||||
case WritePrecision::NS:
|
||||
return "ns";
|
||||
case WritePrecision::S:
|
||||
return "s";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
InfluxDBClient::InfluxDBClient() {
|
||||
resetBuffer();
|
||||
}
|
||||
|
||||
InfluxDBClient::InfluxDBClient(const char *serverUrl, const char *db):InfluxDBClient() {
|
||||
setConnectionParamsV1(serverUrl, db);
|
||||
}
|
||||
|
||||
InfluxDBClient::InfluxDBClient(const char *serverUrl, const char *org, const char *bucket, const char *authToken):InfluxDBClient(serverUrl, org, bucket, authToken, nullptr) {
|
||||
}
|
||||
|
||||
InfluxDBClient::InfluxDBClient(const char *serverUrl, const char *org, const char *bucket, const char *authToken, const char *serverCert):InfluxDBClient() {
|
||||
setConnectionParams(serverUrl, org, bucket, authToken, serverCert);
|
||||
}
|
||||
|
||||
void InfluxDBClient::setInsecure(bool value){
|
||||
_connInfo.insecure = value;
|
||||
}
|
||||
|
||||
void InfluxDBClient::setConnectionParams(const char *serverUrl, const char *org, const char *bucket, const char *authToken, const char *certInfo) {
|
||||
clean();
|
||||
_connInfo.serverUrl = serverUrl;
|
||||
_connInfo.bucket = bucket;
|
||||
_connInfo.org = org;
|
||||
_connInfo.authToken = authToken;
|
||||
_connInfo.certInfo = certInfo;
|
||||
_connInfo.dbVersion = 2;
|
||||
}
|
||||
|
||||
void InfluxDBClient::setConnectionParamsV1(const char *serverUrl, const char *db, const char *user, const char *password, const char *certInfo) {
|
||||
clean();
|
||||
_connInfo.serverUrl = serverUrl;
|
||||
_connInfo.bucket = db;
|
||||
_connInfo.user = user;
|
||||
_connInfo.password = password;
|
||||
_connInfo.certInfo = certInfo;
|
||||
_connInfo.dbVersion = 1;
|
||||
}
|
||||
|
||||
bool InfluxDBClient::init() {
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Init\n");
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Library version: " INFLUXDB_CLIENT_VERSION "\n");
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Device : " INFLUXDB_CLIENT_PLATFORM "\n");
|
||||
INFLUXDB_CLIENT_DEBUG("[D] SDK version: " INFLUXDB_CLIENT_PLATFORM_VERSION "\n");
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Server url: %s\n", _connInfo.serverUrl.c_str());
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Org: %s\n", _connInfo.org.c_str());
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Bucket: %s\n", _connInfo.bucket.c_str());
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Token: %s\n", _connInfo.authToken.c_str());
|
||||
INFLUXDB_CLIENT_DEBUG("[D] DB version: %d\n", _connInfo.dbVersion);
|
||||
if(_connInfo.serverUrl.length() == 0 || (_connInfo.dbVersion == 2 && (_connInfo.org.length() == 0 || _connInfo.bucket.length() == 0 || _connInfo.authToken.length() == 0))) {
|
||||
INFLUXDB_CLIENT_DEBUG("[E] Invalid parameters\n");
|
||||
_connInfo.lastError = F("Invalid parameters");
|
||||
return false;
|
||||
}
|
||||
if(_connInfo.serverUrl.endsWith("/")) {
|
||||
_connInfo.serverUrl = _connInfo.serverUrl.substring(0,_connInfo.serverUrl.length()-1);
|
||||
}
|
||||
if(!_connInfo.serverUrl.startsWith("http")) {
|
||||
_connInfo.lastError = F("Invalid URL scheme");
|
||||
return false;
|
||||
}
|
||||
_service = new HTTPService(&_connInfo);
|
||||
|
||||
setUrls();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
InfluxDBClient::~InfluxDBClient() {
|
||||
if(_writeBuffer) {
|
||||
delete [] _writeBuffer;
|
||||
_writeBuffer = nullptr;
|
||||
_bufferPointer = 0;
|
||||
_batchPointer = 0;
|
||||
_bufferCeiling = 0;
|
||||
}
|
||||
clean();
|
||||
}
|
||||
|
||||
void InfluxDBClient::clean() {
|
||||
if(_service) {
|
||||
delete _service;
|
||||
_service = nullptr;
|
||||
}
|
||||
_buckets = nullptr;
|
||||
_lastFlushed = millis();
|
||||
_retryTime = 0;
|
||||
}
|
||||
|
||||
bool InfluxDBClient::setUrls() {
|
||||
if(!_service && !init()) {
|
||||
return false;
|
||||
}
|
||||
INFLUXDB_CLIENT_DEBUG("[D] setUrls\n");
|
||||
if( _connInfo.dbVersion == 2) {
|
||||
_writeUrl = _service->getServerAPIURL();
|
||||
_writeUrl += "write?org=";
|
||||
_writeUrl += urlEncode(_connInfo.org.c_str());
|
||||
_writeUrl += "&bucket=";
|
||||
_writeUrl += urlEncode(_connInfo.bucket.c_str());
|
||||
INFLUXDB_CLIENT_DEBUG("[D] writeUrl: %s\n", _writeUrl.c_str());
|
||||
_queryUrl = _service->getServerAPIURL();;
|
||||
_queryUrl += "query?org=";
|
||||
_queryUrl += urlEncode(_connInfo.org.c_str());
|
||||
INFLUXDB_CLIENT_DEBUG("[D] queryUrl: %s\n", _queryUrl.c_str());
|
||||
} else {
|
||||
_writeUrl = _connInfo.serverUrl;
|
||||
_writeUrl += "/write?db=";
|
||||
_writeUrl += urlEncode(_connInfo.bucket.c_str());
|
||||
_queryUrl = _connInfo.serverUrl;
|
||||
_queryUrl += "/api/v2/query";
|
||||
if(_connInfo.user.length() > 0 && _connInfo.password.length() > 0) {
|
||||
String auth = "&u=";
|
||||
auth += urlEncode(_connInfo.user.c_str());
|
||||
auth += "&p=";
|
||||
auth += urlEncode(_connInfo.password.c_str());
|
||||
_writeUrl += auth;
|
||||
_queryUrl += "?";
|
||||
_queryUrl += auth;
|
||||
}
|
||||
INFLUXDB_CLIENT_DEBUG("[D] writeUrl: %s\n", _writeUrl.c_str());
|
||||
INFLUXDB_CLIENT_DEBUG("[D] queryUrl: %s\n", _queryUrl.c_str());
|
||||
}
|
||||
if(_writeOptions._writePrecision != WritePrecision::NoTime) {
|
||||
_writeUrl += "&precision=";
|
||||
_writeUrl += precisionToString(_writeOptions._writePrecision, _connInfo.dbVersion);
|
||||
INFLUXDB_CLIENT_DEBUG("[D] writeUrl: %s\n", _writeUrl.c_str());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool InfluxDBClient::setWriteOptions(WritePrecision precision, uint16_t batchSize, uint16_t bufferSize, uint16_t flushInterval, bool preserveConnection) {
|
||||
if(!_service && !init()) {
|
||||
return false;
|
||||
}
|
||||
if(!setWriteOptions(WriteOptions().writePrecision(precision).batchSize(batchSize).bufferSize(bufferSize).flushInterval(flushInterval))) {
|
||||
return false;
|
||||
}
|
||||
if(!setHTTPOptions(_service->getHTTPOptions().connectionReuse(preserveConnection))) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool InfluxDBClient::setWriteOptions(const WriteOptions & writeOptions) {
|
||||
if(_writeOptions._writePrecision != writeOptions._writePrecision) {
|
||||
_writeOptions._writePrecision = writeOptions._writePrecision;
|
||||
if(!setUrls()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool writeBufferSizeChanges = false;
|
||||
if(writeOptions._batchSize > 0 && _writeOptions._batchSize != writeOptions._batchSize) {
|
||||
_writeOptions._batchSize = writeOptions._batchSize;
|
||||
writeBufferSizeChanges = true;
|
||||
}
|
||||
if(writeOptions._bufferSize > 0 && _writeOptions._bufferSize != writeOptions._bufferSize) {
|
||||
_writeOptions._bufferSize = writeOptions._bufferSize;
|
||||
if(_writeOptions._bufferSize < 2*_writeOptions._batchSize) {
|
||||
_writeOptions._bufferSize = 2*_writeOptions._batchSize;
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Changing buffer size to %d\n", _writeOptions._bufferSize);
|
||||
}
|
||||
writeBufferSizeChanges = true;
|
||||
}
|
||||
if(writeBufferSizeChanges) {
|
||||
resetBuffer();
|
||||
}
|
||||
_writeOptions._flushInterval = writeOptions._flushInterval;
|
||||
_writeOptions._retryInterval = writeOptions._retryInterval;
|
||||
_writeOptions._maxRetryInterval = writeOptions._maxRetryInterval;
|
||||
_writeOptions._maxRetryAttempts = writeOptions._maxRetryAttempts;
|
||||
_writeOptions._defaultTags = writeOptions._defaultTags;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool InfluxDBClient::setHTTPOptions(const HTTPOptions & httpOptions) {
|
||||
if(!_service && !init()) {
|
||||
return false;
|
||||
}
|
||||
_service->setHTTPOptions(httpOptions);
|
||||
return true;
|
||||
}
|
||||
|
||||
BucketsClient InfluxDBClient::getBucketsClient() {
|
||||
if(!_service && !init()) {
|
||||
return BucketsClient();
|
||||
}
|
||||
if(!_buckets) {
|
||||
_buckets = BucketsClient(&_connInfo, _service);
|
||||
}
|
||||
return _buckets;
|
||||
}
|
||||
|
||||
void InfluxDBClient::resetBuffer() {
|
||||
if(_writeBuffer) {
|
||||
for(int i=0;i<_writeBufferSize;i++) {
|
||||
delete _writeBuffer[i];
|
||||
}
|
||||
delete [] _writeBuffer;
|
||||
}
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Reset buffer: buffer Size: %d, batch size: %d\n", _writeOptions._bufferSize, _writeOptions._batchSize);
|
||||
uint16_t a = _writeOptions._bufferSize/_writeOptions._batchSize;
|
||||
//limit to max(byte)
|
||||
_writeBufferSize = a>=(1<<8)?(1<<8)-1:a;
|
||||
if(_writeBufferSize < 2) {
|
||||
_writeBufferSize = 2;
|
||||
}
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Reset buffer: writeBuffSize: %d\n", _writeBufferSize);
|
||||
_writeBuffer = new Batch*[_writeBufferSize];
|
||||
for(int i=0;i<_writeBufferSize;i++) {
|
||||
_writeBuffer[i] = nullptr;
|
||||
}
|
||||
_bufferPointer = 0;
|
||||
_batchPointer = 0;
|
||||
_bufferCeiling = 0;
|
||||
}
|
||||
|
||||
void InfluxDBClient::reserveBuffer(int size) {
|
||||
if(size > _writeBufferSize) {
|
||||
Batch **newBuffer = new Batch*[size];
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Resizing buffer from %d to %d\n",_writeBufferSize, size);
|
||||
for(int i=0;i<_bufferCeiling; i++) {
|
||||
newBuffer[i] = _writeBuffer[i];
|
||||
}
|
||||
|
||||
delete [] _writeBuffer;
|
||||
_writeBuffer = newBuffer;
|
||||
_writeBufferSize = size;
|
||||
}
|
||||
}
|
||||
|
||||
bool InfluxDBClient::writePoint(Point & point) {
|
||||
if (point.hasFields()) {
|
||||
if(_writeOptions._writePrecision != WritePrecision::NoTime && !point.hasTime()) {
|
||||
point.setTime(_writeOptions._writePrecision);
|
||||
}
|
||||
String line = pointToLineProtocol(point);
|
||||
return writeRecord(line);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool InfluxDBClient::Batch::append(String &line) {
|
||||
if(pointer == _size) {
|
||||
//overwriting, clean buffer
|
||||
for(int i=0;i< _size; i++) {
|
||||
buffer[i] = (const char *)nullptr;
|
||||
}
|
||||
pointer = 0;
|
||||
}
|
||||
buffer[pointer] = line;
|
||||
++pointer;
|
||||
return isFull();
|
||||
}
|
||||
|
||||
char * InfluxDBClient::Batch::createData() {
|
||||
int length = 0;
|
||||
char *buff = nullptr;
|
||||
for(int c=0; c < pointer; c++) {
|
||||
length += buffer[c].length();
|
||||
yield();
|
||||
}
|
||||
//create buffer for all lines including new line char and terminating char
|
||||
if(length) {
|
||||
buff = new char[length + pointer + 1];
|
||||
if(buff) {
|
||||
buff[0] = 0;
|
||||
for(int c=0; c < pointer; c++) {
|
||||
strcat(buff+strlen(buff), buffer[c].c_str());
|
||||
strcat(buff+strlen(buff), "\n");
|
||||
yield();
|
||||
}
|
||||
}
|
||||
}
|
||||
return buff;
|
||||
}
|
||||
|
||||
bool InfluxDBClient::writeRecord(String &record) {
|
||||
if(!_writeBuffer[_bufferPointer]) {
|
||||
_writeBuffer[_bufferPointer] = new Batch(_writeOptions._batchSize);
|
||||
}
|
||||
if(isBufferFull() && _batchPointer <= _bufferPointer) {
|
||||
// When we are overwriting buffer and nothing is written, batchPointer must point to the oldest point
|
||||
_batchPointer = _bufferPointer+1;
|
||||
if(_batchPointer == _writeBufferSize) {
|
||||
_batchPointer = 0;
|
||||
}
|
||||
}
|
||||
if(_writeBuffer[_bufferPointer]->append(record)) { //we reached batch size
|
||||
_bufferPointer++;
|
||||
if(_bufferPointer == _writeBufferSize) { // writeBuffer is full
|
||||
_bufferPointer = 0;
|
||||
INFLUXDB_CLIENT_DEBUG("[W] Reached write buffer size, old points will be overwritten\n");
|
||||
}
|
||||
|
||||
if(_bufferCeiling < _writeBufferSize) {
|
||||
_bufferCeiling++;
|
||||
}
|
||||
}
|
||||
INFLUXDB_CLIENT_DEBUG("[D] writeRecord: bufferPointer: %d, batchPointer: %d, _bufferCeiling: %d\n", _bufferPointer, _batchPointer, _bufferCeiling);
|
||||
return checkBuffer();
|
||||
}
|
||||
|
||||
bool InfluxDBClient::checkBuffer() {
|
||||
// in case we (over)reach batchSize with non full buffer
|
||||
bool bufferReachedBatchsize = _writeBuffer[_batchPointer] && _writeBuffer[_batchPointer]->isFull();
|
||||
// or flush interval timed out
|
||||
bool flushTimeout = _writeOptions._flushInterval > 0 && ((millis() - _lastFlushed)/1000) >= _writeOptions._flushInterval;
|
||||
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Flushing buffer: is oversized %s, is timeout %s, is buffer full %s\n",
|
||||
bool2string(bufferReachedBatchsize),bool2string(flushTimeout), bool2string(isBufferFull()));
|
||||
|
||||
if(bufferReachedBatchsize || flushTimeout || isBufferFull() ) {
|
||||
|
||||
return flushBufferInternal(!flushTimeout);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool InfluxDBClient::flushBuffer() {
|
||||
return flushBufferInternal(false);
|
||||
}
|
||||
|
||||
uint32_t InfluxDBClient::getRemainingRetryTime() {
|
||||
uint32_t rem = 0;
|
||||
if(_retryTime > 0) {
|
||||
int32_t diff = _retryTime - (millis()-_service->getLastRequestTime())/1000;
|
||||
rem = diff<0?0:(uint32_t)diff;
|
||||
}
|
||||
return rem;
|
||||
}
|
||||
|
||||
bool InfluxDBClient::flushBufferInternal(bool flashOnlyFull) {
|
||||
uint32_t rwt = getRemainingRetryTime();
|
||||
if(rwt > 0) {
|
||||
INFLUXDB_CLIENT_DEBUG("[W] Cannot write yet, pause %ds, %ds yet\n", _retryTime, rwt);
|
||||
// retry after period didn't run out yet
|
||||
_connInfo.lastError = FPSTR(TooEarlyMessage);
|
||||
_connInfo.lastError += String(rwt);
|
||||
_connInfo.lastError += "s";
|
||||
return false;
|
||||
}
|
||||
char *data;
|
||||
bool success = true;
|
||||
// send all batches, It could happen there was long network outage and buffer is full
|
||||
while(_writeBuffer[_batchPointer] && (!flashOnlyFull || _writeBuffer[_batchPointer]->isFull())) {
|
||||
data = _writeBuffer[_batchPointer]->createData();
|
||||
if(!_writeBuffer[_batchPointer]->isFull() && _writeBuffer[_batchPointer]->retryCount == 0 ) { //do not increase pointer in case of retrying
|
||||
// points will be written so increase _bufferPointer as it happen when buffer is flushed when is full
|
||||
if(++_bufferPointer == _writeBufferSize) {
|
||||
_bufferPointer = 0;
|
||||
}
|
||||
}
|
||||
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Writing batch, batchpointer: %d, size %d\n", _batchPointer, _writeBuffer[_batchPointer]->pointer);
|
||||
if(data) {
|
||||
int statusCode = postData(data);
|
||||
delete [] data;
|
||||
// retry on unsuccessfull connection or retryable status codes
|
||||
bool retry = statusCode < 0 || statusCode >= 429;
|
||||
success = statusCode >= 200 && statusCode < 300;
|
||||
// advance even on message failure x e <300;429)
|
||||
if(success || !retry) {
|
||||
_lastFlushed = millis();
|
||||
dropCurrentBatch();
|
||||
} else if(retry) {
|
||||
_writeBuffer[_batchPointer]->retryCount++;
|
||||
if(statusCode > 0) { //apply retry strategy only in case of HTTP errors
|
||||
if(_writeBuffer[_batchPointer]->retryCount > _writeOptions._maxRetryAttempts) {
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Reached max retry count, dropping batch\n");
|
||||
dropCurrentBatch();
|
||||
}
|
||||
if(!_retryTime) {
|
||||
_retryTime = _writeOptions._retryInterval;
|
||||
if(_writeBuffer[_batchPointer]) {
|
||||
for(int i=1;i<_writeBuffer[_batchPointer]->retryCount;i++) {
|
||||
_retryTime *= _writeOptions._retryInterval;
|
||||
}
|
||||
if(_retryTime > _writeOptions._maxRetryInterval) {
|
||||
_retryTime = _writeOptions._maxRetryInterval;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Leaving data in buffer for retry, retryInterval: %d\n",_retryTime);
|
||||
// in case of retryable failure break loop
|
||||
break;
|
||||
}
|
||||
}
|
||||
yield();
|
||||
}
|
||||
//Have we emptied the buffer?
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Success: %d, _bufferPointer: %d, _batchPointer: %d, _writeBuffer[_bufferPointer]_%p\n",success,_bufferPointer,_batchPointer, _writeBuffer[_bufferPointer]);
|
||||
if(_batchPointer == _bufferPointer && !_writeBuffer[_bufferPointer]) {
|
||||
_bufferPointer = 0;
|
||||
_batchPointer = 0;
|
||||
_bufferCeiling = 0;
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Buffer empty\n");
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
void InfluxDBClient::dropCurrentBatch() {
|
||||
delete _writeBuffer[_batchPointer];
|
||||
_writeBuffer[_batchPointer] = nullptr;
|
||||
_batchPointer++;
|
||||
//did we got over top?
|
||||
if(_batchPointer == _writeBufferSize) {
|
||||
// restart _batchPointer in ring buffer from start
|
||||
_batchPointer = 0;
|
||||
// we reached buffer size, that means buffer was full and now lower ceiling
|
||||
_bufferCeiling = _bufferPointer;
|
||||
}
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Dropped batch, batchpointer: %d\n", _batchPointer);
|
||||
}
|
||||
|
||||
String InfluxDBClient::pointToLineProtocol(const Point& point) {
|
||||
return point.createLineProtocol(_writeOptions._defaultTags);
|
||||
}
|
||||
|
||||
bool InfluxDBClient::validateConnection() {
|
||||
if(!_service && !init()) {
|
||||
return false;
|
||||
}
|
||||
// on version 1.x /ping will by default return status code 204, without verbose
|
||||
String url = _connInfo.serverUrl + (_connInfo.dbVersion==2?"/health":"/ping?verbose=true");
|
||||
if(_connInfo.dbVersion==1 && _connInfo.user.length() > 0 && _connInfo.password.length() > 0) {
|
||||
url += "&u=";
|
||||
url += urlEncode(_connInfo.user.c_str());
|
||||
url += "&p=";
|
||||
url += urlEncode(_connInfo.password.c_str());
|
||||
}
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Validating connection to %s\n", url.c_str());
|
||||
|
||||
return _service->doGET(url.c_str(), 200, nullptr);
|
||||
}
|
||||
|
||||
int InfluxDBClient::postData(const char *data) {
|
||||
if(!_service && !init()) {
|
||||
return 0;
|
||||
}
|
||||
if(data) {
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Writing to %s\n", _writeUrl.c_str());
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Sending:\n%s\n", data);
|
||||
|
||||
_service->doPOST(_writeUrl.c_str(), data, PSTR("text/plain"), 204, nullptr);
|
||||
_retryTime = _service->getLastRetryAfter();
|
||||
return _service->getLastStatusCode();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static const char QueryDialect[] PROGMEM = "\
|
||||
\"dialect\": {\
|
||||
\"annotations\": [\
|
||||
\"datatype\"\
|
||||
],\
|
||||
\"dateTimeFormat\": \"RFC3339\",\
|
||||
\"header\": true,\
|
||||
\"delimiter\": \",\",\
|
||||
\"commentPrefix\": \"#\"\
|
||||
}}";
|
||||
|
||||
FluxQueryResult InfluxDBClient::query(String fluxQuery) {
|
||||
uint32_t rwt = getRemainingRetryTime();
|
||||
if(rwt > 0) {
|
||||
INFLUXDB_CLIENT_DEBUG("[W] Cannot query yet, pause %ds, %ds yet\n", _retryTime, rwt);
|
||||
// retry after period didn't run out yet
|
||||
String mess = FPSTR(TooEarlyMessage);
|
||||
mess += String(rwt);
|
||||
mess += "s";
|
||||
return FluxQueryResult(mess);
|
||||
}
|
||||
if(!_service && !init()) {
|
||||
return FluxQueryResult(_connInfo.lastError);
|
||||
}
|
||||
INFLUXDB_CLIENT_DEBUG("[D] Query to %s\n", _queryUrl.c_str());
|
||||
INFLUXDB_CLIENT_DEBUG("[D] JSON query:\n%s\n", fluxQuery.c_str());
|
||||
|
||||
String body = F("{\"type\":\"flux\",\"query\":\"");
|
||||
body += escapeJSONString(fluxQuery) + "\",";
|
||||
body += FPSTR(QueryDialect);
|
||||
|
||||
CsvReader *reader = nullptr;
|
||||
_retryTime = 0;
|
||||
if(_service->doPOST(_queryUrl.c_str(), body.c_str(), PSTR("application/json"), 200, [&](HTTPClient *httpClient){
|
||||
bool chunked = false;
|
||||
if(httpClient->hasHeader(TransferEncoding)) {
|
||||
String header = httpClient->header(TransferEncoding);
|
||||
chunked = header.equalsIgnoreCase("chunked");
|
||||
}
|
||||
INFLUXDB_CLIENT_DEBUG("[D] chunked: %s\n", bool2string(chunked));
|
||||
HttpStreamScanner *scanner = new HttpStreamScanner(httpClient, chunked);
|
||||
reader = new CsvReader(scanner);
|
||||
return false;
|
||||
})) {
|
||||
return FluxQueryResult(reader);
|
||||
} else {
|
||||
_retryTime = _service->getLastRetryAfter();
|
||||
return FluxQueryResult(_service->getLastErrorMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static String escapeJSONString(String &value) {
|
||||
String ret;
|
||||
int d = 0;
|
||||
int i,from = 0;
|
||||
while((i = value.indexOf('"',from)) > -1) {
|
||||
d++;
|
||||
if(i == (int)value.length()-1) {
|
||||
break;
|
||||
}
|
||||
from = i+1;
|
||||
}
|
||||
ret.reserve(value.length()+d); //most probably we will escape just double quotes
|
||||
for (char c: value)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case '"': ret += "\\\""; break;
|
||||
case '\\': ret += "\\\\"; break;
|
||||
case '\b': ret += "\\b"; break;
|
||||
case '\f': ret += "\\f"; break;
|
||||
case '\n': ret += "\\n"; break;
|
||||
case '\r': ret += "\\r"; break;
|
||||
case '\t': ret += "\\t"; break;
|
||||
default:
|
||||
if (c <= '\x1f') {
|
||||
ret += "\\u";
|
||||
char buf[3 + 8 * sizeof(unsigned int)];
|
||||
sprintf(buf, "\\u%04u", c);
|
||||
ret += buf;
|
||||
} else {
|
||||
ret += c;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
@ -1,222 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* InfluxDBClient.h: InfluxDB Client for Arduino
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020 InfluxData
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#ifndef _INFLUXDB_CLIENT_H_
|
||||
#define _INFLUXDB_CLIENT_H_
|
||||
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "HTTPService.h"
|
||||
#include "Point.h"
|
||||
#include "WritePrecision.h"
|
||||
#include "query/FluxParser.h"
|
||||
#include "util/helpers.h"
|
||||
#include "Options.h"
|
||||
#include "BucketsClient.h"
|
||||
|
||||
#ifdef USING_AXTLS
|
||||
#error AxTLS does not work
|
||||
#endif
|
||||
|
||||
class Test;
|
||||
|
||||
/**
|
||||
* InfluxDBClient handles connection and basic operations for an InfluxDB server.
|
||||
* It provides write API with ability to write data in batches and retrying failed writes.
|
||||
* Automaticaly retries failed writes during next write, if server is overloaded.
|
||||
*/
|
||||
class InfluxDBClient {
|
||||
public:
|
||||
// Creates InfluxDBClient unconfigured instance.
|
||||
// Call to setConnectionParams is required to set up client
|
||||
InfluxDBClient();
|
||||
// Creates InfluxDBClient instance for unsecured connection to InfluxDB 1
|
||||
// serverUrl - url of the InfluxDB 1 server (e.g. http://localhost:8086)
|
||||
// db - database name where to store or read data
|
||||
InfluxDBClient(const char *serverUrl, const char *db);
|
||||
// Creates InfluxDBClient instance for unsecured connection
|
||||
// serverUrl - url of the InfluxDB 2 server (e.g. http://localhost:8086)
|
||||
// org - name of the organization, which bucket belongs to
|
||||
// bucket - name of the bucket to write data into
|
||||
// authToken - InfluxDB 2 authorization token
|
||||
InfluxDBClient(const char *serverUrl, const char *org, const char *bucket, const char *authToken);
|
||||
// Creates InfluxDBClient instance for secured connection
|
||||
// serverUrl - url of the InfluxDB 2 server (e.g. https://localhost:8086)
|
||||
// org - name of the organization, which bucket belongs to
|
||||
// bucket - name of the bucket to write data into
|
||||
// authToken - InfluxDB 2 authorization token
|
||||
// certInfo - InfluxDB 2 server trusted certificate (or CA certificate) or certificate SHA1 fingerprint. Should be stored in PROGMEM.
|
||||
InfluxDBClient(const char *serverUrl, const char *org, const char *bucket, const char *authToken, const char *certInfo);
|
||||
// Clears instance.
|
||||
~InfluxDBClient();
|
||||
// Allows insecure connection by skiping server certificate validation.
|
||||
// setInsecure must be called before calling any method initiating a connection to server.
|
||||
void setInsecure(bool value = true);
|
||||
// Sets custom write options.
|
||||
// Must be called before calling any method initiating a connection to server.
|
||||
// precision - timestamp precision of written data
|
||||
// batchSize - number of points that will be written to the databases at once. Default 1 - writes immediately
|
||||
// bufferSize - maximum size of Points buffer. Buffer contains new data that will be written to the database
|
||||
// and also data that failed to be written due to network failure or server overloading
|
||||
// flushInterval - maximum number of seconds data will be held in buffer before are written to the db.
|
||||
// Data are written either when number of points in buffer reaches batchSize or time of
|
||||
// preserveConnection - true if HTTP connection should be kept open. Usable for frequent writes.
|
||||
// Returns true if setting was successful. Otherwise check getLastErrorMessage() for an error.
|
||||
bool setWriteOptions(WritePrecision precision, uint16_t batchSize = 1, uint16_t bufferSize = 5, uint16_t flushInterval = 60, bool preserveConnection = true) __attribute__ ((deprecated("Use setWriteOptions(const WriteOptions &writeOptions)")));
|
||||
// Sets custom write options. See WriteOptions doc for more info.
|
||||
// Must be called before calling any method initiating a connection to server.
|
||||
// Returns true if setting was successful. Otherwise check getLastErrorMessage() for an error.
|
||||
// Example:
|
||||
// client.setWriteOptions(WriteOptions().batchSize(10).bufferSize(50)).
|
||||
bool setWriteOptions(const WriteOptions &writeOptions);
|
||||
// Sets custom HTTP options. See HTTPOptions doc for more info.
|
||||
// Must be called before calling any method initiating a connection to server.
|
||||
// Returns true if setting was successful. Otherwise check getLastErrorMessage() for an error.
|
||||
// Example:
|
||||
// client.setHTTPOptions(HTTPOptions().httpReadTimeout(20000)).
|
||||
bool setHTTPOptions(const HTTPOptions &httpOptions);
|
||||
// Sets connection parameters for InfluxDB 2
|
||||
// Must be called before calling any method initiating a connection to server.
|
||||
// serverUrl - url of the InfluxDB 2 server (e.g. https//localhost:8086)
|
||||
// org - name of the organization, which bucket belongs to
|
||||
// bucket - name of the bucket to write data into
|
||||
// authToken - InfluxDB 2 authorization token
|
||||
// serverCert - Optional. InfluxDB 2 server trusted certificate (or CA certificate) or certificate SHA1 fingerprint. Should be stored in PROGMEM. Only in case of https connection.
|
||||
void setConnectionParams(const char *serverUrl, const char *org, const char *bucket, const char *authToken, const char *certInfo = nullptr);
|
||||
// Sets parameters for connection to InfluxDB 1
|
||||
// Must be called before calling any method initiating a connection to server.
|
||||
// serverUrl - url of the InfluxDB server (e.g. http://localhost:8086)
|
||||
// db - database name where to store or read data
|
||||
// user - Optional. User name, in case of server requires authetication
|
||||
// password - Optional. User password, in case of server requires authetication
|
||||
// certInfo - Optional. InfluxDB server trusted certificate (or CA certificate) or certificate SHA1 fingerprint. Should be stored in PROGMEM. Only in case of https connection.
|
||||
void setConnectionParamsV1(const char *serverUrl, const char *db, const char *user = nullptr, const char *password = nullptr, const char *certInfo = nullptr);
|
||||
// Creates line protocol string from point data and optional default tags set in WriteOptions.
|
||||
String pointToLineProtocol(const Point& point);
|
||||
// Validates connection parameters by conecting to server
|
||||
// Returns true if successful, false in case of any error
|
||||
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);
|
||||
// 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);
|
||||
// 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();
|
||||
// Returns true if points buffer is full. Usefull when server is overloaded and we may want increase period of write points or decrease number of points
|
||||
bool isBufferFull() const { return _bufferCeiling == _writeBufferSize; };
|
||||
// Returns true if buffer is empty. Usefull when going to sleep and check if there is sth in write buffer (it can happens when batch size if bigger than 1). Call flushBuffer() then.
|
||||
bool isBufferEmpty() const { return _bufferCeiling == 0 && !_writeBuffer[0]; };
|
||||
// Checks points buffer status and flushes if number of points reached batch size or flush interval runs out.
|
||||
// Returns true if successful, false in case of any error
|
||||
bool checkBuffer();
|
||||
// Wipes out buffered points
|
||||
void resetBuffer();
|
||||
// Returns HTTP status of last request to server. Usefull for advanced handling of failures.
|
||||
int getLastStatusCode() const { return _service?_service->getLastStatusCode():0; }
|
||||
// Returns last response when operation failed
|
||||
String getLastErrorMessage() const { return _connInfo.lastError; }
|
||||
// Returns server url
|
||||
String getServerUrl() const { return _connInfo.serverUrl; }
|
||||
// Check if it is possible to send write/query request to server.
|
||||
// Returns true if write or query can be send, or false, if server is overloaded and retry strategy is applied.
|
||||
// Use getRemainingRetryTime() to get wait time in such case.
|
||||
bool canSendRequest() { return getRemainingRetryTime() == 0; }
|
||||
// Returns remaining wait time in seconds when retry strategy is applied.
|
||||
uint32_t getRemainingRetryTime();
|
||||
// Returns sub-client for managing buckets
|
||||
BucketsClient getBucketsClient();
|
||||
protected:
|
||||
// Checks params and sets up security, if needed.
|
||||
// Returns true in case of success, otherwise false
|
||||
bool init();
|
||||
// Cleans instances
|
||||
void clean();
|
||||
protected:
|
||||
class Batch {
|
||||
private:
|
||||
uint8_t _size = 0;
|
||||
public:
|
||||
uint8_t pointer = 0;
|
||||
String *buffer = nullptr;
|
||||
uint8_t retryCount = 0;
|
||||
Batch(int size):_size(size) { buffer = new String[size]; }
|
||||
~Batch() { delete [] buffer; }
|
||||
bool append(String &line);
|
||||
char *createData();
|
||||
bool isFull() const {
|
||||
return pointer == _size;
|
||||
}
|
||||
};
|
||||
friend class Test;
|
||||
ConnectionInfo _connInfo;
|
||||
// Cached full write url
|
||||
String _writeUrl;
|
||||
// Cached full query url
|
||||
String _queryUrl;
|
||||
// Points buffer
|
||||
Batch **_writeBuffer = nullptr;
|
||||
// Batch buffer size
|
||||
uint8_t _writeBufferSize;
|
||||
// Write options
|
||||
WriteOptions _writeOptions;
|
||||
// Store retry timeout suggested by server or computed
|
||||
int _retryTime = 0;
|
||||
// HTTP operations object
|
||||
HTTPService *_service = nullptr;
|
||||
// Index to buffer where to store new batch
|
||||
uint8_t _bufferPointer = 0;
|
||||
// Actual count of batches in buffer
|
||||
uint8_t _bufferCeiling = 0;
|
||||
// Index of bath start for next write
|
||||
uint8_t _batchPointer = 0;
|
||||
// Last time in sec buffer has been successfully flushed
|
||||
uint32_t _lastFlushed;
|
||||
// Bucket sub-client
|
||||
BucketsClient _buckets;
|
||||
protected:
|
||||
// Sends POST request with data in body
|
||||
int postData(const char *data);
|
||||
// Sets cached InfluxDB server API URLs
|
||||
bool setUrls();
|
||||
// Ensures buffer has required size
|
||||
void reserveBuffer(int size);
|
||||
// Drops current batch and advances batch pointer
|
||||
void dropCurrentBatch();
|
||||
// Writes all points in buffer, with respect to the batch size, and in case of success clears the buffer.
|
||||
// flashOnlyFull - whether to flush only full batches
|
||||
// Returns true if successful, false in case of any error
|
||||
bool flushBufferInternal(bool flashOnlyFull);
|
||||
};
|
||||
|
||||
|
||||
#endif //_INFLUXDB_CLIENT_H_
|
@ -1,68 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* InfluxDBCloud.h: InfluxDB Client for Arduino
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020 InfluxData
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#ifndef _INFLUXDB_CLOUD_H_
|
||||
#define _INFLUXDB_CLOUD_H_
|
||||
|
||||
// Root Certificate Authority of InfluxData Cloud 2 servers.
|
||||
// Valid with all providers (AWS, Azure, GCP)
|
||||
// ISRG Root X1
|
||||
// Valid until 2035-04-04T04:04:38Z
|
||||
const char InfluxDbCloud2CACert[] PROGMEM = R"EOF(
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
|
||||
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
|
||||
cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
|
||||
WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
|
||||
ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY
|
||||
MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc
|
||||
h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+
|
||||
0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U
|
||||
A5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+sW
|
||||
T8KOEUt+zwvo/7V3LvSye0rgTBIlDHCNAymg4VMk7BPZ7hm/ELNKjD+Jo2FR3qyH
|
||||
B5T0Y3HsLuJvW5iB4YlcNHlsdu87kGJ55tukmi8mxdAQ4Q7e2RCOFvu396j3x+UC
|
||||
B5iPNgiV5+I3lg02dZ77DnKxHZu8A/lJBdiB3QW0KtZB6awBdpUKD9jf1b0SHzUv
|
||||
KBds0pjBqAlkd25HN7rOrFleaJ1/ctaJxQZBKT5ZPt0m9STJEadao0xAH0ahmbWn
|
||||
OlFuhjuefXKnEgV4We0+UXgVCwOPjdAvBbI+e0ocS3MFEvzG6uBQE3xDk3SzynTn
|
||||
jh8BCNAw1FtxNrQHusEwMFxIt4I7mKZ9YIqioymCzLq9gwQbooMDQaHWBfEbwrbw
|
||||
qHyGO0aoSCqI3Haadr8faqU9GY/rOPNk3sgrDQoo//fb4hVC1CLQJ13hef4Y53CI
|
||||
rU7m2Ys6xt0nUW7/vGT1M0NPAgMBAAGjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNV
|
||||
HRMBAf8EBTADAQH/MB0GA1UdDgQWBBR5tFnme7bl5AFzgAiIyBpY9umbbjANBgkq
|
||||
hkiG9w0BAQsFAAOCAgEAVR9YqbyyqFDQDLHYGmkgJykIrGF1XIpu+ILlaS/V9lZL
|
||||
ubhzEFnTIZd+50xx+7LSYK05qAvqFyFWhfFQDlnrzuBZ6brJFe+GnY+EgPbk6ZGQ
|
||||
3BebYhtF8GaV0nxvwuo77x/Py9auJ/GpsMiu/X1+mvoiBOv/2X/qkSsisRcOj/KK
|
||||
NFtY2PwByVS5uCbMiogziUwthDyC3+6WVwW6LLv3xLfHTjuCvjHIInNzktHCgKQ5
|
||||
ORAzI4JMPJ+GslWYHb4phowim57iaztXOoJwTdwJx4nLCgdNbOhdjsnvzqvHu7Ur
|
||||
TkXWStAmzOVyyghqpZXjFaH3pO3JLF+l+/+sKAIuvtd7u+Nxe5AW0wdeRlN8NwdC
|
||||
jNPElpzVmbUq4JUagEiuTDkHzsxHpFKVK7q4+63SM1N95R1NbdWhscdCb+ZAJzVc
|
||||
oyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq
|
||||
4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA
|
||||
mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d
|
||||
emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=
|
||||
-----END CERTIFICATE-----
|
||||
)EOF";
|
||||
|
||||
#endif //_INFLUXDB_CLOUD_H_
|
@ -1,39 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* Options.cpp: InfluxDB Client write options and HTTP options
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020 InfluxData
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#include <Arduino.h>
|
||||
#include "Options.h"
|
||||
#include "util/helpers.h"
|
||||
|
||||
WriteOptions& WriteOptions::addDefaultTag(String name, String value) {
|
||||
if(_defaultTags.length() > 0) {
|
||||
_defaultTags += ',';
|
||||
}
|
||||
_defaultTags += escapeKey(name);
|
||||
_defaultTags += '=';
|
||||
_defaultTags += escapeKey(value);
|
||||
return *this;
|
||||
}
|
@ -1,111 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* Options.h: InfluxDB Client write options and HTTP options
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020 InfluxData
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#ifndef _OPTIONS_H_
|
||||
#define _OPTIONS_H_
|
||||
|
||||
#include "WritePrecision.h"
|
||||
|
||||
class InfluxDBClient;
|
||||
class HTTPService;
|
||||
class Influxdb;
|
||||
class Test;
|
||||
|
||||
/**
|
||||
* WriteOptions holds write related options
|
||||
*/
|
||||
class WriteOptions {
|
||||
private:
|
||||
friend class InfluxDBClient;
|
||||
friend class Influxdb;
|
||||
friend class Test;
|
||||
// Points timestamp precision
|
||||
WritePrecision _writePrecision;
|
||||
// Number of points that will be written to the databases at once.
|
||||
// Default 1 (immediate write, no batching)
|
||||
uint16_t _batchSize;
|
||||
// Write buffer size - maximum number of record to keep.
|
||||
// When max size is reached, oldest records are overwritten.
|
||||
// Default 5
|
||||
uint16_t _bufferSize;
|
||||
// Maximum number of seconds points can be held in buffer before are written to the db.
|
||||
// Buffer is flushed when it reaches batch size or when flush interval runs out.
|
||||
uint16_t _flushInterval;
|
||||
// Default retry interval in sec, if not sent by server. Default 5s
|
||||
uint16_t _retryInterval;
|
||||
// Maximum retry interval in sec, default 5min (300s)
|
||||
uint16_t _maxRetryInterval;
|
||||
// Maximum count of retry attempts of failed writes, default 3
|
||||
uint16_t _maxRetryAttempts;
|
||||
// Default tags. Default tags are added to every written point.
|
||||
// There cannot be duplicate tags in default tags and tags included in a point.
|
||||
String _defaultTags;
|
||||
public:
|
||||
WriteOptions():
|
||||
_writePrecision(WritePrecision::NoTime),
|
||||
_batchSize(1),
|
||||
_bufferSize(5),
|
||||
_flushInterval(60),
|
||||
_retryInterval(5),
|
||||
_maxRetryInterval(300),
|
||||
_maxRetryAttempts(3) {
|
||||
}
|
||||
WriteOptions& writePrecision(WritePrecision precision) { _writePrecision = precision; return *this; }
|
||||
WriteOptions& batchSize(uint16_t batchSize) { _batchSize = batchSize; return *this; }
|
||||
WriteOptions& bufferSize(uint16_t bufferSize) { _bufferSize = bufferSize; return *this; }
|
||||
WriteOptions& flushInterval(uint16_t flushIntervalSec) { _flushInterval = flushIntervalSec; return *this; }
|
||||
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& clearDefaultTags() { _defaultTags = (char *)nullptr; return *this; }
|
||||
};
|
||||
|
||||
/**
|
||||
* HTTPOptions holds HTTP related options
|
||||
*/
|
||||
class HTTPOptions {
|
||||
private:
|
||||
friend class InfluxDBClient;
|
||||
friend class HTTPService;
|
||||
friend class Influxdb;
|
||||
friend class Test;
|
||||
// true if HTTP connection should be kept open. Usable for frequent writes.
|
||||
// Default false.
|
||||
bool _connectionReuse;
|
||||
// Timeout [ms] for reading server response.
|
||||
// Default 5000ms
|
||||
int _httpReadTimeout;
|
||||
public:
|
||||
HTTPOptions():
|
||||
_connectionReuse(false),
|
||||
_httpReadTimeout(5000) {
|
||||
}
|
||||
HTTPOptions& connectionReuse(bool connectionReuse) { _connectionReuse = connectionReuse; return *this; }
|
||||
HTTPOptions& httpReadTimeout(int httpReadTimeoutMs) { _httpReadTimeout = httpReadTimeoutMs; return *this; }
|
||||
};
|
||||
|
||||
#endif //_OPTIONS_H_
|
@ -1,17 +0,0 @@
|
||||
#ifndef _PLATFORM_H_
|
||||
#define _PLATFORM_H_
|
||||
|
||||
#include <core_version.h>
|
||||
|
||||
#define STRHELPER(x) #x
|
||||
#define STR(x) STRHELPER(x) // stringifier
|
||||
|
||||
#if defined(ESP8266)
|
||||
# define INFLUXDB_CLIENT_PLATFORM "ESP8266"
|
||||
# define INFLUXDB_CLIENT_PLATFORM_VERSION STR(ARDUINO_ESP8266_GIT_DESC)
|
||||
#elif defined(ESP32)
|
||||
# define INFLUXDB_CLIENT_PLATFORM "ESP32"
|
||||
# define INFLUXDB_CLIENT_PLATFORM_VERSION STR(ARDUINO_ESP32_GIT_DESC)
|
||||
#endif
|
||||
|
||||
#endif //_PLATFORM_H_
|
@ -1,127 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* Point.cpp: Point for write into InfluxDB server
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020 InfluxData
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "Point.h"
|
||||
#include "util/helpers.h"
|
||||
|
||||
Point::Point(String measurement):
|
||||
_measurement(escapeKey(measurement, false)),
|
||||
_tags(""),
|
||||
_fields(""),
|
||||
_timestamp("")
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Point::addTag(String name, String value) {
|
||||
if(_tags.length() > 0) {
|
||||
_tags += ',';
|
||||
}
|
||||
_tags += escapeKey(name);
|
||||
_tags += '=';
|
||||
_tags += escapeKey(value);
|
||||
}
|
||||
|
||||
void Point::addField(String name, const char *value) {
|
||||
putField(name, "\"" + escapeValue(value) + "\"");
|
||||
}
|
||||
|
||||
void Point::putField(String name, String value) {
|
||||
if(_fields.length() > 0) {
|
||||
_fields += ',';
|
||||
}
|
||||
_fields += escapeKey(name);
|
||||
_fields += '=';
|
||||
_fields += value;
|
||||
}
|
||||
|
||||
String Point::toLineProtocol(String includeTags) const {
|
||||
return createLineProtocol(includeTags);
|
||||
}
|
||||
|
||||
String Point::createLineProtocol(String &incTags) const {
|
||||
String line;
|
||||
line.reserve(_measurement.length() + 1 + incTags.length() + 1 + _tags.length() + 1 + _fields.length() + 1 + _timestamp.length());
|
||||
line += _measurement;
|
||||
if(incTags.length()>0) {
|
||||
line += ",";
|
||||
line += incTags;
|
||||
}
|
||||
if(hasTags()) {
|
||||
line += ",";
|
||||
line += _tags;
|
||||
}
|
||||
if(hasFields()) {
|
||||
line += " ";
|
||||
line += _fields;
|
||||
}
|
||||
if(hasTime()) {
|
||||
line += " ";
|
||||
line += _timestamp;
|
||||
}
|
||||
return line;
|
||||
}
|
||||
|
||||
void Point::setTime(WritePrecision precision) {
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
|
||||
switch(precision) {
|
||||
case WritePrecision::NS:
|
||||
setTime(getTimeStamp(&tv,9));
|
||||
break;
|
||||
case WritePrecision::US:
|
||||
setTime(getTimeStamp(&tv,6));
|
||||
break;
|
||||
case WritePrecision::MS:
|
||||
setTime(getTimeStamp(&tv,3));
|
||||
break;
|
||||
case WritePrecision::S:
|
||||
setTime(getTimeStamp(&tv,0));
|
||||
break;
|
||||
case WritePrecision::NoTime:
|
||||
_timestamp = "";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Point::setTime(unsigned long long timestamp) {
|
||||
_timestamp = timeStampToString(timestamp);
|
||||
}
|
||||
|
||||
void Point::setTime(String timestamp) {
|
||||
_timestamp = timestamp;
|
||||
}
|
||||
|
||||
void Point::clearFields() {
|
||||
_fields = (char *)nullptr;
|
||||
_timestamp = (char *)nullptr;
|
||||
}
|
||||
|
||||
void Point:: clearTags() {
|
||||
_tags = (char *)nullptr;
|
||||
}
|
@ -1,87 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* Point.h: Point for write into InfluxDB server
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020 InfluxData
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#ifndef _POINT_H_
|
||||
#define _POINT_H_
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "WritePrecision.h"
|
||||
#include "util/helpers.h"
|
||||
|
||||
/**
|
||||
* Class Point represents InfluxDB point in line protocol.
|
||||
* It defines data to be written to InfluxDB.
|
||||
*/
|
||||
class Point {
|
||||
friend class InfluxDBClient;
|
||||
public:
|
||||
Point(String measurement);
|
||||
// Adds string tag
|
||||
void addTag(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, 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);
|
||||
// Clear all fields. Usefull for reusing point
|
||||
void clearFields();
|
||||
// Clear tags
|
||||
void clearTags();
|
||||
// True if a point contains at least one field. Points without a field cannot be written to db
|
||||
bool hasFields() const { return _fields.length() > 0; }
|
||||
// True if a point contains at least one tag
|
||||
bool hasTags() const { return _tags.length() > 0; }
|
||||
// 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;
|
||||
// returns current timestamp
|
||||
String getTime() const { return _timestamp; }
|
||||
protected:
|
||||
String _measurement;
|
||||
String _tags;
|
||||
String _fields;
|
||||
String _timestamp;
|
||||
protected:
|
||||
// method for formating field into line protocol
|
||||
void putField(String name, String value);
|
||||
// Creates line protocol string
|
||||
String createLineProtocol(String &incTags) const;
|
||||
};
|
||||
#endif //_POINT_H_
|
@ -1,32 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* Version.h: Version constant
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020 InfluxData
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#ifndef _VERSION_H_
|
||||
#define _VERSION_H_
|
||||
|
||||
#define INFLUXDB_CLIENT_VERSION "3.9.0"
|
||||
|
||||
#endif //_VERSION_H_
|
@ -1,43 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* WritePrecision.h: Write precision for InfluxDB Client for Arduino
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020 InfluxData
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#ifndef _WRITE_PRECISION_H_
|
||||
#define _WRITE_PRECISION_H_
|
||||
// Enum WritePrecision defines constants for specifying InfluxDB write prcecision
|
||||
enum class WritePrecision {
|
||||
// Specifyies that points has no timestamp (default). Server will assign timestamp.
|
||||
NoTime = 0,
|
||||
// Seconds
|
||||
S,
|
||||
// Milli-seconds
|
||||
MS,
|
||||
// Micro-seconds
|
||||
US,
|
||||
// Nano-seconds
|
||||
NS
|
||||
};
|
||||
|
||||
#endif //_WRITE_PRECISION_H_
|
@ -1,108 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* CsvReader.cpp: Simple Csv parser for comma separated values, with double quotes suppport
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020 InfluxData
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#include "CsvReader.h"
|
||||
|
||||
CsvReader::CsvReader(HttpStreamScanner *scanner) {
|
||||
_scanner = scanner;
|
||||
}
|
||||
|
||||
CsvReader::~CsvReader() {
|
||||
delete _scanner;
|
||||
}
|
||||
|
||||
std::vector<String> CsvReader::getRow() {
|
||||
return _row;
|
||||
};
|
||||
|
||||
void CsvReader::close() {
|
||||
clearRow();
|
||||
_scanner->close();
|
||||
}
|
||||
|
||||
void CsvReader::clearRow() {
|
||||
std::for_each(_row.begin(), _row.end(), [](String &value){ value = (const char *)nullptr; });
|
||||
_row.clear();
|
||||
}
|
||||
|
||||
enum class CsvParsingState {
|
||||
UnquotedField,
|
||||
QuotedField,
|
||||
QuotedQuote
|
||||
};
|
||||
|
||||
bool CsvReader::next() {
|
||||
clearRow();
|
||||
bool status = _scanner->next();
|
||||
if(!status) {
|
||||
_error = _scanner->getError();
|
||||
return false;
|
||||
}
|
||||
String line = _scanner->getLine();
|
||||
CsvParsingState state = CsvParsingState::UnquotedField;
|
||||
std::vector<String> fields {""};
|
||||
size_t i = 0; // index of the current field
|
||||
for (char c : line) {
|
||||
switch (state) {
|
||||
case CsvParsingState::UnquotedField:
|
||||
switch (c) {
|
||||
case ',': // end of field
|
||||
fields.push_back(""); i++;
|
||||
break;
|
||||
case '"': state = CsvParsingState::QuotedField;
|
||||
break;
|
||||
default: fields[i] += c;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case CsvParsingState::QuotedField:
|
||||
switch (c) {
|
||||
case '"': state = CsvParsingState::QuotedQuote;
|
||||
break;
|
||||
default: fields[i] += c;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case CsvParsingState::QuotedQuote:
|
||||
switch (c) {
|
||||
case ',': // , after closing quote
|
||||
fields.push_back(""); i++;
|
||||
state = CsvParsingState::UnquotedField;
|
||||
break;
|
||||
case '"': // "" -> "
|
||||
fields[i] += '"';
|
||||
state = CsvParsingState::QuotedField;
|
||||
break;
|
||||
default: // end of quote
|
||||
state = CsvParsingState::UnquotedField;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
_row = fields;
|
||||
return true;
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* CsvReader.h: Simple Csv parser for comma separated values, with double quotes suppport
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020 InfluxData
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#ifndef _CSV_READER_
|
||||
#define _CSV_READER_
|
||||
|
||||
#include "HttpStreamScanner.h"
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
* CsvReader parses csv line to token by ',' (comma) character.
|
||||
* It suppports escaped quotes, excaped comma
|
||||
**/
|
||||
class CsvReader {
|
||||
public:
|
||||
CsvReader(HttpStreamScanner *scanner);
|
||||
~CsvReader();
|
||||
bool next();
|
||||
void close();
|
||||
std::vector<String> getRow();
|
||||
int getError() const { return _error; };
|
||||
private:
|
||||
void clearRow();
|
||||
HttpStreamScanner *_scanner = nullptr;
|
||||
std::vector<String> _row;
|
||||
int _error = 0;
|
||||
};
|
||||
#endif //_CSV_READER_
|
@ -1,275 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* FluxParser.cpp: InfluxDB flux query result parser
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2018-2020 InfluxData
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "FluxParser.h"
|
||||
// Uncomment bellow in case of a problem and rebuild sketch
|
||||
//#define INFLUXDB_CLIENT_DEBUG_ENABLE
|
||||
#include "util/debug.h"
|
||||
|
||||
FluxQueryResult::FluxQueryResult(CsvReader *reader) {
|
||||
_data = std::make_shared<Data>(reader);
|
||||
}
|
||||
|
||||
FluxQueryResult::FluxQueryResult(String error):FluxQueryResult((CsvReader *)nullptr) {
|
||||
_data->_error = error;
|
||||
}
|
||||
|
||||
FluxQueryResult::FluxQueryResult(const FluxQueryResult &other) {
|
||||
_data = other._data;
|
||||
}
|
||||
FluxQueryResult &FluxQueryResult::operator=(const FluxQueryResult &other) {
|
||||
if(this != &other) {
|
||||
_data = other._data;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
FluxQueryResult::~FluxQueryResult() {
|
||||
}
|
||||
|
||||
int FluxQueryResult::getColumnIndex(String columnName) {
|
||||
int i = -1;
|
||||
std::vector<String>::iterator it = find(_data->_columnNames.begin(), _data->_columnNames.end(), columnName);
|
||||
if (it != _data->_columnNames.end()) {
|
||||
i = distance(_data->_columnNames.begin(), it);
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
FluxValue FluxQueryResult::getValueByIndex(int index) {
|
||||
FluxValue ret;
|
||||
if(index >= 0 && index < (int)_data->_columnValues.size()) {
|
||||
ret = _data->_columnValues[index];
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
FluxValue FluxQueryResult::getValueByName(String columnName) {
|
||||
FluxValue ret;
|
||||
int i = getColumnIndex(columnName);
|
||||
if(i > -1) {
|
||||
ret = getValueByIndex(i);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void FluxQueryResult::close() {
|
||||
clearValues();
|
||||
clearColumns();
|
||||
if(_data->_reader) {
|
||||
_data->_reader->close();
|
||||
}
|
||||
}
|
||||
|
||||
void FluxQueryResult::clearValues() {
|
||||
std::for_each(_data->_columnValues.begin(), _data->_columnValues.end(), [](FluxValue &value){ value = nullptr; });
|
||||
_data->_columnValues.clear();
|
||||
}
|
||||
|
||||
void FluxQueryResult::clearColumns() {
|
||||
std::for_each(_data->_columnNames.begin(), _data->_columnNames.end(), [](String &value){ value = (const char *)nullptr; });
|
||||
_data->_columnNames.clear();
|
||||
|
||||
std::for_each(_data->_columnDatatypes.begin(), _data->_columnDatatypes.end(), [](String &value){ value = (const char *)nullptr; });
|
||||
_data->_columnDatatypes.clear();
|
||||
}
|
||||
|
||||
FluxQueryResult::Data::Data(CsvReader *reader):_reader(reader) {}
|
||||
|
||||
FluxQueryResult::Data::~Data() {
|
||||
delete _reader;
|
||||
}
|
||||
|
||||
enum ParsingState {
|
||||
ParsingStateNormal = 0,
|
||||
ParsingStateNameRow,
|
||||
ParsingStateError
|
||||
};
|
||||
|
||||
bool FluxQueryResult::next() {
|
||||
if(!_data->_reader) {
|
||||
return false;
|
||||
}
|
||||
ParsingState parsingState = ParsingStateNormal;
|
||||
_data->_tableChanged = false;
|
||||
clearValues();
|
||||
_data->_error = "";
|
||||
readRow:
|
||||
bool stat = _data->_reader->next();
|
||||
if(!stat) {
|
||||
if(_data->_reader->getError()< 0) {
|
||||
_data->_error = HTTPClient::errorToString(_data->_reader->getError());
|
||||
INFLUXDB_CLIENT_DEBUG("Error '%s'\n", _data->_error.c_str());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
std::vector<String> vals = _data->_reader->getRow();
|
||||
INFLUXDB_CLIENT_DEBUG("[D] FluxQueryResult: vals.size %d\n", vals.size());
|
||||
if(vals.size() < 2) {
|
||||
goto readRow;
|
||||
}
|
||||
if(vals[0] == "") {
|
||||
if (parsingState == ParsingStateError) {
|
||||
String message ;
|
||||
if (vals.size() > 1 && vals[1].length() > 0) {
|
||||
message = vals[1];
|
||||
} else {
|
||||
message = F("Unknown query error");
|
||||
}
|
||||
String reference = "";
|
||||
if (vals.size() > 2 && vals[2].length() > 0) {
|
||||
reference = "," + vals[2];
|
||||
}
|
||||
_data->_error = message + reference;
|
||||
INFLUXDB_CLIENT_DEBUG("Error '%s'\n", _data->_error.c_str());
|
||||
return false;
|
||||
} else if (parsingState == ParsingStateNameRow) {
|
||||
if (vals[1] == "error") {
|
||||
parsingState = ParsingStateError;
|
||||
} else {
|
||||
if (vals.size()-1 != _data->_columnDatatypes.size()) {
|
||||
_data->_error = String(F("Parsing error, header has different number of columns than table: ")) + String(vals.size()-1) + " vs " + String(_data->_columnDatatypes.size());
|
||||
INFLUXDB_CLIENT_DEBUG("Error '%s'\n", _data->_error.c_str());
|
||||
return false;
|
||||
} else {
|
||||
for(unsigned int i=1;i < vals.size(); i++) {
|
||||
_data->_columnNames.push_back(vals[i]);
|
||||
}
|
||||
}
|
||||
parsingState = ParsingStateNormal;
|
||||
}
|
||||
goto readRow;
|
||||
}
|
||||
if(_data->_columnDatatypes.size() == 0) {
|
||||
_data->_error = F("Parsing error, datatype annotation not found");
|
||||
INFLUXDB_CLIENT_DEBUG("Error '%s'\n", _data->_error.c_str());
|
||||
return false;
|
||||
}
|
||||
if (vals.size()-1 != _data->_columnNames.size()) {
|
||||
_data->_error = String(F("Parsing error, row has different number of columns than table: ")) + String(vals.size()-1) + " vs " + String(_data->_columnNames.size());
|
||||
INFLUXDB_CLIENT_DEBUG("Error '%s'\n", _data->_error.c_str());
|
||||
return false;
|
||||
}
|
||||
for(unsigned int i=1;i < vals.size(); i++) {
|
||||
FluxBase *v = nullptr;
|
||||
if(vals[i].length() > 0) {
|
||||
v = convertValue(vals[i], _data->_columnDatatypes[i-1]);
|
||||
if(!v) {
|
||||
_data->_error = String(F("Unsupported datatype: ")) + _data->_columnDatatypes[i-1];
|
||||
INFLUXDB_CLIENT_DEBUG("Error '%s'\n", _data->_error.c_str());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
FluxValue val(v);
|
||||
_data->_columnValues.push_back(val);
|
||||
}
|
||||
} else if(vals[0] == "#datatype") {
|
||||
_data->_tablePosition++;
|
||||
clearColumns();
|
||||
_data->_tableChanged = true;
|
||||
for(unsigned int i=1;i < vals.size(); i++) {
|
||||
_data->_columnDatatypes.push_back(vals[i]);
|
||||
}
|
||||
parsingState = ParsingStateNameRow;
|
||||
goto readRow;
|
||||
} else {
|
||||
goto readRow;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
FluxDateTime *FluxQueryResult::convertRfc3339(String value, const char *type) {
|
||||
tm t = {0,0,0,0,0,0,0,0,0};
|
||||
// has the time part
|
||||
int zet = value.indexOf('Z');
|
||||
unsigned long fracts = 0;
|
||||
if(value.indexOf('T') > 0 && zet > 0) { //Full datetime string - 2020-05-22T11:25:22.037735433Z
|
||||
int f = sscanf(value.c_str(),"%d-%d-%dT%d:%d:%d", &t.tm_year,&t.tm_mon,&t.tm_mday, &t.tm_hour,&t.tm_min,&t.tm_sec);
|
||||
if(f != 6) {
|
||||
return nullptr;
|
||||
}
|
||||
t.tm_year -= 1900; //adjust to years after 1900
|
||||
t.tm_mon -= 1; //adjust to range 0-11
|
||||
int dot = value.indexOf('.');
|
||||
|
||||
if(dot > 0) {
|
||||
int tail = zet;
|
||||
int len = zet-dot-1;
|
||||
if (len > 6) {
|
||||
tail = dot + 7;
|
||||
len = 6;
|
||||
}
|
||||
String secParts = value.substring(dot+1, tail);
|
||||
fracts = strtoul((const char *) secParts.c_str(), NULL, 10);
|
||||
if(len < 6) {
|
||||
fracts *= 10^(6-len);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
int f = sscanf(value.c_str(),"%d-%d-%d", &t.tm_year,&t.tm_mon,&t.tm_mday);
|
||||
if(f != 3) {
|
||||
return nullptr;
|
||||
}
|
||||
t.tm_year -= 1900; //adjust to years after 1900
|
||||
t.tm_mon -= 1; //adjust to range 0-11
|
||||
}
|
||||
return new FluxDateTime(value, type, t, fracts);
|
||||
}
|
||||
|
||||
FluxBase *FluxQueryResult::convertValue(String value, String dataType) {
|
||||
FluxBase *ret = nullptr;
|
||||
if(dataType.equals(FluxDatatypeDatetimeRFC3339) || dataType.equals(FluxDatatypeDatetimeRFC3339Nano)) {
|
||||
const char *type = FluxDatatypeDatetimeRFC3339;
|
||||
if(dataType.equals(FluxDatatypeDatetimeRFC3339Nano)) {
|
||||
type = FluxDatatypeDatetimeRFC3339Nano;
|
||||
}
|
||||
ret = convertRfc3339(value, type);
|
||||
if (!ret) {
|
||||
_data->_error = String(F("Invalid value for '")) + dataType + F("': ") + value;
|
||||
}
|
||||
} else if(dataType.equals(FluxDatatypeDouble)) {
|
||||
double val = strtod((const char *) value.c_str(), NULL);
|
||||
ret = new FluxDouble(value, val);
|
||||
} else if(dataType.equals(FluxDatatypeBool)) {
|
||||
bool val = value.equalsIgnoreCase("true");
|
||||
ret = new FluxBool(value, val);
|
||||
} else if(dataType.equals(FluxDatatypeLong)) {
|
||||
long l = strtol((const char *) value.c_str(), NULL, 10);
|
||||
ret = new FluxLong(value, l);
|
||||
} else if(dataType.equals(FluxDatatypeUnsignedLong)) {
|
||||
unsigned long ul = strtoul((const char *) value.c_str(), NULL, 10);
|
||||
ret = new FluxUnsignedLong(value, ul);
|
||||
} else if(dataType.equals(FluxBinaryDataTypeBase64)) {
|
||||
ret = new FluxString(value, FluxBinaryDataTypeBase64);
|
||||
} else if(dataType.equals(FluxDatatypeDuration)) {
|
||||
ret = new FluxString(value, FluxDatatypeDuration);
|
||||
} else if(dataType.equals(FluxDatatypeString)) {
|
||||
ret = new FluxString(value, FluxDatatypeString);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
@ -1,109 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* FLuxParser.h: InfluxDB flux query result parser
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020 InfluxData
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#ifndef _FLUX_PARSER_H_
|
||||
#define _FLUX_PARSER_H_
|
||||
|
||||
#include <vector>
|
||||
#include "CsvReader.h"
|
||||
#include "FluxTypes.h"
|
||||
|
||||
|
||||
/**
|
||||
* FluxQueryResult represents result from InfluxDB flux query.
|
||||
* It parses stream from server, line by line, so it allows to read a huge responses.
|
||||
*
|
||||
* Browsing thought the result is done by repeatedly calling the next() method, until it returns false.
|
||||
* Unsuccesful reading is distinqushed by non empty value from getError().
|
||||
*
|
||||
* As a flux query result can contain several tables differing by grouping key, use hasTableChanged() to
|
||||
* know when there is a new table.
|
||||
*
|
||||
* Single values are returned using getValueByIndex() or getValueByName() methods.
|
||||
* All row values are retreived by getValues().
|
||||
*
|
||||
* Always call close() at the of reading.
|
||||
*
|
||||
* FluxQueryResult supports passing by value.
|
||||
*/
|
||||
class FluxQueryResult {
|
||||
public:
|
||||
// Constructor for reading result
|
||||
FluxQueryResult(CsvReader *reader);
|
||||
// Constructor for error result
|
||||
FluxQueryResult(String error);
|
||||
// Copy constructor
|
||||
FluxQueryResult(const FluxQueryResult &other);
|
||||
// Assignment operator
|
||||
FluxQueryResult &operator=(const FluxQueryResult &other);
|
||||
// Advances to next values row in the result set.
|
||||
// Returns true on successful reading new row, false means end of the result set
|
||||
// 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);
|
||||
// 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);
|
||||
// Returns flux datatypes of all columns
|
||||
std::vector<String> getColumnsDatatype() { return _data->_columnDatatypes; }
|
||||
// Returns names of all columns
|
||||
std::vector<String> getColumnsName() { return _data->_columnNames; }
|
||||
// Returns all values from current row
|
||||
std::vector<FluxValue> getValues() { return _data->_columnValues; }
|
||||
// Returns true if new table was encountered
|
||||
bool hasTableChanged() const { return _data->_tableChanged; }
|
||||
// Returns current table position in the results set
|
||||
int getTablePosition() const { return _data->_tablePosition; }
|
||||
// Returns an error found during parsing if any, othewise empty string
|
||||
String getError() { return _data->_error; }
|
||||
// Releases all resources and closes server reponse. It must be always called at end of reading.
|
||||
void close();
|
||||
// Descructor
|
||||
~FluxQueryResult();
|
||||
protected:
|
||||
FluxBase *convertValue(String value, String dataType);
|
||||
static FluxDateTime *convertRfc3339(String value, const char *type);
|
||||
void clearValues();
|
||||
void clearColumns();
|
||||
private:
|
||||
class Data {
|
||||
public:
|
||||
Data(CsvReader *reader);
|
||||
~Data();
|
||||
CsvReader *_reader;
|
||||
int _tablePosition = -1;
|
||||
bool _tableChanged = false;
|
||||
std::vector<String> _columnDatatypes;
|
||||
std::vector<String> _columnNames;
|
||||
std::vector<FluxValue> _columnValues;
|
||||
String _error;
|
||||
};
|
||||
std::shared_ptr<Data> _data;
|
||||
};
|
||||
|
||||
#endif //#_FLUX_PARSER_H_
|
@ -1,181 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* FluxTypes.cpp: InfluxDB flux query types support
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2018-2020 InfluxData
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "FluxTypes.h"
|
||||
|
||||
const char *FluxDatatypeString = "string";
|
||||
const char *FluxDatatypeDouble = "double";
|
||||
const char *FluxDatatypeBool = "boolean";
|
||||
const char *FluxDatatypeLong = "long";
|
||||
const char *FluxDatatypeUnsignedLong = "unsignedLong";
|
||||
const char *FluxDatatypeDuration = "duration";
|
||||
const char *FluxBinaryDataTypeBase64 = "base64Binary";
|
||||
const char *FluxDatatypeDatetimeRFC3339 = "dateTime:RFC3339";
|
||||
const char *FluxDatatypeDatetimeRFC3339Nano = "dateTime:RFC3339Nano";
|
||||
|
||||
FluxBase::FluxBase(String rawValue) {
|
||||
_rawValue = rawValue;
|
||||
}
|
||||
|
||||
FluxBase::~FluxBase() {
|
||||
}
|
||||
|
||||
|
||||
FluxLong::FluxLong(String rawValue, long value):FluxBase(rawValue),value(value) {
|
||||
|
||||
}
|
||||
|
||||
const char *FluxLong::getType() {
|
||||
return FluxDatatypeLong;
|
||||
}
|
||||
|
||||
FluxUnsignedLong::FluxUnsignedLong(String rawValue, unsigned long value):FluxBase(rawValue),value(value) {
|
||||
}
|
||||
|
||||
const char *FluxUnsignedLong::getType() {
|
||||
return FluxDatatypeUnsignedLong;
|
||||
}
|
||||
|
||||
FluxDouble::FluxDouble(String rawValue, double value):FluxBase(rawValue),value(value) {
|
||||
|
||||
}
|
||||
|
||||
const char *FluxDouble::getType() {
|
||||
return FluxDatatypeDouble;
|
||||
}
|
||||
|
||||
FluxBool::FluxBool(String rawValue, bool value):FluxBase(rawValue),value(value) {
|
||||
}
|
||||
|
||||
const char *FluxBool::getType() {
|
||||
return FluxDatatypeBool;
|
||||
}
|
||||
|
||||
|
||||
FluxDateTime::FluxDateTime(String rawValue, const char *type, struct tm value, unsigned long microseconds):FluxBase(rawValue),_type(type),value(value), microseconds(microseconds) {
|
||||
|
||||
}
|
||||
|
||||
const char *FluxDateTime::getType() {
|
||||
return _type;
|
||||
}
|
||||
|
||||
String FluxDateTime::format(String formatString) {
|
||||
int len = formatString.length() + 20; //+20 for safety
|
||||
char *buff = new char[len];
|
||||
strftime(buff,len, formatString.c_str(),&value);
|
||||
String str = buff;
|
||||
delete [] buff;
|
||||
return str;
|
||||
}
|
||||
|
||||
FluxString::FluxString(String rawValue, const char *type):FluxBase(rawValue),_type(type),value(_rawValue) {
|
||||
|
||||
}
|
||||
|
||||
const char *FluxString::getType() {
|
||||
return _type;
|
||||
}
|
||||
|
||||
|
||||
FluxValue::FluxValue() {}
|
||||
|
||||
FluxValue::FluxValue(FluxBase *fluxValue):_data(fluxValue) {
|
||||
|
||||
}
|
||||
|
||||
FluxValue::FluxValue(const FluxValue &other) {
|
||||
_data = other._data;
|
||||
}
|
||||
|
||||
FluxValue& FluxValue::operator=(const FluxValue& other) {
|
||||
if(this != &other) {
|
||||
_data = other._data;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Type accessor. If value is different type zero value for given time is returned.
|
||||
String FluxValue::getString() {
|
||||
if(_data && (_data->getType() == FluxDatatypeString ||_data->getType() == FluxDatatypeDuration || _data->getType() == FluxBinaryDataTypeBase64)) {
|
||||
FluxString *s = (FluxString *)_data.get();
|
||||
return s->value;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
long FluxValue::getLong() {
|
||||
if(_data && _data->getType() == FluxDatatypeLong) {
|
||||
FluxLong *l = (FluxLong *)_data.get();
|
||||
return l->value;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned long FluxValue::getUnsignedLong() {
|
||||
if(_data && _data->getType() == FluxDatatypeUnsignedLong) {
|
||||
FluxUnsignedLong *l = (FluxUnsignedLong *)_data.get();
|
||||
return l->value;
|
||||
}
|
||||
return 0;
|
||||
|
||||
}
|
||||
FluxDateTime FluxValue::getDateTime() {
|
||||
if(_data && (_data->getType() == FluxDatatypeDatetimeRFC3339 ||_data->getType() == FluxDatatypeDatetimeRFC3339Nano)) {
|
||||
FluxDateTime *d = (FluxDateTime *)_data.get();
|
||||
return *d;
|
||||
}
|
||||
return FluxDateTime("",FluxDatatypeDatetimeRFC3339, {0,0,0,0,0,0,0,0,0}, 0 );
|
||||
}
|
||||
|
||||
bool FluxValue::getBool() {
|
||||
if(_data && _data->getType() == FluxDatatypeBool) {
|
||||
FluxBool *b = (FluxBool *)_data.get();
|
||||
return b->value;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
double FluxValue::getDouble() {
|
||||
if(_data && _data->getType() == FluxDatatypeDouble) {
|
||||
FluxDouble *d = (FluxDouble *)_data.get();
|
||||
return d->value;
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
// returns string representation of non-string values
|
||||
String FluxValue::getRawValue() {
|
||||
if(_data) {
|
||||
return _data->getRawValue();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
bool FluxValue::isNull() {
|
||||
return _data == nullptr;
|
||||
}
|
@ -1,167 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* FLuxTypes.h: InfluxDB flux types representation
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020 InfluxData
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#ifndef _FLUX_TYPES_H_
|
||||
#define _FLUX_TYPES_H_
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <memory>
|
||||
|
||||
/** Supported flux types:
|
||||
* - long - converts to long
|
||||
* - unsignedLong - converts to unsigned long
|
||||
* - double - converts to double
|
||||
* - bool - converts to bool
|
||||
* - dateTime:RFC3339 - converts to FluxDataTime
|
||||
* - dateTime:RFC3339Nano - converts to FluxDataTime
|
||||
* other types defaults to String
|
||||
*/
|
||||
|
||||
extern const char *FluxDatatypeString;
|
||||
extern const char *FluxDatatypeDouble;
|
||||
extern const char *FluxDatatypeBool;
|
||||
extern const char *FluxDatatypeLong;
|
||||
extern const char *FluxDatatypeUnsignedLong;
|
||||
extern const char *FluxDatatypeDuration;
|
||||
extern const char *FluxBinaryDataTypeBase64;
|
||||
extern const char *FluxDatatypeDatetimeRFC3339;
|
||||
extern const char *FluxDatatypeDatetimeRFC3339Nano;
|
||||
|
||||
// Base type for all specific flux types
|
||||
class FluxBase {
|
||||
protected:
|
||||
String _rawValue;
|
||||
public:
|
||||
FluxBase(String rawValue);
|
||||
virtual ~FluxBase();
|
||||
String getRawValue() const { return _rawValue; }
|
||||
virtual const char *getType() = 0;
|
||||
};
|
||||
|
||||
// Represents flux long
|
||||
class FluxLong : public FluxBase {
|
||||
public:
|
||||
FluxLong(String rawValue, long value);
|
||||
long value;
|
||||
virtual const char *getType() override;
|
||||
};
|
||||
|
||||
// Represents flux unsignedLong
|
||||
class FluxUnsignedLong : public FluxBase {
|
||||
public:
|
||||
FluxUnsignedLong(String rawValue, unsigned long value);
|
||||
unsigned long value;
|
||||
virtual const char *getType() override;
|
||||
};
|
||||
|
||||
// Represents flux double
|
||||
class FluxDouble : public FluxBase {
|
||||
public:
|
||||
FluxDouble(String rawValue, double value);
|
||||
double value;
|
||||
virtual const char *getType() override;
|
||||
};
|
||||
|
||||
// Represents flux bool
|
||||
class FluxBool : public FluxBase {
|
||||
public:
|
||||
FluxBool(String rawValue, bool value);
|
||||
bool value;
|
||||
virtual const char *getType() override;
|
||||
};
|
||||
|
||||
// Represents flux dateTime:RFC3339 and dateTime:RFC3339Nano
|
||||
// Date and time are stored in classic struct tm.
|
||||
// Fraction of second is stored in microseconds
|
||||
// There are several classic functions for using struct tm: http://www.cplusplus.com/reference/ctime/
|
||||
class FluxDateTime : public FluxBase {
|
||||
protected:
|
||||
const char *_type;
|
||||
public:
|
||||
FluxDateTime(String rawValue, const char *type, struct tm value, unsigned long microseconds);
|
||||
// Struct tm for date and time
|
||||
struct tm value;
|
||||
// microseconds part
|
||||
unsigned long microseconds;
|
||||
// Formats the value part to string according to the given format. Microseconds are skipped.
|
||||
// Format string must be compatible with the http://www.cplusplus.com/reference/ctime/strftime/
|
||||
String format(String formatString);
|
||||
virtual const char *getType() override;
|
||||
};
|
||||
|
||||
// Represents flux string, duration, base64binary
|
||||
class FluxString : public FluxBase {
|
||||
protected:
|
||||
const char *_type;
|
||||
public:
|
||||
FluxString(String rawValue, const char *type);
|
||||
String value;
|
||||
virtual const char *getType() override;
|
||||
};
|
||||
|
||||
/**
|
||||
* FluxValue wraps a value from a flux query result column.
|
||||
* It provides getter methods for supported flux types:
|
||||
* * getString() - string, base64binary or duration
|
||||
* * getLong() - long
|
||||
* * getUnsignedLong() - unsignedLong
|
||||
* * getDateTime() - dateTime:RFC3339 or dateTime:RFC3339Nano
|
||||
* * getBool() - bool
|
||||
* * getDouble() - double
|
||||
*
|
||||
* Calling improper type getter will result in zero (empty) value.
|
||||
* Check for null value usig isNull().
|
||||
* Use getRawValue() for getting original string form.
|
||||
*
|
||||
**/
|
||||
|
||||
class FluxValue {
|
||||
public:
|
||||
FluxValue();
|
||||
FluxValue(FluxBase *value);
|
||||
FluxValue(const FluxValue &other);
|
||||
FluxValue& operator=(const FluxValue& other);
|
||||
// Check if value represent null - not present - value.
|
||||
bool isNull();
|
||||
// Returns a value of string, base64binary or duration type column, or empty string if column is a different type.
|
||||
String getString();
|
||||
// Returns a value of long type column, or zero if column is a different type.
|
||||
long getLong();
|
||||
// Returns a value of unsigned long type column, or zero if column is a different type.
|
||||
unsigned long getUnsignedLong();
|
||||
// Returns a value of dateTime:RFC3339 or dateTime:RFC3339Nano, or zeroed FluxDateTime instance if column is a different type.
|
||||
FluxDateTime getDateTime();
|
||||
// Returns a value of bool type column, or false if column is a different type.
|
||||
bool getBool();
|
||||
// Returns a value of double type column, or 0.0 if column is a different type.
|
||||
double getDouble();
|
||||
// Returns a value in the original string form, as presented in the response.
|
||||
String getRawValue();
|
||||
private:
|
||||
std::shared_ptr<FluxBase> _data;
|
||||
};
|
||||
|
||||
#endif //_FLUX_TYPES_H_
|
@ -1,104 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* HttpStreamScanner.cpp: Scannes HttpClient stream for lines. Supports chunking.
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020 InfluxData
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#include "HttpStreamScanner.h"
|
||||
|
||||
// Uncomment bellow in case of a problem and rebuild sketch
|
||||
//#define INFLUXDB_CLIENT_DEBUG_ENABLE
|
||||
#include "util/debug.h"
|
||||
#include "util/helpers.h"
|
||||
|
||||
HttpStreamScanner::HttpStreamScanner(HTTPClient *client, bool chunked)
|
||||
{
|
||||
_client = client;
|
||||
_stream = client->getStreamPtr();
|
||||
_chunked = chunked;
|
||||
_chunkHeader = chunked;
|
||||
_len = client->getSize();
|
||||
INFLUXDB_CLIENT_DEBUG("[D] HttpStreamScanner: chunked: %s, size: %d\n", bool2string(_chunked), _len);
|
||||
}
|
||||
|
||||
bool HttpStreamScanner::next() {
|
||||
while(_client->connected() && (_len > 0 || _len == -1)) {
|
||||
_line = _stream->readStringUntil('\n');
|
||||
INFLUXDB_CLIENT_DEBUG("[D] HttpStreamScanner: line: %s\n", _line.c_str());
|
||||
++_linesNum;
|
||||
int lineLen = _line.length();
|
||||
if(lineLen == 0) {
|
||||
_error = HTTPC_ERROR_READ_TIMEOUT;
|
||||
return false;
|
||||
}
|
||||
int r = lineLen +1; //+1 for terminating \n
|
||||
_line.trim(); //remove \r
|
||||
if(!_chunked || !_chunkHeader) {
|
||||
_read += r;
|
||||
if(_lastChunkLine.length() > 0) { //fix broken line
|
||||
_line = _lastChunkLine + _line;
|
||||
_lastChunkLine = "";
|
||||
}
|
||||
|
||||
}
|
||||
if(_chunkHeader && r == 2) { //empty line at the end of chunk
|
||||
//last line was complete so return
|
||||
_line = _lastChunkLine;
|
||||
_lastChunkLine = "";
|
||||
return true;
|
||||
}
|
||||
if(_chunkHeader){
|
||||
_chunkLen = (int) strtol((const char *) _line.c_str(), NULL, 16);
|
||||
INFLUXDB_CLIENT_DEBUG("[D] HttpStreamScanner chunk len: %d\n", _chunkLen);
|
||||
_chunkHeader = false;
|
||||
_read = 0;
|
||||
if(_chunkLen == 0) { //last chunk
|
||||
_error = 0;
|
||||
_line = "";
|
||||
return false;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
} else if(_chunked && _read >= _chunkLen){ //we reached end of chunk.
|
||||
_lastChunkLine = _line;
|
||||
_chunkHeader = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(_len > 0) {
|
||||
_len -= r;
|
||||
INFLUXDB_CLIENT_DEBUG("[D] HttpStreamScanner new len: %d\n", _len);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if(!_client->connected() && ( (_chunked && _chunkLen > 0) || (!_chunked && _len > 0))) { //report error only if we didn't went to
|
||||
_error = HTTPC_ERROR_CONNECTION_LOST;
|
||||
INFLUXDB_CLIENT_DEBUG("HttpStreamScanner connection lost\n");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void HttpStreamScanner::close() {
|
||||
_client->end();
|
||||
}
|
||||
|
@ -1,64 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* HttpStreamScanner.h: Scannes HttpClient stream for lines. Supports chunking.
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020 InfluxData
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#ifndef _HTTP_STREAM_SCANNER_
|
||||
#define _HTTP_STREAM_SCANNER_
|
||||
|
||||
#if defined(ESP8266)
|
||||
# include <ESP8266HTTPClient.h>
|
||||
#elif defined(ESP32)
|
||||
# include <HTTPClient.h>
|
||||
#endif //ESP8266
|
||||
|
||||
/**
|
||||
* HttpStreamScanner parses response stream from HTTPClient for lines.
|
||||
* By repeatedly calling next() it searches for new line.
|
||||
* If next() returns false, it can mean end of stream or an error.
|
||||
* Check getError() for nonzero if an error occured
|
||||
*/
|
||||
class HttpStreamScanner {
|
||||
public:
|
||||
HttpStreamScanner(HTTPClient *client, bool chunked);
|
||||
bool next();
|
||||
void close();
|
||||
const String &getLine() const { return _line; }
|
||||
int getError() const { return _error; }
|
||||
int getLinesNum() const {return _linesNum; }
|
||||
private:
|
||||
HTTPClient *_client;
|
||||
Stream *_stream = nullptr;
|
||||
int _len;
|
||||
String _line;
|
||||
int _linesNum= 0;
|
||||
int _read = 0;
|
||||
bool _chunked;
|
||||
bool _chunkHeader;
|
||||
int _chunkLen = 0;
|
||||
String _lastChunkLine;
|
||||
int _error = 0;
|
||||
};
|
||||
|
||||
#endif //#_HTTP_STREAM_SCANNER_
|
@ -1,38 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* debug.h: InfluxDB Client debug macros
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020 InfluxData
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#ifndef _INFLUXDB_CLIENT_DEBUG_H
|
||||
#define _INFLUXDB_CLIENT_DEBUG_H
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#ifdef INFLUXDB_CLIENT_DEBUG_ENABLE
|
||||
# define INFLUXDB_CLIENT_DEBUG(fmt, ...) Serial.printf("%.03f ",millis()/1000.0f);Serial.printf_P( (PGM_P)PSTR(fmt), ## __VA_ARGS__ )
|
||||
#else
|
||||
# define INFLUXDB_CLIENT_DEBUG(fmt, ...)
|
||||
#endif //INFLUXDB_CLIENT_DEBUG
|
||||
|
||||
#endif //# _INFLUXDB_CLIENT_DEBUG_H
|
@ -1,166 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* helpers.cpp: InfluxDB Client util functions
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020 InfluxData
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#include "helpers.h"
|
||||
|
||||
void timeSync(const char *tzInfo, const char* ntpServer1, const char* ntpServer2, const char* ntpServer3) {
|
||||
// Accurate time is necessary for certificate validion
|
||||
|
||||
configTzTime(tzInfo,ntpServer1, ntpServer2, ntpServer3);
|
||||
|
||||
// Wait till time is synced
|
||||
Serial.print("Syncing time");
|
||||
int i = 0;
|
||||
while (time(nullptr) < 1000000000l && i < 40) {
|
||||
Serial.print(".");
|
||||
delay(500);
|
||||
i++;
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
// Show time
|
||||
time_t tnow = time(nullptr);
|
||||
Serial.print("Synchronized time: ");
|
||||
Serial.println(ctime(&tnow));
|
||||
}
|
||||
|
||||
unsigned long long getTimeStamp(struct timeval *tv, int secFracDigits) {
|
||||
unsigned long long tsVal = 0;
|
||||
switch(secFracDigits) {
|
||||
case 0:
|
||||
tsVal = tv->tv_sec;
|
||||
break;
|
||||
case 6:
|
||||
tsVal = tv->tv_sec * 1000000LL + tv->tv_usec;
|
||||
break;
|
||||
case 9:
|
||||
tsVal = tv->tv_sec * 1000000000LL + tv->tv_usec * 1000LL;
|
||||
break;
|
||||
case 3:
|
||||
default:
|
||||
tsVal = tv->tv_sec * 1000LL + tv->tv_usec / 1000LL;
|
||||
break;
|
||||
|
||||
}
|
||||
return tsVal;
|
||||
}
|
||||
|
||||
String timeStampToString(unsigned long long timestamp) {
|
||||
static char buff[50];
|
||||
snprintf(buff, 50, "%llu", timestamp);
|
||||
return String(buff);
|
||||
}
|
||||
|
||||
String escapeKey(String key, bool escapeEqual) {
|
||||
String ret;
|
||||
ret.reserve(key.length()+5); //5 is estimate of chars needs to escape,
|
||||
|
||||
for (char c: key)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case '\r':
|
||||
case '\n':
|
||||
case '\t':
|
||||
case ' ':
|
||||
case ',':
|
||||
ret += '\\';
|
||||
break;
|
||||
case '=':
|
||||
if(escapeEqual) {
|
||||
ret += '\\';
|
||||
}
|
||||
break;
|
||||
}
|
||||
ret += c;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
String escapeValue(const char *value) {
|
||||
String ret;
|
||||
int len = strlen_P(value);
|
||||
ret.reserve(len+5); //5 is estimate of max chars needs to escape,
|
||||
for(int i=0;i<len;i++)
|
||||
{
|
||||
switch (value[i])
|
||||
{
|
||||
case '\\':
|
||||
case '\"':
|
||||
ret += '\\';
|
||||
break;
|
||||
}
|
||||
|
||||
ret += value[i];
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static char invalidChars[] = "$&+,/:;=?@ <>#%{}|\\^~[]`";
|
||||
|
||||
static char hex_digit(char c) {
|
||||
return "0123456789ABCDEF"[c & 0x0F];
|
||||
}
|
||||
|
||||
String urlEncode(const char* src) {
|
||||
int n=0;
|
||||
char c,*s = (char *)src;
|
||||
while ((c = *s++)) {
|
||||
if(strchr(invalidChars, c)) {
|
||||
n++;
|
||||
}
|
||||
}
|
||||
String ret;
|
||||
ret.reserve(strlen(src)+2*n+1);
|
||||
s = (char *)src;
|
||||
while ((c = *s++)) {
|
||||
if (strchr(invalidChars,c)) {
|
||||
ret += '%';
|
||||
ret += hex_digit(c >> 4);
|
||||
ret += hex_digit(c);
|
||||
}
|
||||
else ret += c;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool isValidID(const char *idString) {
|
||||
if(strlen(idString) != 16) {
|
||||
return false;
|
||||
}
|
||||
for(int i=0;i<16;i++) {
|
||||
//0-9,a-f
|
||||
if(!((idString[i] >= '0' && idString[i] <= '9') || (idString[i] >= 'a' && idString[i] <= 'f'))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const char *bool2string(bool val) {
|
||||
return (val?"true":"false");
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* helpers.h: InfluxDB Client util functions
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020 InfluxData
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#ifndef _INFLUXDB_CLIENT_HELPERS_H
|
||||
#define _INFLUXDB_CLIENT_HELPERS_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
// Synchronize time with NTP servers and waits for completition. Prints waiting progress and final synchronized time to the serial.
|
||||
// Accurate time is necessary for certificate validion and writing points in batch
|
||||
// For the fastest time sync find NTP servers in your area: https://www.pool.ntp.org/zone/
|
||||
void timeSync(const char *tzInfo, const char* ntpServer1, const char* ntpServer2 = nullptr, const char* ntpServer3 = nullptr);
|
||||
|
||||
// Create timestamp in offset from epoch. secFracDigits specify resulution. 0 - seconds, 3 - milliseconds, etc. Maximum and default is 9 - nanoseconds.
|
||||
unsigned long long getTimeStamp(struct timeval *tv, int secFracDigits = 3);
|
||||
|
||||
// Converts unsigned long long timestamp to String
|
||||
String timeStampToString(unsigned long long timestamp);
|
||||
|
||||
// Escape invalid chars in measurement, tag key, tag value and field key
|
||||
String escapeKey(String key, bool escapeEqual = true);
|
||||
|
||||
// Escape invalid chars in field value
|
||||
String escapeValue(const char *value);
|
||||
// Encode URL string for invalid chars
|
||||
String urlEncode(const char* src);
|
||||
// Returns true of string contains valid InfluxDB ID type
|
||||
bool isValidID(const char *idString);
|
||||
// Return "true" if val is true, otherwise "false"
|
||||
const char *bool2string(bool val);
|
||||
|
||||
#endif //_INFLUXDB_CLIENT_HELPERS_H
|
Loading…
Reference in New Issue