Search in sources :

Example 11 with ConfigDescriptionParameter

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

the class ConfigDescriptionsTest method parametersWithOptionsAndFiltersShouldLoadProperly.

@Test
public void parametersWithOptionsAndFiltersShouldLoadProperly() throws Exception {
    int initialNumberOfConfigDescriptions = configDescriptionRegistry.getConfigDescriptions().size();
    // install test bundle
    Bundle bundle = SyntheticBundleInstaller.install(bundleContext, TEST_BUNDLE_NAME);
    assertThat(bundle, is(notNullValue()));
    Collection<ConfigDescription> configDescriptions = configDescriptionRegistry.getConfigDescriptions();
    assertThat(configDescriptions.size(), is(initialNumberOfConfigDescriptions + 3));
    URI dummyURI = new URI("thing-type:hue:dummy");
    ConfigDescription bridgeConfigDescription = configDescriptions.stream().filter(it -> it.getUID().equals(dummyURI)).findFirst().get();
    assertThat(bridgeConfigDescription, is(notNullValue()));
    Collection<ConfigDescriptionParameter> parameters = bridgeConfigDescription.getParameters();
    assertThat(parameters.size(), is(2));
    ConfigDescriptionParameter unitParameter = parameters.stream().filter(it -> it.getName().equals("unit")).findFirst().get();
    assertThat(unitParameter, is(notNullValue()));
    assertThat(join(unitParameter.getOptions(), ","), is("ParameterOption [value=\"us\", label=\"US\"],ParameterOption [value=\"metric\", label=\"Metric\"]"));
    ConfigDescriptionParameter lightParameter = parameters.stream().filter(it -> it.getName().equals("color-alarming-light")).findFirst().get();
    assertThat(lightParameter, is(notNullValue()));
    assertThat(join(lightParameter.getFilterCriteria(), ","), is("FilterCriteria [name=\"tags\", value=\"alarm, light\"],FilterCriteria [name=\"type\", value=\"color\"],FilterCriteria [name=\"binding-id\", value=\"hue\"]"));
}
Also used : Bundle(org.osgi.framework.Bundle) ConfigDescription(org.eclipse.smarthome.config.core.ConfigDescription) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter) URI(java.net.URI) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 12 with ConfigDescriptionParameter

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

the class AbstractResourceBundleProvider method getLocalizedConfigurationDescription.

protected List<ConfigDescriptionParameter> getLocalizedConfigurationDescription(TranslationProvider i18nProvider, List<ConfigDescriptionParameter> config, Bundle bundle, String uid, String prefix, Locale locale) {
    List<ConfigDescriptionParameter> configDescriptions = new ArrayList<ConfigDescriptionParameter>();
    if (config != null) {
        ConfigDescriptionI18nUtil util = new ConfigDescriptionI18nUtil(i18nProvider);
        for (ConfigDescriptionParameter parameter : config) {
            String parameterName = parameter.getName();
            URI uri = null;
            try {
                uri = new URI(prefix + ":" + uid + ".name");
            } catch (URISyntaxException e) {
                logger.error("Constructed invalid uri '{}:{}.name'", prefix, uid, e);
            }
            String llabel = parameter.getLabel();
            if (llabel != null) {
                llabel = util.getParameterLabel(bundle, uri, parameterName, llabel, locale);
            }
            String ldescription = parameter.getDescription();
            if (ldescription != null) {
                ldescription = util.getParameterDescription(bundle, uri, parameterName, ldescription, locale);
            }
            String lpattern = parameter.getPattern();
            if (lpattern != null) {
                lpattern = util.getParameterPattern(bundle, uri, parameterName, lpattern, locale);
            }
            List<ParameterOption> loptions = parameter.getOptions();
            if (loptions != null && !loptions.isEmpty()) {
                for (ParameterOption option : loptions) {
                    String label = util.getParameterOptionLabel(bundle, uri, parameterName, option.getValue(), option.getLabel(), locale);
                    option = new ParameterOption(option.getValue(), label);
                }
            }
            String lunitLabel = parameter.getUnitLabel();
            if (lunitLabel != null) {
                lunitLabel = util.getParameterUnitLabel(bundle, uri, parameterName, parameter.getUnit(), lunitLabel, locale);
            }
            configDescriptions.add(ConfigDescriptionParameterBuilder.create(parameterName, parameter.getType()).withMinimum(parameter.getMinimum()).withMaximum(parameter.getMaximum()).withStepSize(parameter.getStepSize()).withPattern(lpattern).withRequired(parameter.isRequired()).withMultiple(parameter.isMultiple()).withReadOnly(parameter.isReadOnly()).withContext(parameter.getContext()).withDefault(parameter.getDefault()).withLabel(llabel).withDescription(ldescription).withFilterCriteria(parameter.getFilterCriteria()).withGroupName(parameter.getGroupName()).withAdvanced(parameter.isAdvanced()).withOptions(loptions).withLimitToOptions(parameter.getLimitToOptions()).withMultipleLimit(parameter.getMultipleLimit()).withUnit(parameter.getUnit()).withUnitLabel(lunitLabel).build());
        }
    }
    return configDescriptions;
}
Also used : ParameterOption(org.eclipse.smarthome.config.core.ParameterOption) ArrayList(java.util.ArrayList) ConfigDescriptionI18nUtil(org.eclipse.smarthome.config.core.i18n.ConfigDescriptionI18nUtil) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 13 with ConfigDescriptionParameter

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

the class AirConditionerRuleTemplate method initialize.

public static AirConditionerRuleTemplate initialize() {
    // initialize triggers
    List<Trigger> triggers = new ArrayList<Trigger>();
    triggers.add(new Trigger(TRIGGER_ID, AirConditionerTriggerType.UID, null));
    // initialize conditions
    // here the tricky part is the giving a value to the condition configuration parameter.
    Configuration conditionConfig = new Configuration();
    conditionConfig.put(StateConditionType.CONFIG_STATE, "on");
    // here the tricky part is the referring into the condition input - trigger output.
    // The syntax is a similar to the JUEL syntax.
    Map<String, String> conditionInputs = new HashMap<String, String>();
    conditionInputs.put(StateConditionType.INPUT_CURRENT_STATE, TRIGGER_ID + "." + StateConditionType.INPUT_CURRENT_STATE);
    Condition stateCondition = new Condition("AirConditionerStateCondition", StateConditionType.UID, conditionConfig, conditionInputs);
    // here the tricky part is the referring into the condition configuration parameter - the
    // template configuration parameter. The syntax is a similar to the JUEL syntax.
    conditionConfig = new Configuration();
    conditionConfig.put(TemperatureConditionType.CONFIG_TEMPERATURE, "$" + CONFIG_TARGET_TEMPERATURE);
    conditionConfig.put(TemperatureConditionType.CONFIG_OPERATOR, "$" + CONFIG_OPERATION);
    // here the tricky part is the referring into the condition input - trigger output.
    // The syntax is a similar to the JUEL syntax.
    conditionInputs = new HashMap<String, String>();
    conditionInputs.put(TemperatureConditionType.INPUT_CURRENT_TEMPERATURE, TRIGGER_ID + "." + TemperatureConditionType.INPUT_CURRENT_TEMPERATURE);
    Condition temperatureCondition = new Condition("AirConditionerTemperatureCondition", TemperatureConditionType.UID, conditionConfig, conditionInputs);
    List<Condition> conditions = new ArrayList<Condition>();
    conditions.add(stateCondition);
    conditions.add(temperatureCondition);
    // initialize actions - here the tricky part is the referring into the action configuration parameter - the
    // template configuration parameter. The syntax is a similar to the JUEL syntax.
    Configuration actionConfig = new Configuration();
    actionConfig.put(WelcomeHomeActionType.CONFIG_DEVICE, "$" + WelcomeHomeRulesProvider.CONFIG_UNIT);
    actionConfig.put(WelcomeHomeActionType.CONFIG_RESULT, "$" + WelcomeHomeRulesProvider.CONFIG_EXPECTED_RESULT);
    List<Action> actions = new ArrayList<Action>();
    actions.add(new Action("AirConditionerSwitchOnAction", WelcomeHomeActionType.UID, actionConfig, null));
    // initialize configDescriptions
    List<ConfigDescriptionParameter> configDescriptions = new ArrayList<ConfigDescriptionParameter>();
    final ConfigDescriptionParameter device = ConfigDescriptionParameterBuilder.create(WelcomeHomeRulesProvider.CONFIG_UNIT, Type.TEXT).withRequired(true).withReadOnly(true).withMultiple(false).withLabel("Device").withDescription("Device description").build();
    final ConfigDescriptionParameter result = ConfigDescriptionParameterBuilder.create(WelcomeHomeRulesProvider.CONFIG_EXPECTED_RESULT, Type.TEXT).withRequired(true).withReadOnly(true).withMultiple(false).withLabel("Result").withDescription("Result description").build();
    final ConfigDescriptionParameter temperature = ConfigDescriptionParameterBuilder.create(CONFIG_TARGET_TEMPERATURE, Type.INTEGER).withRequired(true).withReadOnly(true).withMultiple(false).withLabel("Target temperature").withDescription("Indicates the target temperature.").build();
    final ConfigDescriptionParameter operation = ConfigDescriptionParameterBuilder.create(CONFIG_OPERATION, Type.TEXT).withRequired(true).withReadOnly(true).withMultiple(false).withLabel("Heating/Cooling").withDescription("Indicates Heating or Cooling is set.").build();
    configDescriptions.add(device);
    configDescriptions.add(result);
    configDescriptions.add(temperature);
    configDescriptions.add(operation);
    // initialize tags
    Set<String> tags = new HashSet<String>();
    tags.add("AirConditioner");
    tags.add("LivingRoom");
    // create the template
    return new AirConditionerRuleTemplate(tags, triggers, conditions, actions, configDescriptions);
}
Also used : Condition(org.eclipse.smarthome.automation.Condition) Action(org.eclipse.smarthome.automation.Action) Configuration(org.eclipse.smarthome.config.core.Configuration) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Trigger(org.eclipse.smarthome.automation.Trigger) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter) HashSet(java.util.HashSet)

Example 14 with ConfigDescriptionParameter

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

the class ConfigI18nLocalizationService method getLocalizedConfigDescription.

/**
 * Localize a config description.
 *
 * @param bundle the bundle the i18n resources are located
 * @param configDescription the config description that should be localized
 * @param locale the locale it should be localized to
 * @return a localized configuration description on success, a non-localized one on error (e.g. no translation is
 *         found).
 */
public ConfigDescription getLocalizedConfigDescription(final Bundle bundle, final ConfigDescription configDescription, @Nullable final Locale locale) {
    final List<ConfigDescriptionParameter> localizedConfigDescriptionParameters = new ArrayList<>(configDescription.getParameters().size());
    // Loop through all the configuration parameters and localize them
    for (final ConfigDescriptionParameter configDescriptionParameter : configDescription.getParameters()) {
        final ConfigDescriptionParameter localizedConfigDescriptionParameter = getLocalizedConfigDescriptionParameter(bundle, configDescription, configDescriptionParameter, locale);
        localizedConfigDescriptionParameters.add(localizedConfigDescriptionParameter);
    }
    final List<ConfigDescriptionParameterGroup> localizedConfigDescriptionGroups = new ArrayList<>(configDescription.getParameterGroups().size());
    // Loop through all the configuration groups and localize them
    for (final ConfigDescriptionParameterGroup configDescriptionParameterGroup : configDescription.getParameterGroups()) {
        final ConfigDescriptionParameterGroup localizedConfigDescriptionGroup = getLocalizedConfigDescriptionGroup(bundle, configDescription, configDescriptionParameterGroup, locale);
        localizedConfigDescriptionGroups.add(localizedConfigDescriptionGroup);
    }
    return new ConfigDescription(configDescription.getUID(), localizedConfigDescriptionParameters, localizedConfigDescriptionGroups);
}
Also used : ConfigDescriptionParameterGroup(org.eclipse.smarthome.config.core.ConfigDescriptionParameterGroup) ArrayList(java.util.ArrayList) ConfigDescription(org.eclipse.smarthome.config.core.ConfigDescription) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter)

Example 15 with ConfigDescriptionParameter

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

the class ConfigDescriptionValidatorImpl method validate.

/**
 * Validates the given configuration parameters against the given configuration description having the given URI.
 *
 * @param configurationParameters the configuration parameters to be validated
 * @param configDescriptionURI the URI of the configuration description against which the configuration parameters
 *            are to be validated
 *
 * @throws ConfigValidationException if one or more configuration parameters do not match with the configuration
 *             description having the given URI
 * @throws NullPointerException if given config description URI or configuration parameters are null
 */
@Override
@SuppressWarnings("unchecked")
public void validate(Map<String, Object> configurationParameters, URI configDescriptionURI) {
    Objects.requireNonNull(configurationParameters, "Configuration parameters must not be null");
    Objects.requireNonNull(configDescriptionURI, "Config description URI must not be null");
    ConfigDescription configDescription = getConfigDescription(configDescriptionURI);
    if (configDescription == null) {
        logger.warn("Skipping config description validation because no config description found for URI '{}'", configDescriptionURI);
        return;
    }
    Map<String, ConfigDescriptionParameter> map = configDescription.toParametersMap();
    Collection<ConfigValidationMessage> configDescriptionValidationMessages = new ArrayList<>();
    for (String key : configurationParameters.keySet()) {
        ConfigDescriptionParameter configDescriptionParameter = map.get(key);
        if (configDescriptionParameter != null) {
            // If the parameter supports multiple selection, then it may be provided as an array
            if (configDescriptionParameter.isMultiple() && configurationParameters.get(key) instanceof List) {
                // Perform validation on each value in the list separately
                for (Object value : (List<Object>) configurationParameters.get(key)) {
                    ConfigValidationMessage message = validateParameter(configDescriptionParameter, value);
                    if (message != null) {
                        configDescriptionValidationMessages.add(message);
                    }
                }
            } else {
                ConfigValidationMessage message = validateParameter(configDescriptionParameter, configurationParameters.get(key));
                if (message != null) {
                    configDescriptionValidationMessages.add(message);
                }
            }
        }
    }
    if (!configDescriptionValidationMessages.isEmpty()) {
        throw new ConfigValidationException(bundleContext.getBundle(), translationProvider, configDescriptionValidationMessages);
    }
}
Also used : ConfigValidationMessage(org.eclipse.smarthome.config.core.validation.ConfigValidationMessage) ArrayList(java.util.ArrayList) ConfigDescription(org.eclipse.smarthome.config.core.ConfigDescription) ArrayList(java.util.ArrayList) List(java.util.List) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter) ConfigValidationException(org.eclipse.smarthome.config.core.validation.ConfigValidationException)

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