This patch adds more robust value comparison in the rule engine (#1456)

Previously, the state values were compared only if the item state was instanceof DecimalType
which did not work for channels that had a dimension specifier, such as Type:Length. The itemState
instance was of type QuantityType<Quantity<BigDecimal>> and therefore was never compared in the rules

Signed-off-by: Sebastian Irimia <aisebastian@yahoo.com>
pull/1464/head
aisebastian 2020-05-04 22:58:39 +03:00 committed by GitHub
parent dbdc0a846b
commit aca4513601
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 8 deletions

View File

@ -92,6 +92,7 @@ public class ItemStateConditionHandler extends BaseConditionModuleHandler {
Item item = itemRegistry.getItem(itemName);
State compareState = TypeParser.parseState(item.getAcceptedDataTypes(), state);
State itemState = item.getState();
DecimalType decimalState = itemState.as(DecimalType.class);
logger.debug("ItemStateCondition '{}'checking if {} (State={}) {} {}", module.getId(), itemName, itemState,
operator, compareState);
switch (operator) {
@ -101,25 +102,26 @@ public class ItemStateConditionHandler extends BaseConditionModuleHandler {
case "!=":
return !itemState.equals(compareState);
case "<":
if (itemState instanceof DecimalType && compareState instanceof DecimalType) {
return ((DecimalType) itemState).compareTo((DecimalType) compareState) < 0;
if (null != decimalState && compareState instanceof DecimalType) {
return decimalState.compareTo((DecimalType) compareState) < 0;
}
break;
case "<=":
case "=<":
if (itemState instanceof DecimalType && compareState instanceof DecimalType) {
return ((DecimalType) itemState).compareTo((DecimalType) compareState) <= 0;
if (null != decimalState && compareState instanceof DecimalType) {
return decimalState.compareTo((DecimalType) compareState) <= 0;
}
break;
case ">":
if (itemState instanceof DecimalType && compareState instanceof DecimalType) {
return ((DecimalType) itemState).compareTo((DecimalType) compareState) > 0;
if (null != decimalState && compareState instanceof DecimalType) {
return decimalState.compareTo((DecimalType) compareState) > 0;
}
break;
case ">=":
case "=>":
if (itemState instanceof DecimalType && compareState instanceof DecimalType) {
return ((DecimalType) itemState).compareTo((DecimalType) compareState) >= 0;
if (null != decimalState && compareState instanceof DecimalType) {
return decimalState.compareTo((DecimalType) compareState) >= 0;
}
break;
default: