Search in sources :

Example 26 with Action

use of org.eclipse.smarthome.automation.Action 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)

Example 27 with Action

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

the class ScriptModuleHandlerFactory method internalCreate.

@Override
protected ModuleHandler internalCreate(Module module, String ruleUID) {
    logger.trace("create {} -> {}", module.getId(), module.getTypeUID());
    String moduleTypeUID = module.getTypeUID();
    if (moduleTypeUID != null) {
        if (ScriptConditionHandler.SCRIPT_CONDITION.equals(moduleTypeUID) && module instanceof Condition) {
            ScriptConditionHandler handler = new ScriptConditionHandler((Condition) module, ruleUID, scriptEngineManager);
            return handler;
        } else if (ScriptActionHandler.SCRIPT_ACTION_ID.equals(moduleTypeUID) && module instanceof Action) {
            ScriptActionHandler handler = new ScriptActionHandler((Action) module, ruleUID, scriptEngineManager);
            return handler;
        } else {
            logger.error("The ModuleHandler is not supported: {}", moduleTypeUID);
        }
    } else {
        logger.error("ModuleType is not registered: {}", moduleTypeUID);
    }
    return null;
}
Also used : Condition(org.eclipse.smarthome.automation.Condition) ScriptConditionHandler(org.eclipse.smarthome.automation.module.script.internal.handler.ScriptConditionHandler) ScriptActionHandler(org.eclipse.smarthome.automation.module.script.internal.handler.ScriptActionHandler) Action(org.eclipse.smarthome.automation.Action)

Example 28 with Action

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

the class ScriptedAutomationManager method addRule.

public Rule addRule(Rule element) {
    Rule rule = element.getUID() == null ? new Rule(generateUID()) : new Rule(element.getUID());
    String name = element.getName();
    if (name == null || name.isEmpty()) {
        name = element.getClass().getSimpleName();
        if (name.contains("$")) {
            name = name.substring(0, name.indexOf('$'));
        }
    }
    rule.setName(name);
    rule.setDescription(element.getDescription());
    rule.setTags(element.getTags());
    // used for numbering the modules of the rule
    int moduleIndex = 1;
    try {
        ArrayList<Condition> conditions = new ArrayList<>();
        for (Condition cond : element.getConditions()) {
            Condition toAdd = cond;
            if (cond.getId() == null || cond.getId().isEmpty()) {
                toAdd = new Condition(Integer.toString(moduleIndex++), cond.getTypeUID(), cond.getConfiguration(), cond.getInputs());
            }
            conditions.add(toAdd);
        }
        rule.setConditions(conditions);
    } catch (Exception ex) {
    // conditions are optional
    }
    try {
        ArrayList<Trigger> triggers = new ArrayList<>();
        for (Trigger trigger : element.getTriggers()) {
            Trigger toAdd = trigger;
            if (trigger.getId() == null || trigger.getId().isEmpty()) {
                toAdd = new Trigger(Integer.toString(moduleIndex++), trigger.getTypeUID(), trigger.getConfiguration());
            }
            triggers.add(toAdd);
        }
        rule.setTriggers(triggers);
    } catch (Exception ex) {
    // triggers are optional
    }
    ArrayList<Action> actions = new ArrayList<>();
    actions.addAll(element.getActions());
    if (element instanceof SimpleRuleActionHandler) {
        String privId = addPrivateActionHandler(new SimpleRuleActionHandlerDelegate((SimpleRuleActionHandler) element));
        Action scriptedAction = new Action(Integer.toString(moduleIndex++), "jsr223.ScriptedAction", new Configuration(), null);
        scriptedAction.getConfiguration().put("privId", privId);
        actions.add(scriptedAction);
    }
    rule.setActions(actions);
    ruleRegistryDelegate.add(rule);
    return rule;
}
Also used : Condition(org.eclipse.smarthome.automation.Condition) Action(org.eclipse.smarthome.automation.Action) Configuration(org.eclipse.smarthome.config.core.Configuration) SimpleRuleActionHandlerDelegate(org.eclipse.smarthome.automation.module.script.rulesupport.shared.simple.SimpleRuleActionHandlerDelegate) ArrayList(java.util.ArrayList) Trigger(org.eclipse.smarthome.automation.Trigger) Rule(org.eclipse.smarthome.automation.Rule) SimpleRuleActionHandler(org.eclipse.smarthome.automation.module.script.rulesupport.shared.simple.SimpleRuleActionHandler)

Aggregations

Action (org.eclipse.smarthome.automation.Action)28 Trigger (org.eclipse.smarthome.automation.Trigger)15 Configuration (org.eclipse.smarthome.config.core.Configuration)15 Condition (org.eclipse.smarthome.automation.Condition)9 Rule (org.eclipse.smarthome.automation.Rule)9 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)8 HashMap (java.util.HashMap)6 Set (java.util.Set)5 RuleRegistry (org.eclipse.smarthome.automation.RuleRegistry)5 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)5 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)5 LinkedList (java.util.LinkedList)4 Event (org.eclipse.smarthome.core.events.Event)4 HashSet (java.util.HashSet)3 RuleStatus (org.eclipse.smarthome.automation.RuleStatus)3 ActionHandler (org.eclipse.smarthome.automation.handler.ActionHandler)3 ActionType (org.eclipse.smarthome.automation.type.ActionType)3 CompositeActionType (org.eclipse.smarthome.automation.type.CompositeActionType)3 ConfigDescriptionParameter (org.eclipse.smarthome.config.core.ConfigDescriptionParameter)3