Search in sources :

Example 1 with WeatherBindingConfig

use of org.openhab.binding.weather.internal.common.binding.WeatherBindingConfig 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 WeatherBindingConfig

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

the class WeatherGenericBindingProvider method processBindingConfiguration.

/**
     * {@inheritDoc}
     */
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    super.processBindingConfiguration(context, item, bindingConfig);
    WeatherBindingConfig config = parser.parse(item, bindingConfig);
    logger.debug("Adding item {} with {}", item.getName(), config);
    addBindingConfig(item, config);
}
Also used : WeatherBindingConfig(org.openhab.binding.weather.internal.common.binding.WeatherBindingConfig)

Example 3 with WeatherBindingConfig

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

the class ItemIterator method iterate.

/**
     * Iterates through all items and calls the callback.
     */
public void iterate(ItemIteratorCallback callback) {
    for (WeatherBindingProvider provider : context.getProviders()) {
        for (String itemName : provider.getItemNames()) {
            WeatherBindingConfig bindingConfig = provider.getBindingFor(itemName);
            callback.next(bindingConfig, itemName);
        }
    }
}
Also used : WeatherBindingConfig(org.openhab.binding.weather.internal.common.binding.WeatherBindingConfig) WeatherBindingProvider(org.openhab.binding.weather.WeatherBindingProvider)

Aggregations

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