From 47ce7c731264a6c4ec23099a37131aaed129e093 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Thu, 21 Jan 2021 14:54:38 +0100 Subject: [PATCH] Add rule trigger string comparisons Add rule trigger string comparisons for EndsWith ``$>``, StartsWith ``$<`` and Contains ``$|`` (#10538) --- CHANGELOG.md | 1 + RELEASENOTES.md | 1 + tasmota/xdrv_10_rules.ino | 46 +++++++++++++++++++++++++++++---------- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a99f6ce5d..e8d7f6ced 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ All notable changes to this project will be documented in this file. - Support for SM2135 current selection using GPIO ``SM2135 DAT`` index (#10634) - Basic support for ESP32 M5stack core2 16MB binary tasmota32-core2.bin (#10635) - Support for Sugar Valley NeoPool Controller by Norbert Richter (#10637) +- Rule trigger string comparisons for EndsWith ``$>``, StartsWith ``$<`` and Contains ``$|`` (#10538) ### Breaking Changed - ESP32 switch from default SPIFFS to default LittleFS file system loosing current (zigbee) files diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 30513e317..5303ed3cd 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -86,6 +86,7 @@ The attached binaries can also be downloaded from http://ota.tasmota.com/tasmota - Support character `#` to be replaced by `space`-character in command ``Publish`` topic [#10258](https://github.com/arendst/Tasmota/issues/10258) - Basic support for ESP32 Odroid Go 16MB binary tasmota32-odroidgo.bin [#8630](https://github.com/arendst/Tasmota/issues/8630) - Basic support for ESP32 M5stack core2 16MB binary tasmota32-core2.bin [#10635](https://github.com/arendst/Tasmota/issues/10635) +- Rule trigger string comparisons for EndsWith ``$>``, StartsWith ``$<`` and Contains ``$|`` [#10538](https://github.com/arendst/Tasmota/issues/10538) - SPI display driver SSD1331 Color oled by Jeroen Vermeulen [#10376](https://github.com/arendst/Tasmota/issues/10376) - Compile time option ``USE_MQTT_TLS_DROP_OLD_FINGERPRINT`` to drop old (less secure) TLS fingerprint diff --git a/tasmota/xdrv_10_rules.ino b/tasmota/xdrv_10_rules.ino index 4a6d3c9c1..cff843d4a 100644 --- a/tasmota/xdrv_10_rules.ino +++ b/tasmota/xdrv_10_rules.ino @@ -87,17 +87,20 @@ #define D_JSON_INITIATED "Initiated" -#define COMPARE_OPERATOR_NONE -1 -#define COMPARE_OPERATOR_EQUAL 0 -#define COMPARE_OPERATOR_BIGGER 1 -#define COMPARE_OPERATOR_SMALLER 2 -#define COMPARE_OPERATOR_EXACT_DIVISION 3 -#define COMPARE_OPERATOR_NUMBER_EQUAL 4 -#define COMPARE_OPERATOR_NOT_EQUAL 5 -#define COMPARE_OPERATOR_BIGGER_EQUAL 6 -#define COMPARE_OPERATOR_SMALLER_EQUAL 7 -#define MAXIMUM_COMPARE_OPERATOR COMPARE_OPERATOR_SMALLER_EQUAL -const char kCompareOperators[] PROGMEM = "=\0>\0<\0|\0==!=>=<="; +#define COMPARE_OPERATOR_NONE -1 +#define COMPARE_OPERATOR_EQUAL 0 +#define COMPARE_OPERATOR_BIGGER 1 +#define COMPARE_OPERATOR_SMALLER 2 +#define COMPARE_OPERATOR_EXACT_DIVISION 3 +#define COMPARE_OPERATOR_NUMBER_EQUAL 4 +#define COMPARE_OPERATOR_NOT_EQUAL 5 +#define COMPARE_OPERATOR_BIGGER_EQUAL 6 +#define COMPARE_OPERATOR_SMALLER_EQUAL 7 +#define COMPARE_OPERATOR_STRING_ENDS_WITH 8 +#define COMPARE_OPERATOR_STRING_STARTS_WITH 9 +#define COMPARE_OPERATOR_STRING_CONTAINS 10 +#define MAXIMUM_COMPARE_OPERATOR COMPARE_OPERATOR_STRING_CONTAINS +const char kCompareOperators[] PROGMEM = "=\0>\0<\0|\0==!=>=<=$>$<$|"; #ifdef USE_EXPRESSION #include // Import LinkedList library @@ -416,7 +419,7 @@ bool RulesRuleMatch(uint8_t rule_set, String &event, String &rule, bool stop_all // Step1: Analyse rule String rule_expr = rule; // "TELE-INA219#CURRENT>0.100" if (Rules.teleperiod) { - int ppos = rule_expr.indexOf(F("TELE-")); // "TELE-INA219#CURRENT>0.100" or "INA219#CURRENT>0.100" + int ppos = rule_expr.indexOf(F("TELE-")); // "TELE-INA219#CURRENT>0.100" or "INA219#CURRENT>0.100" if (ppos == -1) { return false; } // No pre-amble in rule rule_expr = rule.substring(5); // "INA219#CURRENT>0.100" or "SYSTEM#BOOT" } @@ -553,6 +556,7 @@ bool RulesRuleMatch(uint8_t rule_set, String &event, String &rule, bool stop_all value = CharToFloat((char*)str_value); int int_value = int(value); int int_rule_value = int(rule_value); + String str_str_value = String(str_value); switch (compareOperator) { case COMPARE_OPERATOR_EXACT_DIVISION: match = (int_rule_value && (int_value % int_rule_value) == 0); @@ -578,6 +582,15 @@ bool RulesRuleMatch(uint8_t rule_set, String &event, String &rule, bool stop_all case COMPARE_OPERATOR_SMALLER_EQUAL: match = (value <= rule_value); break; + case COMPARE_OPERATOR_STRING_ENDS_WITH: + match = str_str_value.endsWith(rule_svalue); + break; + case COMPARE_OPERATOR_STRING_STARTS_WITH: + match = str_str_value.startsWith(rule_svalue); + break; + case COMPARE_OPERATOR_STRING_CONTAINS: + match = (str_str_value.indexOf(rule_svalue) > 0); + break; default: match = true; } @@ -1614,6 +1627,15 @@ bool evaluateComparisonExpression(const char *expression, int len) case COMPARE_OPERATOR_SMALLER_EQUAL: bResult = (leftValue <= rightValue); break; + case COMPARE_OPERATOR_STRING_ENDS_WITH: + bResult = leftExpr.endsWith(rightExpr); + break; + case COMPARE_OPERATOR_STRING_STARTS_WITH: + bResult = leftExpr.startsWith(rightExpr); + break; + case COMPARE_OPERATOR_STRING_CONTAINS: + bResult = (leftExpr.indexOf(rightExpr) > 0); + break; } return bResult; }