@@ -109,13 +109,14 @@ void InfluxDBClient::setConnectionParamsV1(const char *serverUrl, const char *db
109
109
}
110
110
111
111
bool InfluxDBClient::init () {
112
- INFLUXDB_CLIENT_DEBUG (" Init\n " );
113
- INFLUXDB_CLIENT_DEBUG (" Library version: " INFLUXDB_CLIENT_VERSION " \n " );
114
- INFLUXDB_CLIENT_DEBUG (" Server url: %s\n " , _serverUrl.c_str ());
115
- INFLUXDB_CLIENT_DEBUG (" Org: %s\n " , _org.c_str ());
116
- INFLUXDB_CLIENT_DEBUG (" Bucket: %s\n " , _bucket.c_str ());
117
- INFLUXDB_CLIENT_DEBUG (" Token: %s\n " , _authToken.c_str ());
118
- INFLUXDB_CLIENT_DEBUG (" DB version: %d\n " , _dbVersion);
112
+ INFLUXDB_CLIENT_DEBUG (" [D] Init\n " );
113
+ INFLUXDB_CLIENT_DEBUG (" [D] Library version: " INFLUXDB_CLIENT_VERSION " \n " );
114
+ INFLUXDB_CLIENT_DEBUG (" [D] Server url: %s\n " , _serverUrl.c_str ());
115
+ INFLUXDB_CLIENT_DEBUG (" [D] Org: %s\n " , _org.c_str ());
116
+ INFLUXDB_CLIENT_DEBUG (" [D] Bucket: %s\n " , _bucket.c_str ());
117
+ INFLUXDB_CLIENT_DEBUG (" [D] Token: %s\n " , _authToken.c_str ());
118
+ INFLUXDB_CLIENT_DEBUG (" [D] DB version: %d\n " , _dbVersion);
119
+ INFLUXDB_CLIENT_DEBUG (" [D] Connection reuse: %s\n " , _httpOptions._connectionReuse ?" true" :" false" );
119
120
if (_serverUrl.length () == 0 || (_dbVersion == 2 && (_org.length () == 0 || _bucket.length () == 0 || _authToken.length () == 0 ))) {
120
121
INFLUXDB_CLIENT_DEBUG (" [E] Invalid parameters\n " );
121
122
return false ;
@@ -150,9 +151,12 @@ bool InfluxDBClient::init() {
150
151
} else {
151
152
_wifiClient = new WiFiClient;
152
153
}
153
- _httpClient.setReuse (_httpOptions._connectionReuse );
154
+ if (!_httpClient) {
155
+ _httpClient = new HTTPClient;
156
+ }
157
+ _httpClient->setReuse (_httpOptions._connectionReuse );
154
158
155
- _httpClient. setUserAgent (FPSTR (UserAgent));
159
+ _httpClient-> setUserAgent (FPSTR (UserAgent));
156
160
return true ;
157
161
}
158
162
@@ -192,9 +196,9 @@ bool checkMFLN(BearSSL::WiFiClientSecure *client, String url) {
192
196
portS.remove (0 , (index + 1 )); // remove hostname + :
193
197
port = portS.toInt (); // get port
194
198
}
195
- INFLUXDB_CLIENT_DEBUG (" probeMaxFragmentLength to %s:%d\n " , host.c_str (), port);
199
+ INFLUXDB_CLIENT_DEBUG (" [D] probeMaxFragmentLength to %s:%d\n " , host.c_str (), port);
196
200
bool mfln = client->probeMaxFragmentLength (host, port, 1024 );
197
- INFLUXDB_CLIENT_DEBUG (" MFLN:%s\n " , mfln ? " yes" : " no" );
201
+ INFLUXDB_CLIENT_DEBUG (" [D] MFLN:%s\n " , mfln ? " yes" : " no" );
198
202
if (mfln) {
199
203
client->setBufferSizes (1024 , 1024 );
200
204
}
@@ -214,7 +218,14 @@ InfluxDBClient::~InfluxDBClient() {
214
218
}
215
219
216
220
void InfluxDBClient::clean () {
217
- _wifiClient = nullptr ;
221
+ if (_httpClient) {
222
+ delete _httpClient;
223
+ _httpClient = nullptr ;
224
+ }
225
+ if (_wifiClient) {
226
+ delete _wifiClient;
227
+ _wifiClient = nullptr ;
228
+ }
218
229
#if defined(ESP8266)
219
230
if (_cert) {
220
231
delete _cert;
@@ -229,18 +240,18 @@ void InfluxDBClient::clean() {
229
240
}
230
241
231
242
void InfluxDBClient::setUrls () {
232
- INFLUXDB_CLIENT_DEBUG (" setUrls\n " );
243
+ INFLUXDB_CLIENT_DEBUG (" [D] setUrls\n " );
233
244
if (_dbVersion == 2 ) {
234
245
_writeUrl = _serverUrl;
235
246
_writeUrl += " /api/v2/write?org=" ;
236
247
_writeUrl += urlEncode (_org.c_str ());
237
248
_writeUrl += " &bucket=" ;
238
249
_writeUrl += urlEncode (_bucket.c_str ());
239
- INFLUXDB_CLIENT_DEBUG (" writeUrl: %s\n " , _writeUrl.c_str ());
250
+ INFLUXDB_CLIENT_DEBUG (" [D] writeUrl: %s\n " , _writeUrl.c_str ());
240
251
_queryUrl = _serverUrl;
241
252
_queryUrl += " /api/v2/query?org=" ;
242
253
_queryUrl += urlEncode (_org.c_str ());
243
- INFLUXDB_CLIENT_DEBUG (" queryUrl: %s\n " , _queryUrl.c_str ());
254
+ INFLUXDB_CLIENT_DEBUG (" [D] queryUrl: %s\n " , _queryUrl.c_str ());
244
255
} else {
245
256
_writeUrl = _serverUrl;
246
257
_writeUrl += " /write?db=" ;
@@ -256,13 +267,13 @@ void InfluxDBClient::setUrls() {
256
267
_queryUrl += " ?" ;
257
268
_queryUrl += auth;
258
269
}
259
- INFLUXDB_CLIENT_DEBUG (" writeUrl: %s\n " , _writeUrl.c_str ());
260
- INFLUXDB_CLIENT_DEBUG (" queryUrl: %s\n " , _queryUrl.c_str ());
270
+ INFLUXDB_CLIENT_DEBUG (" [D] writeUrl: %s\n " , _writeUrl.c_str ());
271
+ INFLUXDB_CLIENT_DEBUG (" [D] queryUrl: %s\n " , _queryUrl.c_str ());
261
272
}
262
273
if (_writeOptions._writePrecision != WritePrecision::NoTime) {
263
274
_writeUrl += " &precision=" ;
264
275
_writeUrl += precisionToString (_writeOptions._writePrecision , _dbVersion);
265
- INFLUXDB_CLIENT_DEBUG (" writeUrl: %s\n " , _writeUrl.c_str ());
276
+ INFLUXDB_CLIENT_DEBUG (" [D] writeUrl: %s\n " , _writeUrl.c_str ());
266
277
}
267
278
268
279
}
@@ -297,8 +308,11 @@ void InfluxDBClient::setWriteOptions(const WriteOptions & writeOptions) {
297
308
298
309
void InfluxDBClient::setHTTPOptions (const HTTPOptions & httpOptions) {
299
310
_httpOptions = httpOptions;
300
- _httpClient.setReuse (_httpOptions._connectionReuse );
301
- _httpClient.setTimeout (_httpOptions._httpReadTimeout );
311
+ if (!_httpClient) {
312
+ _httpClient = new HTTPClient;
313
+ }
314
+ _httpClient->setReuse (_httpOptions._connectionReuse );
315
+ _httpClient->setTimeout (_httpOptions._httpReadTimeout );
302
316
}
303
317
304
318
void InfluxDBClient::resetBuffer () {
@@ -312,7 +326,7 @@ void InfluxDBClient::resetBuffer() {
312
326
if (_writeBufferSize < 2 ) {
313
327
_writeBufferSize = 2 ;
314
328
}
315
- INFLUXDB_CLIENT_DEBUG (" Reset buffer: writeBuffSize: %d\n " , _writeBufferSize);
329
+ INFLUXDB_CLIENT_DEBUG (" [D] Reset buffer: writeBuffSize: %d\n " , _writeBufferSize);
316
330
_writeBuffer = new Batch*[_writeBufferSize];
317
331
for (int i=0 ;i<_writeBufferSize;i++) {
318
332
_writeBuffer[i] = nullptr ;
@@ -325,7 +339,7 @@ void InfluxDBClient::resetBuffer() {
325
339
void InfluxDBClient::reserveBuffer (int size) {
326
340
if (size > _writeBufferSize) {
327
341
Batch **newBuffer = new Batch*[size];
328
- INFLUXDB_CLIENT_DEBUG (" Resizing buffer from %d to %d\n " ,_writeBufferSize, size);
342
+ INFLUXDB_CLIENT_DEBUG (" [D] Resizing buffer from %d to %d\n " ,_writeBufferSize, size);
329
343
for (int i=0 ;i<_bufferCeiling; i++) {
330
344
newBuffer[i] = _writeBuffer[i];
331
345
}
@@ -533,29 +547,29 @@ bool InfluxDBClient::validateConnection() {
533
547
String url = _serverUrl + (_dbVersion==2 ?" /health" :" /ping?verbose=true" );
534
548
INFLUXDB_CLIENT_DEBUG (" [D] Validating connection to %s\n " , url.c_str ());
535
549
536
- if (!_httpClient. begin (*_wifiClient, url)) {
550
+ if (!_httpClient-> begin (*_wifiClient, url)) {
537
551
INFLUXDB_CLIENT_DEBUG (" [E] begin failed\n " );
538
552
return false ;
539
553
}
540
- _httpClient. addHeader (F (" Accept" ), F (" application/json" ));
554
+ _httpClient-> addHeader (F (" Accept" ), F (" application/json" ));
541
555
542
- _lastStatusCode = _httpClient. GET ();
556
+ _lastStatusCode = _httpClient-> GET ();
543
557
544
558
_lastErrorResponse = " " ;
545
559
546
560
afterRequest (200 , false );
547
561
548
- _httpClient. end ();
562
+ _httpClient-> end ();
549
563
550
564
return _lastStatusCode == 200 ;
551
565
}
552
566
553
567
void InfluxDBClient::beforeRequest () {
554
568
if (_authToken.length () > 0 ) {
555
- _httpClient. addHeader (F (" Authorization" ), " Token " + _authToken);
569
+ _httpClient-> addHeader (F (" Authorization" ), " Token " + _authToken);
556
570
}
557
571
const char * headerKeys[] = {RetryAfter, TransferEncoding} ;
558
- _httpClient. collectHeaders (headerKeys, 2 );
572
+ _httpClient-> collectHeaders (headerKeys, 2 );
559
573
}
560
574
561
575
int InfluxDBClient::postData (const char *data) {
@@ -566,22 +580,22 @@ int InfluxDBClient::postData(const char *data) {
566
580
}
567
581
if (data) {
568
582
INFLUXDB_CLIENT_DEBUG (" [D] Writing to %s\n " , _writeUrl.c_str ());
569
- if (!_httpClient. begin (*_wifiClient, _writeUrl)) {
583
+ if (!_httpClient-> begin (*_wifiClient, _writeUrl)) {
570
584
INFLUXDB_CLIENT_DEBUG (" [E] Begin failed\n " );
571
585
return false ;
572
586
}
573
587
INFLUXDB_CLIENT_DEBUG (" [D] Sending:\n %s\n " , data);
574
588
575
- _httpClient. addHeader (F (" Content-Type" ), F (" text/plain" ));
589
+ _httpClient-> addHeader (F (" Content-Type" ), F (" text/plain" ));
576
590
577
591
beforeRequest ();
578
592
579
- _lastStatusCode = _httpClient. POST ((uint8_t *)data, strlen (data));
593
+ _lastStatusCode = _httpClient-> POST ((uint8_t *)data, strlen (data));
580
594
581
595
afterRequest (204 );
582
596
583
597
584
- _httpClient. end ();
598
+ _httpClient-> end ();
585
599
}
586
600
return _lastStatusCode;
587
601
}
@@ -615,11 +629,11 @@ FluxQueryResult InfluxDBClient::query(String fluxQuery) {
615
629
return FluxQueryResult (_lastErrorResponse);
616
630
}
617
631
INFLUXDB_CLIENT_DEBUG (" [D] Query to %s\n " , _queryUrl.c_str ());
618
- if (!_httpClient. begin (*_wifiClient, _queryUrl)) {
632
+ if (!_httpClient-> begin (*_wifiClient, _queryUrl)) {
619
633
INFLUXDB_CLIENT_DEBUG (" [E] begin failed\n " );
620
634
return FluxQueryResult (" " );;
621
635
}
622
- _httpClient. addHeader (F (" Content-Type" ), F (" application/json" ));
636
+ _httpClient-> addHeader (F (" Content-Type" ), F (" application/json" ));
623
637
624
638
beforeRequest ();
625
639
@@ -629,22 +643,22 @@ FluxQueryResult InfluxDBClient::query(String fluxQuery) {
629
643
body += escapeJSONString (fluxQuery) + " \" ," ;
630
644
body += FPSTR (QueryDialect);
631
645
632
- _lastStatusCode = _httpClient. POST (body);
646
+ _lastStatusCode = _httpClient-> POST (body);
633
647
634
648
afterRequest (200 );
635
649
if (_lastStatusCode == 200 ) {
636
650
bool chunked = false ;
637
- if (_httpClient. hasHeader (TransferEncoding)) {
638
- String header = _httpClient. header (TransferEncoding);
651
+ if (_httpClient-> hasHeader (TransferEncoding)) {
652
+ String header = _httpClient-> header (TransferEncoding);
639
653
chunked = header.equalsIgnoreCase (" chunked" );
640
654
}
641
655
INFLUXDB_CLIENT_DEBUG (" [D] chunked: %s\n " , chunked?" true" :" false" );
642
- HttpStreamScanner *scanner = new HttpStreamScanner (& _httpClient, chunked);
656
+ HttpStreamScanner *scanner = new HttpStreamScanner (_httpClient, chunked);
643
657
CsvReader *reader = new CsvReader (scanner);
644
658
645
659
return FluxQueryResult (reader);
646
660
} else {
647
- _httpClient. end ();
661
+ _httpClient-> end ();
648
662
return FluxQueryResult (_lastErrorResponse);
649
663
}
650
664
}
@@ -655,19 +669,19 @@ void InfluxDBClient::afterRequest(int expectedStatusCode, bool modifyLastConnSt
655
669
INFLUXDB_CLIENT_DEBUG (" [D] HTTP status code - %d\n " , _lastStatusCode);
656
670
_lastRetryAfter = 0 ;
657
671
if (_lastStatusCode >= 429 ) { // retryable server errors
658
- if (_httpClient. hasHeader (RetryAfter)) {
659
- _lastRetryAfter = _httpClient. header (RetryAfter).toInt ();
672
+ if (_httpClient-> hasHeader (RetryAfter)) {
673
+ _lastRetryAfter = _httpClient-> header (RetryAfter).toInt ();
660
674
INFLUXDB_CLIENT_DEBUG (" [D] Reply after - %d\n " , _lastRetryAfter);
661
675
}
662
676
}
663
677
}
664
678
_lastErrorResponse = " " ;
665
679
if (_lastStatusCode != expectedStatusCode) {
666
680
if (_lastStatusCode > 0 ) {
667
- _lastErrorResponse = _httpClient. getString ();
681
+ _lastErrorResponse = _httpClient-> getString ();
668
682
INFLUXDB_CLIENT_DEBUG (" [D] Response:\n %s\n " , _lastErrorResponse.c_str ());
669
683
} else {
670
- _lastErrorResponse = _httpClient. errorToString (_lastStatusCode);
684
+ _lastErrorResponse = _httpClient-> errorToString (_lastStatusCode);
671
685
INFLUXDB_CLIENT_DEBUG (" [E] Error - %s\n " , _lastErrorResponse.c_str ());
672
686
}
673
687
}
0 commit comments