Refactor trim

This commit is contained in:
Theo Arends 2021-08-16 11:11:46 +02:00
parent cb955762a6
commit 0d9678eef6
3 changed files with 12 additions and 13 deletions

View File

@ -316,7 +316,7 @@ float CharToFloat(const char *str)
char *pt = strbuf; char *pt = strbuf;
if (*pt == '\0') { return 0.0; } if (*pt == '\0') { return 0.0; }
while ((*pt != '\0') && isblank(*pt)) { pt++; } // Trim leading spaces while ((*pt != '\0') && isspace(*pt)) { pt++; } // Trim leading spaces
signed char sign = 1; signed char sign = 1;
if (*pt == '-') { sign = -1; } if (*pt == '-') { sign = -1; }
@ -529,12 +529,12 @@ bool IsNumeric(const char* value) {
return (*digit == '\0'); return (*digit == '\0');
} }
char* Trim(char* p) char* Trim(char* p) {
{ // Remove leading and trailing tab, \n, \v, \f, \r and space
if (*p != '\0') { if (*p != '\0') {
while ((*p != '\0') && isblank(*p)) { p++; } // Trim leading spaces while ((*p != '\0') && isspace(*p)) { p++; } // Trim leading spaces
char* q = p + strlen(p) -1; char* q = p + strlen(p) -1;
while ((q >= p) && isblank(*q)) { q--; } // Trim trailing spaces while ((q >= p) && isspace(*q)) { q--; } // Trim trailing spaces
q++; q++;
*q = '\0'; *q = '\0';
} }

View File

@ -84,7 +84,7 @@ struct {
String _serverUrl; // Connection info String _serverUrl; // Connection info
String _writeUrl; // Cached full write url String _writeUrl; // Cached full write url
String _lastErrorResponse; // Server reponse or library error message for last failed request String _lastErrorResponse; // Server reponse or library error message for last failed request
uint32_t _lastRequestTime = 0; // Last time in ms we made are a request to server uint32_t _lastRequestTime = 0; // Last time in ms we made a request to server
int interval = 0; int interval = 0;
int _lastStatusCode = 0; // HTTP status code of last request to server int _lastStatusCode = 0; // HTTP status code of last request to server
int _lastRetryAfter = 0; // Store retry timeout suggested by server after last request int _lastRetryAfter = 0; // Store retry timeout suggested by server after last request
@ -161,7 +161,7 @@ void InfluxDbAfterRequest(int expectedStatusCode, bool modifyLastConnStatus) {
IFDB._lastRequestTime = millis(); IFDB._lastRequestTime = millis();
// AddLog(LOG_LEVEL_DEBUG, PSTR("IFX: HTTP status code %d"), IFDB._lastStatusCode); // AddLog(LOG_LEVEL_DEBUG, PSTR("IFX: HTTP status code %d"), IFDB._lastStatusCode);
IFDB._lastRetryAfter = 0; IFDB._lastRetryAfter = 0;
if (IFDB._lastStatusCode >= 429) { //retryable server errors if (IFDB._lastStatusCode >= 429) { // Retryable server errors
if (IFDBhttpClient->hasHeader(RetryAfter)) { if (IFDBhttpClient->hasHeader(RetryAfter)) {
IFDB._lastRetryAfter = IFDBhttpClient->header(RetryAfter).toInt(); IFDB._lastRetryAfter = IFDBhttpClient->header(RetryAfter).toInt();
AddLog(LOG_LEVEL_DEBUG, PSTR("IFX: Reply after %d"), IFDB._lastRetryAfter); AddLog(LOG_LEVEL_DEBUG, PSTR("IFX: Reply after %d"), IFDB._lastRetryAfter);
@ -171,12 +171,12 @@ void InfluxDbAfterRequest(int expectedStatusCode, bool modifyLastConnStatus) {
IFDB._lastErrorResponse = ""; IFDB._lastErrorResponse = "";
if (IFDB._lastStatusCode != expectedStatusCode) { if (IFDB._lastStatusCode != expectedStatusCode) {
if (IFDB._lastStatusCode > 0) { if (IFDB._lastStatusCode > 0) {
IFDB._lastErrorResponse = IFDBhttpClient->getString(); IFDB._lastErrorResponse = IFDBhttpClient->getString(); // {"error":"database not found: \"db\""}\n
AddLog(LOG_LEVEL_INFO, PSTR("IFX: %s"), IFDB._lastErrorResponse.c_str()); // {"error":"database not found: \"db\""}
} else { } else {
IFDB._lastErrorResponse = IFDBhttpClient->errorToString(IFDB._lastStatusCode); IFDB._lastErrorResponse = IFDBhttpClient->errorToString(IFDB._lastStatusCode);
AddLog(LOG_LEVEL_INFO, PSTR("IFX: Error %s"), IFDB._lastErrorResponse.c_str());
} }
IFDB._lastErrorResponse.trim(); // Remove trailing \n
AddLog(LOG_LEVEL_INFO, PSTR("IFX: Error %s"), IFDB._lastErrorResponse.c_str());
} }
} }
@ -191,8 +191,6 @@ bool InfluxDbValidateConnection(void) {
if (1 == Settings->influxdb_version) { if (1 == Settings->influxdb_version) {
url += InfluxDbAuth(); url += InfluxDbAuth();
} }
// on version 1.8.9 /health works fine
// String url = IFDB._serverUrl + "/health";
AddLog(LOG_LEVEL_INFO, PSTR("IFX: Validating connection to %s"), url.c_str()); AddLog(LOG_LEVEL_INFO, PSTR("IFX: Validating connection to %s"), url.c_str());
if (!IFDBhttpClient->begin(*IFDBwifiClient, url)) { if (!IFDBhttpClient->begin(*IFDBwifiClient, url)) {
@ -221,6 +219,7 @@ int InfluxDbPostData(const char *data) {
return false; return false;
} }
Trim((char*)data); // Remove trailing \n
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("IFX: Sending\n%s"), data); AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("IFX: Sending\n%s"), data);
IFDBhttpClient->addHeader(F("Content-Type"), F("text/plain")); IFDBhttpClient->addHeader(F("Content-Type"), F("text/plain"));
InfluxDbBeforeRequest(); InfluxDbBeforeRequest();

View File

@ -1198,7 +1198,7 @@ double CharToDouble(const char *str)
strlcpy(strbuf, str, sizeof(strbuf)); strlcpy(strbuf, str, sizeof(strbuf));
char *pt = strbuf; char *pt = strbuf;
while ((*pt != '\0') && isblank(*pt)) { pt++; } // Trim leading spaces while ((*pt != '\0') && isspace(*pt)) { pt++; } // Trim leading spaces
signed char sign = 1; signed char sign = 1;
if (*pt == '-') { sign = -1; } if (*pt == '-') { sign = -1; }