Search in sources :

Example 1 with ImageItem

use of org.openhab.core.library.items.ImageItem 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 ImageItem

use of org.openhab.core.library.items.ImageItem in project openhab-addons by openhab.

the class AbstractDynamoDBItem method fromStateNew.

public static DynamoDBItem<?> fromStateNew(Item item, ZonedDateTime time, @Nullable Integer expireDays) {
    String name = item.getName();
    State state = item.getState();
    if (item instanceof CallItem) {
        return new DynamoDBStringItem(name, convert(state, StringListType.class).toFullString(), time, expireDays);
    } else if (item instanceof ContactItem) {
        return new DynamoDBBigDecimalItem(name, convert(state, DecimalType.class).toBigDecimal(), time, expireDays);
    } else if (item instanceof DateTimeItem) {
        return new DynamoDBStringItem(name, ZONED_DATE_TIME_CONVERTER_STRING.toString(((DateTimeType) state).getZonedDateTime()), time, expireDays);
    } else if (item instanceof ImageItem) {
        throw new IllegalArgumentException("Unsupported item " + item.getClass().getSimpleName());
    } else if (item instanceof LocationItem) {
        return new DynamoDBStringItem(name, state.toFullString(), time, expireDays);
    } else if (item instanceof NumberItem) {
        return new DynamoDBBigDecimalItem(name, convert(state, DecimalType.class).toBigDecimal(), time, expireDays);
    } else if (item instanceof PlayerItem) {
        if (state instanceof PlayPauseType) {
            switch((PlayPauseType) state) {
                case PLAY:
                    return new DynamoDBBigDecimalItem(name, PLAY_BIGDECIMAL, time, expireDays);
                case PAUSE:
                    return new DynamoDBBigDecimalItem(name, PAUSE_BIGDECIMAL, time, expireDays);
                default:
                    throw new IllegalArgumentException("Unexpected enum with PlayPauseType: " + state.toString());
            }
        } else if (state instanceof RewindFastforwardType) {
            switch((RewindFastforwardType) state) {
                case FASTFORWARD:
                    return new DynamoDBBigDecimalItem(name, FAST_FORWARD_BIGDECIMAL, time, expireDays);
                case REWIND:
                    return new DynamoDBBigDecimalItem(name, REWIND_BIGDECIMAL, time, expireDays);
                default:
                    throw new IllegalArgumentException("Unexpected enum with RewindFastforwardType: " + state.toString());
            }
        } else {
            throw new IllegalStateException(String.format("Unexpected state type %s with PlayerItem", state.getClass().getSimpleName()));
        }
    } else if (item instanceof RollershutterItem) {
        // Normalize UP/DOWN to %
        return new DynamoDBBigDecimalItem(name, convert(state, PercentType.class).toBigDecimal(), time, expireDays);
    } else if (item instanceof StringItem) {
        if (state instanceof StringType) {
            return new DynamoDBStringItem(name, ((StringType) state).toString(), time, expireDays);
        } else if (state instanceof DateTimeType) {
            return new DynamoDBStringItem(name, ZONED_DATE_TIME_CONVERTER_STRING.toString(((DateTimeType) state).getZonedDateTime()), time, expireDays);
        } else {
            throw new IllegalStateException(String.format("Unexpected state type %s with StringItem", state.getClass().getSimpleName()));
        }
    } else if (item instanceof ColorItem) {
        // Note: needs to be before parent class DimmerItem
        return new DynamoDBStringItem(name, convert(state, HSBType.class).toFullString(), time, expireDays);
    } else if (item instanceof DimmerItem) {
        // Normalize ON/OFF to %
        return new DynamoDBBigDecimalItem(name, convert(state, PercentType.class).toBigDecimal(), time, expireDays);
    } else if (item instanceof SwitchItem) {
        // Normalize ON/OFF to 1/0
        return new DynamoDBBigDecimalItem(name, convert(state, DecimalType.class).toBigDecimal(), time, expireDays);
    } else {
        throw new IllegalArgumentException("Unsupported item " + item.getClass().getSimpleName());
    }
}
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) RollershutterItem(org.openhab.core.library.items.RollershutterItem) DimmerItem(org.openhab.core.library.items.DimmerItem) HSBType(org.openhab.core.library.types.HSBType) SwitchItem(org.openhab.core.library.items.SwitchItem) ContactItem(org.openhab.core.library.items.ContactItem) PercentType(org.openhab.core.library.types.PercentType) StringItem(org.openhab.core.library.items.StringItem) RewindFastforwardType(org.openhab.core.library.types.RewindFastforwardType) NumberItem(org.openhab.core.library.items.NumberItem) DateTimeType(org.openhab.core.library.types.DateTimeType) State(org.openhab.core.types.State) PlayPauseType(org.openhab.core.library.types.PlayPauseType) DecimalType(org.openhab.core.library.types.DecimalType) CallItem(org.openhab.core.library.items.CallItem) ImageItem(org.openhab.core.library.items.ImageItem)

Example 3 with ImageItem

use of org.openhab.core.library.items.ImageItem in project openhab-addons by openhab.

the class MeteoBlueHandler method updateChannel.

/**
 * Update the channel from the last weather data retrieved.
 *
 * @param channelId the id of the channel to be updated
 */
private void updateChannel(String channelId) {
    Channel channel = getThing().getChannel(channelId);
    if (channel == null || !isLinked(channelId)) {
        logger.trace("Channel '{}' was null or not linked! Not updated.", channelId);
        return;
    }
    // get the set of channel parameters.
    // the first will be the forecast day (eg. forecastToday),
    // and the second will be the datapoint (eg. snowFraction)
    String[] channelParts = channelId.split("#");
    String forecastDay = channelParts[0];
    String datapointName = channelParts[1];
    if (channelParts.length != 2) {
        logger.debug("Skipped invalid channelId '{}'", channelId);
        return;
    }
    logger.debug("Updating channel '{}'", channelId);
    Forecast forecast = getForecast(forecastDay);
    if (forecast == null) {
        logger.debug("No forecast found for '{}'. Not updating.", forecastDay);
        return;
    }
    Object datapoint = forecast.getDatapoint(datapointName);
    logger.debug("Value for datapoint '{}' is '{}'", datapointName, datapoint);
    if (datapoint == null) {
        logger.debug("Couldn't get datapoint '{}' for '{}'. Not updating.", datapointName, forecastDay);
        return;
    }
    // Build a State from this value
    State state = null;
    if (datapoint instanceof Calendar) {
        state = new DateTimeType(ZonedDateTime.ofInstant(((Calendar) datapoint).toInstant(), ZoneId.systemDefault()));
    } else if (datapoint instanceof Integer) {
        state = getStateForType(channel.getAcceptedItemType(), (Integer) datapoint);
    } else if (datapoint instanceof Number) {
        BigDecimal decimalValue = new BigDecimal(datapoint.toString()).setScale(2, RoundingMode.HALF_UP);
        state = getStateForType(channel.getAcceptedItemType(), decimalValue);
    } else if (datapoint instanceof String) {
        state = new StringType(datapoint.toString());
    } else if (datapoint instanceof BufferedImage) {
        ImageItem item = new ImageItem("rain area");
        state = new RawType(renderImage((BufferedImage) datapoint), "image/png");
        item.setState(state);
    } else {
        logger.debug("Unsupported value type {}", datapoint.getClass().getSimpleName());
    }
    // Update the channel
    if (state != null) {
        logger.trace("Updating channel with state value {}. (object type {})", state, datapoint.getClass().getSimpleName());
        updateState(channelId, state);
    }
}
Also used : StringType(org.openhab.core.library.types.StringType) Channel(org.openhab.core.thing.Channel) Calendar(java.util.Calendar) BigDecimal(java.math.BigDecimal) BufferedImage(java.awt.image.BufferedImage) DateTimeType(org.openhab.core.library.types.DateTimeType) State(org.openhab.core.types.State) Forecast(org.openhab.binding.meteoblue.internal.Forecast) ImageItem(org.openhab.core.library.items.ImageItem) RawType(org.openhab.core.library.types.RawType)

Aggregations

ImageItem (org.openhab.core.library.items.ImageItem)3 State (org.openhab.core.types.State)3 CallItem (org.openhab.core.library.items.CallItem)2 ColorItem (org.openhab.core.library.items.ColorItem)2 ContactItem (org.openhab.core.library.items.ContactItem)2 DateTimeItem (org.openhab.core.library.items.DateTimeItem)2 DimmerItem (org.openhab.core.library.items.DimmerItem)2 LocationItem (org.openhab.core.library.items.LocationItem)2 NumberItem (org.openhab.core.library.items.NumberItem)2 PlayerItem (org.openhab.core.library.items.PlayerItem)2 RollershutterItem (org.openhab.core.library.items.RollershutterItem)2 StringItem (org.openhab.core.library.items.StringItem)2 SwitchItem (org.openhab.core.library.items.SwitchItem)2 DateTimeType (org.openhab.core.library.types.DateTimeType)2 RawType (org.openhab.core.library.types.RawType)2 StringType (org.openhab.core.library.types.StringType)2 BufferedImage (java.awt.image.BufferedImage)1 BigDecimal (java.math.BigDecimal)1 Calendar (java.util.Calendar)1 Test (org.junit.jupiter.api.Test)1