Skip to content

Commit

Permalink
add persistence abstractions
Browse files Browse the repository at this point in the history
  • Loading branch information
querdenker2k committed Jan 2, 2025
1 parent 9820b47 commit 2fb9cf6
Show file tree
Hide file tree
Showing 25 changed files with 420 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public class JRuleEventHandler {
stateMapping.put(JRuleStringListValue.class, StringListType.class);
}

public static Class<? extends State> mapJRuleToOhType(Class<? extends JRuleValue> type) {
return stateMapping.get(type);
}

private static final String LOG_NAME_EVENT = "JRuleEvent";

private static volatile JRuleEventHandler instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,13 @@ public Optional<JRuleValue> deltaUntil(ZonedDateTime timestamp, @Nullable String
.map(v -> JRuleEventHandler.get().toValue(v));
}

@Override
public Optional<JRuleValue> deltaBetween(ZonedDateTime begin, ZonedDateTime end, @Nullable String serviceId) {
Item item = getItem(name);
return Optional.ofNullable(PersistenceExtensions.deltaBetween(item, begin, end, serviceId))
.map(v -> JRuleEventHandler.get().toValue(v));
}

@Deprecated
@Override
public Optional<JRuleDecimalValue> evolutionRate(ZonedDateTime timestamp, @Nullable String serviceId) {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
import java.util.Objects;

import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;

/**
* The {@link JRuleDateTimeValue}
*
* @author Joseph (Seaside) Hagberg - Initial contribution
*/
public class JRuleDateTimeValue implements JRuleValue {
public class JRuleDateTimeValue extends JRuleValueBase implements JRuleValue {
private final DateTimeType ohType;

public JRuleDateTimeValue(ZonedDateTime value) {
Expand All @@ -46,18 +45,8 @@ public ZonedDateTime getValue() {
}

@Override
public String stringValue() {
return this.ohType.toFullString();
}

@Override
public Command toOhCommand() {
return this.ohType;
}

@Override
public State toOhState() {
return this.ohType;
protected State getOhType() {
return ohType;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
import java.util.Objects;

import org.openhab.core.library.types.DecimalType;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;

/**
* The {@link JRuleDecimalValue}
*
* @author Joseph (Seaside) Hagberg - Initial contribution
*/
public class JRuleDecimalValue implements JRuleValue {
public class JRuleDecimalValue extends JRuleValueBase implements JRuleValue {
private final DecimalType ohType;

public JRuleDecimalValue(BigDecimal value) {
Expand All @@ -44,18 +43,8 @@ public BigDecimal getValue() {
}

@Override
public String stringValue() {
return this.ohType.toFullString();
}

@Override
public Command toOhCommand() {
return this.ohType;
}

@Override
public State toOhState() {
return this.ohType;
public State getOhType() {
return ohType;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.HSBType;
import org.openhab.core.library.types.PercentType;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;

/**
* The {@link JRuleHsbValue} JRule Command
*
* @author Joseph (Seaside) Hagberg - Initial contribution
*/
public class JRuleHsbValue implements JRuleValue {
public class JRuleHsbValue extends JRuleValueBase implements JRuleValue {
private final HSBType ohType;

public JRuleHsbValue(String value) {
Expand All @@ -41,21 +40,6 @@ public JRuleHsbValue(BigDecimal hue, BigDecimal saturation, BigDecimal brightnes
this.ohType = new HSBType(new DecimalType(hue), new PercentType(saturation), new PercentType(brightness));
}

@Override
public String stringValue() {
return this.ohType.toFullString();
}

@Override
public Command toOhCommand() {
return this.ohType;
}

@Override
public State toOhState() {
return this.ohType;
}

public BigDecimal getHue() {
return this.ohType.getHue().toBigDecimal();
}
Expand All @@ -68,6 +52,11 @@ public BigDecimal getSaturation() {
return this.ohType.getSaturation().toBigDecimal();
}

@Override
public State getOhType() {
return ohType;
}

@Override
public boolean equals(Object o) {
if (this == o)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public State toOhState() {
throw new IllegalStateException("not a state type");
}

@Override
public JRuleValue as(Class<? extends JRuleValue> target) {
throw new IllegalStateException("cannot cast");
}

@Override
public String toString() {
return ohType.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public State toOhState() {
throw new IllegalStateException("not a state type");
}

@Override
public JRuleValue as(Class<? extends JRuleValue> target) {
throw new IllegalStateException("cannot cast");
}

@Override
public String toString() {
return ohType.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ public enum JRuleOnOffValue implements JRuleValue {
}

public static JRuleOnOffValue getValueFromString(String value) {
switch (value) {
case "ON":
return ON;
case "OFF":
return OFF;
}
return null;
return switch (value) {
case "ON" -> ON;
case "OFF" -> OFF;
default -> null;
};
}

public static JRuleValue valueOf(boolean command) {
Expand All @@ -60,6 +58,11 @@ public State toOhState() {
return this.ohType;
}

@Override
public JRuleValue as(Class<? extends JRuleValue> target) {
throw new IllegalStateException("cannot cast");
}

@Override
public String toString() {
return ohType.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public State toOhState() {
return this.ohType;
}

@Override
public JRuleValue as(Class<? extends JRuleValue> target) {
throw new IllegalStateException("cannot cast");
}

@Override
public String toString() {
return ohType.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
import java.util.Objects;

import org.openhab.core.library.types.PercentType;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;

/**
* The {@link JRulePercentValue}
*
* @author Joseph (Seaside) Hagberg - Initial contribution
*/
public class JRulePercentValue implements JRuleValue {
public class JRulePercentValue extends JRuleValueBase implements JRuleValue {
private final PercentType ohType;

public JRulePercentValue(int value) {
Expand All @@ -43,21 +42,6 @@ public JRulePercentValue(boolean value) {
this.ohType = new PercentType(value ? 100 : 0);
}

@Override
public String stringValue() {
return this.ohType.toFullString();
}

@Override
public Command toOhCommand() {
return this.ohType;
}

@Override
public State toOhState() {
return this.ohType;
}

public double doubleValue() {
return this.ohType.doubleValue();
}
Expand All @@ -74,6 +58,11 @@ public long longValue() {
return this.ohType.longValue();
}

@Override
public State getOhType() {
return ohType;
}

@Override
public boolean equals(Object o) {
if (this == o)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public State toOhState() {
return this.ohType;
}

@Override
public JRuleValue as(Class<? extends JRuleValue> target) {
throw new IllegalStateException("cannot cast");
}

@Override
public String toString() {
return ohType.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@

import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.PointType;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;

/**
* The {@link JRuleStringValue} JRule Command
*
* @author Robert Delbrück - Initial contribution
*/
public class JRulePointValue implements JRuleValue {
public class JRulePointValue extends JRuleValueBase implements JRuleValue {
private final PointType ohType;

public JRulePointValue(BigDecimal latitude, BigDecimal longitude, BigDecimal altitude) {
Expand Down Expand Up @@ -61,18 +60,8 @@ public BigDecimal getAltitude() {
}

@Override
public String stringValue() {
return this.ohType.toFullString();
}

@Override
public Command toOhCommand() {
return this.ohType;
}

@Override
public State toOhState() {
return this.ohType;
public State getOhType() {
return ohType;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.Objects;

import org.openhab.core.library.types.QuantityType;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;

import tech.units.indriya.format.SimpleUnitFormat;
Expand All @@ -25,7 +24,7 @@
*
* @author Robert Delbrück - Initial contribution
*/
public class JRuleQuantityValue implements JRuleValue {
public class JRuleQuantityValue extends JRuleValueBase implements JRuleValue {
private final QuantityType<?> ohType;

public JRuleQuantityValue(String value) {
Expand All @@ -37,17 +36,7 @@ public JRuleQuantityValue(Number value, String unit) {
}

@Override
public String stringValue() {
return this.ohType.toFullString();
}

@Override
public Command toOhCommand() {
return this.ohType;
}

@Override
public State toOhState() {
public State getOhType() {
return ohType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
import java.util.Objects;

import org.openhab.core.library.types.RawType;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;

/**
* The {@link JRuleRawValue} JRule Command
*
* @author Arne Seime - Initial contribution
*/
public class JRuleRawValue implements JRuleValue {
public class JRuleRawValue extends JRuleValueBase implements JRuleValue {
private final RawType ohType;

public JRuleRawValue(String mimeType, byte[] data) {
Expand All @@ -43,18 +42,8 @@ public byte[] getData() {
}

@Override
public String stringValue() {
return this.ohType.toFullString();
}

@Override
public Command toOhCommand() {
throw new IllegalStateException("not a command type");
}

@Override
public State toOhState() {
return this.ohType;
public State getOhType() {
return ohType;
}

@Override
Expand Down
Loading

0 comments on commit 2fb9cf6

Please sign in to comment.