-
-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve existing and add new persistence filters (#3642)
* Improve existing and add new persistence filters * include filter Signed-off-by: Jan N. Klug <github@klug.nrw>
- Loading branch information
Showing
13 changed files
with
503 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...ersistence/src/main/java/org/openhab/core/persistence/filter/PersistenceEqualsFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* Copyright (c) 2010-2023 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.core.persistence.filter; | ||
|
||
import java.util.Collection; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.openhab.core.items.Item; | ||
|
||
/** | ||
* The {@link PersistenceEqualsFilter} is a filter that allows only specific values to pass | ||
* <p /> | ||
* The filter returns {@code false} if the string representation of the item's state is not in the given list | ||
* | ||
* @author Jan N. Klug - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class PersistenceEqualsFilter extends PersistenceFilter { | ||
private final Collection<String> values; | ||
private final boolean inverted; | ||
|
||
public PersistenceEqualsFilter(String name, Collection<String> values, boolean inverted) { | ||
super(name); | ||
this.values = values; | ||
this.inverted = inverted; | ||
} | ||
|
||
public Collection<String> getValues() { | ||
return values; | ||
} | ||
|
||
public boolean getInverted() { | ||
return inverted; | ||
} | ||
|
||
@Override | ||
public boolean apply(Item item) { | ||
return values.contains(item.getState().toFullString()) != inverted; | ||
} | ||
|
||
@Override | ||
public void persisted(Item item) { | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("%s [name=%s, value=%s, inverted=]", getClass().getSimpleName(), getName(), values); | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
...rsistence/src/main/java/org/openhab/core/persistence/filter/PersistenceIncludeFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/** | ||
* Copyright (c) 2010-2023 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.core.persistence.filter; | ||
|
||
import java.math.BigDecimal; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.openhab.core.items.Item; | ||
import org.openhab.core.library.types.DecimalType; | ||
import org.openhab.core.library.types.QuantityType; | ||
import org.openhab.core.types.State; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* The {@link PersistenceIncludeFilter} is a filter that allows only specific values to pass | ||
* <p /> | ||
* The filter returns {@code false} if the string representation of the item's state is not in the given list | ||
* | ||
* @author Jan N. Klug - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class PersistenceIncludeFilter extends PersistenceFilter { | ||
private final Logger logger = LoggerFactory.getLogger(PersistenceIncludeFilter.class); | ||
|
||
private final BigDecimal lower; | ||
private final BigDecimal upper; | ||
private final String unit; | ||
private final boolean inverted; | ||
|
||
public PersistenceIncludeFilter(String name, BigDecimal lower, BigDecimal upper, String unit, boolean inverted) { | ||
super(name); | ||
this.lower = lower; | ||
this.upper = upper; | ||
this.unit = unit; | ||
this.inverted = inverted; | ||
} | ||
|
||
public BigDecimal getLower() { | ||
return lower; | ||
} | ||
|
||
public BigDecimal getUpper() { | ||
return upper; | ||
} | ||
|
||
public String getUnit() { | ||
return unit; | ||
} | ||
|
||
public boolean getInverted() { | ||
return inverted; | ||
} | ||
|
||
@Override | ||
public boolean apply(Item item) { | ||
State state = item.getState(); | ||
BigDecimal compareValue = null; | ||
if (state instanceof DecimalType decimalType) { | ||
compareValue = decimalType.toBigDecimal(); | ||
} else if (state instanceof QuantityType<?> quantityType) { | ||
if (!unit.isBlank()) { | ||
QuantityType<?> convertedQuantity = quantityType.toUnit(unit); | ||
if (convertedQuantity != null) { | ||
compareValue = convertedQuantity.toBigDecimal(); | ||
} | ||
} | ||
} | ||
if (compareValue == null) { | ||
logger.warn("Cannot compare {} to range {}{} - {}{} ", state, lower, unit, upper, unit); | ||
return true; | ||
} | ||
|
||
if (inverted) { | ||
logger.error("Compare {} {} to {} -> {}, {} -> {}", inverted, compareValue, lower, | ||
compareValue.compareTo(lower) <= 0, upper, compareValue.compareTo(upper) >= 0); | ||
|
||
return compareValue.compareTo(lower) <= 0 || compareValue.compareTo(upper) >= 0; | ||
} else { | ||
|
||
logger.error("Compare {} {} to {} -> {}, {} -> {}", inverted, compareValue, lower, | ||
compareValue.compareTo(lower) >= 0, upper, compareValue.compareTo(upper) <= 0); | ||
return compareValue.compareTo(lower) >= 0 && compareValue.compareTo(upper) <= 0; | ||
} | ||
} | ||
|
||
@Override | ||
public void persisted(Item item) { | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("%s [name=%s, lower=%s, upper=%s, unit=%s, inverted=%b]", getClass().getSimpleName(), | ||
getName(), lower, upper, unit, inverted); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.