InfluxDb allow all numeric data

This commit is contained in:
Theo Arends 2021-08-14 17:03:05 +02:00
parent 0f39fab3ae
commit 64c527e3c3

View File

@ -230,15 +230,22 @@ int InfluxDbPostData(const char *data) {
* Data preparation * Data preparation
\*********************************************************************************************/ \*********************************************************************************************/
char* InfluxDbMakeNumber(char* dest, const char* source) { char* InfluxDbNumber(char* alternative, const char* source) {
// Test for valid numeric data ('-.0123456789') or ON, OFF etc. as defined in kOptions
if (source != nullptr) {
char* out = (char*)source;
// Convert special text as found in kOptions to a number // Convert special text as found in kOptions to a number
// Like "OFF" -> 0, "ON" -> 1, "TOGGLE" -> 2 // Like "OFF" -> 0, "ON" -> 1, "TOGGLE" -> 2
int number = GetStateNumber(source); int number = GetStateNumber(source);
if (number >= 0) { if (number >= 0) {
itoa(number, dest, 10); itoa(number, alternative, 10);
return dest; out = alternative;
} }
return (char*)source; if (IsNumeric(out)) {
return out;
}
}
return nullptr;
} }
void InfluxDbProcessJson(void) { void InfluxDbProcessJson(void) {
@ -250,7 +257,7 @@ void InfluxDbProcessJson(void) {
JsonParser parser((char *)jsonStr.c_str()); JsonParser parser((char *)jsonStr.c_str());
JsonParserObject root = parser.getRootObject(); JsonParserObject root = parser.getRootObject();
if (root) { if (root) {
char number[32]; char number[12]; // '1' to '255'
char linebuf[128]; // 'temperature,device=demo,sensor=ds18b20,id=01144A0CB2AA value=26.44\n' char linebuf[128]; // 'temperature,device=demo,sensor=ds18b20,id=01144A0CB2AA value=26.44\n'
char sensor[64]; // 'ds18b20' char sensor[64]; // 'ds18b20'
char type[64]; // 'temperature' char type[64]; // 'temperature'
@ -268,10 +275,8 @@ void InfluxDbProcessJson(void) {
if (value2.isObject()) { if (value2.isObject()) {
JsonParserObject Object3 = value2.getObject(); JsonParserObject Object3 = value2.getObject();
for (auto key3 : Object3) { for (auto key3 : Object3) {
const char *value = key3.getValue().getStr(); const char* value = InfluxDbNumber(number, key3.getValue().getStr());
if (value != nullptr) { if (value != nullptr) {
value = InfluxDbMakeNumber(number, value);
if (IsNumeric(value)) {
// Level 3 // Level 3
LowerCase(sensor, key2.getStr()); LowerCase(sensor, key2.getStr());
LowerCase(type, key3.getStr()); LowerCase(type, key3.getStr());
@ -281,21 +286,17 @@ void InfluxDbProcessJson(void) {
data += linebuf; data += linebuf;
} }
} }
}
} else { } else {
// Level 2 // 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}, ... } // { ... "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}, ... }
bool isarray = value2.isArray(); bool isarray = value2.isArray();
const char *value = (isarray) ? (value2.getArray())[0].getStr() : value2.getStr(); const char* value = InfluxDbNumber(number, (isarray) ? (value2.getArray())[0].getStr() : value2.getStr());
if (value != nullptr) { if (value != nullptr) {
value = InfluxDbMakeNumber(number, value);
if (IsNumeric(value)) {
LowerCase(sensor, key1.getStr()); LowerCase(sensor, key1.getStr());
LowerCase(type, key2.getStr()); LowerCase(type, key2.getStr());
// AddLog(LOG_LEVEL_DEBUG, PSTR("IFX2: sensor %s (%s), type %s (%s)"), key1.getStr(), sensor, key2.getStr(), type); // AddLog(LOG_LEVEL_DEBUG, PSTR("IFX2: sensor %s (%s), type %s (%s)"), key1.getStr(), sensor, key2.getStr(), type);
if (strcmp(type, "totalstarttime") != 0) { // Not needed/wanted
if (strcmp(type, "id") == 0) { // Index for DS18B20 if (strcmp(type, "id") == 0) { // Index for DS18B20
snprintf_P(sensor_id, sizeof(sensor_id), PSTR(",id=%s"), value); snprintf_P(sensor_id, sizeof(sensor_id), PSTR(",id=%s"), value);
} else { } else {
@ -321,31 +322,20 @@ void InfluxDbProcessJson(void) {
} }
} }
} }
}
}
} else { } else {
// Level 1 // Level 1
// {"Time":"2021-08-13T14:15:56","Switch1":"ON","Switch2":"OFF", ... "TempUnit":"C"} // {"Time":"2021-08-13T14:15:56","Switch1":"ON","Switch2":"OFF", ... "TempUnit":"C"}
const char *value = value1.getStr(); const char* value = InfluxDbNumber(number, value1.getStr());
if (value != nullptr) { if (value != nullptr) {
value = InfluxDbMakeNumber(number, value);
if (IsNumeric(value)) {
LowerCase(type, key1.getStr()); LowerCase(type, key1.getStr());
// switch1,device=demo,sensor=device value=0
if (!((strcasecmp_P(type, PSTR(D_JSON_TIME)) == 0) || // No time,device=demo value=2021-08-11T09:46:29 // power1,device=demo,sensor=device value=1
(strcasecmp_P(type, PSTR(D_JSON_TEMPERATURE_UNIT)) == 0) || // No tempunit,device=demo value=C
(strcasecmp_P(type, PSTR(D_JSON_PRESSURE_UNIT)) == 0) ||
(strcasecmp_P(type, PSTR(D_JSON_SPEED_UNIT)) == 0)
)) {
// switch1,device=demo value=1
snprintf_P(linebuf, sizeof(linebuf), PSTR("%s,device=%s,sensor=device value=%s\n"), snprintf_P(linebuf, sizeof(linebuf), PSTR("%s,device=%s,sensor=device value=%s\n"),
type, TasmotaGlobal.mqtt_topic, value); type, TasmotaGlobal.mqtt_topic, value);
data += linebuf; data += linebuf;
} }
} }
} }
}
}
if (data.length() > 0 ) { if (data.length() > 0 ) {
// AddLog(LOG_LEVEL_DEBUG, PSTR("IFX: Sensor data:\n%s"), data.c_str()); // AddLog(LOG_LEVEL_DEBUG, PSTR("IFX: Sensor data:\n%s"), data.c_str());
InfluxDbPostData(data.c_str()); InfluxDbPostData(data.c_str());