mirror of
https://github.com/arendst/Tasmota.git
synced 2025-07-23 10:46:31 +00:00
Refactor rules JSON decoding
Refactor rules JSON decoding allowing up to 10 subtypes deep
This commit is contained in:
parent
ddd1f4c379
commit
473299ffcd
@ -255,37 +255,42 @@ bool RulesRuleMatch(uint8_t rule_set, String &event, String &rule)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Step2: Search rule_task and rule_name
|
// Step2: Search rule_task and rule_name
|
||||||
StaticJsonBuffer<1024> jsonBuf;
|
int rule_name_idx = 0;
|
||||||
JsonObject &root = jsonBuf.parseObject(event);
|
if ((pos = rule_name.indexOf("[")) > 0) { // "SUBTYPE1#CURRENT[1]"
|
||||||
if (!root.success()) { return false; } // No valid JSON data
|
rule_name_idx = rule_name.substring(pos +1).toInt();
|
||||||
|
|
||||||
String rule_subfield = "";
|
|
||||||
const char* str_value;
|
|
||||||
if ((pos = rule_name.indexOf("[")) > 0) { // "CURRENT[1]"
|
|
||||||
int rule_name_idx = rule_name.substring(pos +1).toInt();
|
|
||||||
if ((rule_name_idx < 1) || (rule_name_idx > 6)) { // Allow indexes 1 to 6
|
if ((rule_name_idx < 1) || (rule_name_idx > 6)) { // Allow indexes 1 to 6
|
||||||
rule_name_idx = 1;
|
rule_name_idx = 1;
|
||||||
}
|
}
|
||||||
rule_name = rule_name.substring(0, pos); // "CURRENT"
|
rule_name = rule_name.substring(0, pos); // "SUBTYPE1#CURRENT"
|
||||||
str_value = root[rule_task][rule_name][rule_name_idx -1]; // "ENERGY" and "CURRENT" and 0
|
|
||||||
}
|
|
||||||
else if ((pos = rule_name.indexOf("#")) > 0) { // "VIBRATION_SENSOR#AQARACUBESIDE"
|
|
||||||
rule_subfield = rule_name.substring(pos +1); // "AQARACUBESIDE"
|
|
||||||
rule_name = rule_name.substring(0, pos); // "VIBRATION_SENSOR"
|
|
||||||
str_value = root[rule_task][rule_name][rule_subfield]; // "ZIGBEERECEIVED", "VIBRATION_SENSOR" and "AQARACUBESIDE"
|
|
||||||
} else {
|
|
||||||
str_value = root[rule_task][rule_name]; // "INA219" and "CURRENT"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//AddLog_P2(LOG_LEVEL_DEBUG, PSTR("RUL: Task %s, Name %s, Value |%s|, TrigCnt %d, TrigSt %d, Source %s, Json %s"),
|
StaticJsonBuffer<1024> jsonBuf;
|
||||||
// rule_task.c_str(), rule_name.c_str(), rule_svalue, Rules.trigger_count[rule_set], bitRead(Rules.triggers[rule_set], Rules.trigger_count[rule_set]), event.c_str(), (str_value) ? str_value : "none");
|
JsonObject &root = jsonBuf.parseObject(event);
|
||||||
|
if (!root.success()) { return false; } // No valid JSON data
|
||||||
|
if (!root[rule_task].success()) { return false; } // No rule_task in JSON data
|
||||||
|
|
||||||
if (rule_subfield.length()) {
|
JsonObject &obj1 = root[rule_task];
|
||||||
if (!root[rule_task][rule_name][rule_subfield].success()) { return false; }
|
JsonObject *obj = &obj1;
|
||||||
} else {
|
String subtype;
|
||||||
if (!root[rule_task][rule_name].success()) { return false; }
|
uint32_t i = 0;
|
||||||
|
while ((pos = rule_name.indexOf("#")) > 0) { // "SUBTYPE1#SUBTYPE2#CURRENT"
|
||||||
|
subtype = rule_name.substring(0, pos);
|
||||||
|
if (!(*obj)[subtype].success()) { return false; } // No subtype in JSON data
|
||||||
|
JsonObject &obj2 = (*obj)[subtype];
|
||||||
|
obj = &obj2;
|
||||||
|
rule_name = rule_name.substring(pos +1);
|
||||||
|
if (i++ > 10) { return false; } // Abandon possible loop
|
||||||
}
|
}
|
||||||
// No value but rule_name is ok
|
if (!(*obj)[rule_name].success()) { return false; } // No name in JSON data
|
||||||
|
const char* str_value;
|
||||||
|
if (rule_name_idx) {
|
||||||
|
str_value = (*obj)[rule_name][rule_name_idx -1]; // "CURRENT[1]"
|
||||||
|
} else {
|
||||||
|
str_value = (*obj)[rule_name]; // "CURRENT"
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("RUL: Task %s, Name %s, Value |%s|, TrigCnt %d, TrigSt %d, Source %s, Json %s"),
|
||||||
|
// rule_task.c_str(), rule_name.c_str(), rule_svalue, Rules.trigger_count[rule_set], bitRead(Rules.triggers[rule_set], Rules.trigger_count[rule_set]), event.c_str(), (str_value) ? str_value : "none");
|
||||||
|
|
||||||
Rules.event_value = str_value; // Prepare %value%
|
Rules.event_value = str_value; // Prepare %value%
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user