Search in sources :

Example 1 with ManagedRuleProvider

use of org.eclipse.smarthome.automation.core.ManagedRuleProvider in project smarthome by eclipse.

the class RuleRegistryImpl method addProvider.

@Override
protected void addProvider(Provider<Rule> provider) {
    super.addProvider(provider);
    forEach(provider, rule -> {
        try {
            Rule resolvedRule = resolveRuleByTemplate(rule);
            if (rule != resolvedRule && provider instanceof ManagedRuleProvider) {
                update(resolvedRule);
            }
        } catch (IllegalArgumentException e) {
            logger.error("Added rule '{}' is invalid", rule.getUID(), e);
        }
    });
}
Also used : ManagedRuleProvider(org.eclipse.smarthome.automation.core.ManagedRuleProvider) Rule(org.eclipse.smarthome.automation.Rule)

Example 2 with ManagedRuleProvider

use of org.eclipse.smarthome.automation.core.ManagedRuleProvider in project smarthome by eclipse.

the class RuleRegistryImpl method added.

@Override
public void added(RuleTemplate element) {
    String templateUID = element.getUID();
    Set<String> rules = new HashSet<String>();
    synchronized (this) {
        Set<String> rulesForResolving = mapTemplateToRules.get(templateUID);
        if (rulesForResolving != null) {
            rules.addAll(rulesForResolving);
        }
    }
    for (String rUID : rules) {
        try {
            Rule unresolvedRule = get(rUID);
            Rule resolvedRule = resolveRuleByTemplate(unresolvedRule);
            Provider<Rule> provider = getProvider(rUID);
            if (provider instanceof ManagedRuleProvider) {
                update(resolvedRule);
            } else {
                updated(provider, unresolvedRule, unresolvedRule);
            }
        } catch (IllegalArgumentException e) {
            logger.error("Resolving the rule '{}' by template '{}' failed", rUID, templateUID, e);
        }
    }
}
Also used : ManagedRuleProvider(org.eclipse.smarthome.automation.core.ManagedRuleProvider) Rule(org.eclipse.smarthome.automation.Rule) HashSet(java.util.HashSet)

Example 3 with ManagedRuleProvider

use of org.eclipse.smarthome.automation.core.ManagedRuleProvider in project smarthome by eclipse.

the class AutomationIntegrationTest method before.

@Before
public void before() {
    logger.info("@Before.begin");
    getService(ItemRegistry.class);
    ItemProvider itemProvider = new ItemProvider() {

        @Override
        public void addProviderChangeListener(@NonNull ProviderChangeListener<@NonNull Item> listener) {
        }

        @Override
        @NonNull
        public Collection<@NonNull Item> getAll() {
            Set<Item> items = new HashSet<>();
            items.add(new SwitchItem("myMotionItem"));
            items.add(new SwitchItem("myPresenceItem"));
            items.add(new SwitchItem("myLampItem"));
            items.add(new SwitchItem("myMotionItem2"));
            items.add(new SwitchItem("myPresenceItem2"));
            items.add(new SwitchItem("myLampItem2"));
            items.add(new SwitchItem("myMotionItem3"));
            items.add(new SwitchItem("templ_MotionItem"));
            items.add(new SwitchItem("templ_LampItem"));
            items.add(new SwitchItem("myMotionItem3"));
            items.add(new SwitchItem("myPresenceItem3"));
            items.add(new SwitchItem("myLampItem3"));
            items.add(new SwitchItem("myMotionItem4"));
            items.add(new SwitchItem("myPresenceItem4"));
            items.add(new SwitchItem("myLampItem4"));
            items.add(new SwitchItem("myMotionItem5"));
            items.add(new SwitchItem("myPresenceItem5"));
            items.add(new SwitchItem("myLampItem5"));
            items.add(new SwitchItem("xtempl_MotionItem"));
            items.add(new SwitchItem("xtempl_LampItem"));
            return items;
        }

        @Override
        public void removeProviderChangeListener(@NonNull ProviderChangeListener<@NonNull Item> listener) {
        }
    };
    registerService(itemProvider);
    registerVolatileStorageService();
    StorageService storageService = getService(StorageService.class);
    eventPublisher = getService(EventPublisher.class);
    itemRegistry = getService(ItemRegistry.class);
    ruleRegistry = getService(RuleRegistry.class);
    ruleEngine = getService(RuleManager.class);
    managedRuleProvider = getService(ManagedRuleProvider.class);
    moduleTypeRegistry = getService(ModuleTypeRegistry.class);
    templateRegistry = getService(TemplateRegistry.class);
    waitForAssert(() -> {
        assertThat(eventPublisher, is(notNullValue()));
        assertThat(storageService, is(notNullValue()));
        assertThat(itemRegistry, is(notNullValue()));
        assertThat(ruleRegistry, is(notNullValue()));
        assertThat(ruleEngine, is(notNullValue()));
        assertThat(moduleTypeRegistry, is(notNullValue()));
        assertThat(templateRegistry, is(notNullValue()));
        assertThat(managedRuleProvider, is(notNullValue()));
    }, 9000, 1000);
    logger.info("@Before.finish");
}
Also used : ItemProvider(org.eclipse.smarthome.core.items.ItemProvider) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ModuleTypeRegistry(org.eclipse.smarthome.automation.type.ModuleTypeRegistry) RuleRegistry(org.eclipse.smarthome.automation.RuleRegistry) ManagedRuleProvider(org.eclipse.smarthome.automation.core.ManagedRuleProvider) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) StorageService(org.eclipse.smarthome.core.storage.StorageService) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Item(org.eclipse.smarthome.core.items.Item) TemplateRegistry(org.eclipse.smarthome.automation.template.TemplateRegistry) NonNull(org.eclipse.jdt.annotation.NonNull) ProviderChangeListener(org.eclipse.smarthome.core.common.registry.ProviderChangeListener) RuleManager(org.eclipse.smarthome.automation.RuleManager) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) HashSet(java.util.HashSet) Before(org.junit.Before)

Example 4 with ManagedRuleProvider

use of org.eclipse.smarthome.automation.core.ManagedRuleProvider in project smarthome by eclipse.

the class RuleRegistryImpl method added.

@Override
public void added(Provider<Rule> provider, Rule element) {
    String ruleUID = element.getUID();
    Rule resolvedRule = element;
    try {
        resolvedRule = resolveRuleByTemplate(element);
    } catch (IllegalArgumentException e) {
        logger.debug("Added rule '{}' is invalid", ruleUID, e);
    }
    super.added(provider, element);
    if (element != resolvedRule) {
        if (provider instanceof ManagedRuleProvider) {
            update(resolvedRule);
        } else {
            super.updated(provider, element, resolvedRule);
        }
    }
}
Also used : ManagedRuleProvider(org.eclipse.smarthome.automation.core.ManagedRuleProvider) Rule(org.eclipse.smarthome.automation.Rule)

Example 5 with ManagedRuleProvider

use of org.eclipse.smarthome.automation.core.ManagedRuleProvider in project smarthome by eclipse.

the class AutomationIntegrationJsonTest method before.

// rules imported from
// json files
@Before
public void before() {
    logger.info("@Before.begin");
    getService(ItemRegistry.class);
    ItemProvider itemProvider = new ItemProvider() {

        @Override
        public void addProviderChangeListener(@NonNull ProviderChangeListener<@NonNull Item> listener) {
        }

        @Override
        @NonNull
        public Collection<@NonNull Item> getAll() {
            HashSet<Item> items = new HashSet<>();
            items.add(new SwitchItem("myMotionItem"));
            items.add(new SwitchItem("myPresenceItem"));
            items.add(new SwitchItem("myLampItem"));
            items.add(new SwitchItem("myMotionItem2"));
            items.add(new SwitchItem("myPresenceItem2"));
            items.add(new SwitchItem("myLampItem2"));
            items.add(new SwitchItem("myMotionItem11"));
            items.add(new SwitchItem("myLampItem11"));
            items.add(new SwitchItem("myMotionItem3"));
            items.add(new SwitchItem("templ_MotionItem"));
            items.add(new SwitchItem("templ_LampItem"));
            return items;
        }

        @Override
        public void removeProviderChangeListener(@NonNull ProviderChangeListener<@NonNull Item> listener) {
        }
    };
    registerService(itemProvider);
    registerVolatileStorageService();
    EventSubscriber ruleEventHandler = new EventSubscriber() {

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

        @Override
        @Nullable
        public EventFilter getEventFilter() {
            return null;
        }

        @Override
        public void receive(@NonNull Event e) {
            logger.info("RuleEvent: " + e.getTopic() + " --> " + e.getPayload());
            System.out.println("RuleEvent: " + e.getTopic() + " --> " + e.getPayload());
            if (e.getPayload().contains("RUNNING")) {
                ruleEvent = e;
            }
        }
    };
    registerService(ruleEventHandler);
    StorageService storageService = getService(StorageService.class);
    managedRuleProvider = getService(ManagedRuleProvider.class);
    eventPublisher = getService(EventPublisher.class);
    itemRegistry = getService(ItemRegistry.class);
    ruleRegistry = getService(RuleRegistry.class);
    ruleManager = getService(RuleManager.class);
    moduleTypeRegistry = getService(ModuleTypeRegistry.class);
    waitForAssert(() -> {
        assertThat(storageService, is(notNullValue()));
        // sometimes assert fails because EventPublisher service is null
        assertThat(eventPublisher, is(notNullValue()));
        assertThat(itemRegistry, is(notNullValue()));
        assertThat(ruleRegistry, is(notNullValue()));
        assertThat(ruleManager, is(notNullValue()));
        assertThat(managedRuleProvider, is(notNullValue()));
        assertThat(moduleTypeRegistry, is(notNullValue()));
    }, 9000, 1000);
    logger.info("@Before.finish");
}
Also used : ItemProvider(org.eclipse.smarthome.core.items.ItemProvider) EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ModuleTypeRegistry(org.eclipse.smarthome.automation.type.ModuleTypeRegistry) RuleRegistry(org.eclipse.smarthome.automation.RuleRegistry) ManagedRuleProvider(org.eclipse.smarthome.automation.core.ManagedRuleProvider) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) StorageService(org.eclipse.smarthome.core.storage.StorageService) VolatileStorageService(org.eclipse.smarthome.test.storage.VolatileStorageService) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Item(org.eclipse.smarthome.core.items.Item) NonNull(org.eclipse.jdt.annotation.NonNull) ProviderChangeListener(org.eclipse.smarthome.core.common.registry.ProviderChangeListener) RuleManager(org.eclipse.smarthome.automation.RuleManager) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) RuleStatusInfoEvent(org.eclipse.smarthome.automation.events.RuleStatusInfoEvent) Event(org.eclipse.smarthome.core.events.Event) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) HashSet(java.util.HashSet) Before(org.junit.Before)

Aggregations

ManagedRuleProvider (org.eclipse.smarthome.automation.core.ManagedRuleProvider)5 HashSet (java.util.HashSet)3 Rule (org.eclipse.smarthome.automation.Rule)3 NonNull (org.eclipse.jdt.annotation.NonNull)2 RuleManager (org.eclipse.smarthome.automation.RuleManager)2 RuleRegistry (org.eclipse.smarthome.automation.RuleRegistry)2 ModuleTypeRegistry (org.eclipse.smarthome.automation.type.ModuleTypeRegistry)2 ProviderChangeListener (org.eclipse.smarthome.core.common.registry.ProviderChangeListener)2 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)2 Item (org.eclipse.smarthome.core.items.Item)2 ItemProvider (org.eclipse.smarthome.core.items.ItemProvider)2 ItemRegistry (org.eclipse.smarthome.core.items.ItemRegistry)2 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)2 StorageService (org.eclipse.smarthome.core.storage.StorageService)2 Before (org.junit.Before)2 RuleStatusInfoEvent (org.eclipse.smarthome.automation.events.RuleStatusInfoEvent)1 TemplateRegistry (org.eclipse.smarthome.automation.template.TemplateRegistry)1 Event (org.eclipse.smarthome.core.events.Event)1 EventSubscriber (org.eclipse.smarthome.core.events.EventSubscriber)1 ItemCommandEvent (org.eclipse.smarthome.core.items.events.ItemCommandEvent)1