From 1829df6d5637cb190111bf19432871f649b6de53 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sun, 4 Apr 2021 12:04:36 +0200 Subject: [PATCH] Unify rules process call Unify rules process call (#11572) --- tasmota/support_command.ino | 2 +- tasmota/support_rotary.ino | 2 +- tasmota/support_tasmota.ino | 10 +++------- tasmota/xdrv_01_webserver.ino | 2 +- tasmota/xdrv_02_mqtt.ino | 2 +- tasmota/xdrv_09_timers.ino | 2 +- tasmota/xdrv_10_rules.ino | 9 ++------- tasmota/xdrv_10_scripter.ino | 16 +++++++++------- tasmota/xdrv_16_tuyamcu.ino | 2 +- tasmota/xdrv_20_hue.ino | 2 +- tasmota/xdrv_23_zigbee_2a_devices_impl.ino | 2 +- tasmota/xdrv_23_zigbee_8_parsers.ino | 2 +- tasmota/xdrv_27_shutter.ino | 6 +++--- tasmota/xdrv_35_pwm_dimmer.ino | 2 +- tasmota/xdrv_44_miel_hvac.ino | 4 ++-- tasmota/xdrv_52_9_berry.ino | 14 +++++++------- tasmota/xdrv_interface.ino | 5 +++-- tasmota/xsns_02_analog.ino | 4 ++-- tasmota/xsns_12_ads1115.ino | 2 +- tasmota/xsns_60_GPS.ino | 4 +--- tasmota/xsns_61_MI_NRF24.ino | 6 ++---- tasmota/xsns_62_MI_HM10.ino | 8 +++----- tasmota/xsns_62_esp32_mi.ino | 8 +++----- tasmota/xsns_62_esp32_mi_ble.ino | 10 +++------- tasmota/xsns_68_windmeter.ino | 4 +--- 25 files changed, 55 insertions(+), 75 deletions(-) diff --git a/tasmota/support_command.ino b/tasmota/support_command.ino index f67352ef5..0d54e6820 100644 --- a/tasmota/support_command.ino +++ b/tasmota/support_command.ino @@ -623,7 +623,7 @@ void CmndStatus(void) #endif if (payload) { - XdrvRulesProcess(); // Allow rule processing on single Status command only + XdrvRulesProcess(0); // Allow rule processing on single Status command only } ResponseClear(); diff --git a/tasmota/support_rotary.ino b/tasmota/support_rotary.ino index fa17867c2..d8d106ab1 100644 --- a/tasmota/support_rotary.ino +++ b/tasmota/support_rotary.ino @@ -250,7 +250,7 @@ void RotaryHandler(void) { Encoder[index].abs_position[button_pressed] = Settings.param[P_ROTARY_MAX_STEP]; // SetOption43 - Rotary steps } Response_P(PSTR("{\"Rotary%d\":{\"Pos1\":%d,\"Pos2\":%d}}"), index +1, Encoder[index].abs_position[0], Encoder[index].abs_position[1]); - XdrvRulesProcess(); + XdrvRulesProcess(0); #ifdef USE_LIGHT } #endif // USE_LIGHT diff --git a/tasmota/support_tasmota.ino b/tasmota/support_tasmota.ino index 98e8a4134..49bd70d28 100644 --- a/tasmota/support_tasmota.ino +++ b/tasmota/support_tasmota.ino @@ -550,7 +550,7 @@ bool SendKey(uint32_t key, uint32_t device, uint32_t state) result = !Settings.flag3.button_switch_force_local; // SetOption61 - Force local operation when button/switch topic is set } else { Response_P(PSTR("{\"%s%d\":{\"State\":%d}}"), (key) ? PSTR("Switch") : PSTR("Button"), device, state); - result = XdrvRulesProcess(); + result = XdrvRulesProcess(0); } #ifdef USE_PWM_DIMMER if (PWM_DIMMER != TasmotaGlobal.module_type || !result) { @@ -786,9 +786,7 @@ void MqttPublishTeleState(void) DTVarsTeleperiod(); #endif // USE_DT_VARS -#if defined(USE_RULES) || defined(USE_SCRIPT) - RulesTeleperiod(); // Allow rule based HA messages -#endif // USE_SCRIPT + XdrvRulesProcess(1); // Allow rule based HA messages } void TempHumDewShow(bool json, bool pass_on, const char *types, float f_temperature, float f_humidity) @@ -945,9 +943,7 @@ void PerformEverySecond(void) ResponseClear(); if (MqttShowSensor()) { MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_SENSOR), Settings.flag.mqtt_sensor_retain); // CMND_SENSORRETAIN -#if defined(USE_RULES) || defined(USE_SCRIPT) - RulesTeleperiod(); // Allow rule based HA messages -#endif // USE_RULES + XdrvRulesProcess(1); // Allow rule based HA messages } XsnsCall(FUNC_AFTER_TELEPERIOD); diff --git a/tasmota/xdrv_01_webserver.ino b/tasmota/xdrv_01_webserver.ino index f83e931a5..a1c1f6712 100644 --- a/tasmota/xdrv_01_webserver.ino +++ b/tasmota/xdrv_01_webserver.ino @@ -2898,7 +2898,7 @@ int WebSend(char *buffer) extern uint8_t tasm_cmd_activ; // recursive call must be possible in this case tasm_cmd_activ=0; - XdrvRulesProcess(); + XdrvRulesProcess(0); #endif // USE_SCRIPT #endif // USE_WEBSEND_RESPONSE } diff --git a/tasmota/xdrv_02_mqtt.ino b/tasmota/xdrv_02_mqtt.ino index 2625bf04e..88fd717df 100644 --- a/tasmota/xdrv_02_mqtt.ino +++ b/tasmota/xdrv_02_mqtt.ino @@ -424,7 +424,7 @@ void MqttPublishPrefixTopic_P(uint32_t prefix, const char* subtopic) { void MqttPublishPrefixTopicRulesProcess_P(uint32_t prefix, const char* subtopic, bool retained) { MqttPublishPrefixTopic_P(prefix, subtopic, retained); - XdrvRulesProcess(); + XdrvRulesProcess(0); } void MqttPublishPrefixTopicRulesProcess_P(uint32_t prefix, const char* subtopic) { diff --git a/tasmota/xdrv_09_timers.ino b/tasmota/xdrv_09_timers.ino index 81b454cf5..113c7f1c6 100644 --- a/tasmota/xdrv_09_timers.ino +++ b/tasmota/xdrv_09_timers.ino @@ -286,7 +286,7 @@ void TimerEverySecond(void) #if defined(USE_RULES) || defined(USE_SCRIPT) if (POWER_BLINK == xtimer.power) { // Blink becomes Rule disregarding device and allowing use of Backlog commands Response_P(PSTR("{\"Clock\":{\"Timer\":%d}}"), i +1); - XdrvRulesProcess(); + XdrvRulesProcess(0); } else #endif // USE_RULES if (TasmotaGlobal.devices_present) { ExecuteCommandPower(xtimer.device +1, xtimer.power, SRC_TIMER); } diff --git a/tasmota/xdrv_10_rules.ino b/tasmota/xdrv_10_rules.ino index b4e21e578..46bc5a607 100644 --- a/tasmota/xdrv_10_rules.ino +++ b/tasmota/xdrv_10_rules.ino @@ -1033,13 +1033,6 @@ void RulesSetPower(void) Rules.new_power = XdrvMailbox.index; } -void RulesTeleperiod(void) -{ - Rules.teleperiod = true; - RulesProcess(); - Rules.teleperiod = false; -} - #ifdef SUPPORT_MQTT_EVENT /********************************************************************************************/ /* @@ -2347,7 +2340,9 @@ bool Xdrv10(uint8_t function) result = DecodeCommand(kRulesCommands, RulesCommand); break; case FUNC_RULES_PROCESS: + Rules.teleperiod = (XdrvMailbox.index); // Signal teleperiod event result = RulesProcess(); + Rules.teleperiod = false; break; case FUNC_SAVE_BEFORE_RESTART: RulesSaveBeforeRestart(); diff --git a/tasmota/xdrv_10_scripter.ino b/tasmota/xdrv_10_scripter.ino index 61252cd11..7edd081d8 100755 --- a/tasmota/xdrv_10_scripter.ino +++ b/tasmota/xdrv_10_scripter.ino @@ -505,10 +505,6 @@ void ScriptEverySecond(void) { } } -void RulesTeleperiod(void) { - if (bitRead(Settings.rule_enabled, 0) && TasmotaGlobal.mqtt_data[0]) Run_Scripter(">T", 2, TasmotaGlobal.mqtt_data); -} - void SetChanged(uint32_t index) { glob_script_mem.type[index].bits.changed = 1; #ifdef USE_HOMEKIT @@ -7838,8 +7834,14 @@ bool Xdrv10(uint8_t function) break; case FUNC_RULES_PROCESS: if (bitRead(Settings.rule_enabled, 0)) { - Run_Scripter(">E", 2, TasmotaGlobal.mqtt_data); - result = glob_script_mem.event_handeled; + if (XdrvMailbox.index) { // Signal teleperiod event + if (TasmotaGlobal.mqtt_data[0]) { + Run_Scripter(">T", 2, TasmotaGlobal.mqtt_data); + } + } else { + Run_Scripter(">E", 2, TasmotaGlobal.mqtt_data); + result = glob_script_mem.event_handeled; + } } break; #ifdef USE_WEBSERVER @@ -7873,7 +7875,7 @@ bool Xdrv10(uint8_t function) Webserver->on("/exs", HTTP_GET, ScriptExecuteUploadSuccess); #endif // USE_WEBSERVER break; - + case FUNC_SAVE_BEFORE_RESTART: if (bitRead(Settings.rule_enabled, 0)) { Run_Scripter(">R", 2, 0); diff --git a/tasmota/xdrv_16_tuyamcu.ino b/tasmota/xdrv_16_tuyamcu.ino index 8e03f2faf..ddd57415b 100644 --- a/tasmota/xdrv_16_tuyamcu.ino +++ b/tasmota/xdrv_16_tuyamcu.ino @@ -1190,7 +1190,7 @@ void TuyaSerialInput(void) } else { AddLog_P(LOG_LEVEL_DEBUG, TasmotaGlobal.mqtt_data); } - XdrvRulesProcess(); + XdrvRulesProcess(0); if (dpId != 0 && Settings.tuyamcu_topic) { // Publish a /STAT Topic ready to use for any home automation system if (!Tuya.SuspendTopic) { diff --git a/tasmota/xdrv_20_hue.ino b/tasmota/xdrv_20_hue.ino index 8c50b5d37..a38e67a23 100644 --- a/tasmota/xdrv_20_hue.ino +++ b/tasmota/xdrv_20_hue.ino @@ -893,7 +893,7 @@ void HueLightsCommand(uint8_t device, uint32_t device_id, String &response) { } else { MqttPublishPrefixTopic_P(RESULT_OR_STAT, PSTR(D_CMND_DIMMER)); } - XdrvRulesProcess(); + XdrvRulesProcess(0); } change = false; } diff --git a/tasmota/xdrv_23_zigbee_2a_devices_impl.ino b/tasmota/xdrv_23_zigbee_2a_devices_impl.ino index 1c2b04b63..38ab5158b 100644 --- a/tasmota/xdrv_23_zigbee_2a_devices_impl.ino +++ b/tasmota/xdrv_23_zigbee_2a_devices_impl.ino @@ -583,7 +583,7 @@ void Z_Device::jsonPublishAttrList(const char * json_prefix, const Z_attribute_l } else { MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_SENSOR), Settings.flag.mqtt_sensor_retain); } - XdrvRulesProcess(); // apply rules + XdrvRulesProcess(0); // apply rules } void Z_Devices::jsonPublishFlush(uint16_t shortaddr) { diff --git a/tasmota/xdrv_23_zigbee_8_parsers.ino b/tasmota/xdrv_23_zigbee_8_parsers.ino index 0a5fe2e2d..63ef02c83 100644 --- a/tasmota/xdrv_23_zigbee_8_parsers.ino +++ b/tasmota/xdrv_23_zigbee_8_parsers.ino @@ -786,7 +786,7 @@ int32_t Z_ReceiveSimpleDesc(int32_t res, const SBuffer &buf) { } ResponseAppend_P(PSTR("]}}")); MqttPublishPrefixTopic_P(RESULT_OR_TELE, PSTR(D_JSON_ZIGBEEZCL_RECEIVED)); - XdrvRulesProcess(); + XdrvRulesProcess(0); } // If tuya protocol, change the model information diff --git a/tasmota/xdrv_27_shutter.ino b/tasmota/xdrv_27_shutter.ino index 95c3b70e5..5c0ebcc5e 100644 --- a/tasmota/xdrv_27_shutter.ino +++ b/tasmota/xdrv_27_shutter.ino @@ -498,7 +498,7 @@ void ShutterUpdatePosition(void) if (Shutter[i].direction != 0) { if (!ShutterGlobal.start_reported) { ShutterReportPosition(true, i); - XdrvRulesProcess(); + XdrvRulesProcess(0); ShutterGlobal.start_reported = 1; } AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Time %d, cStop %d, cVelo %d, mVelo %d, aVelo %d, mRun %d, aPos %d, nStop %d, Trgt %d, mVelo %d, Dir %d"), @@ -523,7 +523,7 @@ void ShutterUpdatePosition(void) MqttPublish(stopic, Settings.flag.mqtt_power_retain); // CMND_POWERRETAIN ShutterReportPosition(true, i); TasmotaGlobal.rules_flag.shutter_moved = 1; - XdrvRulesProcess(); + XdrvRulesProcess(0); } } } @@ -543,7 +543,7 @@ void ShutterAllowPreStartProcedure(uint8_t i) uint32_t uptime_Local=0; AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Delay Start. var%d <99>=<%s>, max10s?"),i+i, rules_vars[i]); TasmotaGlobal.rules_flag.shutter_moving = 1; - XdrvRulesProcess(); + XdrvRulesProcess(0); uptime_Local = TasmotaGlobal.uptime; while (uptime_Local+10 > TasmotaGlobal.uptime && (String)rules_vars[i] == "99") { loop(); diff --git a/tasmota/xdrv_35_pwm_dimmer.ino b/tasmota/xdrv_35_pwm_dimmer.ino index d30e7cba1..76e8b458c 100644 --- a/tasmota/xdrv_35_pwm_dimmer.ino +++ b/tasmota/xdrv_35_pwm_dimmer.ino @@ -795,7 +795,7 @@ bool Xdrv35(uint8_t function) #ifdef USE_RULES sprintf(TasmotaGlobal.mqtt_data, PSTR("{\"Button%u\":{\"State\":3}}"), button_index + 1); Rules.no_execute = true; - if (!XdrvRulesProcess()) { + if (!XdrvRulesProcess(0)) { #endif // USE_RULES PWMDimmerHandleButton(button_index, true); button_held[button_index] = true; diff --git a/tasmota/xdrv_44_miel_hvac.ino b/tasmota/xdrv_44_miel_hvac.ino index 189b00d1a..12cecdd14 100644 --- a/tasmota/xdrv_44_miel_hvac.ino +++ b/tasmota/xdrv_44_miel_hvac.ino @@ -906,7 +906,7 @@ miel_hvac_publish_settings(struct miel_hvac_softc *sc) MqttPublishPrefixTopic_P(TELE, PSTR("HVACSettings")); - XdrvRulesProcess(); + XdrvRulesProcess(0); } static void @@ -948,7 +948,7 @@ miel_hvac_data_response(struct miel_hvac_softc *sc, ToHex_P((uint8_t *)d, sizeof(*d), hex, sizeof(hex))); MqttPublishPrefixTopic_P(TELE, PSTR("HVACData")); - XdrvRulesProcess(); + XdrvRulesProcess(0); } static void diff --git a/tasmota/xdrv_52_9_berry.ino b/tasmota/xdrv_52_9_berry.ino index 5f8f11218..f9994f271 100644 --- a/tasmota/xdrv_52_9_berry.ino +++ b/tasmota/xdrv_52_9_berry.ino @@ -77,7 +77,7 @@ extern "C" { /*********************************************************************************************\ * Handlers for Berry calls and async - * + * \*********************************************************************************************/ // // call a function (if exists) of type void -> void @@ -234,7 +234,7 @@ void BrReset(void) { int32_t ret_code1, ret_code2; bool berry_init_ok = false; - do { + do { berry.vm = be_vm_new(); /* create a virtual machine instance */ be_set_obs_hook(berry.vm, &BerryObservability); be_load_custom_libs(berry.vm); @@ -261,7 +261,7 @@ void BrReset(void) { } // AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_BERRY "Berry code ran, RAM used=%u"), be_gc_memcount(berry.vm)); be_pop(berry.vm, 1); - + if (be_top(berry.vm) > 0) { be_dumpstack(berry.vm); } @@ -381,7 +381,7 @@ void BrREPLRun(char * cmd) { char * cmd2 = (char*) malloc(cmd2_len); do { int32_t ret_code; - + snprintf_P(cmd2, cmd2_len, PSTR("return (%s)"), cmd); ret_code = be_loadbuffer(berry.vm, PSTR("input"), cmd2, strlen(cmd2)); // AddLog(LOG_LEVEL_INFO, PSTR(">>>> be_loadbuffer cmd2 '%s', ret=%i"), cmd2, ret_code); @@ -424,7 +424,7 @@ const char HTTP_SCRIPT_BERRY_CONSOLE[] PROGMEM = "var sn=0,id=0,ft,ltm=%d;" // Scroll position, Get most of weblog initially // Console command history "var hc=[],cn=0;" // hc = History commands, cn = Number of history being shown - + "function l(p){" // Console log and command service "var c,cc,o='';" "clearTimeout(lt);" @@ -515,7 +515,7 @@ const char HTTP_SCRIPT_BERRY_CONSOLE2[] PROGMEM = // "13==c&&(hc.length>19&&hc.pop(),hc.unshift(b.value),cn=0)" // Enter, 19 = Max number -1 of commands in history "});" "}" - "wl(h);"; // Add console command key eventlistener after name has been synced with id (= wl(jd)) + "wl(h);"; // Add console command key eventlistener after name has been synced with id (= wl(jd)) const char HTTP_BERRY_STYLE_CMND[] PROGMEM = "