Search in sources :

Example 1 with ActionHandler

use of org.openhab.core.automation.handler.ActionHandler 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 ActionHandler

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

the class CompositeActionHandler method execute.

/**
 * The method calls handlers of child action, collect their outputs and sets the output of the parent action.
 *
 * @see org.openhab.core.automation.handler.ActionHandler#execute(java.util.Map)
 */
@Override
@Nullable
public Map<String, Object> execute(Map<String, Object> context) {
    final Map<String, Object> result = new HashMap<>();
    final List<Action> children = getChildren();
    final Map<String, Object> compositeContext = getCompositeContext(context);
    for (Action child : children) {
        ActionHandler childHandler = moduleHandlerMap.get(child);
        Map<String, Object> childContext = Collections.unmodifiableMap(getChildContext(child, compositeContext));
        Map<String, Object> childResults = childHandler == null ? null : childHandler.execute(childContext);
        if (childResults != null) {
            for (Entry<String, Object> childResult : childResults.entrySet()) {
                String childOuputName = child.getId() + "." + childResult.getKey();
                Output output = compositeOutputs.get(childOuputName);
                if (output != null) {
                    String childOuputRef = output.getReference();
                    if (childOuputRef != null && childOuputRef.length() > childOuputName.length()) {
                        childOuputRef = childOuputRef.substring(childOuputName.length());
                        result.put(output.getName(), ReferenceResolver.resolveComplexDataReference(childResult.getValue(), childOuputRef));
                    } else {
                        result.put(output.getName(), childResult.getValue());
                    }
                }
            }
        }
    }
    return !result.isEmpty() ? result : null;
}
Also used : Action(org.openhab.core.automation.Action) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Output(org.openhab.core.automation.type.Output) ActionHandler(org.openhab.core.automation.handler.ActionHandler) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 3 with ActionHandler

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

the class CompositeModuleHandlerFactory method internalCreate.

@Override
@Nullable
public ModuleHandler internalCreate(Module module, String ruleUID) {
    ModuleHandler handler = null;
    String moduleType = module.getTypeUID();
    ModuleType mt = mtRegistry.get(moduleType);
    if (mt instanceof CompositeTriggerType) {
        List<Trigger> childModules = ((CompositeTriggerType) mt).getChildren();
        LinkedHashMap<Trigger, @Nullable TriggerHandler> mapModuleToHandler = getChildHandlers(module.getId(), module.getConfiguration(), childModules, ruleUID);
        if (mapModuleToHandler != null) {
            handler = new CompositeTriggerHandler((Trigger) module, (CompositeTriggerType) mt, mapModuleToHandler, ruleUID);
        }
    } else if (mt instanceof CompositeConditionType) {
        List<Condition> childModules = ((CompositeConditionType) mt).getChildren();
        LinkedHashMap<Condition, @Nullable ConditionHandler> mapModuleToHandler = getChildHandlers(module.getId(), module.getConfiguration(), childModules, ruleUID);
        if (mapModuleToHandler != null) {
            handler = new CompositeConditionHandler((Condition) module, (CompositeConditionType) mt, mapModuleToHandler, ruleUID);
        }
    } else if (mt instanceof CompositeActionType) {
        List<Action> childModules = ((CompositeActionType) mt).getChildren();
        LinkedHashMap<Action, @Nullable ActionHandler> mapModuleToHandler = getChildHandlers(module.getId(), module.getConfiguration(), childModules, ruleUID);
        if (mapModuleToHandler != null) {
            handler = new CompositeActionHandler((Action) module, (CompositeActionType) mt, mapModuleToHandler, ruleUID);
        }
    }
    if (handler != null) {
        logger.debug("Set module handler: {}  -> {} of rule {}.", module.getId(), handler.getClass().getSimpleName() + "(" + moduleType + ")", ruleUID);
    } else {
        logger.debug("Not found module handler {} for moduleType {} of rule {}.", module.getId(), moduleType, ruleUID);
    }
    return handler;
}
Also used : Action(org.openhab.core.automation.Action) TriggerHandler(org.openhab.core.automation.handler.TriggerHandler) CompositeConditionType(org.openhab.core.automation.type.CompositeConditionType) LinkedHashMap(java.util.LinkedHashMap) ModuleHandler(org.openhab.core.automation.handler.ModuleHandler) CompositeTriggerType(org.openhab.core.automation.type.CompositeTriggerType) ModuleType(org.openhab.core.automation.type.ModuleType) Trigger(org.openhab.core.automation.Trigger) ArrayList(java.util.ArrayList) List(java.util.List) CompositeActionType(org.openhab.core.automation.type.CompositeActionType) ActionHandler(org.openhab.core.automation.handler.ActionHandler) Nullable(org.eclipse.jdt.annotation.Nullable) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 4 with ActionHandler

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

the class RuleEngineImpl 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(WrappedRule rule, boolean stopOnFirstFail) {
    final String ruleUID = rule.getUID();
    final Collection<WrappedAction> actions = rule.getActions();
    if (actions.isEmpty()) {
        return;
    }
    RuleStatus ruleStatus = null;
    for (WrappedAction wrappedAction : actions) {
        ruleStatus = getRuleStatus(ruleUID);
        if (ruleStatus != RuleStatus.RUNNING) {
            return;
        }
        final Action action = wrappedAction.unwrap();
        ActionHandler aHandler = wrappedAction.getModuleHandler();
        if (aHandler != null) {
            Map<String, Object> context = getContext(ruleUID, wrappedAction.getConnections());
            try {
                Map<String, ?> outputs = aHandler.execute(Collections.unmodifiableMap(context));
                if (outputs != null) {
                    context = getContext(ruleUID, null);
                    updateContext(ruleUID, action.getId(), outputs);
                }
            } catch (Throwable t) {
                String errMessage = "Fail to execute action: " + action.getId();
                if (stopOnFirstFail) {
                    RuntimeException re = new RuntimeException(errMessage, t);
                    throw re;
                } else {
                    logger.warn(errMessage, t);
                }
            }
        }
    }
}
Also used : Action(org.openhab.core.automation.Action) WrappedAction(org.openhab.core.automation.internal.ruleengine.WrappedAction) RuleStatus(org.openhab.core.automation.RuleStatus) WrappedAction(org.openhab.core.automation.internal.ruleengine.WrappedAction) ActionHandler(org.openhab.core.automation.handler.ActionHandler)

Aggregations

ActionHandler (org.openhab.core.automation.handler.ActionHandler)4 Action (org.openhab.core.automation.Action)3 LinkedHashMap (java.util.LinkedHashMap)2 Nullable (org.eclipse.jdt.annotation.Nullable)2 TriggerHandler (org.openhab.core.automation.handler.TriggerHandler)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 RuleStatus (org.openhab.core.automation.RuleStatus)1 Trigger (org.openhab.core.automation.Trigger)1 ConditionHandler (org.openhab.core.automation.handler.ConditionHandler)1 ModuleHandler (org.openhab.core.automation.handler.ModuleHandler)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 CompositeActionType (org.openhab.core.automation.type.CompositeActionType)1 CompositeConditionType (org.openhab.core.automation.type.CompositeConditionType)1 CompositeTriggerType (org.openhab.core.automation.type.CompositeTriggerType)1 ModuleType (org.openhab.core.automation.type.ModuleType)1 Output (org.openhab.core.automation.type.Output)1