Search in sources :

Example 1 with ConfigDescriptionParameter

use of org.eclipse.smarthome.config.core.ConfigDescriptionParameter in project smarthome by eclipse.

the class PersistentInbox method getPropsAndConfigParams.

/**
 * Get the properties and configuration parameters for the thing with the given {@link DiscoveryResult}.
 *
 * @param discoveryResult the DiscoveryResult
 * @param props the location the properties should be stored to.
 * @param configParams the location the configuration parameters should be stored to.
 */
private void getPropsAndConfigParams(final DiscoveryResult discoveryResult, final Map<String, String> props, final Map<String, Object> configParams) {
    final List<ConfigDescriptionParameter> configDescParams = getConfigDescParams(discoveryResult);
    final Set<String> paramNames = getConfigDescParamNames(configDescParams);
    final Map<String, Object> resultProps = discoveryResult.getProperties();
    for (String resultKey : resultProps.keySet()) {
        if (paramNames.contains(resultKey)) {
            ConfigDescriptionParameter param = getConfigDescriptionParam(configDescParams, resultKey);
            Object normalizedValue = ConfigUtil.normalizeType(resultProps.get(resultKey), param);
            configParams.put(resultKey, normalizedValue);
        } else {
            props.put(resultKey, String.valueOf(resultProps.get(resultKey)));
        }
    }
}
Also used : ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter)

Example 2 with ConfigDescriptionParameter

use of org.eclipse.smarthome.config.core.ConfigDescriptionParameter in project smarthome by eclipse.

the class ConfigDescriptionConverter method unmarshal.

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    ConfigDescription configDescription = null;
    // read attributes
    Map<String, String> attributes = this.attributeMapValidator.readValidatedAttributes(reader);
    String uriText = attributes.get("uri");
    if (uriText == null) {
        // the URI could be overridden by a context field if it could be
        // automatically extracted
        uriText = (String) context.get("config-description.uri");
    }
    URI uri = null;
    try {
        uri = new URI(uriText);
    } catch (NullPointerException | URISyntaxException ex) {
        throw new ConversionException("The URI '" + uriText + "' in node '" + reader.getNodeName() + "' is invalid!", ex);
    }
    // create the lists to hold parameters and groups
    List<ConfigDescriptionParameter> configDescriptionParams = new ArrayList<ConfigDescriptionParameter>();
    List<ConfigDescriptionParameterGroup> configDescriptionGroups = new ArrayList<ConfigDescriptionParameterGroup>();
    // read values
    List<?> nodes = (List<?>) context.convertAnother(context, List.class);
    NodeIterator nodeIterator = new NodeIterator(nodes);
    // respective arrays
    while (nodeIterator.hasNext() == true) {
        Object node = nodeIterator.next();
        if (node instanceof ConfigDescriptionParameter) {
            configDescriptionParams.add((ConfigDescriptionParameter) node);
        }
        if (node instanceof ConfigDescriptionParameterGroup) {
            configDescriptionGroups.add((ConfigDescriptionParameterGroup) node);
        }
    }
    ConverterAssertion.assertEndOfType(reader);
    // create object
    configDescription = new ConfigDescription(uri, configDescriptionParams, configDescriptionGroups);
    return configDescription;
}
Also used : ConversionException(com.thoughtworks.xstream.converters.ConversionException) NodeIterator(org.eclipse.smarthome.config.xml.util.NodeIterator) ArrayList(java.util.ArrayList) ConfigDescription(org.eclipse.smarthome.config.core.ConfigDescription) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ConfigDescriptionParameterGroup(org.eclipse.smarthome.config.core.ConfigDescriptionParameterGroup) ArrayList(java.util.ArrayList) List(java.util.List) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter)

Example 3 with ConfigDescriptionParameter

use of org.eclipse.smarthome.config.core.ConfigDescriptionParameter in project smarthome by eclipse.

the class ConfigDescriptionParameterConverter method unmarshal.

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    ConfigDescriptionParameter configDescriptionParam = null;
    // read attributes
    Map<String, String> attributes = this.attributeMapValidator.readValidatedAttributes(reader);
    String name = attributes.get("name");
    Type type = toType(attributes.get("type"));
    BigDecimal min = toNumber(attributes.get("min"));
    BigDecimal max = toNumber(attributes.get("max"));
    BigDecimal step = toNumber(attributes.get("step"));
    String patternString = attributes.get("pattern");
    Boolean required = toBoolean(attributes.get("required"));
    Boolean readOnly = falseIfNull(toBoolean(attributes.get("readOnly")));
    Boolean multiple = falseIfNull(toBoolean(attributes.get("multiple")));
    String groupName = attributes.get("groupName");
    String unit = attributes.get("unit");
    // read values
    ConverterValueMap valueMap = new ConverterValueMap(reader, context);
    String parameterContext = valueMap.getString("context");
    if (required == null) {
        // fallback to deprecated "required" element
        required = valueMap.getBoolean("required", false);
    }
    String defaultValue = valueMap.getString("default");
    String label = valueMap.getString("label");
    String description = valueMap.getString("description");
    Boolean advanced = valueMap.getBoolean("advanced", false);
    Boolean verify = valueMap.getBoolean("verify", false);
    Boolean limitToOptions = valueMap.getBoolean("limitToOptions", true);
    Integer multipleLimit = valueMap.getInteger("multipleLimit");
    String unitLabel = null;
    if (unit == null) {
        unitLabel = valueMap.getString("unitLabel");
    }
    // read options and filter criteria
    List<ParameterOption> options = readParameterOptions(valueMap.getObject("options"));
    @SuppressWarnings("unchecked") List<FilterCriteria> filterCriteria = (List<FilterCriteria>) valueMap.getObject("filter");
    // create object
    configDescriptionParam = ConfigDescriptionParameterBuilder.create(name, type).withMinimum(min).withMaximum(max).withStepSize(step).withPattern(patternString).withRequired(required).withReadOnly(readOnly).withMultiple(multiple).withContext(parameterContext).withDefault(defaultValue).withLabel(label).withDescription(description).withOptions(options).withFilterCriteria(filterCriteria).withGroupName(groupName).withAdvanced(advanced).withVerify(verify).withLimitToOptions(limitToOptions).withMultipleLimit(multipleLimit).withUnit(unit).withUnitLabel(unitLabel).build();
    return configDescriptionParam;
}
Also used : FilterCriteria(org.eclipse.smarthome.config.core.FilterCriteria) BigDecimal(java.math.BigDecimal) ConverterValueMap(org.eclipse.smarthome.config.xml.util.ConverterValueMap) Type(org.eclipse.smarthome.config.core.ConfigDescriptionParameter.Type) ParameterOption(org.eclipse.smarthome.config.core.ParameterOption) ArrayList(java.util.ArrayList) List(java.util.List) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter)

Example 4 with ConfigDescriptionParameter

use of org.eclipse.smarthome.config.core.ConfigDescriptionParameter in project smarthome by eclipse.

the class RuleEngineTest method createConfigDescriptions.

private List<ConfigDescriptionParameter> createConfigDescriptions() {
    List<ConfigDescriptionParameter> configDescriptions = new ArrayList<ConfigDescriptionParameter>();
    List<ParameterOption> options = new ArrayList<ParameterOption>();
    options.add(new ParameterOption("1", "one"));
    options.add(new ParameterOption("2", "two"));
    String groupName = null;
    Boolean advanced = false;
    Boolean limitToOptions = true;
    Integer multipleLimit = 0;
    String label = "label1";
    String pattern = null;
    String context = "context1";
    String description = "description1";
    BigDecimal min = null;
    BigDecimal max = null;
    BigDecimal step = null;
    Boolean required = true;
    Boolean multiple = false;
    Boolean readOnly = false;
    String typeStr = ConfigDescriptionParameter.Type.INTEGER.name();
    String defValue = "3";
    List<FilterCriteria> filter = new ArrayList<FilterCriteria>();
    String configPropertyName = "config1";
    ConfigDescriptionParameter cfgDP = ConfigDescriptionParameterBuilder.create(configPropertyName, Type.valueOf(typeStr)).withMaximum(max).withMinimum(min).withStepSize(step).withPattern(pattern).withRequired(required).withReadOnly(readOnly).withMultiple(multiple).withContext(context).withDefault(defValue).withLabel(label).withDescription(description).withOptions(options).withFilterCriteria(filter).withGroupName(groupName).withAdvanced(advanced).withLimitToOptions(limitToOptions).withMultipleLimit(multipleLimit).build();
    configDescriptions.add(cfgDP);
    return configDescriptions;
}
Also used : ParameterOption(org.eclipse.smarthome.config.core.ParameterOption) ArrayList(java.util.ArrayList) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter) FilterCriteria(org.eclipse.smarthome.config.core.FilterCriteria) BigDecimal(java.math.BigDecimal)

Example 5 with ConfigDescriptionParameter

use of org.eclipse.smarthome.config.core.ConfigDescriptionParameter in project smarthome by eclipse.

the class RuleEngine method normalizeConfiguration.

private void normalizeConfiguration(Configuration config, Map<String, ConfigDescriptionParameter> mapCD) {
    if (config != null && mapCD != null) {
        for (String propName : mapCD.keySet()) {
            ConfigDescriptionParameter cd = mapCD.get(propName);
            if (cd != null) {
                Object tmp = config.get(propName);
                Object defaultValue = cd.getDefault();
                if (tmp == null && defaultValue != null) {
                    config.put(propName, defaultValue);
                }
                if (cd.isMultiple()) {
                    tmp = config.get(propName);
                    if (tmp != null && tmp instanceof String) {
                        String sValue = (String) tmp;
                        if (gson == null) {
                            gson = new Gson();
                        }
                        try {
                            Object value = gson.fromJson(sValue, List.class);
                            config.put(propName, value);
                        } catch (JsonSyntaxException e) {
                            logger.error("Can't parse {} to list value.", sValue, e);
                        }
                        continue;
                    }
                }
            }
            Object value = ConfigUtil.normalizeType(config.get(propName), cd);
            if (value != null) {
                config.put(propName, value);
            }
        }
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) Gson(com.google.gson.Gson) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter)

Aggregations

ConfigDescriptionParameter (org.eclipse.smarthome.config.core.ConfigDescriptionParameter)29 ArrayList (java.util.ArrayList)13 ConfigDescription (org.eclipse.smarthome.config.core.ConfigDescription)9 URI (java.net.URI)7 Configuration (org.eclipse.smarthome.config.core.Configuration)5 ParameterOption (org.eclipse.smarthome.config.core.ParameterOption)4 Test (org.junit.Test)4 Bundle (org.osgi.framework.Bundle)4 BigDecimal (java.math.BigDecimal)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Action (org.eclipse.smarthome.automation.Action)3 Condition (org.eclipse.smarthome.automation.Condition)3 Trigger (org.eclipse.smarthome.automation.Trigger)3 ConfigDescriptionParameterGroup (org.eclipse.smarthome.config.core.ConfigDescriptionParameterGroup)3 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)3 URISyntaxException (java.net.URISyntaxException)2 HashSet (java.util.HashSet)2 Rule (org.eclipse.smarthome.automation.Rule)2 Input (org.eclipse.smarthome.automation.type.Input)2