From 95690ab1b9b031ea40399cd50fbcf9a21930f372 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sat, 7 Jan 2023 17:31:10 +0100 Subject: [PATCH] Add recursive rule MQTT subscribe support (#16943) --- tasmota/tasmota_support/support_tasmota.ino | 5 ---- tasmota/tasmota_xdrv_driver/xdrv_10_rules.ino | 27 ++++++++++++++----- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/tasmota/tasmota_support/support_tasmota.ino b/tasmota/tasmota_support/support_tasmota.ino index f33390e97..85055969f 100644 --- a/tasmota/tasmota_support/support_tasmota.ino +++ b/tasmota/tasmota_support/support_tasmota.ino @@ -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; diff --git a/tasmota/tasmota_xdrv_driver/xdrv_10_rules.ino b/tasmota/tasmota_xdrv_driver/xdrv_10_rules.ino index f99856144..0454646a0 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_10_rules.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_10_rules.ino @@ -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":{"": .. } 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;