Unify rules process call

Unify rules process call (#11572)
This commit is contained in:
Theo Arends 2021-04-04 12:04:36 +02:00
parent 6a167710bd
commit 1829df6d56
25 changed files with 55 additions and 75 deletions

View File

@ -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();

View File

@ -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

View File

@ -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);

View File

@ -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
}

View File

@ -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) {

View File

@ -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); }

View File

@ -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();

View File

@ -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);

View File

@ -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) {

View File

@ -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;
}

View File

@ -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) {

View File

@ -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

View File

@ -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();

View File

@ -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;

View File

@ -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

View File

@ -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 =
"<style>"
@ -710,7 +710,7 @@ bool Xdrv52(uint8_t function)
result = callBerryEventDispatcher(PSTR("cmd"), XdrvMailbox.topic, XdrvMailbox.index, XdrvMailbox.data);
}
break;
// Module specific events
case FUNC_EVERY_100_MSECOND:
callBerryEventDispatcher(PSTR("every_100ms"), nullptr, 0, nullptr);

View File

@ -1079,11 +1079,12 @@ void XsnsDriverState(void)
/*********************************************************************************************/
bool XdrvRulesProcess(void)
{
bool XdrvRulesProcess(bool teleperiod) {
XdrvMailbox.index = teleperiod; // Signal teleperiod event
#ifdef USE_BERRY
// events are passed to both Rules engine AND Berry engine
bool rule_handled = XdrvCallDriver(10, FUNC_RULES_PROCESS);
XdrvMailbox.index = teleperiod; // Signal teleperiod event
bool berry_handled = XdrvCallDriver(52, FUNC_RULES_PROCESS);
return rule_handled || berry_handled;
#else

View File

@ -291,7 +291,7 @@ void AdcEvery250ms(void) {
Adc[idx].last_value = new_value;
uint16_t value = Adc[idx].last_value / ANALOG_PERCENT;
Response_P(PSTR("{\"ANALOG\":{\"A%ddiv10\":%d}}"), idx + offset, (value > 99) ? 100 : value);
XdrvRulesProcess();
XdrvRulesProcess(0);
}
}
else if (ADC_JOY == Adc[idx].type) {
@ -300,7 +300,7 @@ void AdcEvery250ms(void) {
Adc[idx].last_value = new_value;
uint16_t value = new_value / Adc[idx].param1;
Response_P(PSTR("{\"ANALOG\":{\"Joy%s\":%d}}"), adc_idx, value);
XdrvRulesProcess();
XdrvRulesProcess(0);
} else {
Adc[idx].last_value = 0;
}

View File

@ -232,7 +232,7 @@ void AdsEvery250ms(void)
}
ResponseJsonEndEnd();
XdrvRulesProcess();
XdrvRulesProcess(0);
}
}

View File

@ -343,9 +343,7 @@ void UBXTriggerTele(void)
ResponseClear();
if (MqttShowSensor()) {
MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_SENSOR), Settings.flag.mqtt_sensor_retain);
#ifdef USE_RULES
RulesTeleperiod(); // Allow rule based HA messages
#endif // USE_RULES
XdrvRulesProcess(1); // Allow rule based HA messages
}
}

View File

@ -736,7 +736,7 @@ void MINRFbeaconCounter(void) {
RulesProcessEvent(stemp);
*/
Response_P(PSTR("{%s:{\"Beacon\":%u}}"), D_CMND_NRF, MINRF.beacon.time);
XdrvRulesProcess();
XdrvRulesProcess(0);
}
}
@ -1166,9 +1166,7 @@ void MINRFtriggerTele(void){
ResponseClear();
if (MqttShowSensor()) {
MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_SENSOR), Settings.flag.mqtt_sensor_retain);
#ifdef USE_RULES
RulesTeleperiod(); // Allow rule based HA messages
#endif // USE_RULES
XdrvRulesProcess(1); // Allow rule based HA messages
}
}

View File

@ -1466,7 +1466,7 @@ void HM10StatusInfo() {
RulesProcessEvent(stemp);
*/
Response_P(PSTR("{\"%s\":{\"found\":%u}}"), D_CMND_HM10, MIBLEsensors.size());
XdrvRulesProcess();
XdrvRulesProcess(0);
}
/**
@ -1493,7 +1493,7 @@ void HM10EverySecond(bool restart){
_activeBeacons++;
_beacon.time++;
Response_P(PSTR("{\"Beacon%u\":{\"Time\":%u}}"), _idx, _beacon.time);
XdrvRulesProcess();
XdrvRulesProcess(0);
}
if(_activeBeacons==0) HM10.mode.activeBeacon = 0;
@ -1568,9 +1568,7 @@ void HM10triggerTele(void){
ResponseClear();
if (MqttShowSensor()) {
MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_SENSOR), Settings.flag.mqtt_sensor_retain);
#ifdef USE_RULES
RulesTeleperiod(); // Allow rule based HA messages
#endif // USE_RULES
XdrvRulesProcess(1); // Allow rule based HA messages
}
}

View File

@ -749,9 +749,7 @@ void MI32triggerTele(void){
ResponseClear();
if (MqttShowSensor()) {
MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_SENSOR), Settings.flag.mqtt_sensor_retain);
#ifdef USE_RULES
RulesTeleperiod(); // Allow rule based HA messages
#endif // USE_RULES
XdrvRulesProcess(1); // Allow rule based HA messages
}
}
@ -762,7 +760,7 @@ void MI32triggerTele(void){
void MI32StatusInfo() {
MI32.mode.shallShowStatusInfo = 0;
Response_P(PSTR("{\"%s\":{\"found\":%u}}"), D_CMND_MI32, MIBLEsensors.size());
XdrvRulesProcess();
XdrvRulesProcess(0);
}
/*********************************************************************************************\
@ -1731,7 +1729,7 @@ void MI32EverySecond(bool restart){
_activeBeacons++;
_beacon.time++;
Response_P(PSTR("{\"Beacon%u\":{\"Time\":%u}}"), _idx, _beacon.time);
XdrvRulesProcess();
XdrvRulesProcess(0);
}
if(_activeBeacons==0) MI32.mode.activeBeacon = 0;

View File

@ -1490,7 +1490,7 @@ void MI32triggerTele(void){
void MI32StatusInfo() {
MI32.mode.shallShowStatusInfo = 0;
Response_P(PSTR("{\"%s\":{\"found\":%u}}"), D_CMND_MI32, MIBLEsensors.size());
XdrvRulesProcess();
XdrvRulesProcess(0);
}
/*********************************************************************************************\
@ -2704,9 +2704,7 @@ void MI32ShowSomeSensors(){
MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_SENSOR), Settings.flag.mqtt_sensor_retain);
//AddLog(LOG_LEVEL_DEBUG,PSTR("M32: %s: show some %d %s"),D_CMND_MI32, MI32.mqttCurrentSlot, TasmotaGlobal.mqtt_data);
#ifdef USE_RULES
RulesTeleperiod(); // Allow rule based HA messages
#endif // USE_RULES
XdrvRulesProcess(1); // Allow rule based HA messages
#ifdef USE_HOME_ASSISTANT
if(hass_mode==2){
@ -3095,9 +3093,7 @@ void MI32ShowTriggeredSensors(){
}
AddLog(LOG_LEVEL_DEBUG,PSTR("M32: %s: triggered %d %s"),D_CMND_MI32, sensor, TasmotaGlobal.mqtt_data);
#ifdef USE_RULES
RulesTeleperiod(); // Allow rule based HA messages
#endif // USE_RULES
XdrvRulesProcess(1); // Allow rule based HA messages
} else { // else don't and clear
ResponseClear();

View File

@ -277,9 +277,7 @@ void WindMeterTriggerTele(void)
ResponseClear();
if (MqttShowSensor()) {
MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_SENSOR), Settings.flag.mqtt_sensor_retain);
#ifdef USE_RULES
RulesTeleperiod(); // Allow rule based HA messages
#endif // USE_RULES
XdrvRulesProcess(1); // Allow rule based HA messages
}
}