Search in sources :

Example 1 with CallItem

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")));
}
Also used : LocationItem(org.openhab.core.library.items.LocationItem) ContactItem(org.openhab.core.library.items.ContactItem) ColorItem(org.openhab.core.library.items.ColorItem) DateTimeItem(org.openhab.core.library.items.DateTimeItem) PlayerItem(org.openhab.core.library.items.PlayerItem) StringItem(org.openhab.core.library.items.StringItem) NumberItem(org.openhab.core.library.items.NumberItem) State(org.openhab.core.types.State) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) CallItem(org.openhab.core.library.items.CallItem) RawType(org.openhab.core.library.types.RawType) ImageItem(org.openhab.core.library.items.ImageItem) SwitchItem(org.openhab.core.library.items.SwitchItem) Test(org.junit.jupiter.api.Test)

Example 2 with CallItem

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;
    }
}
Also used : LocationItem(org.openhab.core.library.items.LocationItem) StringType(org.openhab.core.library.types.StringType) ColorItem(org.openhab.core.library.items.ColorItem) DateTimeItem(org.openhab.core.library.items.DateTimeItem) PlayerItem(org.openhab.core.library.items.PlayerItem) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) HSBType(org.openhab.core.library.types.HSBType) StringListType(org.openhab.core.library.types.StringListType) SwitchItem(org.openhab.core.library.items.SwitchItem) ContactItem(org.openhab.core.library.items.ContactItem) PercentType(org.openhab.core.library.types.PercentType) BigDecimal(java.math.BigDecimal) DateTimeParseException(java.time.format.DateTimeParseException) NumberItem(org.openhab.core.library.items.NumberItem) DateTimeParseException(java.time.format.DateTimeParseException) DateTimeType(org.openhab.core.library.types.DateTimeType) QuantityType(org.openhab.core.library.types.QuantityType) State(org.openhab.core.types.State) PointType(org.openhab.core.library.types.PointType) DecimalType(org.openhab.core.library.types.DecimalType) CallItem(org.openhab.core.library.items.CallItem) Nullable(org.eclipse.jdt.annotation.Nullable) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 3 with CallItem

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));
}
Also used : CallItem(org.openhab.core.library.items.CallItem) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 4 with CallItem

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();
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) LocationItem(org.openhab.core.library.items.LocationItem) ContactItem(org.openhab.core.library.items.ContactItem) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) ColorItem(org.openhab.core.library.items.ColorItem) GroupItem(org.openhab.core.items.GroupItem) CallItem(org.openhab.core.library.items.CallItem) DateTimeItem(org.openhab.core.library.items.DateTimeItem) StringItem(org.openhab.core.library.items.StringItem) PlayerItem(org.openhab.core.library.items.PlayerItem) SwitchItem(org.openhab.core.library.items.SwitchItem) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 5 with CallItem

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"));
}
Also used : GenericItem(org.openhab.core.items.GenericItem) CallItem(org.openhab.core.library.items.CallItem) StringListType(org.openhab.core.library.types.StringListType) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

CallItem (org.openhab.core.library.items.CallItem)6 ColorItem (org.openhab.core.library.items.ColorItem)4 ContactItem (org.openhab.core.library.items.ContactItem)4 DateTimeItem (org.openhab.core.library.items.DateTimeItem)4 DimmerItem (org.openhab.core.library.items.DimmerItem)4 LocationItem (org.openhab.core.library.items.LocationItem)4 NumberItem (org.openhab.core.library.items.NumberItem)4 PlayerItem (org.openhab.core.library.items.PlayerItem)4 RollershutterItem (org.openhab.core.library.items.RollershutterItem)4 SwitchItem (org.openhab.core.library.items.SwitchItem)4 StringItem (org.openhab.core.library.items.StringItem)3 State (org.openhab.core.types.State)3 BeforeAll (org.junit.jupiter.api.BeforeAll)2 ImageItem (org.openhab.core.library.items.ImageItem)2 DateTimeType (org.openhab.core.library.types.DateTimeType)2 DecimalType (org.openhab.core.library.types.DecimalType)2 HSBType (org.openhab.core.library.types.HSBType)2 PercentType (org.openhab.core.library.types.PercentType)2 StringListType (org.openhab.core.library.types.StringListType)2 StringType (org.openhab.core.library.types.StringType)2