From 03a91f2af9e29ed022295a92365793668bea0166 Mon Sep 17 00:00:00 2001 From: Arne Seime Date: Wed, 10 Jul 2024 12:34:56 +0200 Subject: [PATCH] Add metadata registry and methods to access item metadata --- pom.xml | 2 + .../jrule/internal/handler/JRuleHandler.java | 19 ++------ .../internal/handler/JRuleItemHandler.java | 47 ++++++++++++++++--- 3 files changed, 47 insertions(+), 21 deletions(-) diff --git a/pom.xml b/pom.xml index 18eb19cc..e911a829 100644 --- a/pom.xml +++ b/pom.xml @@ -25,6 +25,8 @@ org.zeroturnaround.javarebel.*;resolution:=optional,\ org.apache.tools.ant.*;resolution:=optional + 17 + 17 diff --git a/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleHandler.java b/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleHandler.java index e09ff78b..4222aeb3 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleHandler.java +++ b/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleHandler.java @@ -14,32 +14,20 @@ import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; import java.lang.reflect.InvocationTargetException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Objects; -import java.util.Set; +import java.util.*; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.automation.jrule.actions.JRuleActionClassGenerator; -import org.openhab.automation.jrule.internal.JRuleConfig; -import org.openhab.automation.jrule.internal.JRuleConstants; -import org.openhab.automation.jrule.internal.JRuleDelayedDebouncingExecutor; -import org.openhab.automation.jrule.internal.JRuleLog; -import org.openhab.automation.jrule.internal.JRuleUtil; +import org.openhab.automation.jrule.internal.*; import org.openhab.automation.jrule.internal.compiler.JRuleCompiler; import org.openhab.automation.jrule.internal.compiler.JRuleJarExtractor; import org.openhab.automation.jrule.internal.engine.JRuleEngine; @@ -162,6 +150,7 @@ public JRuleHandler(JRuleConfig config, ItemRegistry itemRegistry, ItemChannelLi final JRuleItemHandler itemHandler = JRuleItemHandler.get(); itemHandler.setItemRegistry(itemRegistry); itemHandler.setItemChannelLinkRegistry(itemChannelLinkRegistry); + itemHandler.setMetadataRegistry(metadataRegistry); logDebug("JRuleHandler()"); } diff --git a/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleItemHandler.java b/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleItemHandler.java index 3b22b6d6..95a9bf87 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleItemHandler.java +++ b/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleItemHandler.java @@ -1,22 +1,24 @@ /** * Copyright (c) 2010-2023 Contributors to the openHAB project - * + *

* See the NOTICE file(s) distributed with this work for additional * information. - * + *

* This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0 - * + *

* SPDX-License-Identifier: EPL-2.0 */ package org.openhab.automation.jrule.internal.handler; import java.util.Arrays; +import java.util.Collection; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; -import org.openhab.core.items.GroupItem; -import org.openhab.core.items.Item; -import org.openhab.core.items.ItemRegistry; +import org.openhab.core.items.*; import org.openhab.core.library.items.NumberItem; import org.openhab.core.library.items.StringItem; import org.openhab.core.library.items.SwitchItem; @@ -42,6 +44,12 @@ private JRuleItemHandler() { private ItemRegistry itemRegistry; private ItemChannelLinkRegistry itemChannelLinkRegistry; + private MetadataRegistry metadataRegistry; + + public void setMetadataRegistry(MetadataRegistry metadataRegistry) { + this.metadataRegistry = metadataRegistry; + } + public void setItemRegistry(ItemRegistry itemRegistry) { this.itemRegistry = itemRegistry; } @@ -161,4 +169,31 @@ public void linkItemWithChannel(String itemName, ChannelUID uid) { ItemChannelLink link = new ItemChannelLink(itemName, uid); itemChannelLinkRegistry.add(link); } + + public Collection getChannelLinks(String itemName) { + return itemChannelLinkRegistry.getLinks(itemName); + } + + public void unlinkItemFromChannel(String itemName) { + itemChannelLinkRegistry.removeLinksForItem(itemName); + } + + public Collection getItemsWithMetadata(String namespace, String value) { + Set itemsWithMatchingMetadata = itemRegistry.getItems().stream().filter(item -> { + MetadataKey key = new MetadataKey(namespace, item.getName()); + Metadata metadata = metadataRegistry.get(key); + return metadata != null && metadata.getValue().equals(value); + }).collect(Collectors.toSet()); + return itemsWithMatchingMetadata; + } + + public Map getItemMetadataConfiguration(String namespace, String value, Item item) { + MetadataKey key = new MetadataKey(namespace, item.getName()); + Metadata metadata = metadataRegistry.get(key); + if (metadata != null && metadata.getValue().equals(value)) { + return metadata.getConfiguration(); + } else { + return null; + } + } }