Add recursive rule MQTT subscribe support (#16943)

This commit is contained in:
Theo Arends 2023-01-07 17:31:10 +01:00
parent dad059737d
commit 95690ab1b9
2 changed files with 21 additions and 11 deletions

View File

@ -1429,11 +1429,6 @@ void Every250mSeconds(void)
// TasmotaGlobal.restart_flag = 2; // Restart anyway to keep memory clean webserver
MqttPublishPrefixTopicRulesProcess_P(STAT, PSTR(D_CMND_UPGRADE));
AllowInterrupts(1);
/*
#ifdef USE_COUNTER
CounterInterruptDisable(false);
#endif // USE_COUNTER
*/
}
}
break;

View File

@ -66,7 +66,15 @@
* RuleTimer2 100
\*********************************************************************************************/
#define XDRV_10 10
#define XDRV_10 10
#ifndef RULE_MAX_EVENTSZ
#define RULE_MAX_EVENTSZ 100
#endif
#ifndef RULE_MAX_MQTT_EVENTSZ
#define RULE_MAX_MQTT_EVENTSZ 256
#endif
//#define DEBUG_RULES
@ -186,7 +194,7 @@ struct RULES {
bool busy = false;
bool no_execute = false; // Don't actually execute rule commands
char event_data[256];
char event_data[RULE_MAX_EVENTSZ];
} Rules;
char rules_vars[MAX_RULE_VARS][33] = {{ 0 }};
@ -936,7 +944,7 @@ void RulesInit(void)
void RulesEvery50ms(void)
{
if ((Settings->rule_enabled || BERRY_RULES) && !Rules.busy) { // Any rule enabled
char json_event[300];
char json_event[RULE_MAX_EVENTSZ +16]; // Add 16 chars for {"Event": .. }
if (-1 == Rules.new_power) { Rules.new_power = TasmotaGlobal.power; }
if (Rules.new_power != Rules.old_power) {
@ -1152,9 +1160,8 @@ void RulesSetPower(void)
* true - The message is consumed.
* false - The message is not in our list.
*/
bool RulesMqttData(void)
{
if (XdrvMailbox.data_len < 1 || XdrvMailbox.data_len > 256) {
bool RulesMqttData(void) {
if ((XdrvMailbox.data_len < 1) || (XdrvMailbox.data_len > RULE_MAX_MQTT_EVENTSZ)) {
return false;
}
bool serviced = false;
@ -1163,6 +1170,7 @@ bool RulesMqttData(void)
//AddLog(LOG_LEVEL_DEBUG, PSTR("RUL: MQTT Topic %s, Event %s"), XdrvMailbox.topic, XdrvMailbox.data);
MQTT_Subscription event_item;
//Looking for matched topic
char json_event[RULE_MAX_MQTT_EVENTSZ +32]; // Add chars for {"Event":{"<item.Event>": .. }
for (uint32_t index = 0; index < subscriptions.size(); index++) {
String sData = buData;
@ -1201,8 +1209,15 @@ bool RulesMqttData(void)
}
}
value.trim();
/*
//Create an new event. Cannot directly call RulesProcessEvent().
snprintf_P(Rules.event_data, sizeof(Rules.event_data), PSTR("%s=%s"), event_item.Event.c_str(), value.c_str());
// 20230107 Superseded by the following code
*/
bool quotes = (value[0] != '{');
snprintf_P(json_event, sizeof(json_event), PSTR("{\"Event\":{\"%s\":%s%s%s}}"), event_item.Event.c_str(), (quotes)?"\"":"", value.c_str(), (quotes)?"\"":"");
RulesProcessEvent(json_event);
}
}
return serviced;