Search in sources :

Example 6 with Action

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

the class RuntimeRuleTest method ruleEnableHandlerWorks.

@Test
@Ignore
public void ruleEnableHandlerWorks() throws ItemNotFoundException {
    final RuleRegistry ruleRegistry = getService(RuleRegistry.class);
    final String firstRuleUID = "FirstTestRule";
    final String secondRuleUID = "SecondTestRule";
    final String thirdRuleUID = "ThirdTestRule";
    final String[] firstConfig = new String[] { "FirstTestRule", "SecondTestRule" };
    final String[] secondConfig = new String[] { "FirstTestRule" };
    final String firstRuleAction = "firstRuleAction";
    final String secondRuleAction = "secondRuleAction";
    try {
        final Configuration triggerConfig = new Configuration(Stream.of(new SimpleEntry<>("itemName", "myMotionItem3")).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
        final Configuration actionConfig = new Configuration(Stream.of(new SimpleEntry<>("enable", false), new SimpleEntry<>("ruleUIDs", firstConfig)).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
        final Rule rule = new Rule(firstRuleAction);
        rule.setTriggers(Arrays.asList(new Trigger[] { new Trigger("ItemStateChangeTrigger3", "core.ItemStateChangeTrigger", triggerConfig) }));
        rule.setActions(Arrays.asList(new Action[] { new Action("RuleAction", "core.RuleEnablementAction", actionConfig, null) }));
        ruleRegistry.add(new Rule(firstRuleUID));
        ruleRegistry.add(new Rule(secondRuleUID));
        ruleRegistry.add(new Rule(thirdRuleUID));
        ruleRegistry.add(rule);
        final ItemRegistry itemRegistry = getService(ItemRegistry.class);
        final EventPublisher eventPublisher = getService(EventPublisher.class);
        final Item myMotionItem = itemRegistry.getItem("myMotionItem3");
        eventPublisher.post(ItemEventFactory.createCommandEvent("myMotionItem3", TypeParser.parseCommand(myMotionItem.getAcceptedCommandTypes(), "ON")));
        waitForAssert(() -> {
            Assert.assertEquals(RuleStatus.DISABLED, ruleRegistry.getStatus(firstRuleUID));
            Assert.assertEquals(RuleStatus.DISABLED, ruleRegistry.getStatus(secondRuleUID));
            Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.getStatus(thirdRuleUID));
        });
        final Configuration triggerConfig2 = new Configuration(Stream.of(new SimpleEntry<>("itemName", "myMotionItem3")).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
        final Configuration actionConfig2 = new Configuration(Stream.of(new SimpleEntry<>("enable", true), new SimpleEntry<>("ruleUIDs", secondConfig)).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
        final Rule rule2 = new Rule(secondRuleAction);
        rule2.setTriggers(Arrays.asList(new Trigger[] { new Trigger("ItemStateChangeTrigger3", "core.ItemStateChangeTrigger", triggerConfig2) }));
        rule2.setActions(Arrays.asList(new Action[] { new Action("RuleAction", "core.RuleEnablementAction", actionConfig2, null) }));
        ruleRegistry.add(rule2);
        eventPublisher.post(ItemEventFactory.createCommandEvent("myMotionItem3", TypeParser.parseCommand(myMotionItem.getAcceptedCommandTypes(), "OFF")));
        waitForAssert(() -> {
            Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.getStatus(firstRuleUID));
            Assert.assertEquals(RuleStatus.DISABLED, ruleRegistry.getStatus(secondRuleUID));
            Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.getStatus(thirdRuleUID));
        });
    } finally {
        ruleRegistry.remove(firstRuleUID);
        ruleRegistry.remove(secondRuleUID);
        ruleRegistry.remove(thirdRuleUID);
        ruleRegistry.remove(firstRuleAction);
        ruleRegistry.remove(secondRuleAction);
    }
}
Also used : SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Item(org.eclipse.smarthome.core.items.Item) Action(org.eclipse.smarthome.automation.Action) Trigger(org.eclipse.smarthome.automation.Trigger) Configuration(org.eclipse.smarthome.config.core.Configuration) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) RuleRegistry(org.eclipse.smarthome.automation.RuleRegistry) Rule(org.eclipse.smarthome.automation.Rule) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) Ignore(org.junit.Ignore) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 7 with Action

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

the class RuleUtils method getActionsCopy.

/**
 * This method creates deep copy of list of actions
 *
 * @param actions list of actions
 * @return deep copy of list of actions or empty list when the parameter is null.
 */
public static List<Action> getActionsCopy(List<Action> actions) {
    List<Action> res = new ArrayList<Action>();
    if (actions != null) {
        for (Action a : actions) {
            Action action = new Action(a.getId(), a.getTypeUID(), new Configuration(a.getConfiguration().getProperties()), new HashMap<String, String>(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 8 with Action

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

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

the class ModuleI18nUtil method createLocalizedAction.

private static Action createLocalizedAction(Action module, String label, String description) {
    Action action = new Action(module.getId(), module.getTypeUID(), module.getConfiguration(), module.getInputs());
    action.setLabel(label);
    action.setDescription(description);
    return action;
}
Also used : Action(org.eclipse.smarthome.automation.Action)

Example 10 with Action

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

the class AirConditionerRuleTemplate method initialize.

public static AirConditionerRuleTemplate initialize() {
    // initialize triggers
    List<Trigger> triggers = new ArrayList<Trigger>();
    triggers.add(new Trigger(TRIGGER_ID, AirConditionerTriggerType.UID, null));
    // initialize conditions
    // here the tricky part is the giving a value to the condition configuration parameter.
    Configuration conditionConfig = new Configuration();
    conditionConfig.put(StateConditionType.CONFIG_STATE, "on");
    // here the tricky part is the referring into the condition input - trigger output.
    // The syntax is a similar to the JUEL syntax.
    Map<String, String> conditionInputs = new HashMap<String, String>();
    conditionInputs.put(StateConditionType.INPUT_CURRENT_STATE, TRIGGER_ID + "." + StateConditionType.INPUT_CURRENT_STATE);
    Condition stateCondition = new Condition("AirConditionerStateCondition", StateConditionType.UID, conditionConfig, conditionInputs);
    // here the tricky part is the referring into the condition configuration parameter - the
    // template configuration parameter. The syntax is a similar to the JUEL syntax.
    conditionConfig = new Configuration();
    conditionConfig.put(TemperatureConditionType.CONFIG_TEMPERATURE, "$" + CONFIG_TARGET_TEMPERATURE);
    conditionConfig.put(TemperatureConditionType.CONFIG_OPERATOR, "$" + CONFIG_OPERATION);
    // here the tricky part is the referring into the condition input - trigger output.
    // The syntax is a similar to the JUEL syntax.
    conditionInputs = new HashMap<String, String>();
    conditionInputs.put(TemperatureConditionType.INPUT_CURRENT_TEMPERATURE, TRIGGER_ID + "." + TemperatureConditionType.INPUT_CURRENT_TEMPERATURE);
    Condition temperatureCondition = new Condition("AirConditionerTemperatureCondition", TemperatureConditionType.UID, conditionConfig, conditionInputs);
    List<Condition> conditions = new ArrayList<Condition>();
    conditions.add(stateCondition);
    conditions.add(temperatureCondition);
    // initialize actions - 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.
    Configuration actionConfig = new Configuration();
    actionConfig.put(WelcomeHomeActionType.CONFIG_DEVICE, "$" + WelcomeHomeRulesProvider.CONFIG_UNIT);
    actionConfig.put(WelcomeHomeActionType.CONFIG_RESULT, "$" + WelcomeHomeRulesProvider.CONFIG_EXPECTED_RESULT);
    List<Action> actions = new ArrayList<Action>();
    actions.add(new Action("AirConditionerSwitchOnAction", WelcomeHomeActionType.UID, actionConfig, null));
    // initialize 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();
    final ConfigDescriptionParameter temperature = ConfigDescriptionParameterBuilder.create(CONFIG_TARGET_TEMPERATURE, Type.INTEGER).withRequired(true).withReadOnly(true).withMultiple(false).withLabel("Target temperature").withDescription("Indicates the target temperature.").build();
    final ConfigDescriptionParameter operation = ConfigDescriptionParameterBuilder.create(CONFIG_OPERATION, Type.TEXT).withRequired(true).withReadOnly(true).withMultiple(false).withLabel("Heating/Cooling").withDescription("Indicates Heating or Cooling is set.").build();
    configDescriptions.add(device);
    configDescriptions.add(result);
    configDescriptions.add(temperature);
    configDescriptions.add(operation);
    // initialize tags
    Set<String> tags = new HashSet<String>();
    tags.add("AirConditioner");
    tags.add("LivingRoom");
    // create the template
    return new AirConditionerRuleTemplate(tags, triggers, conditions, actions, configDescriptions);
}
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) HashSet(java.util.HashSet)

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