Search in sources :

Example 1 with ConditionHandler

use of org.eclipse.smarthome.automation.handler.ConditionHandler in project smarthome by eclipse.

the class RuleEngine method setModuleHandlers.

/**
 * This method links modules to corresponding module handlers.
 *
 * @param rUID id of rule containing these modules
 * @param modules list of modules
 * @return null when all modules are connected or list of RuleErrors for missing handlers.
 */
private <T extends Module> String setModuleHandlers(String rUID, List<T> modules) {
    StringBuffer sb = null;
    if (modules != null) {
        for (T m : modules) {
            try {
                ModuleHandler moduleHandler = getModuleHandler(m, rUID);
                if (moduleHandler != null) {
                    if (m instanceof RuntimeAction) {
                        ((RuntimeAction) m).setModuleHandler((ActionHandler) moduleHandler);
                    } else if (m instanceof RuntimeCondition) {
                        ((RuntimeCondition) m).setModuleHandler((ConditionHandler) moduleHandler);
                    } else if (m instanceof RuntimeTrigger) {
                        ((RuntimeTrigger) m).setModuleHandler((TriggerHandler) moduleHandler);
                    }
                } else {
                    if (sb == null) {
                        sb = new StringBuffer();
                    }
                    String message = "Missing handler '" + m.getTypeUID() + "' for module '" + m.getId() + "'";
                    sb.append(message).append("\n");
                    logger.trace(message);
                }
            } catch (Throwable t) {
                if (sb == null) {
                    sb = new StringBuffer();
                }
                String message = "Getting handler '" + m.getTypeUID() + "' for module '" + m.getId() + "' failed: " + t.getMessage();
                sb.append(message).append("\n");
                logger.trace(message);
            }
        }
    }
    return sb != null ? sb.toString() : null;
}
Also used : ModuleHandler(org.eclipse.smarthome.automation.handler.ModuleHandler) ConditionHandler(org.eclipse.smarthome.automation.handler.ConditionHandler)

Example 2 with ConditionHandler

use of org.eclipse.smarthome.automation.handler.ConditionHandler in project smarthome by eclipse.

the class RuleEngine method calculateConditions.

/**
 * This method checks if all rule's condition are satisfied or not.
 *
 * @param rule the checked rule
 * @return true when all conditions of the rule are satisfied, false otherwise.
 */
private boolean calculateConditions(Rule rule) {
    List<Condition> conditions = ((RuntimeRule) rule).getConditions();
    if (conditions.size() == 0) {
        return true;
    }
    RuleStatus ruleStatus = null;
    for (Iterator<Condition> it = conditions.iterator(); it.hasNext(); ) {
        ruleStatus = getRuleStatus(rule.getUID());
        if (ruleStatus != RuleStatus.RUNNING) {
            return false;
        }
        RuntimeCondition c = (RuntimeCondition) it.next();
        ConditionHandler tHandler = c.getModuleHandler();
        Map<String, Object> context = getContext(rule.getUID(), c.getConnections());
        if (!tHandler.isSatisfied(Collections.unmodifiableMap(context))) {
            logger.debug("The condition '{}' of rule '{}' is unsatisfied.", new Object[] { c.getId(), rule.getUID() });
            return false;
        }
    }
    return true;
}
Also used : Condition(org.eclipse.smarthome.automation.Condition) ConditionHandler(org.eclipse.smarthome.automation.handler.ConditionHandler) RuleStatus(org.eclipse.smarthome.automation.RuleStatus)

Example 3 with ConditionHandler

use of org.eclipse.smarthome.automation.handler.ConditionHandler in project smarthome by eclipse.

the class CompositeConditionHandler method isSatisfied.

/**
 * The method calls handlers of child modules and return true only when they all are satisfied.
 *
 * @see org.eclipse.smarthome.automation.handler.ConditionHandler#isSatisfied(java.util.Map)
 */
@Override
public boolean isSatisfied(Map<String, Object> context) {
    List<Condition> children = getChildren();
    Map<String, Object> compositeContext = getCompositeContext(context);
    for (Condition child : children) {
        Map<String, Object> childContext = Collections.unmodifiableMap(getChildContext(child, compositeContext));
        ConditionHandler childHandler = moduleHandlerMap.get(child);
        boolean isSatisfied = childHandler.isSatisfied(childContext);
        if (!isSatisfied) {
            return false;
        }
    }
    return true;
}
Also used : Condition(org.eclipse.smarthome.automation.Condition) ConditionHandler(org.eclipse.smarthome.automation.handler.ConditionHandler)

Aggregations

ConditionHandler (org.eclipse.smarthome.automation.handler.ConditionHandler)3 Condition (org.eclipse.smarthome.automation.Condition)2 RuleStatus (org.eclipse.smarthome.automation.RuleStatus)1 ModuleHandler (org.eclipse.smarthome.automation.handler.ModuleHandler)1