Add IPv4 DNS lookup to influxdb

- Add IPv4 DNS lookup to influxdb (#18015)
- Add response to influxdb send
This commit is contained in:
Theo Arends 2023-05-21 15:34:36 +02:00
parent a76ebaae48
commit ae518424bc
4 changed files with 31 additions and 7 deletions

View File

@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file.
### Breaking Changed ### Breaking Changed
### Changed ### Changed
- InfluxDb resolves DNS name before request (#18015)
### Fixed ### Fixed
- Shutter bootloop using more than 4 shutters (#18673) - Shutter bootloop using more than 4 shutters (#18673)

View File

@ -135,6 +135,7 @@ The latter links can be used for OTA upgrades too like ``OtaUrl https://ota.tasm
### Changed ### Changed
- IRremoteESP8266 library from v2.8.4 to v2.8.5 - IRremoteESP8266 library from v2.8.4 to v2.8.5
- ESP32 Framework (Core) from v2.0.7 to v2.0.9 - ESP32 Framework (Core) from v2.0.7 to v2.0.9
- InfluxDb resolves DNS name before request [#18015](https://github.com/arendst/Tasmota/issues/18015)
- Refactored Zero Cross Dimmer [#18481](https://github.com/arendst/Tasmota/issues/18481) - Refactored Zero Cross Dimmer [#18481](https://github.com/arendst/Tasmota/issues/18481)
- Energy power delta report delayed by two seconds allowing hardware to stabilize [#17751](https://github.com/arendst/Tasmota/issues/17751) - Energy power delta report delayed by two seconds allowing hardware to stabilize [#17751](https://github.com/arendst/Tasmota/issues/17751)

View File

@ -2371,7 +2371,8 @@ bool GetLog(uint32_t req_loglevel, uint32_t* index_p, char** entry_pp, size_t* l
} }
void AddLogData(uint32_t loglevel, const char* log_data, const char* log_data_payload = nullptr, const char* log_data_retained = nullptr) { void AddLogData(uint32_t loglevel, const char* log_data, const char* log_data_payload = nullptr, const char* log_data_retained = nullptr) {
if (loglevel > TasmotaGlobal.maxlog_level) { return; } // Ignore any logging when maxlog_level = 0 OR logging for levels equal or lower than maxlog_level
if (!TasmotaGlobal.maxlog_level || (loglevel > TasmotaGlobal.maxlog_level)) { return; }
// Store log_data in buffer // Store log_data in buffer
// To lower heap usage log_data_payload may contain the payload data from MqttPublishPayload() // To lower heap usage log_data_payload may contain the payload data from MqttPublishPayload()
// and log_data_retained may contain optional retained message from MqttPublishPayload() // and log_data_retained may contain optional retained message from MqttPublishPayload()

View File

@ -40,6 +40,7 @@
* IfxPeriod - Set Influxdb period. If not set (or 0), use Teleperiod * IfxPeriod - Set Influxdb period. If not set (or 0), use Teleperiod
* IfxSensor - Set Influxdb sensor logging off (0) or on (1) * IfxSensor - Set Influxdb sensor logging off (0) or on (1)
* IfxRP - Set Influxdb retention policy * IfxRP - Set Influxdb retention policy
* IfxLog - Set Influxdb logging level (4 = default)
* *
* The following triggers result in automatic influxdb numeric feeds without appended time: * The following triggers result in automatic influxdb numeric feeds without appended time:
* - this driver initiated state message * - this driver initiated state message
@ -73,7 +74,10 @@
#define INFLUXDB_BUCKET "db" // [IfxDatabase, IfxBucket] Influxdb v1 database or v2 bucket #define INFLUXDB_BUCKET "db" // [IfxDatabase, IfxBucket] Influxdb v1 database or v2 bucket
#endif #endif
#ifndef INFLUXDB_RP #ifndef INFLUXDB_RP
#define INFLUXDB_RP "" // [IfxRP] Influxdb v1 retention policy (blank is default, usually autogen infinite) #define INFLUXDB_RP "" // [IfxRP] Influxdb v1 retention policy (blank is default, usually autogen infinite)
#endif
#ifndef INFLUXDB_CONNECT_TIMEOUT
#define INFLUXDB_CONNECT_TIMEOUT 2000 // Default timeout in milliseconds
#endif #endif
static const char UninitializedMessage[] PROGMEM = "Unconfigured instance"; static const char UninitializedMessage[] PROGMEM = "Unconfigured instance";
@ -112,6 +116,19 @@ String InfluxDbAuth(void) {
return auth; return auth;
} }
bool InfluxDbHostByName(void) {
IPAddress ifdb_ip;
if (!WifiHostByName(SettingsText(SET_INFLUXDB_HOST), ifdb_ip)) {
AddLog(LOG_LEVEL_DEBUG, PSTR("IFX: Invalid ifxhost"));
return false;
}
IFDB._serverUrl = "http://";
IFDB._serverUrl += ifdb_ip.toString();
IFDB._serverUrl += ":";
IFDB._serverUrl += Settings->influxdb_port;
return true;
}
bool InfluxDbParameterInit(void) { bool InfluxDbParameterInit(void) {
if (strlen(SettingsText(SET_INFLUXDB_BUCKET)) == 0 || if (strlen(SettingsText(SET_INFLUXDB_BUCKET)) == 0 ||
(2 == Settings->influxdb_version && (strlen(SettingsText(SET_INFLUXDB_ORG)) == 0 || (2 == Settings->influxdb_version && (strlen(SettingsText(SET_INFLUXDB_ORG)) == 0 ||
@ -119,10 +136,7 @@ bool InfluxDbParameterInit(void) {
AddLog(LOG_LEVEL_DEBUG, PSTR("IFX: Invalid parameters")); AddLog(LOG_LEVEL_DEBUG, PSTR("IFX: Invalid parameters"));
return false; return false;
} }
IFDB._serverUrl = "http://"; if (!InfluxDbHostByName()) { return false; }
IFDB._serverUrl += SettingsText(SET_INFLUXDB_HOST);
IFDB._serverUrl += ":";
IFDB._serverUrl += Settings->influxdb_port;
IFDB._writeUrl = IFDB._serverUrl; IFDB._writeUrl = IFDB._serverUrl;
if (2 == Settings->influxdb_version) { if (2 == Settings->influxdb_version) {
@ -150,6 +164,9 @@ bool InfluxDbInit(void) {
IFDBhttpClient = new HTTPClient; IFDBhttpClient = new HTTPClient;
} }
IFDBhttpClient->setReuse(IFDB._connectionReuse); IFDBhttpClient->setReuse(IFDB._connectionReuse);
#ifdef ESP32
IFDBhttpClient->setConnectTimeout(INFLUXDB_CONNECT_TIMEOUT);
#endif // ESP32
char server[32]; char server[32];
snprintf_P(server, sizeof(server), PSTR("Tasmota/%s (%s)"), TasmotaGlobal.version, GetDeviceHardware().c_str()); snprintf_P(server, sizeof(server), PSTR("Tasmota/%s (%s)"), TasmotaGlobal.version, GetDeviceHardware().c_str());
IFDBhttpClient->setUserAgent(server); IFDBhttpClient->setUserAgent(server);
@ -186,6 +203,8 @@ void InfluxDbAfterRequest(int expectedStatusCode, bool modifyLastConnStatus) {
} }
IFDB._lastErrorResponse.trim(); // Remove trailing \n IFDB._lastErrorResponse.trim(); // Remove trailing \n
AddLog(LOG_LEVEL_INFO, PSTR("IFX: Error %s"), IFDB._lastErrorResponse.c_str()); AddLog(LOG_LEVEL_INFO, PSTR("IFX: Error %s"), IFDB._lastErrorResponse.c_str());
} else {
AddLog(IFDB.log_level, PSTR("IFX: Done"));
} }
} }
@ -196,6 +215,8 @@ bool InfluxDbValidateConnection(void) {
return false; return false;
} }
// on version 1.x /ping will by default return status code 204, without verbose // on version 1.x /ping will by default return status code 204, without verbose
if (!InfluxDbHostByName()) { return false; }
String url = IFDB._serverUrl + (2 == Settings->influxdb_version ? "/health" : "/ping?verbose=true"); String url = IFDB._serverUrl + (2 == Settings->influxdb_version ? "/health" : "/ping?verbose=true");
if (1 == Settings->influxdb_version) { if (1 == Settings->influxdb_version) {
url += InfluxDbAuth(); url += InfluxDbAuth();
@ -433,7 +454,7 @@ void InfluxDbLoop(void) {
#define D_CMND_INFLUXDBBUCKET "Bucket" #define D_CMND_INFLUXDBBUCKET "Bucket"
#define D_CMND_INFLUXDBPERIOD "Period" #define D_CMND_INFLUXDBPERIOD "Period"
#define D_CMND_INFLUXDBSENSOR "Sensor" #define D_CMND_INFLUXDBSENSOR "Sensor"
#define D_CMND_INFLUXDBRP "RP" #define D_CMND_INFLUXDBRP "RP"
const char kInfluxDbCommands[] PROGMEM = D_PRFX_INFLUXDB "|" // Prefix const char kInfluxDbCommands[] PROGMEM = D_PRFX_INFLUXDB "|" // Prefix
"|" D_CMND_INFLUXDBLOG "|" "|" D_CMND_INFLUXDBLOG "|"