Skip to content

Commit

Permalink
Add metadata registry and methods to access item metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
seime committed Jul 10, 2024
1 parent 4fb627d commit 03a91f2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
org.zeroturnaround.javarebel.*;resolution:=optional,\
org.apache.tools.ant.*;resolution:=optional
</bnd.importpackage>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<build>
<testResources>
Expand Down
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
@@ -1,22 +1,24 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* <p>
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* <p>
* 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
*
* <p>
* 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;
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;
}
}
}

0 comments on commit 03a91f2

Please sign in to comment.