Search in sources :

Example 1 with ActionHandler

use of org.eclipse.smarthome.automation.handler.ActionHandler 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 2 with ActionHandler

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

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.eclipse.smarthome.automation.handler.ActionHandler#execute(java.util.Map)
 */
@Override
public Map<String, Object> execute(Map<String, Object> context) {
    final Map<String, Object> result = new HashMap<String, Object>();
    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.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(), ReferenceResolverUtil.getValue(childResult.getValue(), childOuputRef));
                    } else {
                        result.put(output.getName(), childResult.getValue());
                    }
                }
            }
        }
    }
    return result.size() > 0 ? result : null;
}
Also used : Action(org.eclipse.smarthome.automation.Action) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) Output(org.eclipse.smarthome.automation.type.Output) ActionHandler(org.eclipse.smarthome.automation.handler.ActionHandler)

Example 3 with ActionHandler

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

the class CompositeModuleHandlerFactory method internalCreate.

@Override
public ModuleHandler internalCreate(Module module, String ruleUID) {
    ModuleHandler handler = null;
    if (module != null) {
        String moduleType = module.getTypeUID();
        ModuleType mt = mtRegistry.get(moduleType);
        if (mt instanceof CompositeTriggerType) {
            List<Trigger> childModules = ((CompositeTriggerType) mt).getChildren();
            LinkedHashMap<Trigger, 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, 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, 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.eclipse.smarthome.automation.Action) TriggerHandler(org.eclipse.smarthome.automation.handler.TriggerHandler) CompositeConditionType(org.eclipse.smarthome.automation.type.CompositeConditionType) LinkedHashMap(java.util.LinkedHashMap) ModuleHandler(org.eclipse.smarthome.automation.handler.ModuleHandler) CompositeTriggerType(org.eclipse.smarthome.automation.type.CompositeTriggerType) ModuleType(org.eclipse.smarthome.automation.type.ModuleType) Trigger(org.eclipse.smarthome.automation.Trigger) List(java.util.List) CompositeActionType(org.eclipse.smarthome.automation.type.CompositeActionType) ActionHandler(org.eclipse.smarthome.automation.handler.ActionHandler)

Aggregations

Action (org.eclipse.smarthome.automation.Action)3 ActionHandler (org.eclipse.smarthome.automation.handler.ActionHandler)3 LinkedHashMap (java.util.LinkedHashMap)2 HashMap (java.util.HashMap)1 List (java.util.List)1 RuleStatus (org.eclipse.smarthome.automation.RuleStatus)1 Trigger (org.eclipse.smarthome.automation.Trigger)1 ModuleHandler (org.eclipse.smarthome.automation.handler.ModuleHandler)1 TriggerHandler (org.eclipse.smarthome.automation.handler.TriggerHandler)1 CompositeActionType (org.eclipse.smarthome.automation.type.CompositeActionType)1 CompositeConditionType (org.eclipse.smarthome.automation.type.CompositeConditionType)1 CompositeTriggerType (org.eclipse.smarthome.automation.type.CompositeTriggerType)1 ModuleType (org.eclipse.smarthome.automation.type.ModuleType)1 Output (org.eclipse.smarthome.automation.type.Output)1