Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods to access item Metadata registry #201

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -161,4 +169,31 @@ public void linkItemWithChannel(String itemName, ChannelUID uid) {
ItemChannelLink link = new ItemChannelLink(itemName, uid);
itemChannelLinkRegistry.add(link);
}

public Collection<ItemChannelLink> getChannelLinks(String itemName) {
return itemChannelLinkRegistry.getLinks(itemName);
}

public void unlinkItemFromChannel(String itemName) {
itemChannelLinkRegistry.removeLinksForItem(itemName);
}

public Collection<Item> getItemsWithMetadata(String namespace, String value) {
Set<Item> 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<String, Object> 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;
}
}
}
Loading