Search in sources :

Example 16 with Action

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

the class WelcomeHomeRulesProvider method createLightsRule.

/**
 * This method creates a rule from scratch by using trigger, condition, action, configDescriptions and
 * configuration, tags.
 *
 * @return the created rule
 */
private Rule createLightsRule() {
    // initialize the trigger
    String triggerId = "LightsSwitchOnRuleTrigger";
    List<Trigger> triggers = new ArrayList<Trigger>();
    triggers.add(new Trigger(triggerId, LightsTriggerType.UID, null));
    // initialize the condition - here the tricky part is the referring into the condition input - trigger output.
    // The syntax is a similar to the JUEL syntax.
    Configuration config = new Configuration();
    config.put(StateConditionType.CONFIG_STATE, "on");
    List<Condition> conditions = new ArrayList<Condition>();
    Map<String, String> inputs = new HashMap<String, String>();
    inputs.put(StateConditionType.INPUT_CURRENT_STATE, triggerId + "." + StateConditionType.INPUT_CURRENT_STATE);
    conditions.add(new Condition("LightsStateCondition", StateConditionType.UID, config, inputs));
    // initialize the action - here the tricky part is the referring into the action configuration parameter - the
    // template configuration parameter. The syntax is a similar to the JUEL syntax.
    config = new Configuration();
    config.put(WelcomeHomeActionType.CONFIG_DEVICE, "Lights");
    config.put(WelcomeHomeActionType.CONFIG_RESULT, "Lights are switched on");
    List<Action> actions = new ArrayList<Action>();
    actions.add(new Action("LightsSwitchOnAction", WelcomeHomeActionType.UID, config, null));
    // initialize the configDescriptions
    List<ConfigDescriptionParameter> configDescriptions = new ArrayList<ConfigDescriptionParameter>();
    final ConfigDescriptionParameter device = ConfigDescriptionParameterBuilder.create(WelcomeHomeRulesProvider.CONFIG_UNIT, Type.TEXT).withRequired(true).withReadOnly(true).withMultiple(false).withLabel("Device").withDescription("Device description").build();
    final ConfigDescriptionParameter result = ConfigDescriptionParameterBuilder.create(WelcomeHomeRulesProvider.CONFIG_EXPECTED_RESULT, Type.TEXT).withRequired(true).withReadOnly(true).withMultiple(false).withLabel("Result").withDescription("Result description").build();
    configDescriptions.add(device);
    configDescriptions.add(result);
    // initialize the configuration
    config = new Configuration();
    config.put(CONFIG_UNIT, "Lights");
    config.put(CONFIG_EXPECTED_RESULT, "The lights are switched on.");
    // create the rule
    Rule lightsSwitchOn = new Rule(L_UID);
    lightsSwitchOn.setTriggers(triggers);
    lightsSwitchOn.setConfigurationDescriptions(configDescriptions);
    lightsSwitchOn.setConditions(conditions);
    lightsSwitchOn.setActions(actions);
    // initialize the tags
    Set<String> tags = new HashSet<String>();
    tags.add("lights");
    // set the tags
    lightsSwitchOn.setTags(tags);
    return lightsSwitchOn;
}
Also used : Condition(org.eclipse.smarthome.automation.Condition) Action(org.eclipse.smarthome.automation.Action) Configuration(org.eclipse.smarthome.config.core.Configuration) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Trigger(org.eclipse.smarthome.automation.Trigger) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter) Rule(org.eclipse.smarthome.automation.Rule) HashSet(java.util.HashSet)

Example 17 with Action

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

the class RuleTemplateRegistry method copyActions.

private List<Action> copyActions(List<Action> actions) {
    List<Action> res = new ArrayList<Action>();
    if (actions != null) {
        for (Action a : actions) {
            Configuration c = new Configuration();
            c.setProperties(a.getConfiguration().getProperties());
            Action action = new Action(a.getId(), a.getTypeUID(), c, a.getInputs());
            action.setLabel(a.getLabel());
            action.setDescription(a.getDescription());
            res.add(action);
        }
    }
    return res;
}
Also used : Action(org.eclipse.smarthome.automation.Action) Configuration(org.eclipse.smarthome.config.core.Configuration) ArrayList(java.util.ArrayList)

Example 18 with Action

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

the class ModuleTypeRegistryImpl method copyActions.

private static List<Action> copyActions(List<Action> actions) {
    List<Action> res = new ArrayList<Action>();
    if (actions != null) {
        for (Action a : actions) {
            Configuration c = new Configuration();
            c.setProperties(a.getConfiguration().getProperties());
            Action action = new Action(a.getId(), a.getTypeUID(), c, a.getInputs());
            action.setLabel(a.getLabel());
            action.setDescription(a.getDescription());
            res.add(action);
        }
    }
    return res;
}
Also used : Action(org.eclipse.smarthome.automation.Action) Configuration(org.eclipse.smarthome.config.core.Configuration) ArrayList(java.util.ArrayList)

Example 19 with Action

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

the class ConnectionValidator method checkConnection.

/**
 * The method validates the connection between outputs of list of triggers and actions to the action's input. It
 * checks if the input is unconnected and compatibility of data types of the input and connected output. Throws
 * exception if they are incompatible.
 *
 * @param connection that should be validated
 * @param input that should be validated
 * @param triggers is a list with triggers of the rule on which the action belongs
 * @param actions is a list with actions of the rule on which the action belongs
 * @throws IllegalArgumentException when validation fails.
 */
private static void checkConnection(Connection connection, Input input, List<Trigger> triggers, List<Action> actions) {
    Map<String, Action> actionsMap = new HashMap<String, Action>();
    for (Action a : actions) {
        actionsMap.put(a.getId(), a);
    }
    String moduleId = connection.getOuputModuleId();
    Action action = actionsMap.get(moduleId);
    String msg = " Invalid Connection \"" + connection.getInputName() + "\" : ";
    if (moduleId != null && action != null) {
        String typeUID = action.getTypeUID();
        ActionType actionType = (ActionType) mtRegistry.get(typeUID);
        if (actionType == null) {
            throw new IllegalArgumentException(msg + " Action Type with UID \"" + typeUID + "\" does not exist!");
        }
        checkCompatibility(msg, connection, input, actionType.getOutputs());
    } else {
        checkConnection(connection, input, triggers);
    }
}
Also used : RuntimeAction(org.eclipse.smarthome.automation.core.internal.RuntimeAction) Action(org.eclipse.smarthome.automation.Action) ActionType(org.eclipse.smarthome.automation.type.ActionType) HashMap(java.util.HashMap)

Example 20 with Action

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

the class RunRuleModuleTest method createOuterRule.

private Rule createOuterRule() {
    final Configuration outerRuleTriggerConfig = new Configuration(Collections.unmodifiableMap(Stream.of(new SimpleEntry<>("eventSource", "ruleTrigger"), new SimpleEntry<>("eventTopic", "smarthome/*"), new SimpleEntry<>("eventTypes", "ItemStateEvent")).collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue()))));
    final Configuration outerRuleActionConfig = new Configuration(Collections.unmodifiableMap(Stream.of(new SimpleEntry<>("ruleUIDs", "[exampleSceneRule]")).collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue()))));
    final Rule outerRule = new Rule("sceneActivationRule");
    outerRule.setTriggers(Arrays.asList(new Trigger[] { new Trigger("ItemStateChangeTrigger2", "core.GenericEventTrigger", outerRuleTriggerConfig) }));
    outerRule.setActions(Arrays.asList(new Action[] { new Action("RunRuleAction1", "core.RunRuleAction", outerRuleActionConfig, null) }));
    outerRule.setName("scene activator");
    return outerRule;
}
Also used : Trigger(org.eclipse.smarthome.automation.Trigger) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Arrays(java.util.Arrays) ProviderChangeListener(org.eclipse.smarthome.core.common.registry.ProviderChangeListener) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) LoggerFactory(org.slf4j.LoggerFactory) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) ItemProvider(org.eclipse.smarthome.core.items.ItemProvider) EventFilter(org.eclipse.smarthome.core.events.EventFilter) EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) LinkedList(java.util.LinkedList) SimpleEntry(java.util.AbstractMap.SimpleEntry) Configuration(org.eclipse.smarthome.config.core.Configuration) RuleRegistry(org.eclipse.smarthome.automation.RuleRegistry) Before(org.junit.Before) RuleStatus(org.eclipse.smarthome.automation.RuleStatus) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) VolatileStorageService(org.eclipse.smarthome.test.storage.VolatileStorageService) Logger(org.slf4j.Logger) ItemEventFactory(org.eclipse.smarthome.core.items.events.ItemEventFactory) Collection(java.util.Collection) Set(java.util.Set) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) Test(org.junit.Test) Rule(org.eclipse.smarthome.automation.Rule) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException) Collectors(java.util.stream.Collectors) Item(org.eclipse.smarthome.core.items.Item) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) Stream(java.util.stream.Stream) Action(org.eclipse.smarthome.automation.Action) Queue(java.util.Queue) Assert(org.junit.Assert) Event(org.eclipse.smarthome.core.events.Event) Collections(java.util.Collections) Action(org.eclipse.smarthome.automation.Action) Trigger(org.eclipse.smarthome.automation.Trigger) Configuration(org.eclipse.smarthome.config.core.Configuration) SimpleEntry(java.util.AbstractMap.SimpleEntry) Rule(org.eclipse.smarthome.automation.Rule)

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