use of org.openhab.core.library.items.CallItem in project openhab-addons by openhab.
the class JdbcBaseDAOTest method testObjectAsStateReturnsValidState.
@Test
public void testObjectAsStateReturnsValidState() {
State decimalType = jdbcBaseDAO.objectAsState(new NumberItem("testNumberItem"), null, 7.3);
assertInstanceOf(DecimalType.class, decimalType);
assertThat(decimalType, is(DecimalType.valueOf("7.3")));
State quantityType = jdbcBaseDAO.objectAsState(new NumberItem("testNumberItem"), SIUnits.CELSIUS, 7.3);
assertInstanceOf(QuantityType.class, quantityType);
assertThat(quantityType, is(QuantityType.valueOf("7.3 °C")));
State dateTimeType = jdbcBaseDAO.objectAsState(new DateTimeItem("testDateTimeItem"), null, java.sql.Timestamp.valueOf("2021-02-01 23:30:02.049"));
assertInstanceOf(DateTimeType.class, dateTimeType);
assertThat(dateTimeType, is(DateTimeType.valueOf("2021-02-01T23:30:02.049")));
State hsbType = jdbcBaseDAO.objectAsState(new ColorItem("testColorItem"), null, "184,100,52");
assertInstanceOf(HSBType.class, hsbType);
assertThat(hsbType, is(HSBType.valueOf("184,100,52")));
State percentType = jdbcBaseDAO.objectAsState(new DimmerItem("testDimmerItem"), null, 52);
assertInstanceOf(PercentType.class, percentType);
assertThat(percentType, is(PercentType.valueOf("52")));
percentType = jdbcBaseDAO.objectAsState(new RollershutterItem("testRollershutterItem"), null, 39);
assertInstanceOf(PercentType.class, percentType);
assertThat(percentType, is(PercentType.valueOf("39")));
State openClosedType = jdbcBaseDAO.objectAsState(new ContactItem("testContactItem"), null, "OPEN");
assertInstanceOf(OpenClosedType.class, openClosedType);
assertThat(openClosedType, is(OpenClosedType.OPEN));
State playPauseType = jdbcBaseDAO.objectAsState(new PlayerItem("testPlayerItem"), null, "PLAY");
assertInstanceOf(PlayPauseType.class, playPauseType);
assertThat(playPauseType, is(PlayPauseType.PLAY));
State rewindFastforwardType = jdbcBaseDAO.objectAsState(new PlayerItem("testPlayerItem"), null, "REWIND");
assertInstanceOf(RewindFastforwardType.class, rewindFastforwardType);
assertThat(rewindFastforwardType, is(RewindFastforwardType.REWIND));
State onOffType = jdbcBaseDAO.objectAsState(new SwitchItem("testSwitchItem"), null, "ON");
assertInstanceOf(OnOffType.class, onOffType);
assertThat(onOffType, is(OnOffType.ON));
State stringListType = jdbcBaseDAO.objectAsState(new CallItem("testCallItem"), null, "0699222222,0179999998");
assertInstanceOf(StringListType.class, stringListType);
assertThat(stringListType, is(StringListType.valueOf("0699222222,0179999998")));
State expectedRawType = new RawType(new byte[0], "application/octet-stream");
State rawType = jdbcBaseDAO.objectAsState(new ImageItem("testImageItem"), null, expectedRawType.toFullString());
assertInstanceOf(RawType.class, rawType);
assertThat(rawType, is(expectedRawType));
State pointType = jdbcBaseDAO.objectAsState(new LocationItem("testLocationItem"), null, "1,2,3");
assertInstanceOf(PointType.class, pointType);
assertThat(pointType, is(PointType.valueOf("1,2,3")));
State stringType = jdbcBaseDAO.objectAsState(new StringItem("testStringItem"), null, "String");
assertInstanceOf(StringType.class, stringType);
assertThat(stringType, is(StringType.valueOf("String")));
}
use of org.openhab.core.library.items.CallItem 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.items.CallItem in project openhab-addons by openhab.
the class CallItemIntegrationTest method storeData.
@SuppressWarnings("null")
@BeforeAll
public static void storeData() throws InterruptedException {
CallItem item = (CallItem) ITEMS.get(NAME);
item.setState(STATE1);
beforeStore = ZonedDateTime.now();
Thread.sleep(10);
service.store(item);
afterStore1 = ZonedDateTime.now();
Thread.sleep(10);
item.setState(STATE2);
service.store(item);
Thread.sleep(10);
afterStore2 = ZonedDateTime.now();
LOGGER.info("Created item between {} and {}", AbstractDynamoDBItem.DATEFORMATTER.format(beforeStore), AbstractDynamoDBItem.DATEFORMATTER.format(afterStore1));
}
use of org.openhab.core.library.items.CallItem in project openhab-addons by openhab.
the class BaseIntegrationTest method populateItems.
@BeforeAll
protected static void populateItems() {
ITEMS.put("dimmer", new DimmerItem("dimmer"));
ITEMS.put("number", new NumberItem("number"));
NumberItem temperatureItem = new NumberItem("Number:Temperature", "numberTemperature");
ITEMS.put("numberTemperature", temperatureItem);
GroupItem groupTemperature = new GroupItem("groupNumberTemperature", temperatureItem);
ITEMS.put("groupNumberTemperature", groupTemperature);
NumberItem dimensionlessItem = new NumberItem("Number:Dimensionless", "numberDimensionless");
ITEMS.put("numberDimensionless", dimensionlessItem);
GroupItem groupDimensionless = new GroupItem("groupNumberDimensionless", dimensionlessItem);
ITEMS.put("groupNumberDimensionless", groupDimensionless);
GroupItem groupDummy = new GroupItem("dummyGroup", null);
ITEMS.put("groupDummy", groupDummy);
ITEMS.put("string", new StringItem("string"));
ITEMS.put("switch", new SwitchItem("switch"));
ITEMS.put("contact", new ContactItem("contact"));
ITEMS.put("color", new ColorItem("color"));
ITEMS.put("rollershutter", new RollershutterItem("rollershutter"));
ITEMS.put("datetime", new DateTimeItem("datetime"));
ITEMS.put("call", new CallItem("call"));
ITEMS.put("location", new LocationItem("location"));
ITEMS.put("player_playpause", new PlayerItem("player_playpause"));
ITEMS.put("player_rewindfastforward", new PlayerItem("player_rewindfastforward"));
injectItemServices();
}
use of org.openhab.core.library.items.CallItem in project openhab-addons by openhab.
the class AbstractDynamoDBItemSerializationTest method testCallTypeWithCallItemLegacy.
@ParameterizedTest
@CsvSource({ "true", "false" })
public void testCallTypeWithCallItemLegacy(boolean legacy) throws IOException {
GenericItem item = new CallItem("foo");
final DynamoDBItem<?> dbitem = testSerializationToDTO(legacy, item, new StringListType("origNum", "destNum"), "origNum,destNum");
testAsHistoricGeneric(dbitem, item, new StringListType("origNum", "destNum"));
}
Aggregations