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")));
}
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());
}
}
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);
}
}
Aggregations