Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -24,6 +24,7 @@
import org.openhab.core.events.Event;
import org.openhab.core.events.EventFilter;
import org.openhab.core.events.EventSubscriber;
import org.openhab.core.events.TopicEventFilter;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.events.ChannelTriggeredEvent;
import org.osgi.framework.BundleContext;
Expand All @@ -48,7 +49,8 @@ public class ChannelEventTriggerHandler extends BaseTriggerModuleHandler impleme
private final Logger logger = LoggerFactory.getLogger(ChannelEventTriggerHandler.class);

private @Nullable final String eventOnChannel;
private final ChannelUID channelUID;
private final @Nullable ChannelUID channelUID;
private final @Nullable TopicEventFilter eventTopicFilter;
private final Set<String> types;
private final BundleContext bundleContext;
private final ServiceRegistration<?> eventSubscriberRegistration;
Expand All @@ -57,7 +59,18 @@ public ChannelEventTriggerHandler(Trigger module, BundleContext bundleContext) {
super(module);

this.eventOnChannel = (String) module.getConfiguration().get(CFG_CHANNEL_EVENT);
this.channelUID = new ChannelUID((String) module.getConfiguration().get(CFG_CHANNEL));
String cfgChannel = (String) module.getConfiguration().get(CFG_CHANNEL);
TopicEventFilter topicFilter = null;
ChannelUID parsedChannel = null;
if (cfgChannel != null && (cfgChannel.contains("?") || cfgChannel.contains("*"))) {
String topicRegex = "^openhab/channels/" + cfgChannel.replace("?", ".?").replace("*", ".*?")
+ "/triggered$";
topicFilter = new TopicEventFilter(topicRegex);
Comment thread
jimtng marked this conversation as resolved.
Outdated
} else {
parsedChannel = new ChannelUID(cfgChannel);
}
Comment thread
jimtng marked this conversation as resolved.
this.channelUID = parsedChannel;
this.eventTopicFilter = topicFilter;
this.types = Set.of("ChannelTriggeredEvent");
this.bundleContext = bundleContext;

Expand All @@ -78,19 +91,19 @@ public void receive(Event event) {
public boolean apply(Event event) {
boolean eventMatches = false;
if (event instanceof ChannelTriggeredEvent cte) {
if (channelUID.equals(cte.getChannel())) {
String eventOnChannel = this.eventOnChannel;
logger.trace("->FILTER: {}:{}", cte.getEvent(), eventOnChannel);
eventMatches = eventOnChannel == null || eventOnChannel.isBlank()
|| eventOnChannel.equals(cte.getEvent());
if (channelUID != null && !channelUID.equals(cte.getChannel())) {
return false;
}
String eventOnChannel = this.eventOnChannel;
logger.trace("->FILTER: {}:{}", cte.getEvent(), eventOnChannel);
eventMatches = eventOnChannel == null || eventOnChannel.isBlank() || eventOnChannel.equals(cte.getEvent());
}
return eventMatches;
}

@Override
public @Nullable EventFilter getEventFilter() {
return this;
return eventTopicFilter != null ? eventTopicFilter : this;
}
Comment thread
jimtng marked this conversation as resolved.

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"type": "TEXT",
"context": "channel",
"label": "Channel",
"description": "the id of the channel which should be observed for triggers",
"description": "the id of the channel which should be observed for triggers. '*' and '?' can be used as wildcards to match multiple channels.",
"required": true,
"filterCriteria": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module-type.core.ChannelEventTrigger.label = a trigger channel fires
module-type.core.ChannelEventTrigger.description = React on events from a trigger channel of a thing.
module-type.core.ChannelEventTrigger.config.channelUID.label = Channel
module-type.core.ChannelEventTrigger.config.channelUID.description = the id of the channel which should be observed for triggers
module-type.core.ChannelEventTrigger.config.channelUID.description = the id of the channel which should be observed for triggers. '*' and '?' can be used as wildcards to match multiple channels.
module-type.core.ChannelEventTrigger.config.event.label = Event
module-type.core.ChannelEventTrigger.config.event.description = the event on the channel to react on
module-type.core.ChannelEventTrigger.output.event.label = Event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,22 @@ public void testSubstringMatchingChannelIsNotApplied() {

assertFalse(handler.apply(ThingEventFactory.createTriggerEvent("PRESSED", new ChannelUID("foo:bar:baz:quux"))));
}

@Test
public void testWildcardAsteriskMatchingChannelIsApplied() {
when(moduleMock.getConfiguration())
.thenReturn(new Configuration(Map.of(ChannelEventTriggerHandler.CFG_CHANNEL, "foo:bar:baz:*")));
handler = new ChannelEventTriggerHandler(moduleMock, contextMock);

assertTrue(handler.apply(ThingEventFactory.createTriggerEvent("PRESSED", new ChannelUID("foo:bar:baz:quux"))));
}

@Test
public void testWildcardQuestionMarkMatchingChannelIsApplied() {
when(moduleMock.getConfiguration())
.thenReturn(new Configuration(Map.of(ChannelEventTriggerHandler.CFG_CHANNEL, "foo:bar:baz:quu?")));
handler = new ChannelEventTriggerHandler(moduleMock, contextMock);

assertTrue(handler.apply(ThingEventFactory.createTriggerEvent("PRESSED", new ChannelUID("foo:bar:baz:quux"))));
}
Comment thread
jimtng marked this conversation as resolved.
}
Loading