Search in sources :

Example 1 with ForecastBindingConfig

use of org.openhab.binding.weather.internal.common.binding.ForecastBindingConfig in project openhab1-addons by openhab.

the class BindingConfigParser method parse.

/**
     * Parses the bindingConfig of an item and returns a WeatherBindingConfig.
     */
public WeatherBindingConfig parse(Item item, String bindingConfig) throws BindingConfigParseException {
    bindingConfig = StringUtils.trimToEmpty(bindingConfig);
    bindingConfig = StringUtils.removeStart(bindingConfig, "{");
    bindingConfig = StringUtils.removeEnd(bindingConfig, "}");
    String[] entries = bindingConfig.split("[,]");
    WeatherBindingConfigHelper helper = new WeatherBindingConfigHelper();
    for (String entry : entries) {
        String[] entryParts = StringUtils.trimToEmpty(entry).split("[=]");
        if (entryParts.length != 2) {
            throw new BindingConfigParseException("A bindingConfig must have a key and a value");
        }
        String key = StringUtils.trim(entryParts[0]);
        String value = StringUtils.trim(entryParts[1]);
        value = StringUtils.removeStart(value, "\"");
        value = StringUtils.removeEnd(value, "\"");
        try {
            helper.getClass().getDeclaredField(key).set(helper, value);
        } catch (Exception e) {
            throw new BindingConfigParseException("Could not set value " + value + " for attribute " + key);
        }
    }
    if (!helper.isValid()) {
        throw new BindingConfigParseException("Invalid binding: " + bindingConfig);
    }
    helper.type = StringUtils.replace(helper.type, "athmosphere", "atmosphere");
    WeatherBindingConfig weatherConfig = null;
    if (helper.isForecast()) {
        Integer forecast = parseInteger(helper.forecast, bindingConfig);
        if (forecast < 0) {
            throw new BindingConfigParseException("Invalid binding, forecast must be >= 0: " + bindingConfig);
        }
        weatherConfig = new ForecastBindingConfig(helper.locationId, forecast, helper.type, helper.property);
    } else {
        weatherConfig = new WeatherBindingConfig(helper.locationId, helper.type, helper.property);
    }
    Weather validationInstance = new Weather(null);
    String property = weatherConfig.getWeatherProperty();
    if (!Weather.isVirtualProperty(property) && !PropertyUtils.hasProperty(validationInstance, property)) {
        throw new BindingConfigParseException("Invalid binding, unknown type or property: " + bindingConfig);
    }
    boolean isDecimalTypeItem = item.getAcceptedDataTypes().contains(DecimalType.class);
    if (isDecimalTypeItem || Weather.isVirtualProperty(property)) {
        RoundingMode roundingMode = RoundingMode.HALF_UP;
        if (helper.roundingMode != null) {
            try {
                roundingMode = RoundingMode.valueOf(StringUtils.upperCase(helper.roundingMode));
            } catch (IllegalArgumentException ex) {
                throw new BindingConfigParseException("Invalid binding, unknown roundingMode: " + bindingConfig);
            }
        }
        Integer scale = 2;
        if (helper.scale != null) {
            scale = parseInteger(helper.scale, bindingConfig);
            if (scale < 0) {
                throw new BindingConfigParseException("Invalid binding, scale must be >= 0: " + bindingConfig);
            }
        }
        weatherConfig.setScale(roundingMode, scale);
    }
    weatherConfig.setUnit(Unit.parse(helper.unit));
    if (StringUtils.isNotBlank(helper.unit) && weatherConfig.getUnit() == null) {
        throw new BindingConfigParseException("Invalid binding, unknown unit: " + bindingConfig);
    }
    try {
        if (!Weather.isVirtualProperty(property) && weatherConfig.hasUnit()) {
            String doubleTypeName = Double.class.getName();
            String propertyTypeName = PropertyUtils.getPropertyTypeName(validationInstance, property);
            if (!StringUtils.equals(doubleTypeName, propertyTypeName)) {
                throw new BindingConfigParseException("Invalid binding, unit specified but property is not a double type: " + bindingConfig);
            }
        }
    } catch (IllegalAccessException ex) {
        logger.error(ex.getMessage(), ex);
        throw new BindingConfigParseException(ex.getMessage());
    }
    return weatherConfig;
}
Also used : RoundingMode(java.math.RoundingMode) ForecastBindingConfig(org.openhab.binding.weather.internal.common.binding.ForecastBindingConfig) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) Weather(org.openhab.binding.weather.internal.model.Weather) WeatherBindingConfig(org.openhab.binding.weather.internal.common.binding.WeatherBindingConfig) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

Example 2 with ForecastBindingConfig

use of org.openhab.binding.weather.internal.common.binding.ForecastBindingConfig in project openhab1-addons by openhab.

the class WeatherPublisher method getInstance.

/**
     * Returns the weather or the correct forecast object instance.
     */
private Weather getInstance(Weather weather, WeatherBindingConfig bindingConfig) {
    Weather instance = weather;
    if (bindingConfig instanceof ForecastBindingConfig) {
        ForecastBindingConfig fcConfig = (ForecastBindingConfig) bindingConfig;
        if (fcConfig.getForecastDay() < weather.getForecast().size()) {
            instance = weather.getForecast().get(fcConfig.getForecastDay());
        } else {
            logger.warn("Weather forecast day {} not available for locationId '{}', only {} available", fcConfig.getForecastDay(), bindingConfig.getLocationId(), Math.max(weather.getForecast().size() - 1, 0));
            instance = null;
        }
    }
    return instance;
}
Also used : Weather(org.openhab.binding.weather.internal.model.Weather) ForecastBindingConfig(org.openhab.binding.weather.internal.common.binding.ForecastBindingConfig)

Aggregations

ForecastBindingConfig (org.openhab.binding.weather.internal.common.binding.ForecastBindingConfig)2 Weather (org.openhab.binding.weather.internal.model.Weather)2 RoundingMode (java.math.RoundingMode)1 WeatherBindingConfig (org.openhab.binding.weather.internal.common.binding.WeatherBindingConfig)1 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)1