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

Added more methods #193

Merged
merged 2 commits into from
Mar 20, 2024
Merged
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 @@ -31,6 +31,7 @@
import org.openhab.core.scheduler.CronScheduler;
import org.openhab.core.thing.ThingManager;
import org.openhab.core.thing.ThingRegistry;
import org.openhab.core.thing.link.ItemChannelLinkRegistry;
import org.openhab.core.voice.VoiceManager;
import org.osgi.framework.FrameworkUtil;
import org.osgi.service.component.ComponentContext;
Expand Down Expand Up @@ -62,9 +63,11 @@ public class JRuleFactory {

@Activate
public JRuleFactory(Map<String, Object> properties, final @Reference JRuleEventSubscriber eventSubscriber,
final @Reference ItemRegistry itemRegistry, final @Reference ThingRegistry thingRegistry,
final @Reference ThingManager thingManager, final @Reference EventPublisher eventPublisher,
final @Reference VoiceManager voiceManager, final @Reference AudioHTTPServer audioHTTPServer,
final @Reference ItemRegistry itemRegistry,
final @Reference ItemChannelLinkRegistry itemChannelLinkRegistry,
final @Reference ThingRegistry thingRegistry, final @Reference ThingManager thingManager,
final @Reference EventPublisher eventPublisher, final @Reference VoiceManager voiceManager,
final @Reference AudioHTTPServer audioHTTPServer,
final @Reference NetworkAddressService networkAddressService, final ComponentContext componentContext,
final @Reference CronScheduler cronScheduler, final @Reference MetadataRegistry metadataRegistry,
final @Reference JRuleRuleProvider ruleProvider,
Expand All @@ -80,8 +83,8 @@ public JRuleFactory(Map<String, Object> properties, final @Reference JRuleEventS

jRuleEngine.initialize();
JRuleItemRegistry.setMetadataRegistry(metadataRegistry);
jRuleHandler = new JRuleHandler(config, itemRegistry, thingRegistry, thingManager, eventPublisher,
eventSubscriber, voiceManager, audioHTTPServer, networkAddressService, cronScheduler,
jRuleHandler = new JRuleHandler(config, itemRegistry, itemChannelLinkRegistry, thingRegistry, thingManager,
eventPublisher, eventSubscriber, voiceManager, audioHTTPServer, networkAddressService, cronScheduler,
componentContext.getBundleContext(), metadataRegistry);
delayedInit.call(this::init);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import org.openhab.core.thing.events.ThingAddedEvent;
import org.openhab.core.thing.events.ThingRemovedEvent;
import org.openhab.core.thing.events.ThingUpdatedEvent;
import org.openhab.core.thing.link.ItemChannelLinkRegistry;
import org.openhab.core.voice.VoiceManager;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
Expand All @@ -89,6 +90,9 @@ public class JRuleHandler implements PropertyChangeListener {
@NonNullByDefault({})
private final ItemRegistry itemRegistry;

@NonNullByDefault({})
private final ItemChannelLinkRegistry itemChannelLinkRegistry;

@NonNullByDefault({})
private final ThingRegistry thingRegistry;

Expand Down Expand Up @@ -119,11 +123,13 @@ public class JRuleHandler implements PropertyChangeListener {
private final JRuleDelayedDebouncingExecutor delayedRulesReloader;
private final JRuleDelayedDebouncingExecutor delayedItemsCompiler;

public JRuleHandler(JRuleConfig config, ItemRegistry itemRegistry, ThingRegistry thingRegistry,
ThingManager thingManager, EventPublisher eventPublisher, JRuleEventSubscriber eventSubscriber,
VoiceManager voiceManager, AudioHTTPServer audioHTTPServer, NetworkAddressService networkAddressService,
CronScheduler cronScheduler, BundleContext bundleContext, MetadataRegistry metadataRegistry) {
public JRuleHandler(JRuleConfig config, ItemRegistry itemRegistry, ItemChannelLinkRegistry itemChannelLinkRegistry,
ThingRegistry thingRegistry, ThingManager thingManager, EventPublisher eventPublisher,
JRuleEventSubscriber eventSubscriber, VoiceManager voiceManager, AudioHTTPServer audioHTTPServer,
NetworkAddressService networkAddressService, CronScheduler cronScheduler, BundleContext bundleContext,
MetadataRegistry metadataRegistry) {
this.itemRegistry = itemRegistry;
this.itemChannelLinkRegistry = itemChannelLinkRegistry;
this.thingRegistry = thingRegistry;
this.metadataRegistry = metadataRegistry;
this.eventSubscriber = eventSubscriber;
Expand Down Expand Up @@ -155,7 +161,7 @@ public JRuleHandler(JRuleConfig config, ItemRegistry itemRegistry, ThingRegistry

final JRuleItemHandler itemHandler = JRuleItemHandler.get();
itemHandler.setItemRegistry(itemRegistry);

itemHandler.setItemChannelLinkRegistry(itemChannelLinkRegistry);
logDebug("JRuleHandler()");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@
*/
package org.openhab.automation.jrule.internal.handler;

import java.util.Arrays;

import org.openhab.core.items.GroupItem;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemRegistry;
import org.openhab.core.library.items.NumberItem;
import org.openhab.core.library.items.StringItem;
import org.openhab.core.library.items.SwitchItem;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.link.ItemChannelLink;
import org.openhab.core.thing.link.ItemChannelLinkRegistry;

/**
* The {@link JRuleItemHandler} provides access to item Registry
Expand All @@ -32,11 +40,16 @@ private JRuleItemHandler() {
}

private ItemRegistry itemRegistry;
private ItemChannelLinkRegistry itemChannelLinkRegistry;

public void setItemRegistry(ItemRegistry itemRegistry) {
this.itemRegistry = itemRegistry;
}

public void setItemChannelLinkRegistry(ItemChannelLinkRegistry itemChannelLinkRegistry) {
this.itemChannelLinkRegistry = itemChannelLinkRegistry;
}

public static JRuleItemHandler get() {
if (instance == null) {
synchronized (JRuleItemHandler.class) {
Expand All @@ -56,15 +69,96 @@ public boolean itemRegistryContainsItem(String itemName) {
return itemRegistry.get(itemName) != null;
}

public Item addNumberItem(String name, int value) {
public Item addSwitchItem(String name) {
return addSwitchItem(name, null, null, null);
}

public Item addSwitchItem(String name, Boolean value) {
return addSwitchItem(name, value, null, null);
}

public Item addSwitchItem(String name, Boolean value, String label) {
return addSwitchItem(name, value, label, null);
}

public Item addNumberItem(String name) {
return addNumberItem(name, null, null, null);
}

public Item addNumberItem(String name, Double value) {
return addNumberItem(name, value, null, null);
}

public Item addNumberItem(String name, Double value, String label) {
return addNumberItem(name, value, label, null);
}

public Item addNumberItem(String name, Double value, String label, String[] groupNames) {
final NumberItem numberItem = new NumberItem(name);
numberItem.setState(new DecimalType(value));
if (value != null) {
numberItem.setState(new DecimalType(value));
}
if (label != null) {
numberItem.setLabel(label);
}
if (groupNames != null && groupNames.length > 0) {
Arrays.stream(groupNames).forEach(g -> numberItem.addGroupName(g));
}
return itemRegistry.add(numberItem);
}

public Item addStringItem(String name) {
return addStringItem(name, null, null, null);
}

public Item addStringItem(String name, String value) {
return addStringItem(name, value, null, null);
}

public Item addStringItem(String name, String value, String label) {
return addStringItem(name, value, label, null);
}

public Item addSwitchItem(String name, Boolean value, String label, String[] groupNames) {
final SwitchItem switchItem = new SwitchItem(name);
if (value != null) {
switchItem.setState(OnOffType.from(value));
}
if (label != null) {
switchItem.setLabel(label);
}
if (groupNames != null && groupNames.length > 0) {
Arrays.stream(groupNames).forEach(g -> switchItem.addGroupName(g));
}
return itemRegistry.add(switchItem);
}

public GroupItem addGroupItem(String name, String label) {
GroupItem groupItem = new GroupItem(name);
groupItem.setLabel(label);
return (GroupItem) itemRegistry.add(groupItem);
}

public void removeItem(String name) {
itemRegistry.remove(name, true);
}

public Item addStringItem(String name, String value, String label, String[] groupNames) {
final StringItem stringItem = new StringItem(name);
stringItem.setState(new StringType(value));
if (value != null) {
stringItem.setState(new StringType(value));
}
if (label != null) {
stringItem.setLabel(label);
}
if (groupNames != null && groupNames.length > 0) {
Arrays.stream(groupNames).forEach(g -> stringItem.addGroupName(g));
}
return itemRegistry.add(stringItem);
}

public void linkItemWithChannel(String itemName, ChannelUID uid) {
ItemChannelLink link = new ItemChannelLink(itemName, uid);
itemChannelLinkRegistry.add(link);
}
}
Loading