Search in sources :

Example 1 with ConditionHandler

use of org.openhab.core.automation.handler.ConditionHandler in project openhab-core by openhab.

the class RuleEngineImpl method register.

/**
 * This method register the Rule to start working. This is the final step of initialization process where
 * triggers received {@link TriggerHandlerCallback}s object and starts to notify the rule engine when they are
 * triggered. After activating all triggers the rule goes into IDLE state.
 *
 * @param rule an initialized rule which has to starts tracking the triggers.
 */
private void register(WrappedRule rule) {
    final String ruleUID = rule.getUID();
    TriggerHandlerCallback thCallback = getTriggerHandlerCallback(ruleUID);
    rule.getTriggers().forEach(trigger -> {
        TriggerHandler triggerHandler = trigger.getModuleHandler();
        if (triggerHandler != null) {
            triggerHandler.setCallback(thCallback);
        }
    });
    rule.getConditions().forEach(condition -> {
        ConditionHandler conditionHandler = condition.getModuleHandler();
        if (conditionHandler != null) {
            conditionHandler.setCallback(moduleHandlerCallback);
        }
    });
    rule.getActions().forEach(action -> {
        ActionHandler actionHandler = action.getModuleHandler();
        if (actionHandler != null) {
            actionHandler.setCallback(moduleHandlerCallback);
        }
    });
}
Also used : TriggerHandler(org.openhab.core.automation.handler.TriggerHandler) SystemTriggerHandler(org.openhab.core.automation.internal.module.handler.SystemTriggerHandler) ConditionHandler(org.openhab.core.automation.handler.ConditionHandler) TriggerHandlerCallback(org.openhab.core.automation.handler.TriggerHandlerCallback) ActionHandler(org.openhab.core.automation.handler.ActionHandler)

Example 2 with ConditionHandler

use of org.openhab.core.automation.handler.ConditionHandler in project openhab-core by openhab.

the class RuleEngineImpl 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.
 */
@Nullable
private <T extends WrappedModule<?, ?>> String setModuleHandlers(String rUID, List<T> modules) {
    StringBuilder sb = null;
    for (T mm : modules) {
        final Module m = mm.unwrap();
        try {
            ModuleHandler moduleHandler = getModuleHandler(m, rUID);
            if (moduleHandler != null) {
                if (mm instanceof WrappedAction) {
                    ((WrappedAction) mm).setModuleHandler((ActionHandler) moduleHandler);
                } else if (mm instanceof WrappedCondition) {
                    ((WrappedCondition) mm).setModuleHandler((ConditionHandler) moduleHandler);
                } else if (mm instanceof WrappedTrigger) {
                    ((WrappedTrigger) mm).setModuleHandler((TriggerHandler) moduleHandler);
                }
            } else {
                if (sb == null) {
                    sb = new StringBuilder();
                }
                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 StringBuilder();
            }
            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.openhab.core.automation.handler.ModuleHandler) WrappedCondition(org.openhab.core.automation.internal.ruleengine.WrappedCondition) ConditionHandler(org.openhab.core.automation.handler.ConditionHandler) WrappedTrigger(org.openhab.core.automation.internal.ruleengine.WrappedTrigger) Module(org.openhab.core.automation.Module) WrappedModule(org.openhab.core.automation.internal.ruleengine.WrappedModule) WrappedAction(org.openhab.core.automation.internal.ruleengine.WrappedAction) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 3 with ConditionHandler

use of org.openhab.core.automation.handler.ConditionHandler in project openhab-core by openhab.

the class RuleEngineImpl 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(WrappedRule rule) {
    List<WrappedCondition> conditions = rule.getConditions();
    if (conditions.isEmpty()) {
        return true;
    }
    final String ruleUID = rule.getUID();
    RuleStatus ruleStatus = null;
    for (WrappedCondition wrappedCondition : conditions) {
        ruleStatus = getRuleStatus(ruleUID);
        if (ruleStatus != RuleStatus.RUNNING) {
            return false;
        }
        final Condition condition = wrappedCondition.unwrap();
        ConditionHandler tHandler = wrappedCondition.getModuleHandler();
        Map<String, Object> context = getContext(ruleUID, wrappedCondition.getConnections());
        if (tHandler != null && !tHandler.isSatisfied(Collections.unmodifiableMap(context))) {
            logger.debug("The condition '{}' of rule '{}' is unsatisfied.", condition.getId(), ruleUID);
            return false;
        }
    }
    return true;
}
Also used : WrappedCondition(org.openhab.core.automation.internal.ruleengine.WrappedCondition) Condition(org.openhab.core.automation.Condition) WrappedCondition(org.openhab.core.automation.internal.ruleengine.WrappedCondition) ConditionHandler(org.openhab.core.automation.handler.ConditionHandler) RuleStatus(org.openhab.core.automation.RuleStatus)

Example 4 with ConditionHandler

use of org.openhab.core.automation.handler.ConditionHandler in project openhab-core by openhab.

the class CompositeConditionHandler method isSatisfied.

/**
 * The method calls handlers of child modules and return true only when they all are satisfied.
 *
 * @see org.openhab.core.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.openhab.core.automation.Condition) ConditionHandler(org.openhab.core.automation.handler.ConditionHandler)

Aggregations

ConditionHandler (org.openhab.core.automation.handler.ConditionHandler)4 Condition (org.openhab.core.automation.Condition)2 WrappedCondition (org.openhab.core.automation.internal.ruleengine.WrappedCondition)2 Nullable (org.eclipse.jdt.annotation.Nullable)1 Module (org.openhab.core.automation.Module)1 RuleStatus (org.openhab.core.automation.RuleStatus)1 ActionHandler (org.openhab.core.automation.handler.ActionHandler)1 ModuleHandler (org.openhab.core.automation.handler.ModuleHandler)1 TriggerHandler (org.openhab.core.automation.handler.TriggerHandler)1 TriggerHandlerCallback (org.openhab.core.automation.handler.TriggerHandlerCallback)1 SystemTriggerHandler (org.openhab.core.automation.internal.module.handler.SystemTriggerHandler)1 WrappedAction (org.openhab.core.automation.internal.ruleengine.WrappedAction)1 WrappedModule (org.openhab.core.automation.internal.ruleengine.WrappedModule)1 WrappedTrigger (org.openhab.core.automation.internal.ruleengine.WrappedTrigger)1