smarthomej / addons

SmartHome/J addons for openHAB
Eclipse Public License 2.0
59 stars 23 forks source link

Accessing Semantic Metadata #422

Closed clinique closed 1 year ago

clinique commented 1 year ago

Hello @J-N-K , is it possible to add access to semantic metadata in javarule ? Especially I'm interest in the ability to access hasLocationproperty of a Point. Thanks.

J-N-K commented 1 year ago

I guess you can get the MetaDataRegistry similar to what I proposed in #406:

ItemChannelLinkRegistry iclr = FrameworkUtil.getBundle(ItemChannelLinkRegistry.class).getBundleContext().getServiceReferences(
                ItemChannelLinkRegistry.class, null).stream().findFirst().map(bundleContext::getService).orElse(null);

and the you should be able to get the metadata from the registry with the item name.

clinique commented 1 year ago

Still one question : how do I get bundleContext from inside a JavaRule ?

lordjaxom commented 1 year ago

I stumbled over this issue since I also want to use metadata in rules to configure behaviour (instead of using non-semantic groups).

FrameworkUtil is not exposed via core-dependencies.jar, so in order to use FrameworkUtil I have to add osgi.core manually to the project's dependencies (as I commented in #406). Then it works in general, but strange things happen if you try to move the code to lookup MetadataRegistry into a class in lib/.

So (and because IMHO metadata should be used more often in rules generally) I'd upvote this :-)

@clinique The working snippet would be:

var bundleContext = FrameworkUtil.getBundle(MetadataRegistry.class).getBundleContext();
var metadataRegistry = bundleContext.getServiceReferences(MetadataRegistry.class, null).stream().findFirst().map(bundleContext::getService).orElse(null);
aitia commented 1 year ago

Hi @lordjaxom , I want to store some data with items and as I believe recommended approach is to use Item metadata. I want to access this metadata in javarule and change them for next javarule run.

Could you please provide more info where to get this lib?

... I have to add osgi.core manually to the project's dependencies (as I commented in #406).

Do I need to add lib this lib only in my project in IDEA, or I need to add this lib in /etc/openhab/automation too? Thank you for help