Search in sources :

Example 21 with Action

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

the class RuntimeRuleTest method ruleTriggeredByCompositeTrigger.

@Test
public void ruleTriggeredByCompositeTrigger() throws ItemNotFoundException, InterruptedException {
    // //Test the creation of a rule out of
    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<>("itemName", "myLampItem3"), new SimpleEntry<>("command", "ON")).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
    final Rule rule = new Rule("myRule21" + new Random().nextInt() + "_COMPOSITE");
    rule.setTriggers(Arrays.asList(new Trigger[] { new Trigger("ItemStateChangeTrigger3", "core.ItemStateChangeTrigger", triggerConfig) }));
    rule.setActions(Arrays.asList(new Action[] { new Action("ItemPostCommandAction3", "core.ItemCommandAction", actionConfig, null) }));
    rule.setName("RuleByJAVA_API_WithCompositeTrigger");
    logger.info("Rule created: {}", rule.getUID());
    final RuleRegistry ruleRegistry = getService(RuleRegistry.class);
    ruleRegistry.add(rule);
    // Test rule
    waitForAssert(() -> {
        Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.getStatusInfo(rule.getUID()).getStatus());
    });
    final Queue<Event> events = new LinkedList<>();
    registerService(new EventSubscriber() {

        @Override
        public void receive(final Event event) {
            logger.info("RuleEvent: {}", event.getTopic());
            events.add(event);
        }

        @Override
        public Set<String> getSubscribedEventTypes() {
            return Collections.singleton(RuleStatusInfoEvent.TYPE);
        }

        @Override
        public EventFilter getEventFilter() {
            return null;
        }
    });
    final EventPublisher eventPublisher = getService(EventPublisher.class);
    eventPublisher.post(ItemEventFactory.createStateEvent("myPresenceItem3", OnOffType.ON));
    eventPublisher.post(ItemEventFactory.createStateEvent("myMotionItem3", OnOffType.ON));
    waitForAssert(() -> {
        assertFalse(events.isEmpty());
        RuleStatusInfoEvent event = (RuleStatusInfoEvent) events.remove();
        assertEquals(RuleStatus.RUNNING, event.getStatusInfo().getStatus());
    });
    waitForAssert(() -> {
        assertFalse(events.isEmpty());
        RuleStatusInfoEvent event = (RuleStatusInfoEvent) events.remove();
        assertEquals(RuleStatus.IDLE, event.getStatusInfo().getStatus());
    });
}
Also used : EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) Action(org.eclipse.smarthome.automation.Action) Set(java.util.Set) Configuration(org.eclipse.smarthome.config.core.Configuration) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) RuleRegistry(org.eclipse.smarthome.automation.RuleRegistry) EventFilter(org.eclipse.smarthome.core.events.EventFilter) LinkedList(java.util.LinkedList) RuleStatusInfoEvent(org.eclipse.smarthome.automation.events.RuleStatusInfoEvent) Trigger(org.eclipse.smarthome.automation.Trigger) Random(java.util.Random) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) RuleStatusInfoEvent(org.eclipse.smarthome.automation.events.RuleStatusInfoEvent) Event(org.eclipse.smarthome.core.events.Event) Rule(org.eclipse.smarthome.automation.Rule) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 22 with Action

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

the class RuntimeRuleTest method itemStateUpdatedBySimpleRule.

@Test
public void itemStateUpdatedBySimpleRule() throws ItemNotFoundException, InterruptedException {
    final Configuration triggerConfig = new Configuration(Stream.of(new SimpleEntry<>("eventSource", "myMotionItem2"), new SimpleEntry<>("eventTopic", "smarthome/*"), new SimpleEntry<>("eventTypes", "ItemStateEvent")).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
    final Configuration actionConfig = new Configuration(Stream.of(new SimpleEntry<>("itemName", "myLampItem2"), new SimpleEntry<>("command", "ON")).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
    final Rule rule = new Rule("myRule21" + new Random().nextInt());
    rule.setTriggers(Arrays.asList(new Trigger[] { new Trigger("ItemStateChangeTrigger2", "core.GenericEventTrigger", triggerConfig) }));
    rule.setActions(Arrays.asList(new Action[] { new Action("ItemPostCommandAction2", "core.ItemCommandAction", actionConfig, null) }));
    // I would expect the factory to create the UID of the rule and the name to be in the list of parameters.
    rule.setName("RuleByJAVA_API");
    logger.info("Rule created: {}", rule.getUID());
    final RuleRegistry ruleRegistry = getService(RuleRegistry.class);
    ruleRegistry.add(rule);
    ruleRegistry.setEnabled(rule.getUID(), true);
    waitForAssert(() -> {
        Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.getStatusInfo(rule.getUID()).getStatus());
    });
    // Test rule
    final EventPublisher eventPublisher = getService(EventPublisher.class);
    Assert.assertNotNull(eventPublisher);
    eventPublisher.post(ItemEventFactory.createStateEvent("myPresenceItem2", OnOffType.ON));
    final Queue<Event> events = new LinkedList<>();
    registerService(new EventSubscriber() {

        @Override
        public void receive(final Event event) {
            logger.info("Event: {}", event.getTopic());
            events.add(event);
        }

        @Override
        public Set<String> getSubscribedEventTypes() {
            return Collections.singleton(ItemCommandEvent.TYPE);
        }

        @Override
        public EventFilter getEventFilter() {
            return null;
        }
    });
    eventPublisher.post(ItemEventFactory.createStateEvent("myMotionItem2", OnOffType.ON));
    waitForAssert(() -> {
        assertFalse(events.isEmpty());
        ItemCommandEvent event = (ItemCommandEvent) events.remove();
        assertEquals("smarthome/items/myLampItem2/command", event.getTopic());
        assertEquals(OnOffType.ON, event.getItemCommand());
    });
}
Also used : EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) Action(org.eclipse.smarthome.automation.Action) Set(java.util.Set) Configuration(org.eclipse.smarthome.config.core.Configuration) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) RuleRegistry(org.eclipse.smarthome.automation.RuleRegistry) EventFilter(org.eclipse.smarthome.core.events.EventFilter) LinkedList(java.util.LinkedList) Trigger(org.eclipse.smarthome.automation.Trigger) Random(java.util.Random) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) RuleStatusInfoEvent(org.eclipse.smarthome.automation.events.RuleStatusInfoEvent) Event(org.eclipse.smarthome.core.events.Event) Rule(org.eclipse.smarthome.automation.Rule) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 23 with Action

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

the class CoreModuleHandlerFactory method internalCreate.

@Override
protected synchronized ModuleHandler internalCreate(final Module module, final String ruleUID) {
    logger.trace("create {} -> {} : {}", module.getId(), module.getTypeUID(), ruleUID);
    final String moduleTypeUID = module.getTypeUID();
    if (module instanceof Trigger) {
        if (GenericEventTriggerHandler.MODULE_TYPE_ID.equals(moduleTypeUID)) {
            return new GenericEventTriggerHandler((Trigger) module, this.bundleContext);
        } else if (ChannelEventTriggerHandler.MODULE_TYPE_ID.equals(moduleTypeUID)) {
            return new ChannelEventTriggerHandler((Trigger) module, this.bundleContext);
        } else if (ItemCommandTriggerHandler.MODULE_TYPE_ID.equals(moduleTypeUID)) {
            return new ItemCommandTriggerHandler((Trigger) module, this.bundleContext);
        } else if (ItemStateTriggerHandler.CHANGE_MODULE_TYPE_ID.equals(moduleTypeUID) || ItemStateTriggerHandler.UPDATE_MODULE_TYPE_ID.equals(moduleTypeUID)) {
            return new ItemStateTriggerHandler((Trigger) module, this.bundleContext);
        }
    } else if (module instanceof Condition) {
        // Handle conditions
        if (ItemStateConditionHandler.ITEM_STATE_CONDITION.equals(moduleTypeUID)) {
            ItemStateConditionHandler handler = new ItemStateConditionHandler((Condition) module);
            handler.setItemRegistry(itemRegistry);
            return handler;
        } else if (GenericEventConditionHandler.MODULETYPE_ID.equals(moduleTypeUID)) {
            return new GenericEventConditionHandler((Condition) module);
        } else if (CompareConditionHandler.MODULE_TYPE.equals(moduleTypeUID)) {
            return new CompareConditionHandler((Condition) module);
        }
    } else if (module instanceof Action) {
        if (ItemCommandActionHandler.ITEM_COMMAND_ACTION.equals(moduleTypeUID)) {
            final ItemCommandActionHandler postCommandActionHandler = new ItemCommandActionHandler((Action) module);
            postCommandActionHandler.setEventPublisher(eventPublisher);
            postCommandActionHandler.setItemRegistry(itemRegistry);
            return postCommandActionHandler;
        } else if (RuleEnablementActionHandler.UID.equals(moduleTypeUID)) {
            return new RuleEnablementActionHandler((Action) module, ruleRegistry);
        } else if (RunRuleActionHandler.UID.equals(moduleTypeUID)) {
            return new RunRuleActionHandler((Action) module, ruleRegistry);
        }
    }
    logger.error("The ModuleHandler is not supported:{}", moduleTypeUID);
    return null;
}
Also used : Condition(org.eclipse.smarthome.automation.Condition) GenericEventConditionHandler(org.eclipse.smarthome.automation.module.core.handler.GenericEventConditionHandler) Action(org.eclipse.smarthome.automation.Action) ItemStateTriggerHandler(org.eclipse.smarthome.automation.module.core.handler.ItemStateTriggerHandler) ItemCommandTriggerHandler(org.eclipse.smarthome.automation.module.core.handler.ItemCommandTriggerHandler) ItemCommandActionHandler(org.eclipse.smarthome.automation.module.core.handler.ItemCommandActionHandler) ChannelEventTriggerHandler(org.eclipse.smarthome.automation.module.core.handler.ChannelEventTriggerHandler) RuleEnablementActionHandler(org.eclipse.smarthome.automation.module.core.handler.RuleEnablementActionHandler) Trigger(org.eclipse.smarthome.automation.Trigger) GenericEventTriggerHandler(org.eclipse.smarthome.automation.module.core.handler.GenericEventTriggerHandler) ItemStateConditionHandler(org.eclipse.smarthome.automation.module.core.handler.ItemStateConditionHandler) CompareConditionHandler(org.eclipse.smarthome.automation.module.core.handler.CompareConditionHandler) RunRuleActionHandler(org.eclipse.smarthome.automation.module.core.handler.RunRuleActionHandler)

Example 24 with Action

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

the class ModuleTypeResourceBundleProvider method createLocalizedActionType.

/**
 * Utility method for localization of ActionTypes.
 *
 * @param at is an ActionType for localization.
 * @param bundle the bundle providing localization resources.
 * @param moduleTypeUID is an ActionType uid.
 * @param locale represents a specific geographical, political, or cultural region.
 * @param lconfigDescriptions are ActionType localized config descriptions.
 * @param llabel is an ActionType localized label.
 * @param ldescription is an ActionType localized description.
 * @return localized ActionType.
 */
private ActionType createLocalizedActionType(ActionType at, Bundle bundle, String moduleTypeUID, Locale locale, List<ConfigDescriptionParameter> lconfigDescriptions, String llabel, String ldescription) {
    List<Input> inputs = ModuleTypeI18nUtil.getLocalizedInputs(i18nProvider, at.getInputs(), bundle, moduleTypeUID, locale);
    List<Output> outputs = ModuleTypeI18nUtil.getLocalizedOutputs(i18nProvider, at.getOutputs(), bundle, moduleTypeUID, locale);
    ActionType lat = null;
    if (at instanceof CompositeActionType) {
        List<Action> modules = ModuleI18nUtil.getLocalizedModules(i18nProvider, ((CompositeActionType) at).getChildren(), bundle, moduleTypeUID, ModuleTypeI18nUtil.MODULE_TYPE, locale);
        lat = new CompositeActionType(moduleTypeUID, lconfigDescriptions, llabel, ldescription, at.getTags(), at.getVisibility(), inputs, outputs, modules);
    } else {
        lat = new ActionType(moduleTypeUID, lconfigDescriptions, llabel, ldescription, at.getTags(), at.getVisibility(), inputs, outputs);
    }
    return lat;
}
Also used : Input(org.eclipse.smarthome.automation.type.Input) Action(org.eclipse.smarthome.automation.Action) CompositeActionType(org.eclipse.smarthome.automation.type.CompositeActionType) ActionType(org.eclipse.smarthome.automation.type.ActionType) Output(org.eclipse.smarthome.automation.type.Output) CompositeActionType(org.eclipse.smarthome.automation.type.CompositeActionType)

Example 25 with Action

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

the class TemplateResourceBundleProvider method getPerLocale.

/**
 * This method is used to localize the {@link Template}s.
 *
 * @param element is the {@link Template} that must be localized.
 * @param locale represents a specific geographical, political, or cultural region.
 * @return the localized {@link Template}.
 */
private RuleTemplate getPerLocale(RuleTemplate defTemplate, Locale locale) {
    if (locale == null || defTemplate == null || i18nProvider == null) {
        return defTemplate;
    }
    String uid = defTemplate.getUID();
    Bundle bundle = getBundle(uid);
    if (defTemplate instanceof RuleTemplate) {
        String llabel = RuleTemplateI18nUtil.getLocalizedRuleTemplateLabel(i18nProvider, bundle, uid, defTemplate.getLabel(), locale);
        String ldescription = RuleTemplateI18nUtil.getLocalizedRuleTemplateDescription(i18nProvider, bundle, uid, defTemplate.getDescription(), locale);
        List<ConfigDescriptionParameter> lconfigDescriptions = getLocalizedConfigurationDescription(i18nProvider, defTemplate.getConfigurationDescriptions(), bundle, uid, RuleTemplateI18nUtil.RULE_TEMPLATE, locale);
        List<Action> lactions = ModuleI18nUtil.getLocalizedModules(i18nProvider, defTemplate.getActions(), bundle, uid, RuleTemplateI18nUtil.RULE_TEMPLATE, locale);
        List<Condition> lconditions = ModuleI18nUtil.getLocalizedModules(i18nProvider, defTemplate.getConditions(), bundle, uid, RuleTemplateI18nUtil.RULE_TEMPLATE, locale);
        List<Trigger> ltriggers = ModuleI18nUtil.getLocalizedModules(i18nProvider, defTemplate.getTriggers(), bundle, uid, RuleTemplateI18nUtil.RULE_TEMPLATE, locale);
        return new RuleTemplate(uid, llabel, ldescription, defTemplate.getTags(), ltriggers, lconditions, lactions, lconfigDescriptions, defTemplate.getVisibility());
    }
    return null;
}
Also used : Condition(org.eclipse.smarthome.automation.Condition) Action(org.eclipse.smarthome.automation.Action) Trigger(org.eclipse.smarthome.automation.Trigger) Bundle(org.osgi.framework.Bundle) RuleTemplate(org.eclipse.smarthome.automation.template.RuleTemplate) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter)

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