Skip to content

Commit

Permalink
Use TypeParser to get matching Item State (#10501)
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
  • Loading branch information
cweitkamp authored Apr 14, 2021
1 parent 70d6c89 commit e24d43a
Showing 1 changed file with 19 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,25 @@
import org.knowm.yank.Yank;
import org.openhab.core.items.GroupItem;
import org.openhab.core.items.Item;
import org.openhab.core.library.items.ColorItem;
import org.openhab.core.library.items.ContactItem;
import org.openhab.core.library.items.DateTimeItem;
import org.openhab.core.library.items.DimmerItem;
import org.openhab.core.library.items.ImageItem;
import org.openhab.core.library.items.NumberItem;
import org.openhab.core.library.items.PlayerItem;
import org.openhab.core.library.items.RollershutterItem;
import org.openhab.core.library.items.StringItem;
import org.openhab.core.library.items.SwitchItem;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.HSBType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.OpenClosedType;
import org.openhab.core.library.types.PercentType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.library.types.RawType;
import org.openhab.core.library.unit.Units;
import org.openhab.core.persistence.FilterCriteria;
import org.openhab.core.persistence.FilterCriteria.Ordering;
import org.openhab.core.persistence.HistoricItem;
import org.openhab.core.types.State;
import org.openhab.core.types.TypeParser;
import org.openhab.persistence.jdbc.model.ItemVO;
import org.openhab.persistence.jdbc.model.ItemsVO;
import org.openhab.persistence.jdbc.model.JdbcHistoricItem;
Expand Down Expand Up @@ -505,23 +503,17 @@ protected State getState(Item item, @Nullable Unit<? extends Quantity<?>> unit,
}
return unit == null ? DecimalType.valueOf(((String) v).toString())
: QuantityType.valueOf(((String) v).toString());
} else if (item instanceof ColorItem) {
return HSBType.valueOf(((String) v).toString());
} else if (item instanceof DimmerItem) {
return new PercentType(objectAsInteger(v));
} else if (item instanceof SwitchItem) {
return OnOffType.valueOf(((String) v).toString().trim());
} else if (item instanceof ContactItem) {
return OpenClosedType.valueOf(((String) v).toString().trim());
} else if (item instanceof RollershutterItem) {
return new PercentType(objectAsInteger(v));
} else if (item instanceof DateTimeItem) {
return new DateTimeType(
ZonedDateTime.ofInstant(Instant.ofEpochMilli(objectAsLong(v)), ZoneId.systemDefault()));
} else if (item instanceof StringItem) {
return StringType.valueOf(((String) v).toString());
} else {// Call, Image, Location, Player, String
return StringType.valueOf(((String) v).toString());
} else if (item instanceof DimmerItem || item instanceof RollershutterItem) {
return new PercentType(objectAsInteger(v));
} else if (item instanceof ImageItem) {
return RawType.valueOf(objectAsString(v));
} else if (item instanceof ContactItem || item instanceof PlayerItem || item instanceof SwitchItem) {
return TypeParser.parseState(item.getAcceptedDataTypes(), ((String) v).toString().trim());
} else {
return TypeParser.parseState(item.getAcceptedDataTypes(), ((String) v).toString());
}
}

Expand All @@ -548,6 +540,13 @@ protected Integer objectAsInteger(Object v) {
return ((Integer) v).intValue();
}

protected String objectAsString(Object v) {
if (v instanceof byte[]) {
return new String((byte[]) v);
}
return ((String) v).toString();
}

public String getItemType(Item i) {
Item item = i;
String def = "STRINGITEM";
Expand Down

0 comments on commit e24d43a

Please sign in to comment.