Search in sources :

Example 1 with RuleTemplate

use of org.eclipse.smarthome.automation.template.RuleTemplate in project smarthome by eclipse.

the class RuleTemplateRegistry method getByTags.

@Override
public Collection<RuleTemplate> getByTags(Locale locale, String... tags) {
    Set<String> tagSet = tags != null ? new HashSet<String>(Arrays.asList(tags)) : null;
    Collection<RuleTemplate> result = new ArrayList<RuleTemplate>(20);
    for (Provider<RuleTemplate> provider : elementMap.keySet()) {
        for (RuleTemplate resultTemplate : elementMap.get(provider)) {
            Collection<String> tTags = resultTemplate.getTags();
            RuleTemplate t = locale == null ? resultTemplate : ((RuleTemplateProvider) provider).getTemplate(resultTemplate.getUID(), locale);
            if (tagSet == null) {
                result.add(t);
            } else if (tTags != null && tTags.containsAll(tagSet)) {
                result.add(t);
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) RuleTemplate(org.eclipse.smarthome.automation.template.RuleTemplate)

Example 2 with RuleTemplate

use of org.eclipse.smarthome.automation.template.RuleTemplate in project smarthome by eclipse.

the class RuleRegistryImpl method resolveRuleByTemplate.

/**
 * The method checks if the rule has to be resolved by template or not. If the rule does not contain tempateUID it
 * returns same rule, otherwise it tries to resolve the rule created from template. If the template is available
 * the method creates a new rule based on triggers, conditions and actions from template. If the template is not
 * available returns the same rule.
 *
 * @param rule a rule defined by template.
 * @return the resolved rule(containing modules defined by the template) or not resolved rule, if the template is
 *         missing.
 */
private Rule resolveRuleByTemplate(Rule rule) {
    String templateUID = rule.getTemplateUID();
    if (templateUID != null) {
        RuleTemplate template = templateRegistry.get(templateUID);
        if (template == null) {
            logger.debug("Rule template {} does not exist.", templateUID);
            return rule;
        } else {
            Rule resolvedRule = new Rule(rule.getUID(), RuleUtils.getTriggersCopy(template.getTriggers()), RuleUtils.getConditionsCopy(template.getConditions()), RuleUtils.getActionsCopy(template.getActions()), template.getConfigurationDescriptions(), rule.getConfiguration(), null, rule.getVisibility());
            String name = rule.getName();
            if (name != null) {
                resolvedRule.setName(name);
            }
            resolvedRule.setTags(rule.getTags());
            String description = rule.getDescription();
            if (description != null) {
                resolvedRule.setDescription(description);
            }
            ruleEngine.resolveConfiguration(resolvedRule);
            return resolvedRule;
        }
    }
    return rule;
}
Also used : RuleTemplate(org.eclipse.smarthome.automation.template.RuleTemplate) Rule(org.eclipse.smarthome.automation.Rule)

Example 3 with RuleTemplate

use of org.eclipse.smarthome.automation.template.RuleTemplate in project smarthome by eclipse.

the class TemplateGSONParser method parse.

@Override
public Set<Template> parse(InputStreamReader reader) throws ParsingException {
    JsonReader jr = new JsonReader(reader);
    try {
        if (jr.hasNext()) {
            JsonToken token = jr.peek();
            Set<Template> templates = new HashSet<>();
            if (JsonToken.BEGIN_ARRAY.equals(token)) {
                templates.addAll(gson.fromJson(jr, new TypeToken<List<RuleTemplate>>() {
                }.getType()));
            } else {
                Template template = gson.fromJson(jr, RuleTemplate.class);
                templates.add(template);
            }
            return templates;
        }
    } catch (Exception e) {
        throw new ParsingException(new ParsingNestedException(ParsingNestedException.TEMPLATE, null, e));
    } finally {
        try {
            jr.close();
        } catch (IOException e) {
        }
    }
    return Collections.emptySet();
}
Also used : ParsingException(org.eclipse.smarthome.automation.parser.ParsingException) ParsingNestedException(org.eclipse.smarthome.automation.parser.ParsingNestedException) JsonReader(com.google.gson.stream.JsonReader) JsonToken(com.google.gson.stream.JsonToken) List(java.util.List) IOException(java.io.IOException) ParsingException(org.eclipse.smarthome.automation.parser.ParsingException) ParsingNestedException(org.eclipse.smarthome.automation.parser.ParsingNestedException) IOException(java.io.IOException) RuleTemplate(org.eclipse.smarthome.automation.template.RuleTemplate) Template(org.eclipse.smarthome.automation.template.Template) HashSet(java.util.HashSet)

Example 4 with RuleTemplate

use of org.eclipse.smarthome.automation.template.RuleTemplate in project smarthome by eclipse.

the class AutomationCommandList method getTemplateByFilter.

/**
 * This method reduces the list of {@link Template}s so that their unique identifier or part of it to match the
 * {@link #id} or
 * the index in the <tt>list</tt> to match the {@link #id}.
 *
 * @param list is the list of {@link Template}s for reducing.
 * @return a collection of {@link Template}s that match the filter.
 */
private Collection<RuleTemplate> getTemplateByFilter(Map<String, String> list) {
    Collection<RuleTemplate> templates = new ArrayList<RuleTemplate>();
    RuleTemplate t = null;
    String uid = list.get(id);
    if (uid != null) {
        t = autoCommands.getTemplate(uid, locale);
        if (t != null) {
            templates.add(t);
            return templates;
        }
    } else {
        t = autoCommands.getTemplate(id, locale);
        if (t != null) {
            templates.add(t);
            return templates;
        } else {
            for (String templateUID : list.keySet()) {
                if (templateUID.indexOf(id) != -1) {
                    templates.add(autoCommands.getTemplate(templateUID, locale));
                }
            }
        }
    }
    return templates;
}
Also used : ArrayList(java.util.ArrayList) RuleTemplate(org.eclipse.smarthome.automation.template.RuleTemplate)

Example 5 with RuleTemplate

use of org.eclipse.smarthome.automation.template.RuleTemplate in project smarthome by eclipse.

the class AutomationCommandList method listTemplates.

/**
 * This method is responsible for execution of command {@link AutomationCommands#LIST_TEMPLATES}.
 *
 * @return a string representing understandable for the user message containing information on the outcome of the
 *         command {@link AutomationCommands#LIST_TEMPLATES}.
 */
private String listTemplates() {
    Collection<RuleTemplate> collection = autoCommands.getTemplates(locale);
    Map<String, Template> templates = new Hashtable<String, Template>();
    Map<String, String> listTemplates = null;
    if (collection != null && !collection.isEmpty()) {
        addCollection(collection, templates);
        String[] uids = new String[templates.size()];
        Utils.quickSort(templates.keySet().toArray(uids), 0, templates.size());
        listTemplates = Utils.putInHastable(uids);
    }
    if (listTemplates != null && !listTemplates.isEmpty()) {
        if (id != null) {
            collection = getTemplateByFilter(listTemplates);
            if (collection.size() == 1) {
                Template t = (Template) collection.toArray()[0];
                if (t != null) {
                    return Printer.printTemplate(t);
                } else {
                    return String.format("Nonexistent ID: %s", id);
                }
            } else if (collection.isEmpty()) {
                return String.format("Nonexistent ID: %s", id);
            } else {
                if (!templates.isEmpty()) {
                    templates.clear();
                }
                addCollection(collection, templates);
                listTemplates = Utils.filterList(templates, listTemplates);
            }
        }
        if (listTemplates != null && !listTemplates.isEmpty()) {
            return Printer.printTemplates(listTemplates);
        }
    }
    return "There are no Templates available!";
}
Also used : Hashtable(java.util.Hashtable) RuleTemplate(org.eclipse.smarthome.automation.template.RuleTemplate) RuleTemplate(org.eclipse.smarthome.automation.template.RuleTemplate) Template(org.eclipse.smarthome.automation.template.Template)

Aggregations

RuleTemplate (org.eclipse.smarthome.automation.template.RuleTemplate)10 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)2 ParsingException (org.eclipse.smarthome.automation.parser.ParsingException)2 ParsingNestedException (org.eclipse.smarthome.automation.parser.ParsingNestedException)2 Template (org.eclipse.smarthome.automation.template.Template)2 JsonReader (com.google.gson.stream.JsonReader)1 JsonToken (com.google.gson.stream.JsonToken)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 InputStreamReader (java.io.InputStreamReader)1 HashSet (java.util.HashSet)1 Hashtable (java.util.Hashtable)1 List (java.util.List)1 Locale (java.util.Locale)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 Action (org.eclipse.smarthome.automation.Action)1 Condition (org.eclipse.smarthome.automation.Condition)1