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..7f4a17df 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 @@ -13,10 +13,12 @@ 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; + } + } }