use of org.openhab.core.library.types.StringListType in project openhab-addons by openhab.
the class JpaHistoricItem method fromPersistedItem.
/**
* Converts the string value of the persisted item to the state of a HistoricItem.
*
* @param pItem the persisted JpaPersistentItem
* @param item the source reference Item
* @return historic item
*/
public static HistoricItem fromPersistedItem(JpaPersistentItem pItem, Item item) {
State state;
if (item instanceof NumberItem) {
state = new DecimalType(Double.valueOf(pItem.getValue()));
} else if (item instanceof DimmerItem) {
state = new PercentType(Integer.valueOf(pItem.getValue()));
} else if (item instanceof SwitchItem) {
state = OnOffType.valueOf(pItem.getValue());
} else if (item instanceof ContactItem) {
state = OpenClosedType.valueOf(pItem.getValue());
} else if (item instanceof RollershutterItem) {
state = PercentType.valueOf(pItem.getValue());
} else if (item instanceof DateTimeItem) {
state = new DateTimeType(ZonedDateTime.ofInstant(Instant.ofEpochMilli(Long.valueOf(pItem.getValue())), ZoneId.systemDefault()));
} else if (item instanceof LocationItem) {
PointType pType = null;
String[] comps = pItem.getValue().split(";");
if (comps.length >= 2) {
pType = new PointType(new DecimalType(comps[0]), new DecimalType(comps[1]));
if (comps.length == 3) {
pType.setAltitude(new DecimalType(comps[2]));
}
}
state = pType;
} else if (item instanceof StringListType) {
state = new StringListType(pItem.getValue());
} else {
state = new StringType(pItem.getValue());
}
return new JpaHistoricItem(item.getName(), state, pItem.getTimestamp());
}
use of org.openhab.core.library.types.StringListType in project openhab-addons by openhab.
the class AbstractDynamoDBItem method asHistoricItem.
@Override
@Nullable
public HistoricItem asHistoricItem(final Item item, @Nullable Unit<?> targetUnit) {
final State deserializedState;
if (this.getState() == null) {
return null;
}
try {
deserializedState = accept(new DynamoDBItemVisitor<@Nullable State>() {
@Override
@Nullable
public State visit(DynamoDBStringItem dynamoStringItem) {
String stringState = dynamoStringItem.getState();
if (stringState == null) {
return null;
}
if (item instanceof ColorItem) {
return new HSBType(stringState);
} else if (item instanceof LocationItem) {
return new PointType(stringState);
} else if (item instanceof PlayerItem) {
// Backwards-compatibility with legacy schema. New schema uses DynamoDBBigDecimalItem
try {
return PlayPauseType.valueOf(stringState);
} catch (IllegalArgumentException e) {
return RewindFastforwardType.valueOf(stringState);
}
} else if (item instanceof DateTimeItem) {
try {
// We convert to default/local timezone for user convenience (e.g. display)
return new DateTimeType(ZONED_DATE_TIME_CONVERTER_STRING.transformTo(stringState).withZoneSameInstant(ZoneId.systemDefault()));
} catch (DateTimeParseException e) {
logger.warn("Failed to parse {} as date. Outputting UNDEF instead", stringState);
return UnDefType.UNDEF;
}
} else if (item instanceof CallItem) {
String parts = stringState;
String[] strings = parts.split(",");
String orig = strings[0];
String dest = strings[1];
return new StringListType(orig, dest);
} else {
return new StringType(dynamoStringItem.getState());
}
}
@Override
@Nullable
public State visit(DynamoDBBigDecimalItem dynamoBigDecimalItem) {
BigDecimal numberState = dynamoBigDecimalItem.getState();
if (numberState == null) {
return null;
}
if (item instanceof NumberItem) {
NumberItem numberItem = ((NumberItem) item);
Unit<? extends Quantity<?>> unit = targetUnit == null ? numberItem.getUnit() : targetUnit;
if (unit != null) {
return new QuantityType<>(numberState, unit);
} else {
return new DecimalType(numberState);
}
} else if (item instanceof DimmerItem) {
// % values have been stored as-is
return new PercentType(numberState);
} else if (item instanceof SwitchItem) {
return numberState.compareTo(BigDecimal.ZERO) != 0 ? OnOffType.ON : OnOffType.OFF;
} else if (item instanceof ContactItem) {
return numberState.compareTo(BigDecimal.ZERO) != 0 ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
} else if (item instanceof RollershutterItem) {
// Percents and UP/DOWN have been stored % values (not fractional)
return new PercentType(numberState);
} else if (item instanceof PlayerItem) {
if (numberState.equals(PLAY_BIGDECIMAL)) {
return PlayPauseType.PLAY;
} else if (numberState.equals(PAUSE_BIGDECIMAL)) {
return PlayPauseType.PAUSE;
} else if (numberState.equals(FAST_FORWARD_BIGDECIMAL)) {
return RewindFastforwardType.FASTFORWARD;
} else if (numberState.equals(REWIND_BIGDECIMAL)) {
return RewindFastforwardType.REWIND;
} else {
throw new IllegalArgumentException("Unknown serialized value");
}
} else {
logger.warn("Not sure how to convert big decimal item {} to type {}. Using StringType as fallback", dynamoBigDecimalItem.getName(), item.getClass());
return new StringType(numberState.toString());
}
}
});
if (deserializedState == null) {
return null;
}
return new DynamoDBHistoricItem(getName(), deserializedState, getTime());
} catch (Exception e) {
logger.trace("Failed to convert state '{}' to item {} {}: {} {}. Data persisted with incompatible item.", this.state, item.getClass().getSimpleName(), item.getName(), e.getClass().getSimpleName(), e.getMessage());
return null;
}
}
use of org.openhab.core.library.types.StringListType in project addons by smarthomej.
the class PhonebookProfile method onStateUpdateFromHandler.
@Override
public void onStateUpdateFromHandler(State state) {
if (state instanceof UnDefType) {
// we cannot adjust UNDEF or NULL values, thus we simply apply them without reporting an error or warning
callback.sendUpdate(state);
}
if (state instanceof StringType) {
Optional<String> match = resolveNumber(state.toString());
State newState = Objects.requireNonNull(match.map(name -> (State) new StringType(name)).orElse(state));
if (newState.equals(state)) {
logger.debug("Number '{}' not found in phonebook '{}' from provider '{}'", state, phonebookName, thingUID);
}
callback.sendUpdate(newState);
} else if (state instanceof StringListType) {
StringListType stringList = (StringListType) state;
try {
String phoneNumber = stringList.getValue(phoneNumberIndex);
Optional<String> match = resolveNumber(phoneNumber);
final State newState;
if (match.isPresent()) {
newState = new StringType(match.get());
} else {
logger.debug("Number '{}' not found in phonebook '{}' from provider '{}'", phoneNumber, phonebookName, thingUID);
newState = new StringType(phoneNumber);
}
callback.sendUpdate(newState);
} catch (IllegalArgumentException e) {
logger.debug("StringListType does not contain a number at index {}", phoneNumberIndex);
}
}
}
use of org.openhab.core.library.types.StringListType in project openhab-core by openhab.
the class CallItemTest method testSetStringListType.
@Test
public void testSetStringListType() {
StringListType callType1 = new StringListType("0699222222", "0179999998");
CallItem callItem1 = new CallItem("testItem");
callItem1.setState(callType1);
assertEquals(callItem1.toString(), "testItem (Type=CallItem, State=0699222222,0179999998, Label=null, Category=null)");
callType1 = new StringListType("0699222222,0179999998");
callItem1.setState(callType1);
assertEquals(callItem1.toString(), "testItem (Type=CallItem, State=0699222222,0179999998, Label=null, Category=null)");
}
use of org.openhab.core.library.types.StringListType in project openhab-core by openhab.
the class StateUtil method getAllStates.
public static List<State> getAllStates() {
List<State> states = new LinkedList<>();
DateTimeType dateTime = new DateTimeType();
states.add(dateTime);
DecimalType decimal = new DecimalType(23);
states.add(decimal);
PercentType percent = new PercentType(50);
states.add(percent);
HSBType hsb = new HSBType("50,75,42");
states.add(hsb);
states.add(OnOffType.ON);
states.add(OnOffType.OFF);
states.add(OpenClosedType.OPEN);
states.add(OpenClosedType.CLOSED);
states.add(PlayPauseType.PLAY);
states.add(PlayPauseType.PAUSE);
PointType point = new PointType("42.23,23.5");
states.add(point);
RawType raw = new RawType(new byte[0], "application/octet-stream");
states.add(raw);
states.add(RewindFastforwardType.REWIND);
states.add(RewindFastforwardType.FASTFORWARD);
StringListType stringList = new StringListType(new String[] { "foo", "bar" });
states.add(stringList);
StringType string = new StringType("foo");
states.add(string);
states.add(UnDefType.NULL);
states.add(UnDefType.UNDEF);
states.add(UpDownType.UP);
states.add(UpDownType.DOWN);
QuantityType<Temperature> quantityType = new QuantityType<>("12 °C");
states.add(quantityType);
return states;
}
Aggregations