From 1c1ed580c5d4eb65113994be58c5b9556ba295d0 Mon Sep 17 00:00:00 2001 From: Arne Seime Date: Tue, 4 Jan 2022 20:37:47 +0100 Subject: [PATCH 1/3] Support for QuantityTypes (Number:XXX) --- .../internal/handler/JRuleEventHandler.java | 3 +++ .../automation/jrule/rules/JRuleEvent.java | 17 +++-------------- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleEventHandler.java b/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleEventHandler.java index 6e361e81..4ff150d9 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleEventHandler.java +++ b/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleEventHandler.java @@ -47,6 +47,7 @@ import org.openhab.core.library.types.OpenClosedType; import org.openhab.core.library.types.PercentType; import org.openhab.core.library.types.PlayPauseType; +import org.openhab.core.library.types.QuantityType; import org.openhab.core.library.types.StopMoveType; import org.openhab.core.library.types.StringType; import org.openhab.core.library.types.UpDownType; @@ -397,6 +398,8 @@ public Double getStateFromItemAsDouble(String name) { } if (state instanceof PercentType) { return ((PercentType) state).doubleValue(); + } else if (state instanceof QuantityType) { + return ((QuantityType) state).doubleValue(); } else { DecimalType decimalType = state.as(DecimalType.class); return decimalType != null ? decimalType.doubleValue() : null; diff --git a/src/main/java/org/openhab/automation/jrule/rules/JRuleEvent.java b/src/main/java/org/openhab/automation/jrule/rules/JRuleEvent.java index 91ad6229..d16b8ba0 100644 --- a/src/main/java/org/openhab/automation/jrule/rules/JRuleEvent.java +++ b/src/main/java/org/openhab/automation/jrule/rules/JRuleEvent.java @@ -15,6 +15,7 @@ import org.openhab.automation.jrule.rules.value.JRuleOnOffValue; import org.openhab.automation.jrule.rules.value.JRuleOpenClosedValue; import org.openhab.automation.jrule.rules.value.JRuleUpDownValue; +import org.openhab.core.library.types.QuantityType; /** * The {@link JRuleEvent} @@ -53,23 +54,11 @@ public JRuleUpDownValue getValueAsUpDownValue() { } public Double getValueAsDouble() { - Double d = null; - try { - d = Double.parseDouble(value); - } catch (NumberFormatException x) { - // ignore - } - return d; + return QuantityType.valueOf(value).doubleValue(); } public Integer getValueAsInteger() { - Integer i = null; - try { - i = Integer.parseInt(value); - } catch (NumberFormatException x) { - // ignore - } - return i; + return QuantityType.valueOf(value).intValue(); } public String getMemberName() { From c73da1d614b33691e81c015ab970adfbfc3d7959 Mon Sep 17 00:00:00 2001 From: Arne Seime Date: Tue, 4 Jan 2022 20:47:09 +0100 Subject: [PATCH 2/3] Add parsing exception handling --- .../automation/jrule/rules/JRuleEvent.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/openhab/automation/jrule/rules/JRuleEvent.java b/src/main/java/org/openhab/automation/jrule/rules/JRuleEvent.java index d16b8ba0..96ba03e7 100644 --- a/src/main/java/org/openhab/automation/jrule/rules/JRuleEvent.java +++ b/src/main/java/org/openhab/automation/jrule/rules/JRuleEvent.java @@ -16,6 +16,8 @@ import org.openhab.automation.jrule.rules.value.JRuleOpenClosedValue; import org.openhab.automation.jrule.rules.value.JRuleUpDownValue; import org.openhab.core.library.types.QuantityType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * The {@link JRuleEvent} @@ -24,6 +26,8 @@ */ public class JRuleEvent { + private static final Logger logger = LoggerFactory.getLogger(JRuleEvent.class); + private final String value; private String memberName; @@ -54,11 +58,21 @@ public JRuleUpDownValue getValueAsUpDownValue() { } public Double getValueAsDouble() { - return QuantityType.valueOf(value).doubleValue(); + try { + return QuantityType.valueOf(value).doubleValue(); + } catch (Exception e) { + logger.warn("Error converting {} to double: {}",value,e.getMessage()); + return null; + } } public Integer getValueAsInteger() { - return QuantityType.valueOf(value).intValue(); + try { + return QuantityType.valueOf(value).intValue(); + } catch (Exception e) { + logger.warn("Error converting {} to int: {}",value,e.getMessage()); + return null; + } } public String getMemberName() { From b8e8495d005f2f58fb220c847a5039fd83179e32 Mon Sep 17 00:00:00 2001 From: Arne Seime Date: Tue, 4 Jan 2022 21:17:56 +0100 Subject: [PATCH 3/3] Formatting --- .../java/org/openhab/automation/jrule/rules/JRuleEvent.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/openhab/automation/jrule/rules/JRuleEvent.java b/src/main/java/org/openhab/automation/jrule/rules/JRuleEvent.java index 96ba03e7..fe693695 100644 --- a/src/main/java/org/openhab/automation/jrule/rules/JRuleEvent.java +++ b/src/main/java/org/openhab/automation/jrule/rules/JRuleEvent.java @@ -59,9 +59,9 @@ public JRuleUpDownValue getValueAsUpDownValue() { public Double getValueAsDouble() { try { - return QuantityType.valueOf(value).doubleValue(); + return QuantityType.valueOf(value).doubleValue(); } catch (Exception e) { - logger.warn("Error converting {} to double: {}",value,e.getMessage()); + logger.warn("Error converting {} to double: {}", value, e.getMessage()); return null; } } @@ -70,7 +70,7 @@ public Integer getValueAsInteger() { try { return QuantityType.valueOf(value).intValue(); } catch (Exception e) { - logger.warn("Error converting {} to int: {}",value,e.getMessage()); + logger.warn("Error converting {} to int: {}", value, e.getMessage()); return null; } }