Skip to content

Commit

Permalink
Merge pull request #65 from querdenker2k/members_returning_items
Browse files Browse the repository at this point in the history
add option to get groupMembers as Items
  • Loading branch information
seaside1 committed Nov 3, 2022
2 parents 8a9be94 + 4b1901f commit 0fc6a77
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.jdt.annotation.NonNull;
import org.openhab.automation.jrule.exception.JRuleItemNotFoundException;
import org.openhab.automation.jrule.exception.JRuleRuntimeException;
import org.openhab.automation.jrule.internal.JRuleItemUtil;
import org.openhab.automation.jrule.internal.JRuleLog;
import org.openhab.automation.jrule.items.JRuleItem;
import org.openhab.automation.jrule.items.JRuleItemRegistry;
import org.openhab.automation.jrule.items.JRulePercentType;
import org.openhab.automation.jrule.rules.value.JRuleColorValue;
import org.openhab.automation.jrule.rules.value.JRuleIncreaseDecreaseValue;
Expand Down Expand Up @@ -552,20 +556,23 @@ public JRuleRawValue getRawValue(String itemName) {
}

public Set<String> getGroupMemberNames(String groupName) {
Item item;
final Set<String> memberNames = new HashSet<>();
return getGroupMemberItems(groupName).stream().map(JRuleItem::getName).collect(Collectors.toSet());
}

public Set<JRuleItem> getGroupMemberItems(String groupName) {
try {
item = itemRegistry.getItem(groupName);
Item item = itemRegistry.getItem(groupName);
if (item instanceof GroupItem) {
GroupItem g = (GroupItem) item;
return g.getMembers().stream().map(item1 -> JRuleItemRegistry.get(item1.getName()))
.collect(Collectors.toSet());
} else {
throw new JRuleRuntimeException(String.format("Given itemname '%s' is not a groupitem", groupName));
}
} catch (ItemNotFoundException e) {
logError("Item not found in registry for group: {}", groupName);
return null;
}
if (item instanceof GroupItem) {
GroupItem g = (GroupItem) item;
Set<@NonNull Item> members = g.getMembers();
members.forEach(m -> memberNames.add(m.getName()));
throw new JRuleItemNotFoundException(
String.format("Item not found in registry for groupname '%s'", groupName));
}
return memberNames;
}

public ItemRegistry getItemRegistry() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import java.util.Set;

import org.openhab.automation.jrule.exception.JRuleItemNotFoundException;
import org.openhab.automation.jrule.internal.handler.JRuleEventHandler;

/**
Expand All @@ -28,14 +27,14 @@ protected JRuleGroupItem(String itemName) {
super(itemName);
}

public static JRuleGroupItem forName(String itemName) throws JRuleItemNotFoundException {
return JRuleItemRegistry.get(itemName, JRuleGroupItem.class);
}

public Set<String> members() {
return JRuleEventHandler.get().getGroupMemberNames(itemName);
}

public Set<JRuleItem> memberItems() {
return JRuleEventHandler.get().getGroupMemberItems(itemName);
}

public void sendCommand(String value) {
final Set<String> groupMemberNames = JRuleEventHandler.get().getGroupMemberNames(itemName);
groupMemberNames.forEach(m -> JRuleEventHandler.get().sendCommand(m, value));
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/openhab/automation/jrule/items/JRuleItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import java.time.ZonedDateTime;
import java.util.Optional;

import org.openhab.automation.jrule.exception.JRuleItemNotFoundException;
import org.openhab.automation.jrule.internal.handler.JRuleEventHandler;

/**
* The {@link JRuleItem} Items
*
Expand All @@ -28,6 +31,14 @@ public JRuleItem(String itemName) {
this.itemName = itemName;
}

public static JRuleItem forName(String itemName) throws JRuleItemNotFoundException {
return JRuleItemRegistry.get(itemName);
}

public String getStateAsString() {
return JRuleEventHandler.get().getStringValue(itemName);
}

public String getName() {
return itemName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import org.openhab.automation.jrule.exception.JRuleItemNotFoundException;
import org.openhab.automation.jrule.internal.handler.JRuleEventHandler;
import org.openhab.core.items.GroupItem;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemNotFoundException;
import org.openhab.core.items.ItemRegistry;
import org.openhab.core.library.CoreItemFactory;

/**
* The {@link JRuleItemRegistry} Items
Expand All @@ -28,21 +32,78 @@
*/

public class JRuleItemRegistry {
private static final Map<String, Class<? extends JRuleItem>> typeMap = new HashMap<>();
private static final Map<String, Class<? extends JRuleItem>> groupTypeMap = new HashMap<>();
private static final Map<String, JRuleItem> itemRegistry = new HashMap<>();

public static void clear() {
itemRegistry.clear();
}

public static <T> T get(String itemName, Class<T> jRuleItemClass) throws JRuleItemNotFoundException {
static {
typeMap.put(GroupItem.TYPE, JRuleGroupItem.class);
typeMap.put(CoreItemFactory.CALL, JRuleCallItem.class);
typeMap.put(CoreItemFactory.CONTACT, JRuleContactItem.class);
typeMap.put(CoreItemFactory.COLOR, JRuleColorItem.class);
typeMap.put(CoreItemFactory.DATETIME, JRuleDateTimeItem.class);
typeMap.put(CoreItemFactory.DIMMER, JRuleDimmerItem.class);
typeMap.put(CoreItemFactory.IMAGE, JRuleImageItem.class);
typeMap.put(CoreItemFactory.LOCATION, JRuleLocationItem.class);
typeMap.put(CoreItemFactory.NUMBER, JRuleNumberItem.class);
typeMap.put(CoreItemFactory.PLAYER, JRulePlayerItem.class);
typeMap.put(CoreItemFactory.ROLLERSHUTTER, JRuleRollershutterItem.class);
typeMap.put(CoreItemFactory.STRING, JRuleStringItem.class);
typeMap.put(CoreItemFactory.SWITCH, JRuleSwitchItem.class);

groupTypeMap.put(CoreItemFactory.CALL, JRuleGroupCallItem.class);
groupTypeMap.put(CoreItemFactory.CONTACT, JRuleGroupContactItem.class);
groupTypeMap.put(CoreItemFactory.COLOR, JRuleGroupColorItem.class);
groupTypeMap.put(CoreItemFactory.DATETIME, JRuleGroupDateTimeItem.class);
groupTypeMap.put(CoreItemFactory.DIMMER, JRuleGroupDimmerItem.class);
groupTypeMap.put(CoreItemFactory.IMAGE, JRuleGroupImageItem.class);
groupTypeMap.put(CoreItemFactory.LOCATION, JRuleGroupLocationItem.class);
groupTypeMap.put(CoreItemFactory.NUMBER, JRuleGroupNumberItem.class);
groupTypeMap.put(CoreItemFactory.PLAYER, JRuleGroupPlayerItem.class);
groupTypeMap.put(CoreItemFactory.ROLLERSHUTTER, JRuleGroupRollershutterItem.class);
groupTypeMap.put(CoreItemFactory.STRING, JRuleGroupStringItem.class);
groupTypeMap.put(CoreItemFactory.SWITCH, JRuleGroupSwitchItem.class);
}

public static JRuleItem get(String itemName) throws JRuleItemNotFoundException {
JRuleItem jruleItem = itemRegistry.get(itemName);
if (jruleItem == null) {
Item item = verifyThatItemExist(itemName);

Class<? extends JRuleItem> jRuleItemClass = typeMap.get(item.getType());
if (jRuleItemClass == JRuleGroupItem.class && item instanceof GroupItem) {
String baseItemType = Optional.ofNullable(((GroupItem) item).getBaseItem()).map(Item::getType)
.orElse(CoreItemFactory.STRING);

jRuleItemClass = groupTypeMap.get(baseItemType);
}

try {
Constructor<? extends JRuleItem> constructor = jRuleItemClass.getDeclaredConstructor(String.class);
constructor.setAccessible(true);
jruleItem = constructor.newInstance(itemName);
itemRegistry.put(itemName, jruleItem);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
return jruleItem;
}

public static <T> T get(String itemName, Class<? extends JRuleItem> jRuleItemClass)
throws JRuleItemNotFoundException {
JRuleItem jruleItem = itemRegistry.get(itemName);
if (jruleItem == null) {
verifyThatItemExist(itemName);

try {
Constructor<T> constructor = jRuleItemClass.getDeclaredConstructor(String.class);
Constructor<? extends JRuleItem> constructor = jRuleItemClass.getDeclaredConstructor(String.class);
constructor.setAccessible(true);
jruleItem = (JRuleItem) constructor.newInstance(itemName);
jruleItem = constructor.newInstance(itemName);
itemRegistry.put(itemName, jruleItem);
} catch (Exception ex) {
throw new RuntimeException(ex);
Expand All @@ -51,14 +112,14 @@ public static <T> T get(String itemName, Class<T> jRuleItemClass) throws JRuleIt
return (T) jruleItem;
}

private static void verifyThatItemExist(String itemName) throws JRuleItemNotFoundException {
private static Item verifyThatItemExist(String itemName) throws JRuleItemNotFoundException {
try {
ItemRegistry itemRegistry = JRuleEventHandler.get().getItemRegistry();
if (itemRegistry == null) {
throw new IllegalStateException(
String.format("Item registry is not set can't get item for name: %s", itemName));
}
itemRegistry.getItem(itemName);
return itemRegistry.getItem(itemName);
} catch (ItemNotFoundException e) {
throw new JRuleItemNotFoundException(String.format("cannot find item for name: %s", itemName), e);
}
Expand Down

0 comments on commit 0fc6a77

Please sign in to comment.