Search in sources :

Example 6 with RuleStatus

use of org.eclipse.smarthome.automation.RuleStatus in project smarthome by eclipse.

the class RuleEngine method added.

@Override
public void added(ModuleType moduleType) {
    String moduleTypeName = moduleType.getUID();
    for (ModuleHandlerFactory moduleHandlerFactory : allModuleHandlerFactories) {
        Collection<String> moduleTypes = moduleHandlerFactory.getTypes();
        if (moduleTypes.contains(moduleTypeName)) {
            synchronized (this) {
                this.moduleHandlerFactories.put(moduleTypeName, moduleHandlerFactory);
            }
            break;
        }
    }
    Set<String> rules = null;
    synchronized (this) {
        Set<String> rulesPerModule = mapModuleTypeToRules.get(moduleTypeName);
        if (rulesPerModule != null) {
            rules = new HashSet<String>();
            rules.addAll(rulesPerModule);
        }
    }
    if (rules != null) {
        for (String rUID : rules) {
            RuleStatus ruleStatus = getRuleStatus(rUID);
            if (ruleStatus == RuleStatus.UNINITIALIZED) {
                scheduleRuleInitialization(rUID);
            }
        }
    }
}
Also used : CompositeModuleHandlerFactory(org.eclipse.smarthome.automation.core.internal.composite.CompositeModuleHandlerFactory) ModuleHandlerFactory(org.eclipse.smarthome.automation.handler.ModuleHandlerFactory) RuleStatus(org.eclipse.smarthome.automation.RuleStatus)

Example 7 with RuleStatus

use of org.eclipse.smarthome.automation.RuleStatus in project smarthome by eclipse.

the class RuleEngine method executeActions.

/**
 * This method evaluates actions of the {@link Rule} and set their {@link Output}s when they exists.
 *
 * @param rule executed rule.
 */
private void executeActions(Rule rule, boolean stopOnFirstFail) {
    List<Action> actions = ((RuntimeRule) rule).getActions();
    if (actions.size() == 0) {
        return;
    }
    RuleStatus ruleStatus = null;
    for (Iterator<Action> it = actions.iterator(); it.hasNext(); ) {
        ruleStatus = getRuleStatus(rule.getUID());
        if (ruleStatus != RuleStatus.RUNNING) {
            return;
        }
        RuntimeAction action = (RuntimeAction) it.next();
        ActionHandler aHandler = action.getModuleHandler();
        String rUID = rule.getUID();
        Map<String, Object> context = getContext(rUID, action.getConnections());
        try {
            Map<String, ?> outputs = aHandler.execute(Collections.unmodifiableMap(context));
            if (outputs != null) {
                context = getContext(rUID);
                updateContext(rUID, action.getId(), outputs);
            }
        } catch (Throwable t) {
            String errMessage = "Failed to execute action '" + action.getId() + "': " + t.getMessage();
            if (stopOnFirstFail) {
                RuntimeException re = new RuntimeException(errMessage, t);
                throw re;
            } else {
                logger.warn("Error message: {}", errMessage);
            }
        }
    }
}
Also used : Action(org.eclipse.smarthome.automation.Action) RuleStatus(org.eclipse.smarthome.automation.RuleStatus) ActionHandler(org.eclipse.smarthome.automation.handler.ActionHandler)

Example 8 with RuleStatus

use of org.eclipse.smarthome.automation.RuleStatus in project smarthome by eclipse.

the class AutomationCommandList method listRules.

/**
 * This method is responsible for execution of command {@link AutomationCommands#LIST_RULES}.
 *
 * @return a string representing understandable for the user message containing information on the outcome of the
 *         command {@link AutomationCommands#LIST_RULES}.
 */
private String listRules() {
    Collection<Rule> collection = autoCommands.getRules();
    Map<String, Rule> rules = new Hashtable<String, Rule>();
    Map<String, String> listRules = null;
    if (collection != null && !collection.isEmpty()) {
        addCollection(collection, rules);
        String[] uids = new String[rules.size()];
        Utils.quickSort(rules.keySet().toArray(uids), 0, rules.size());
        listRules = Utils.putInHastable(uids);
    }
    if (listRules != null && !listRules.isEmpty()) {
        if (id != null) {
            collection = getRuleByFilter(listRules);
            if (collection.size() == 1) {
                Rule r = (Rule) collection.toArray()[0];
                if (r != null) {
                    RuleStatus status = autoCommands.getRuleStatus(r.getUID());
                    return Printer.printRule(r, status);
                } else {
                    return String.format("Nonexistent ID: %s", id);
                }
            } else if (collection.isEmpty()) {
                return String.format("Nonexistent ID: %s", id);
            } else {
                if (!rules.isEmpty()) {
                    rules.clear();
                }
                addCollection(collection, rules);
                listRules = Utils.filterList(rules, listRules);
            }
        }
        return Printer.printRules(autoCommands, listRules);
    }
    return "There are no Rules available!";
}
Also used : Hashtable(java.util.Hashtable) RuleStatus(org.eclipse.smarthome.automation.RuleStatus) Rule(org.eclipse.smarthome.automation.Rule)

Example 9 with RuleStatus

use of org.eclipse.smarthome.automation.RuleStatus in project smarthome by eclipse.

the class RuleEngine method setRuleEnabled.

/**
 * This method can switch enabled/ disabled state of the {@link Rule}
 *
 * @param rUID unique id of the rule
 * @param isEnabled true to enable the rule, false to disable it
 */
protected void setRuleEnabled(Rule rule, boolean isEnabled) {
    String rUID = rule.getUID();
    RuleStatus status = getRuleStatus(rUID);
    String enabled = isEnabled ? "enabled" : "disabled";
    RuntimeRule runtimeRule = getRuntimeRule(rUID);
    if (runtimeRule == null) {
        logger.debug("There is no rule with UID '{}' which could be {}", rUID, enabled);
        return;
    }
    if (isEnabled) {
        if (status == RuleStatus.DISABLED) {
            setRule(runtimeRule);
        } else {
            logger.debug("The rule rId = {} is already enabled.", rUID);
        }
    } else {
        unregister(runtimeRule);
        setRuleStatusInfo(rUID, new RuleStatusInfo(RuleStatus.DISABLED), true);
    }
}
Also used : RuleStatusInfo(org.eclipse.smarthome.automation.RuleStatusInfo) RuleStatus(org.eclipse.smarthome.automation.RuleStatus)

Example 10 with RuleStatus

use of org.eclipse.smarthome.automation.RuleStatus in project smarthome by eclipse.

the class RuleEngine method addNewModuleTypes.

private void addNewModuleTypes(ModuleHandlerFactory mhf, Collection<String> moduleTypes) {
    Set<String> notInitailizedRules = null;
    for (Iterator<String> it = moduleTypes.iterator(); it.hasNext(); ) {
        String moduleTypeName = it.next();
        Set<String> rules = null;
        synchronized (this) {
            moduleHandlerFactories.put(moduleTypeName, mhf);
            Set<String> rulesPerModule = mapModuleTypeToRules.get(moduleTypeName);
            if (rulesPerModule != null) {
                rules = new HashSet<String>();
                rules.addAll(rulesPerModule);
            }
        }
        if (rules != null) {
            for (String rUID : rules) {
                RuleStatus ruleStatus = getRuleStatus(rUID);
                if (ruleStatus == RuleStatus.UNINITIALIZED) {
                    notInitailizedRules = notInitailizedRules != null ? notInitailizedRules : new HashSet<String>(20);
                    notInitailizedRules.add(rUID);
                }
            }
        }
    }
    if (notInitailizedRules != null) {
        for (final String rUID : notInitailizedRules) {
            scheduleRuleInitialization(rUID);
        }
    }
}
Also used : RuleStatus(org.eclipse.smarthome.automation.RuleStatus) HashSet(java.util.HashSet)

Aggregations

RuleStatus (org.eclipse.smarthome.automation.RuleStatus)10 RuleStatusInfo (org.eclipse.smarthome.automation.RuleStatusInfo)5 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Hashtable (java.util.Hashtable)1 List (java.util.List)1 Action (org.eclipse.smarthome.automation.Action)1 Condition (org.eclipse.smarthome.automation.Condition)1 Rule (org.eclipse.smarthome.automation.Rule)1 CompositeModuleHandlerFactory (org.eclipse.smarthome.automation.core.internal.composite.CompositeModuleHandlerFactory)1 ActionHandler (org.eclipse.smarthome.automation.handler.ActionHandler)1 ConditionHandler (org.eclipse.smarthome.automation.handler.ConditionHandler)1 ModuleHandlerFactory (org.eclipse.smarthome.automation.handler.ModuleHandlerFactory)1