Find out all rules

Hi all. I am curious about which components in openHAB 2.5 store and handle the rules? Suppose I want to develop a dummy component in the openHAB, which can capture and output all user-defined automation rules, which bundles/services should I focus more?

Why don’t you just use the REST API to query the system?

Because I want to dive into openHAB more, instead of just normal usage. I am really curious how rules are stored or mapped in the internal of openHAB.

Can’t point you exactly to the right bundle but diving into openHAB starts by reading the docs. Developer guide and appendix gives you a good start. Then make your way through OpenHAB’s github repos. After these you are quite familiar with internals of OH and you can find your way to related bundles.

1 Like

For the new rule engine, look at everything under org.openhab.core.automation. There’s not much you can do with the old one.

1 Like

Hi 5iver, thanks for your advice! Actually I found the org.openhab.core.automation. Inside package org.openhab.core.automation.internal, I found component

@Component(service = RuleRegistry.class, immediate = true)
    public class RuleRegistryImpl extends AbstractRegistry<Rule, String, RuleProvider>
            implements RuleRegistry, RegistryChangeListener<RuleTemplate>

And from the comment here, it said

Get the existing rules with the {@link #get(String)}, {@link #getAll()}, {@link #getByTag(String)}, {@link #getByTags(String[])} methods

So I tried to use the getAll() provided by this service. In my created binding, I write my codes here.

@Component(configurationPid = "binding.mylogger", service = { ThingHandlerFactory.class, EventSubscriber.class })
public class MyLoggerHandlerFactory extends BaseThingHandlerFactory implements EventSubscriber {

    private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.singleton(THING_TYPE_SAMPLE);
    private final Set<String> subscribedEventTypes = new HashSet<String>();
    private final Logger logger = LoggerFactory.getLogger(MyLoggerHandlerFactory.class);

    private @NonNullByDefault({}) RuleRegistryImpl Ruleservice;

    @Reference
    public void setService(RuleRegistryImpl Ruleservice) {
        this.Ruleservice = Ruleservice;
    }

    public void getAllRules() {
        Collection<Rule> rules = Ruleservice.getAll();
        for (Rule rule : rules) {
            logger.info("Current rule UID: {} name: {} tag: {}", rule.getUID(), rule.getName(), rule.getTags());
        }
    }

Note that please omit the EventSubscriber part, because I also implemented this binding to capture all events. I use the Factory class to refer to the RuleRegistryImpl service. However the first time I execute

mvn clean install

It throws a resolution error:

[INFO] — karaf-maven-plugin:4.2.7:verify (karaf-feature-verification) @ org.openhab.binding.mylogger —
missing requirement [org.openhab.binding.mylogger/2.5.0.201912090901] osgi.wiring.package; filter:="(osgi.wiring.package=org.openhab.core.automation)"

I thought this was because when the karaf-maven-plugin tried to verify, but it can not handle the resolution because it can not find the automation package. However the automation bundle is located in my .m2 repo. So I add the automation bundle in the feature.xml

<features name="org.openhab.binding.mylogger-${project.version}" xmlns="http://karaf.apache.org/xmlns/features/v1.4.0">
    <repository>mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-core/${project.version}/xml/features</repository>

    <feature name="openhab-binding-mylogger" description="MyLogger Binding" version="${project.version}">
        <feature>openhab-runtime-base</feature>
        <bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.mylogger/${project.version}</bundle>
        <bundle>mvn:org.openhab.core.bundles/org.openhab.core.automation/${project.version}</bundle>
    </feature>
</features>

However after that, it is still not correct and the new resolution error becomes:

[INFO] — karaf-maven-plugin:4.2.7:verify (karaf-feature-verification) @ org.openhab.binding.mylogger —
missing requirement [org.openhab.binding.mylogger/2.5.0.201912090901] osgi.wiring.package; filter:="(osgi.wiring.package=org.openhab.core.automation.internal)"

I think org.openhab.core.automation.internal is the sub package of org.openhab.core.automation, so why would this error happen? I can not figure it out and could you give me some hints to solve it? Thanks!

internal as the name suggest are internal. Meaning not outside the package available. This is part of the OSGI stuff. So you need to inject the interface, which is exported. In short drop Impl from the name in your code.

Thanks hilbrand, I didn’t check the export part. I will try your suggestion and to see if I can fetch all rules stored in openHAB.