mirror of https://github.com/arendst/Tasmota.git
Add influxdb debug logging control
This commit is contained in:
parent
debef06413
commit
154928fe58
|
@ -88,6 +88,7 @@ struct {
|
|||
int interval = 0;
|
||||
int _lastStatusCode = 0; // HTTP status code of last request to server
|
||||
int _lastRetryAfter = 0; // Store retry timeout suggested by server after last request
|
||||
uint8_t log_level = LOG_LEVEL_DEBUG_MORE;
|
||||
bool _connectionReuse; // true if HTTP connection should be kept open. Usable for frequent writes. Default false
|
||||
bool init = false;
|
||||
} IFDB;
|
||||
|
@ -220,7 +221,7 @@ int InfluxDbPostData(const char *data) {
|
|||
}
|
||||
|
||||
Trim((char*)data); // Remove trailing \n
|
||||
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("IFX: Sending\n%s"), data);
|
||||
AddLog(IFDB.log_level, PSTR("IFX: Sending\n%s"), data);
|
||||
IFDBhttpClient->addHeader(F("Content-Type"), F("text/plain"));
|
||||
InfluxDbBeforeRequest();
|
||||
IFDB._lastStatusCode = IFDBhttpClient->POST((uint8_t*)data, strlen(data));
|
||||
|
@ -234,10 +235,12 @@ int InfluxDbPostData(const char *data) {
|
|||
* Data preparation
|
||||
\*********************************************************************************************/
|
||||
|
||||
char* InfluxDbNumber(char* alternative, const char* source) {
|
||||
char* InfluxDbNumber(char* alternative, JsonParserToken value) {
|
||||
if (value.isValid()) {
|
||||
char* source = (char*)value.getStr();
|
||||
// Test for valid numeric data ('-.0123456789') or ON, OFF etc. as defined in kOptions
|
||||
if (source != nullptr) {
|
||||
char* out = (char*)source;
|
||||
char* out = source;
|
||||
// Convert special text as found in kOptions to a number
|
||||
// Like "OFF" -> 0, "ON" -> 1, "TOGGLE" -> 2
|
||||
int number = GetStateNumber(source);
|
||||
|
@ -249,16 +252,19 @@ char* InfluxDbNumber(char* alternative, const char* source) {
|
|||
return out;
|
||||
}
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void InfluxDbProcessJson(void) {
|
||||
if (!IFDB.init) { return; }
|
||||
|
||||
// AddLog(LOG_LEVEL_DEBUG, PSTR("IFX: JSON %s"), ResponseData());
|
||||
AddLog(IFDB.log_level, PSTR("IFX: Process %s"), ResponseData());
|
||||
|
||||
// String jsonStr = ResponseData(); // Make a copy before use
|
||||
// JsonParser parser((char *)jsonStr.c_str());
|
||||
JsonParser parser((char *)ResponseData()); // Destroys ResponseData but saves heap space
|
||||
|
||||
String jsonStr = ResponseData();
|
||||
JsonParser parser((char *)jsonStr.c_str());
|
||||
JsonParserObject root = parser.getRootObject();
|
||||
if (root) {
|
||||
char number[12]; // '1' to '255'
|
||||
|
@ -279,8 +285,8 @@ void InfluxDbProcessJson(void) {
|
|||
if (value2.isObject()) {
|
||||
JsonParserObject Object3 = value2.getObject();
|
||||
for (auto key3 : Object3) {
|
||||
const char* value = InfluxDbNumber(number, key3.getValue().getStr());
|
||||
if (value != nullptr) {
|
||||
char* value = InfluxDbNumber(number, key3.getValue());
|
||||
if ((value != nullptr) && key2.isValid() && key3.isValid()) {
|
||||
// Level 3
|
||||
LowerCase(sensor, key2.getStr());
|
||||
LowerCase(type, key3.getStr());
|
||||
|
@ -293,16 +299,17 @@ void InfluxDbProcessJson(void) {
|
|||
} else {
|
||||
// Level 2
|
||||
// { ... "ANALOG":{"Temperature":184.72},"DS18B20":{"Id":"01144A0CB2AA","Temperature":24.88},"HTU21":{"Temperature":25.32,"Humidity":49.2,"DewPoint":13.88},"Global":{"Temperature":24.88,"Humidity":49.2,"DewPoint":13.47}, ... }
|
||||
if (!key1.isValid() || !value2.isValid()) { continue; }
|
||||
LowerCase(type, key2.getStr());
|
||||
bool is_id = (!strcmp_P(type, PSTR("id"))); // Index for DS18B20
|
||||
bool is_array = value2.isArray();
|
||||
const char* value = nullptr;
|
||||
char* value = nullptr;
|
||||
if (is_id && !is_array) {
|
||||
snprintf_P(sensor_id, sizeof(sensor_id), PSTR(",id=%s"), value2.getStr());
|
||||
} else {
|
||||
value = InfluxDbNumber(number, (is_array) ? (value2.getArray())[0].getStr() : value2.getStr());
|
||||
value = InfluxDbNumber(number, (is_array) ? (value2.getArray())[0] : value2);
|
||||
}
|
||||
if (value != nullptr) {
|
||||
if ((value != nullptr) && key2.isValid()) {
|
||||
LowerCase(sensor, key1.getStr());
|
||||
|
||||
// AddLog(LOG_LEVEL_DEBUG, PSTR("IFX2: sensor %s (%s), type %s (%s)"), key1.getStr(), sensor, key2.getStr(), type);
|
||||
|
@ -331,8 +338,8 @@ void InfluxDbProcessJson(void) {
|
|||
} else {
|
||||
// Level 1
|
||||
// {"Time":"2021-08-13T14:15:56","Switch1":"ON","Switch2":"OFF", ... "TempUnit":"C"}
|
||||
const char* value = InfluxDbNumber(number, value1.getStr());
|
||||
if (value != nullptr) {
|
||||
char* value = InfluxDbNumber(number, value1);
|
||||
if ((value != nullptr) && key1.isValid()) {
|
||||
LowerCase(type, key1.getStr());
|
||||
// switch1,device=demo,sensor=device value=0
|
||||
// power1,device=demo,sensor=device value=1
|
||||
|
@ -392,6 +399,7 @@ void InfluxDbLoop(void) {
|
|||
\*********************************************************************************************/
|
||||
|
||||
#define D_PRFX_INFLUXDB "Ifx"
|
||||
#define D_CMND_INFLUXDBLOG "Log"
|
||||
#define D_CMND_INFLUXDBHOST "Host"
|
||||
#define D_CMND_INFLUXDBPORT "Port"
|
||||
#define D_CMND_INFLUXDBUSER "User"
|
||||
|
@ -402,14 +410,14 @@ void InfluxDbLoop(void) {
|
|||
#define D_CMND_INFLUXDBBUCKET "Bucket"
|
||||
|
||||
const char kInfluxDbCommands[] PROGMEM = D_PRFX_INFLUXDB "|" // Prefix
|
||||
"|"
|
||||
"|" D_CMND_INFLUXDBLOG "|"
|
||||
D_CMND_INFLUXDBHOST "|" D_CMND_INFLUXDBPORT "|"
|
||||
D_CMND_INFLUXDBUSER "|" D_CMND_INFLUXDBORG "|"
|
||||
D_CMND_INFLUXDBPASSWORD "|" D_CMND_INFLUXDBTOKEN "|"
|
||||
D_CMND_INFLUXDBDATABASE "|" D_CMND_INFLUXDBBUCKET;
|
||||
|
||||
void (* const InfluxCommand[])(void) PROGMEM = {
|
||||
&CmndInfluxDbState,
|
||||
&CmndInfluxDbState, &CmndInfluxDbLog,
|
||||
&CmndInfluxDbHost, &CmndInfluxDbPort,
|
||||
&CmndInfluxDbUser, &CmndInfluxDbUser,
|
||||
&CmndInfluxDbPassword, &CmndInfluxDbPassword,
|
||||
|
@ -438,6 +446,13 @@ void CmndInfluxDbState(void) {
|
|||
}
|
||||
}
|
||||
|
||||
void CmndInfluxDbLog(void) {
|
||||
if ((XdrvMailbox.payload >= LOG_LEVEL_NONE) && (XdrvMailbox.payload <= LOG_LEVEL_DEBUG_MORE)) {
|
||||
IFDB.log_level = XdrvMailbox.payload;
|
||||
}
|
||||
ResponseCmndNumber(IFDB.log_level);
|
||||
}
|
||||
|
||||
void CmndInfluxDbHost(void) {
|
||||
if (XdrvMailbox.data_len > 0) {
|
||||
SettingsUpdateText(SET_INFLUXDB_HOST, (SC_CLEAR == Shortcut()) ? "" : (SC_DEFAULT == Shortcut()) ? PSTR(INFLUXDB_HOST) : XdrvMailbox.data);
|
||||
|
|
Loading…
Reference in New Issue