Skip to content

Commit f002951

Browse files
committed
fix: allow setting of options without connection params
1 parent 9970971 commit f002951

File tree

9 files changed

+74
-52
lines changed

9 files changed

+74
-52
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Changelog
2+
## unreleased
3+
### Fixes
4+
- [210](https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino/pull/210) - Allow setting of options without previously set connection params
5+
26
## 3.13.0 [2022-10-14]
37
### Features
48
- [202](https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino/pull/202) - Added option to specify timestamp precision and do not send timestamp. Set using `WriteOption::useServerTimestamptrue)`.

src/HTTPService.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ HTTPService::HTTPService(ConnectionInfo *pConnInfo):_pConnInfo(pConnInfo) {
5151
if(!_httpClient) {
5252
_httpClient = new HTTPClient;
5353
}
54-
_httpClient->setReuse(_httpOptions._connectionReuse);
54+
_httpClient->setReuse(_pConnInfo->httpOptions._connectionReuse);
5555

5656
_httpClient->setUserAgent(FPSTR(UserAgent));
5757
};
@@ -74,15 +74,14 @@ HTTPService::~HTTPService() {
7474
}
7575

7676

77-
void HTTPService::setHTTPOptions(const HTTPOptions & httpOptions) {
78-
_httpOptions = httpOptions;
79-
if(!_httpClient) {
80-
_httpClient = new HTTPClient;
81-
}
82-
_httpClient->setReuse(_httpOptions._connectionReuse);
83-
_httpClient->setTimeout(_httpOptions._httpReadTimeout);
77+
void HTTPService::setHTTPOptions() {
78+
if(!_httpClient) {
79+
_httpClient = new HTTPClient;
80+
}
81+
_httpClient->setReuse(_pConnInfo->httpOptions._connectionReuse);
82+
_httpClient->setTimeout(_pConnInfo->httpOptions._httpReadTimeout);
8483
#if defined(ESP32)
85-
_httpClient->setConnectTimeout(_httpOptions._httpReadTimeout);
84+
_httpClient->setConnectTimeout(_pConnInfo->httpOptions._httpReadTimeout);
8685
#endif
8786
}
8887

src/HTTPService.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ struct ConnectionInfo {
6262
bool insecure;
6363
// Error message of last failed operation
6464
String lastError;
65+
// HTTP options
66+
HTTPOptions httpOptions;
6567
};
6668

6769
/**
@@ -89,8 +91,7 @@ friend class Test;
8991
#endif
9092
// Store retry timeout suggested by server after last request
9193
int _lastRetryAfter = 0;
92-
// HTTP options
93-
HTTPOptions _httpOptions;
94+
9495
protected:
9596
// Sets request params
9697
bool beforeRequest(const char *url);
@@ -104,13 +105,10 @@ friend class Test;
104105
HTTPService(ConnectionInfo *pConnInfo);
105106
// Clean instance on deletion
106107
~HTTPService();
107-
// Sets custom HTTP options. See HTTPOptions doc for more info.
108-
// Must be called before calling any method initiating a connection to server.
109-
// Example:
110-
// service.setHTTPOptions(HTTPOptions().httpReadTimeout(20000)).
111-
void setHTTPOptions(const HTTPOptions &httpOptions);
108+
// Propagates http options to http client.
109+
void setHTTPOptions();
112110
// Returns current HTTPOption
113-
HTTPOptions &getHTTPOptions() { return _httpOptions; }
111+
HTTPOptions &getHTTPOptions() { return _pConnInfo->httpOptions; }
114112
// Performs HTTP POST by sending data. On success calls response call back
115113
bool doPOST(const char *url, const char *data, const char *contentType, int expectedCode, httpResponseCallback cb);
116114
// Performs HTTP POST by sending stream. On success calls response call back

src/InfluxDbClient.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ void InfluxDBClient::clean() {
144144
}
145145

146146
bool InfluxDBClient::setUrls() {
147-
if(!_service && !init()) {
147+
if(!_service) {
148148
return false;
149149
}
150150
INFLUXDB_CLIENT_DEBUG("[D] setUrls\n");
@@ -186,13 +186,10 @@ bool InfluxDBClient::setUrls() {
186186
}
187187

188188
bool InfluxDBClient::setWriteOptions(WritePrecision precision, uint16_t batchSize, uint16_t bufferSize, uint16_t flushInterval, bool preserveConnection) {
189-
if(!_service && !init()) {
190-
return false;
191-
}
192189
if(!setWriteOptions(WriteOptions().writePrecision(precision).batchSize(batchSize).bufferSize(bufferSize).flushInterval(flushInterval))) {
193190
return false;
194191
}
195-
if(!setHTTPOptions(_service->getHTTPOptions().connectionReuse(preserveConnection))) {
192+
if(!setHTTPOptions(_connInfo.httpOptions.connectionReuse(preserveConnection))) {
196193
return false;
197194
}
198195
return true;
@@ -201,7 +198,7 @@ bool InfluxDBClient::setWriteOptions(WritePrecision precision, uint16_t batchSiz
201198
bool InfluxDBClient::setWriteOptions(const WriteOptions & writeOptions) {
202199
if(_writeOptions._writePrecision != writeOptions._writePrecision) {
203200
_writeOptions._writePrecision = writeOptions._writePrecision;
204-
if(!setUrls()) {
201+
if(_service && !setUrls()) {
205202
return false;
206203
}
207204
}
@@ -212,7 +209,8 @@ bool InfluxDBClient::setWriteOptions(const WriteOptions & writeOptions) {
212209
}
213210
if(writeOptions._bufferSize > 0 && _writeOptions._bufferSize != writeOptions._bufferSize) {
214211
_writeOptions._bufferSize = writeOptions._bufferSize;
215-
if(_writeOptions._bufferSize < 2*_writeOptions._batchSize) {
212+
// If retrying, we need space for at least two batches
213+
if(writeOptions._retryInterval && _writeOptions._bufferSize < 2*_writeOptions._batchSize) {
216214
_writeOptions._bufferSize = 2*_writeOptions._batchSize;
217215
INFLUXDB_CLIENT_DEBUG("[D] Changing buffer size to %d\n", _writeOptions._bufferSize);
218216
}
@@ -231,10 +229,10 @@ bool InfluxDBClient::setWriteOptions(const WriteOptions & writeOptions) {
231229
}
232230

233231
bool InfluxDBClient::setHTTPOptions(const HTTPOptions & httpOptions) {
234-
if(!_service && !init()) {
235-
return false;
232+
_connInfo.httpOptions = httpOptions;
233+
if(_service) {
234+
_service->setHTTPOptions();
236235
}
237-
_service->setHTTPOptions(httpOptions);
238236
return true;
239237
}
240238

src/Options.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,16 @@ WriteOptions& WriteOptions::addDefaultTag(const String &name, const String &valu
4141
delete [] s;
4242
return *this;
4343
}
44+
45+
void WriteOptions::printTo(Print &dest) const {
46+
dest.println("WriteOptions:");
47+
dest.print("\t_precision: "); dest.println((uint8_t)_writePrecision);
48+
dest.print("\t_batchSize: "); dest.println(_batchSize);
49+
dest.print("\t_bufferSize: "); dest.println(_bufferSize);
50+
dest.print("\t_flushInterval: "); dest.println(_flushInterval);
51+
dest.print("\t_retryInterval: "); dest.println(_retryInterval);
52+
dest.print("\t_maxRetryInterval: "); dest.println(_maxRetryInterval);
53+
dest.print("\t_maxRetryAttempts: "); dest.println(_maxRetryAttempts);
54+
dest.print("\t_defaultTags: "); dest.println(_defaultTags);
55+
dest.print("\t_useServerTimestamp: "); dest.println(_useServerTimestamp);
56+
}

src/Options.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ class WriteOptions {
101101
WriteOptions& clearDefaultTags() { _defaultTags = (char *)nullptr; return *this; }
102102
// If timestamp precision is set and useServerTimestamp is true, timestamp from point is not sent, or assigned.
103103
WriteOptions& useServerTimestamp(bool useServerTimestamp) { _useServerTimestamp = useServerTimestamp; return *this; }
104+
// prints options values to a Print device. E.g. opts.printTo(Serial);
105+
void printTo(Print &dest) const;
104106
};
105107

106108
/**

test/Test.cpp

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void Test::run() {
4444
testOldAPI();
4545
testBatch();
4646
testLineProtocol();
47-
testEcaping();
47+
testEscaping();
4848
testUrlEncode();
4949
testIsValidID();
5050
testFluxTypes();
@@ -56,7 +56,7 @@ void Test::run() {
5656
testFluxParserNilValue();
5757
testFluxParserMultiTables(false);
5858
testFluxParserMultiTables(true);
59-
testFluxParserErrorDiffentColumnsNum();
59+
testFluxParserErrorDifferentColumnsNum();
6060
testFluxParserFluxError();
6161
testFluxParserInvalidDatatype();
6262
testFluxParserMissingDatatype();
@@ -140,18 +140,17 @@ void Test::testOptions() {
140140
TEST_ASSERT(c._writeOptions._retryInterval == 5);
141141
TEST_ASSERT(c._writeOptions._maxRetryAttempts == 3);
142142
TEST_ASSERT(c._writeOptions._maxRetryInterval == 300);
143+
TEST_ASSERT(c._writeOptions._defaultTags == "");
144+
TEST_ASSERT(!c._writeOptions._useServerTimestamp);
143145
ConnectionInfo connInfo = {
144146
serverUrl: "http://localhost:8086",
145147
bucket: "",
146148
org: "",
147149
authToken: "my-token"
148150
};
149151
HTTPService s(&connInfo);
150-
TEST_ASSERT(!s._httpOptions._connectionReuse);
151-
TEST_ASSERT(s._httpOptions._httpReadTimeout == 5000);
152-
// Client has no params
153-
TEST_ASSERT(!c.setWriteOptions(defWO));
154-
TEST_ASSERT(c.getLastErrorMessage() == "Invalid parameters");
152+
TEST_ASSERT(!s.getHTTPOptions()._connectionReuse);
153+
TEST_ASSERT(s.getHTTPOptions()._httpReadTimeout == 5000);
155154
c.setConnectionParams("http://localhost:8086","my-org","my-bucket", "my-token");
156155

157156
TEST_ASSERT(c.setWriteOptions(defWO));
@@ -162,19 +161,21 @@ void Test::testOptions() {
162161
TEST_ASSERT(c._writeOptions._retryInterval == 1);
163162
TEST_ASSERT(c._writeOptions._maxRetryAttempts == 5);
164163
TEST_ASSERT(c._writeOptions._maxRetryInterval == 20);
165-
164+
TEST_ASSERT(c._writeOptions._defaultTags == "tag1=val1,tag2=val2");
165+
TEST_ASSERT(c._writeOptions._useServerTimestamp);
166166

167167
TEST_ASSERT(c.setHTTPOptions(defHO));
168-
TEST_ASSERT(c._service->_httpOptions._connectionReuse);
169-
TEST_ASSERT(c._service->_httpOptions._httpReadTimeout == 20000);
168+
TEST_ASSERT(c._service == nullptr);
169+
TEST_ASSERT(c._connInfo.httpOptions._connectionReuse);
170+
TEST_ASSERT(c._connInfo.httpOptions._httpReadTimeout == 20000);
170171

171172
c.setWriteOptions(WritePrecision::MS, 15, 14, 70, false);
172173
TEST_ASSERT(c._writeOptions._writePrecision == WritePrecision::MS);
173174
TEST_ASSERT(c._writeOptions._batchSize == 15);
174175
TEST_ASSERTM(c._writeOptions._bufferSize == 30, String(c._writeOptions._bufferSize));
175176
TEST_ASSERT(c._writeOptions._flushInterval == 70);
176-
TEST_ASSERT(!c._service->_httpOptions._connectionReuse);
177-
TEST_ASSERT(c._service->_httpOptions._httpReadTimeout == 20000);
177+
TEST_ASSERT(!c._connInfo.httpOptions._connectionReuse);
178+
TEST_ASSERT(c._connInfo.httpOptions._httpReadTimeout == 20000);
178179

179180
defWO = WriteOptions().batchSize(100).bufferSize(7000);
180181
c.setWriteOptions(defWO);
@@ -188,8 +189,8 @@ void Test::testOptions() {
188189
}
189190

190191

191-
void Test::testEcaping() {
192-
TEST_INIT("testEcaping");
192+
void Test::testEscaping() {
193+
TEST_INIT("testEscaping");
193194

194195
Point p("t\re=s\nt\t_t e\"s,t");
195196
p.addTag("ta=g","val=ue");
@@ -623,19 +624,22 @@ bool checkLinesParts(InfluxDBClient &client, size_t lineCount, int partCount) {
623624
void Test::testUseServerTimestamp() {
624625
TEST_INIT("testUseServerTimestamp");
625626

626-
InfluxDBClient client(Test::apiUrl, Test::orgName, Test::bucketName, Test::token);
627+
InfluxDBClient client;
627628

628629
TEST_ASSERT(waitServer(Test::managementUrl, true));
629630

630631
// test no precision, no timestamp
631632
Point *p = createPoint("test1");
632-
TEST_ASSERT(client.writePoint(*p));
633+
634+
// Test no precision, custom timestamp
635+
auto opts = WriteOptions().batchSize(1).bufferSize(10);
636+
TEST_ASSERT(client.setWriteOptions(opts));
637+
client.setConnectionParams(Test::apiUrl, Test::orgName, Test::bucketName, Test::token);
633638

639+
TEST_ASSERT(client.writePoint(*p));
634640
TEST_ASSERT(checkLinesParts(client, 1, 9));
635641

636-
// Test no precision, custom timestamp
637-
auto opts = WriteOptions().batchSize(2);
638-
client.setWriteOptions(opts);
642+
TEST_ASSERT(client.setWriteOptions(opts.batchSize(2)));
639643

640644
Point *dir = new Point("dir");
641645
dir->addTag("direction", "check-precision");
@@ -649,7 +653,7 @@ void Test::testUseServerTimestamp() {
649653
TEST_ASSERT(checkLinesParts(client, 1, 10));
650654

651655
// Test writerecitions + ts
652-
client.setWriteOptions(opts.writePrecision(WritePrecision::S));
656+
TEST_ASSERT(client.setWriteOptions(opts.writePrecision(WritePrecision::S)));
653657

654658
dir = new Point("dir");
655659
dir->addTag("direction", "check-precision");
@@ -660,7 +664,7 @@ void Test::testUseServerTimestamp() {
660664

661665
TEST_ASSERT(checkLinesParts(client, 1, 10));
662666
//test sending only precision
663-
client.setWriteOptions(opts.useServerTimestamp(true));
667+
TEST_ASSERT(client.setWriteOptions(opts.useServerTimestamp(true)));
664668

665669
TEST_ASSERT(client.writePoint(*dir));
666670
TEST_ASSERT(client.writePoint(*p));
@@ -2123,8 +2127,8 @@ void Test::testFluxParserMultiTables(bool chunked) {
21232127
TEST_END();
21242128
}
21252129

2126-
void Test::testFluxParserErrorDiffentColumnsNum() {
2127-
TEST_INIT("testFluxParserErrorDiffentColumnsNum");
2130+
void Test::testFluxParserErrorDifferentColumnsNum() {
2131+
TEST_INIT("testFluxParserErrorDifferentColumnsNum");
21282132
InfluxDBClient client(Test::apiUrl, Test::orgName, Test::bucketName, Test::token);
21292133
TEST_ASSERT(waitServer(Test::managementUrl, true));
21302134
FluxQueryResult flux = client.query("testquery-diffNum-data");

test/Test.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Test : public TestBase {
3939
private: // tests
4040
static void testUtils();
4141
static void testOptions();
42-
static void testEcaping();
42+
static void testEscaping();
4343
static void testPoint();
4444
static void testOldAPI();
4545
static void testBatch();
@@ -51,7 +51,7 @@ class Test : public TestBase {
5151
static void testFluxParserSingleTable();
5252
static void testFluxParserNilValue();
5353
static void testFluxParserMultiTables(bool chunked);
54-
static void testFluxParserErrorDiffentColumnsNum();
54+
static void testFluxParserErrorDifferentColumnsNum();
5555
static void testFluxParserFluxError();
5656
static void testFluxParserInvalidDatatype();
5757
static void testFluxParserMissingDatatype();

test/test.ino

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ void initInet() {
8181
while(!wifiOk && j<3) {
8282
Serial.print("Connecting to wifi " INFLUXDB_CLIENT_TESTING_SSID);
8383
WiFi.begin(INFLUXDB_CLIENT_TESTING_SSID, INFLUXDB_CLIENT_TESTING_PASS);
84+
#ifdef ARDUINO_LOLIN_C3_MINI
85+
// Necessary for Lolin C3 mini v1.0
86+
WiFi.setTxPower(WIFI_POWER_8_5dBm);
87+
#endif
8488
while ((WiFi.status() != WL_CONNECTED) && (i < 30)) {
8589
Serial.print(".");
8690
delay(300);

0 commit comments

Comments
 (0)