mirror of
https://github.com/arendst/Tasmota.git
synced 2025-07-27 12:46:34 +00:00
Add recursive rule MQTT subscribe support (#16943)
This commit is contained in:
parent
dad059737d
commit
95690ab1b9
@ -1429,11 +1429,6 @@ void Every250mSeconds(void)
|
|||||||
// TasmotaGlobal.restart_flag = 2; // Restart anyway to keep memory clean webserver
|
// TasmotaGlobal.restart_flag = 2; // Restart anyway to keep memory clean webserver
|
||||||
MqttPublishPrefixTopicRulesProcess_P(STAT, PSTR(D_CMND_UPGRADE));
|
MqttPublishPrefixTopicRulesProcess_P(STAT, PSTR(D_CMND_UPGRADE));
|
||||||
AllowInterrupts(1);
|
AllowInterrupts(1);
|
||||||
/*
|
|
||||||
#ifdef USE_COUNTER
|
|
||||||
CounterInterruptDisable(false);
|
|
||||||
#endif // USE_COUNTER
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -66,7 +66,15 @@
|
|||||||
* RuleTimer2 100
|
* 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
|
//#define DEBUG_RULES
|
||||||
|
|
||||||
@ -186,7 +194,7 @@ struct RULES {
|
|||||||
bool busy = false;
|
bool busy = false;
|
||||||
bool no_execute = false; // Don't actually execute rule commands
|
bool no_execute = false; // Don't actually execute rule commands
|
||||||
|
|
||||||
char event_data[256];
|
char event_data[RULE_MAX_EVENTSZ];
|
||||||
} Rules;
|
} Rules;
|
||||||
|
|
||||||
char rules_vars[MAX_RULE_VARS][33] = {{ 0 }};
|
char rules_vars[MAX_RULE_VARS][33] = {{ 0 }};
|
||||||
@ -936,7 +944,7 @@ void RulesInit(void)
|
|||||||
void RulesEvery50ms(void)
|
void RulesEvery50ms(void)
|
||||||
{
|
{
|
||||||
if ((Settings->rule_enabled || BERRY_RULES) && !Rules.busy) { // Any rule enabled
|
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 (-1 == Rules.new_power) { Rules.new_power = TasmotaGlobal.power; }
|
||||||
if (Rules.new_power != Rules.old_power) {
|
if (Rules.new_power != Rules.old_power) {
|
||||||
@ -1152,9 +1160,8 @@ void RulesSetPower(void)
|
|||||||
* true - The message is consumed.
|
* true - The message is consumed.
|
||||||
* false - The message is not in our list.
|
* false - The message is not in our list.
|
||||||
*/
|
*/
|
||||||
bool RulesMqttData(void)
|
bool RulesMqttData(void) {
|
||||||
{
|
if ((XdrvMailbox.data_len < 1) || (XdrvMailbox.data_len > RULE_MAX_MQTT_EVENTSZ)) {
|
||||||
if (XdrvMailbox.data_len < 1 || XdrvMailbox.data_len > 256) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
bool serviced = 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);
|
//AddLog(LOG_LEVEL_DEBUG, PSTR("RUL: MQTT Topic %s, Event %s"), XdrvMailbox.topic, XdrvMailbox.data);
|
||||||
MQTT_Subscription event_item;
|
MQTT_Subscription event_item;
|
||||||
//Looking for matched topic
|
//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++) {
|
for (uint32_t index = 0; index < subscriptions.size(); index++) {
|
||||||
|
|
||||||
String sData = buData;
|
String sData = buData;
|
||||||
@ -1201,8 +1209,15 @@ bool RulesMqttData(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
value.trim();
|
value.trim();
|
||||||
|
|
||||||
|
/*
|
||||||
//Create an new event. Cannot directly call RulesProcessEvent().
|
//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());
|
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;
|
return serviced;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user