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

Log a warning when item for trigger/condition is missing or removed #3007

Merged
merged 2 commits into from
Jul 10, 2022
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 @@ -108,25 +108,25 @@ public Collection<String> getTypes() {
} else if (ChannelEventTriggerHandler.MODULE_TYPE_ID.equals(moduleTypeUID)) {
return new ChannelEventTriggerHandler((Trigger) module, bundleContext);
} else if (ItemCommandTriggerHandler.MODULE_TYPE_ID.equals(moduleTypeUID)) {
return new ItemCommandTriggerHandler((Trigger) module, bundleContext);
return new ItemCommandTriggerHandler((Trigger) module, ruleUID, bundleContext, itemRegistry);
} else if (SystemTriggerHandler.STARTLEVEL_MODULE_TYPE_ID.equals(moduleTypeUID)) {
return new SystemTriggerHandler((Trigger) module, bundleContext);
} else if (ThingStatusTriggerHandler.CHANGE_MODULE_TYPE_ID.equals(moduleTypeUID)
|| ThingStatusTriggerHandler.UPDATE_MODULE_TYPE_ID.equals(moduleTypeUID)) {
return new ThingStatusTriggerHandler((Trigger) module, bundleContext);
} else if (ItemStateTriggerHandler.CHANGE_MODULE_TYPE_ID.equals(moduleTypeUID)
|| ItemStateTriggerHandler.UPDATE_MODULE_TYPE_ID.equals(moduleTypeUID)) {
return new ItemStateTriggerHandler((Trigger) module, bundleContext);
return new ItemStateTriggerHandler((Trigger) module, ruleUID, bundleContext, itemRegistry);
} else if (GroupCommandTriggerHandler.MODULE_TYPE_ID.equals(moduleTypeUID)) {
return new GroupCommandTriggerHandler((Trigger) module, bundleContext, itemRegistry);
return new GroupCommandTriggerHandler((Trigger) module, ruleUID, bundleContext, itemRegistry);
} else if (GroupStateTriggerHandler.CHANGE_MODULE_TYPE_ID.equals(moduleTypeUID)
|| GroupStateTriggerHandler.UPDATE_MODULE_TYPE_ID.equals(moduleTypeUID)) {
return new GroupStateTriggerHandler((Trigger) module, bundleContext, itemRegistry);
return new GroupStateTriggerHandler((Trigger) module, ruleUID, bundleContext, itemRegistry);
}
} else if (module instanceof Condition) {
// Handle conditions
if (ItemStateConditionHandler.ITEM_STATE_CONDITION.equals(moduleTypeUID)) {
return new ItemStateConditionHandler((Condition) module, itemRegistry);
return new ItemStateConditionHandler((Condition) module, ruleUID, bundleContext, itemRegistry);
} else if (GenericEventConditionHandler.MODULETYPE_ID.equals(moduleTypeUID)) {
return new GenericEventConditionHandler((Condition) module);
} else if (CompareConditionHandler.MODULE_TYPE.equals(moduleTypeUID)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import org.openhab.core.events.EventSubscriber;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemRegistry;
import org.openhab.core.items.events.ItemAddedEvent;
import org.openhab.core.items.events.ItemCommandEvent;
import org.openhab.core.items.events.ItemRemovedEvent;
import org.openhab.core.types.Command;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
Expand Down Expand Up @@ -59,21 +61,29 @@ public class GroupCommandTriggerHandler extends BaseTriggerModuleHandler impleme

public static final String CFG_GROUPNAME = "groupName";
public static final String CFG_COMMAND = "command";
private final String ruleUID;

private ServiceRegistration<?> eventSubscriberRegistration;

public GroupCommandTriggerHandler(Trigger module, BundleContext bundleContext, ItemRegistry itemRegistry) {
public GroupCommandTriggerHandler(Trigger module, String ruleUID, BundleContext bundleContext,
ItemRegistry itemRegistry) {
super(module);
this.groupName = (String) module.getConfiguration().get(CFG_GROUPNAME);
this.command = (String) module.getConfiguration().get(CFG_COMMAND);
this.types = Set.of(ItemCommandEvent.TYPE);
this.types = Set.of(ItemCommandEvent.TYPE, ItemAddedEvent.TYPE, ItemRemovedEvent.TYPE);
this.bundleContext = bundleContext;
this.itemRegistry = itemRegistry;
this.ruleUID = ruleUID;
Dictionary<String, Object> properties = new Hashtable<>();
this.topic = "openhab/items/";
this.topic = "openhab/items/*";
properties.put("event.topics", topic);
eventSubscriberRegistration = this.bundleContext.registerService(EventSubscriber.class.getName(), this,
properties);

if (itemRegistry.get(groupName) == null) {
logger.warn("Group '{}' needed for rule '{}' is missing. Trigger '{}' will not work.", groupName, ruleUID,
module.getId());
}
}

@Override
Expand All @@ -88,6 +98,20 @@ public Set<String> getSubscribedEventTypes() {

@Override
public void receive(Event event) {
if (event instanceof ItemAddedEvent) {
if (groupName.equals(((ItemAddedEvent) event).getItem().name)) {
logger.info("Group '{}' needed for rule '{}' added. Trigger '{}' will now work.", groupName, ruleUID,
module.getId());
return;
}
} else if (event instanceof ItemRemovedEvent) {
if (groupName.equals(((ItemRemovedEvent) event).getItem().name)) {
logger.warn("Group '{}' needed for rule '{}' removed. Trigger '{}' will no longer work.", groupName,
ruleUID, module.getId());
return;
}
}

if (callback instanceof TriggerHandlerCallback) {
TriggerHandlerCallback cb = (TriggerHandlerCallback) callback;
logger.trace("Received Event: Source: {} Topic: {} Type: {} Payload: {}", event.getSource(),
Expand Down Expand Up @@ -123,6 +147,6 @@ public void dispose() {
@Override
public boolean apply(Event event) {
logger.trace("->FILTER: {}", event.getTopic());
return event.getTopic().startsWith(topic);
return event.getTopic().startsWith("openhab/items/");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemRegistry;
import org.openhab.core.items.events.GroupItemStateChangedEvent;
import org.openhab.core.items.events.ItemAddedEvent;
import org.openhab.core.items.events.ItemRemovedEvent;
import org.openhab.core.items.events.ItemStateChangedEvent;
import org.openhab.core.items.events.ItemStateEvent;
import org.openhab.core.types.State;
Expand Down Expand Up @@ -59,28 +61,37 @@ public class GroupStateTriggerHandler extends BaseTriggerModuleHandler implement
private final String groupName;
private final @Nullable String state;
private final String previousState;
private final String ruleUID;
private Set<String> types;
private final BundleContext bundleContext;
private ItemRegistry itemRegistry;

private ServiceRegistration<?> eventSubscriberRegistration;

public GroupStateTriggerHandler(Trigger module, BundleContext bundleContext, ItemRegistry itemRegistry) {
public GroupStateTriggerHandler(Trigger module, String ruleUID, BundleContext bundleContext,
ItemRegistry itemRegistry) {
super(module);
this.groupName = (String) module.getConfiguration().get(CFG_GROUPNAME);
this.state = (String) module.getConfiguration().get(CFG_STATE);
this.previousState = (String) module.getConfiguration().get(CFG_PREVIOUS_STATE);
if (UPDATE_MODULE_TYPE_ID.equals(module.getTypeUID())) {
this.types = Set.of(ItemStateEvent.TYPE);
this.types = Set.of(ItemStateEvent.TYPE, ItemAddedEvent.TYPE, ItemRemovedEvent.TYPE);
} else {
this.types = Set.of(ItemStateChangedEvent.TYPE, GroupItemStateChangedEvent.TYPE);
this.types = Set.of(ItemStateChangedEvent.TYPE, GroupItemStateChangedEvent.TYPE, ItemAddedEvent.TYPE,
ItemRemovedEvent.TYPE);
}
this.bundleContext = bundleContext;
this.ruleUID = ruleUID;
this.itemRegistry = itemRegistry;
Dictionary<String, Object> properties = new Hashtable<>();
properties.put("event.topics", "openhab/items/*");
eventSubscriberRegistration = this.bundleContext.registerService(EventSubscriber.class.getName(), this,
properties);

if (itemRegistry.get(groupName) == null) {
logger.warn("Group '{}' needed for rule '{}' is missing. Trigger '{}' will not work.", groupName, ruleUID,
module.getId());
}
}

@Override
Expand All @@ -95,6 +106,20 @@ public Set<String> getSubscribedEventTypes() {

@Override
public void receive(Event event) {
if (event instanceof ItemAddedEvent) {
if (groupName.equals(((ItemAddedEvent) event).getItem().name)) {
logger.info("Group '{}' needed for rule '{}' added. Trigger '{}' will now work.", groupName, ruleUID,
module.getId());
return;
}
} else if (event instanceof ItemRemovedEvent) {
if (groupName.equals(((ItemRemovedEvent) event).getItem().name)) {
logger.warn("Group '{}' needed for rule '{}' removed. Trigger '{}' will no longer work.", groupName,
ruleUID, module.getId());
return;
}
}

if (callback instanceof TriggerHandlerCallback) {
TriggerHandlerCallback cb = (TriggerHandlerCallback) callback;
logger.trace("Received Event: Source: {} Topic: {} Type: {} Payload: {}", event.getSource(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
import org.openhab.core.events.Event;
import org.openhab.core.events.EventFilter;
import org.openhab.core.events.EventSubscriber;
import org.openhab.core.items.ItemRegistry;
import org.openhab.core.items.events.ItemAddedEvent;
import org.openhab.core.items.events.ItemCommandEvent;
import org.openhab.core.items.events.ItemRemovedEvent;
import org.openhab.core.types.Command;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
Expand Down Expand Up @@ -57,20 +60,27 @@ public class ItemCommandTriggerHandler extends BaseTriggerModuleHandler implemen

private final Set<String> types;
private final BundleContext bundleContext;
private final String ruleUID;

private @Nullable ServiceRegistration<?> eventSubscriberRegistration;
private final ServiceRegistration<?> eventSubscriberRegistration;

public ItemCommandTriggerHandler(Trigger module, BundleContext bundleContext) {
public ItemCommandTriggerHandler(Trigger module, String ruleUID, BundleContext bundleContext,
ItemRegistry itemRegistry) {
super(module);
this.itemName = (String) module.getConfiguration().get(CFG_ITEMNAME);
this.command = (String) module.getConfiguration().get(CFG_COMMAND);
this.types = Set.of(ItemCommandEvent.TYPE);
this.bundleContext = bundleContext;
this.ruleUID = ruleUID;
this.types = Set.of(ItemCommandEvent.TYPE, ItemAddedEvent.TYPE, ItemRemovedEvent.TYPE);
Dictionary<String, Object> properties = new Hashtable<>();
this.topic = "openhab/items/" + itemName + "/command";
this.topic = "openhab/items/" + itemName + "/*";
properties.put("event.topics", topic);
eventSubscriberRegistration = this.bundleContext.registerService(EventSubscriber.class.getName(), this,
properties);
if (itemRegistry.get(itemName) == null) {
logger.warn("Item '{}' needed for rule '{}' is missing. Trigger '{}' will not work.", itemName, ruleUID,
module.getId());
}
}

@Override
Expand All @@ -85,6 +95,20 @@ public Set<String> getSubscribedEventTypes() {

@Override
public void receive(Event event) {
if (event instanceof ItemAddedEvent) {
if (itemName.equals(((ItemAddedEvent) event).getItem().name)) {
logger.info("Item '{}' needed for rule '{}' added. Trigger '{}' will now work.", itemName, ruleUID,
module.getId());
return;
}
} else if (event instanceof ItemRemovedEvent) {
if (itemName.equals(((ItemRemovedEvent) event).getItem().name)) {
logger.warn("Item '{}' needed for rule '{}' removed. Trigger '{}' will no longer work.", itemName,
ruleUID, module.getId());
return;
}
}

ModuleHandlerCallback callback = this.callback;
if (callback != null) {
logger.trace("Received Event: Source: {} Topic: {} Type: {} Payload: {}", event.getSource(),
Expand All @@ -108,15 +132,12 @@ public void receive(Event event) {
@Override
public void dispose() {
super.dispose();
if (eventSubscriberRegistration != null) {
eventSubscriberRegistration.unregister();
eventSubscriberRegistration = null;
}
eventSubscriberRegistration.unregister();
}

@Override
public boolean apply(Event event) {
logger.trace("->FILTER: {}:{}", event.getTopic(), itemName);
return event.getTopic().equals(topic);
return event.getTopic().contains("openhab/items/" + itemName + "/");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,30 @@
*/
package org.openhab.core.automation.internal.module.handler;

import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.automation.Condition;
import org.openhab.core.automation.handler.BaseConditionModuleHandler;
import org.openhab.core.events.Event;
import org.openhab.core.events.EventFilter;
import org.openhab.core.events.EventSubscriber;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemNotFoundException;
import org.openhab.core.items.ItemRegistry;
import org.openhab.core.items.events.ItemAddedEvent;
import org.openhab.core.items.events.ItemRemovedEvent;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.unit.Units;
import org.openhab.core.types.State;
import org.openhab.core.types.TypeParser;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -35,7 +46,7 @@
* @author Kai Kreuzer - refactored and simplified customized module handling
*/
@NonNullByDefault
public class ItemStateConditionHandler extends BaseConditionModuleHandler {
public class ItemStateConditionHandler extends BaseConditionModuleHandler implements EventSubscriber, EventFilter {

/**
* Constants for Config-Parameters corresponding to Definition in ItemModuleTypeDefinition.json
Expand All @@ -49,15 +60,61 @@ public class ItemStateConditionHandler extends BaseConditionModuleHandler {
public static final String ITEM_STATE_CONDITION = "core.ItemStateCondition";

private final ItemRegistry itemRegistry;
private final String ruleUID;
private final String itemName;
private final BundleContext bundleContext;
private final Set<String> types;
private final ServiceRegistration<?> eventSubscriberRegistration;

public ItemStateConditionHandler(Condition condition, ItemRegistry itemRegistry) {
public ItemStateConditionHandler(Condition condition, String ruleUID, BundleContext bundleContext,
ItemRegistry itemRegistry) {
super(condition);
this.itemRegistry = itemRegistry;
this.bundleContext = bundleContext;
this.itemName = (String) module.getConfiguration().get(ITEM_NAME);
this.types = Set.of(ItemAddedEvent.TYPE, ItemRemovedEvent.TYPE);
this.ruleUID = ruleUID;

Dictionary<String, Object> properties = new Hashtable<>();
properties.put("event.topics", "openhab/items/" + itemName + "/*");
eventSubscriberRegistration = this.bundleContext.registerService(EventSubscriber.class.getName(), this,
properties);

if (itemRegistry.get(itemName) == null) {
logger.warn("Item '{}' needed for rule '{}' is missing. Condition '{}' will not work.", itemName, ruleUID,
module.getId());
}
}

@Override
public Set<String> getSubscribedEventTypes() {
return types;
}

@Override
public @Nullable EventFilter getEventFilter() {
return this;
}

@Override
public void receive(Event event) {
if (event instanceof ItemAddedEvent) {
if (itemName.equals(((ItemAddedEvent) event).getItem().name)) {
logger.info("Item '{}' needed for rule '{}' added. Condition '{}' will now work.", itemName, ruleUID,
module.getId());
return;
}
} else if (event instanceof ItemRemovedEvent) {
if (itemName.equals(((ItemRemovedEvent) event).getItem().name)) {
logger.warn("Item '{}' needed for rule '{}' removed. Condition '{}' will no longer work.", itemName,
ruleUID, module.getId());
return;
}
}
}

@Override
public boolean isSatisfied(Map<String, Object> inputs) {
String itemName = (String) module.getConfiguration().get(ITEM_NAME);
String state = (String) module.getConfiguration().get(STATE);
String operator = (String) module.getConfiguration().get(OPERATOR);
if (operator == null || state == null || itemName == null) {
Expand Down Expand Up @@ -167,4 +224,16 @@ private boolean equalsToItemState(String itemName, String state) throws ItemNotF
}
return itemState.equals(compareState);
}

@Override
public void dispose() {
super.dispose();
eventSubscriberRegistration.unregister();
}

@Override
public boolean apply(Event event) {
logger.trace("->FILTER: {}:{}", event.getTopic(), itemName);
return event.getTopic().contains("openhab/items/" + itemName + "/");
}
}
Loading