Search in sources :

Example 1 with Forecast

use of org.openhab.binding.meteoblue.internal.Forecast 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

BufferedImage (java.awt.image.BufferedImage)1 BigDecimal (java.math.BigDecimal)1 Calendar (java.util.Calendar)1 Forecast (org.openhab.binding.meteoblue.internal.Forecast)1 ImageItem (org.openhab.core.library.items.ImageItem)1 DateTimeType (org.openhab.core.library.types.DateTimeType)1 RawType (org.openhab.core.library.types.RawType)1 StringType (org.openhab.core.library.types.StringType)1 Channel (org.openhab.core.thing.Channel)1 State (org.openhab.core.types.State)1