Fix subscribe same topic, multiple events

And even smaller by unstringifying
This commit is contained in:
Theo Arends 2024-01-23 18:17:42 +01:00
parent a7f3a7adad
commit dca33c10a3

View File

@ -1175,53 +1175,49 @@ bool RulesMqttData(void) {
return false; // Process unchanged data return false; // Process unchanged data
} }
bool serviced = false; bool serviced = false;
String buData = XdrvMailbox.data; // Distroyed by JsonParser. Could be very long SENSOR message String buData = XdrvMailbox.data; // Destroyed by JsonParser. Could be very long SENSOR message
char ctopic[strlen(XdrvMailbox.topic)+1]; char ctopic[strlen(XdrvMailbox.topic)+1];
strcpy(ctopic, XdrvMailbox.topic); // Distroyed by result of following iteration strcpy(ctopic, XdrvMailbox.topic); // Destroyed by result of following iteration
// Looking for matched topic for (auto &event_item : subscriptions) { // Looking for all matched topics
for (auto &event_item : subscriptions) {
char etopic[strlen(event_item.topic)+2]; char etopic[strlen(event_item.topic)+2];
strcpy(etopic, event_item.topic); strcpy(etopic, event_item.topic); // tele/tasmota/SENSOR
strcat(etopic, "/"); strcat(etopic, "/"); // tele/tasmota/SENSOR/
if ((strcmp(ctopic, event_item.topic) == 0) || // Equal if ((strcmp(ctopic, event_item.topic) == 0) || // Equal tele/tasmota/SENSOR
(strncmp(ctopic, etopic, strlen(etopic)) == 0)) { // StartsWith (strncmp(ctopic, etopic, strlen(etopic)) == 0)) { // StartsWith tele/tasmota/SENSOR/
// This topic is subscribed by us, so serve it serviced = true; // This topic is subscribed by us, so serve it
serviced = true; String sData = buData; // sData will be destroyed by JsonParser
String value; char* value = nullptr;
if (strlen(event_item.key) == 0) { // If did not specify Key if (strlen(event_item.key) == 0) { // If no key specified
value = buData; value = (char*)buData.c_str(); // {"DS18B20":{"Id":"0000048EC44C","Temperature":23.3}}
} else { // If specified Key, need to parse Key/Value from JSON data } else { // If key specified, need to parse Key/Value from JSON data
String sData = buData;
JsonParser parser((char*)sData.c_str()); JsonParser parser((char*)sData.c_str());
JsonParserObject jsonData = parser.getRootObject(); JsonParserObject jsonData = parser.getRootObject();
if (!jsonData) break; // Failed to parse JSON data, ignore this message. if (!jsonData) { break; } // Failed to parse JSON data, ignore this message.
String key1 = event_item.key; char ckey1[strlen(event_item.key)+1];
String key2; strcpy(ckey1, event_item.key); // DS18B20.Temperature
char* ckey2 = strchr(ckey1, '.');
int dot; if (ckey2 != nullptr) { // .Temperature
if ((dot = key1.indexOf('.')) > 0) { *ckey2++ = '\0'; // Temperature and ckey1 becomes DS18B20
key2 = key1.substring(dot+1); JsonParserToken val = jsonData[ckey1].getObject()[ckey2];
key1 = key1.substring(0, dot); if (val) {
JsonParserToken value_tok = jsonData[key1.c_str()].getObject()[key2.c_str()]; value = (char*)val.getStr(); // 23.3
if (!value_tok) break; // Failed to get the key/value, ignore this message. }
value = value_tok.getStr(); } else { // DS18B20
// if (!jsonData[key1][key2].success()) break; //Failed to get the key/value, ignore this message. JsonParserToken val = jsonData[ckey1];
// value = (const char *)jsonData[key1][key2]; if (val) {
} else { value = (char*)val.getStr(); // \0
JsonParserToken value_tok = jsonData[key1.c_str()]; }
if (!value_tok) break; // Failed to get the key/value, ignore this message.
value = value_tok.getStr();
// if (!jsonData[key1].success()) break;
// value = (const char *)jsonData[key1];
} }
} }
value.trim(); if (value) {
bool quotes = (value[0] != '{'); Trim(value);
Response_P(PSTR("{\"Event\":{\"%s\":%s%s%s}}"), event_item.event, (quotes)?"\"":"", value.c_str(), (quotes)?"\"":""); bool quotes = (value[0] != '{');
RulesProcessEvent(ResponseData()); Response_P(PSTR("{\"Event\":{\"%s\":%s%s%s}}"), event_item.event, (quotes)?"\"":"", value, (quotes)?"\"":"");
RulesProcessEvent(ResponseData());
}
} }
} }
return serviced; return serviced;