Skip to content

Commit

Permalink
[persistence] Exclude items and groups (#4468)
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Herwege <[email protected]>
  • Loading branch information
mherwege authored Dec 8, 2024
1 parent 0c93cc0 commit a35041a
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ NotIncludeFilter:


PersistenceConfiguration:
items+=(AllConfig | ItemConfig | GroupConfig) (',' items+=(AllConfig | ItemConfig | GroupConfig))* ('->' alias=STRING)?
items+=(AllConfig | ItemConfig | GroupConfig | ItemExcludeConfig | GroupExcludeConfig)
(',' items+=(AllConfig | ItemConfig | GroupConfig | ItemExcludeConfig | GroupExcludeConfig))* ('->' alias=STRING)?
((':' ('strategy' '=' strategies+=[Strategy|ID] (',' strategies+=[Strategy|ID])*)?
('filter' '=' filters+=[Filter|ID] (',' filters+=[Filter|ID])*)?)
| ';')
Expand All @@ -75,6 +76,14 @@ GroupConfig:
group=ID '*'
;

ItemExcludeConfig:
'!' itemExclude=ID
;

GroupExcludeConfig:
'!' groupExclude=ID '*'
;

DECIMAL returns ecore::EBigDecimal :
'-'? INT ('.' INT)?
;
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
import org.openhab.core.model.persistence.persistence.EqualsFilter;
import org.openhab.core.model.persistence.persistence.Filter;
import org.openhab.core.model.persistence.persistence.GroupConfig;
import org.openhab.core.model.persistence.persistence.GroupExcludeConfig;
import org.openhab.core.model.persistence.persistence.IncludeFilter;
import org.openhab.core.model.persistence.persistence.ItemConfig;
import org.openhab.core.model.persistence.persistence.ItemExcludeConfig;
import org.openhab.core.model.persistence.persistence.NotEqualsFilter;
import org.openhab.core.model.persistence.persistence.NotIncludeFilter;
import org.openhab.core.model.persistence.persistence.PersistenceConfiguration;
Expand All @@ -43,7 +45,9 @@
import org.openhab.core.persistence.config.PersistenceAllConfig;
import org.openhab.core.persistence.config.PersistenceConfig;
import org.openhab.core.persistence.config.PersistenceGroupConfig;
import org.openhab.core.persistence.config.PersistenceGroupExcludeConfig;
import org.openhab.core.persistence.config.PersistenceItemConfig;
import org.openhab.core.persistence.config.PersistenceItemExcludeConfig;
import org.openhab.core.persistence.filter.PersistenceEqualsFilter;
import org.openhab.core.persistence.filter.PersistenceFilter;
import org.openhab.core.persistence.filter.PersistenceIncludeFilter;
Expand Down Expand Up @@ -98,7 +102,11 @@ public void modelChanged(String modelName, EventType type) {
String serviceName = serviceName(modelName);
if (type == EventType.REMOVED) {
PersistenceServiceConfiguration removed = configurations.remove(serviceName);
notifyListenersAboutRemovedElement(removed);
if (removed == null) {
logger.warn("Service for {} was already removed from registry, ignoring.", modelName);
} else {
notifyListenersAboutRemovedElement(removed);
}
} else {
final PersistenceModel model = (PersistenceModel) modelRepository.getModel(modelName);

Expand Down Expand Up @@ -153,6 +161,10 @@ private PersistenceItemConfiguration mapConfig(PersistenceConfiguration config)
items.add(new PersistenceGroupConfig(groupConfig.getGroup()));
} else if (item instanceof ItemConfig itemConfig) {
items.add(new PersistenceItemConfig(itemConfig.getItem()));
} else if (item instanceof GroupExcludeConfig groupExcludeConfig) {
items.add(new PersistenceGroupExcludeConfig(groupExcludeConfig.getGroupExclude()));
} else if (item instanceof ItemExcludeConfig itemExcludeConfig) {
items.add(new PersistenceItemExcludeConfig(itemExcludeConfig.getItemExclude()));
}
}
return new PersistenceItemConfiguration(items, config.getAlias(), mapStrategies(config.getStrategies()),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright (c) 2010-2024 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* 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
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.persistence.config;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* This class represents the configuration that is used for excluding group items.
*
* @author Mark Herwege - Initial contribution
*/
@NonNullByDefault
public class PersistenceGroupExcludeConfig extends PersistenceConfig {

private final String group;

public PersistenceGroupExcludeConfig(final String group) {
this.group = group;
}

public String getGroup() {
return group;
}

@Override
public String toString() {
return String.format("%s [group=%s]", getClass().getSimpleName(), group);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright (c) 2010-2024 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* 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
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.persistence.config;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* This class represents the configuration that identify item(s) by name for exclusion.
*
* @author Mark Herwege - Initial contribution
*/
@NonNullByDefault
public class PersistenceItemExcludeConfig extends PersistenceConfig {
final String item;

public PersistenceItemExcludeConfig(final String item) {
this.item = item;
}

public String getItem() {
return item;
}

@Override
public String toString() {
return String.format("%s [item=%s]", getClass().getSimpleName(), item);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
package org.openhab.core.persistence.internal;

import static org.openhab.core.persistence.FilterCriteria.Ordering.ASCENDING;
import static org.openhab.core.persistence.strategy.PersistenceStrategy.Globals.FORECAST;
import static org.openhab.core.persistence.strategy.PersistenceStrategy.Globals.RESTORE;
import static org.openhab.core.persistence.strategy.PersistenceStrategy.Globals.UPDATE;
import static org.openhab.core.persistence.strategy.PersistenceStrategy.Globals.*;

import java.time.Instant;
import java.time.ZoneId;
Expand Down Expand Up @@ -57,7 +55,9 @@
import org.openhab.core.persistence.config.PersistenceAllConfig;
import org.openhab.core.persistence.config.PersistenceConfig;
import org.openhab.core.persistence.config.PersistenceGroupConfig;
import org.openhab.core.persistence.config.PersistenceGroupExcludeConfig;
import org.openhab.core.persistence.config.PersistenceItemConfig;
import org.openhab.core.persistence.config.PersistenceItemExcludeConfig;
import org.openhab.core.persistence.registry.PersistenceServiceConfiguration;
import org.openhab.core.persistence.registry.PersistenceServiceConfigurationRegistry;
import org.openhab.core.persistence.registry.PersistenceServiceConfigurationRegistryChangeListener;
Expand Down Expand Up @@ -198,25 +198,39 @@ private void handleStateEvent(Item item, boolean changed) {
* @return true, if the configuration applies to the item
*/
private boolean appliesToItem(PersistenceItemConfiguration itemConfig, Item item) {
boolean applies = false;
for (PersistenceConfig itemCfg : itemConfig.items()) {
if (itemCfg instanceof PersistenceAllConfig) {
return true;
applies = true;
} else if (itemCfg instanceof PersistenceItemConfig persistenceItemConfig) {
if (item.getName().equals(persistenceItemConfig.getItem())) {
return true;
applies = true;
}
} else if (itemCfg instanceof PersistenceItemExcludeConfig persistenceItemExcludeConfig) {
if (item.getName().equals(persistenceItemExcludeConfig.getItem())) {
return false;
}
} else if (itemCfg instanceof PersistenceGroupConfig persistenceGroupConfig) {
try {
Item gItem = itemRegistry.getItem(persistenceGroupConfig.getGroup());
if (gItem instanceof GroupItem gItem2 && gItem2.getAllMembers().contains(item)) {
return true;
applies = true;
}
} catch (ItemNotFoundException e) {
// do nothing
}
} else if (itemCfg instanceof PersistenceGroupExcludeConfig persistenceGroupExcludeConfig) {
try {
Item gItem = itemRegistry.getItem(persistenceGroupExcludeConfig.getGroup());
if (gItem instanceof GroupItem gItem2 && gItem2.getAllMembers().contains(item)) {
return false;
}
} catch (ItemNotFoundException e) {
// do nothing
}
}
}
return false;
return applies;
}

/**
Expand All @@ -226,23 +240,19 @@ private boolean appliesToItem(PersistenceItemConfiguration itemConfig, Item item
* @return all items that this configuration applies to
*/
private Iterable<Item> getAllItems(PersistenceItemConfiguration config) {
// first check, if we should return them all
if (config.items().stream().anyMatch(PersistenceAllConfig.class::isInstance)) {
return itemRegistry.getItems();
}

// otherwise, go through the detailed definitions
Set<Item> items = new HashSet<>();
Set<Item> excludeItems = new HashSet<>();
for (Object itemCfg : config.items()) {
if (itemCfg instanceof PersistenceItemConfig persistenceItemConfig) {
if (itemCfg instanceof PersistenceAllConfig) {
items.addAll(itemRegistry.getItems());
} else if (itemCfg instanceof PersistenceItemConfig persistenceItemConfig) {
String itemName = persistenceItemConfig.getItem();
try {
items.add(itemRegistry.getItem(itemName));
} catch (ItemNotFoundException e) {
logger.debug("Item '{}' does not exist.", itemName);
}
}
if (itemCfg instanceof PersistenceGroupConfig persistenceGroupConfig) {
} else if (itemCfg instanceof PersistenceGroupConfig persistenceGroupConfig) {
String groupName = persistenceGroupConfig.getGroup();
try {
Item gItem = itemRegistry.getItem(groupName);
Expand All @@ -252,8 +262,26 @@ private Iterable<Item> getAllItems(PersistenceItemConfiguration config) {
} catch (ItemNotFoundException e) {
logger.debug("Item group '{}' does not exist.", groupName);
}
} else if (itemCfg instanceof PersistenceItemExcludeConfig persistenceItemConfig) {
String itemName = persistenceItemConfig.getItem();
try {
excludeItems.add(itemRegistry.getItem(itemName));
} catch (ItemNotFoundException e) {
logger.debug("Item '{}' does not exist.", itemName);
}
} else if (itemCfg instanceof PersistenceGroupExcludeConfig persistenceGroupConfig) {
String groupName = persistenceGroupConfig.getGroup();
try {
Item gItem = itemRegistry.getItem(groupName);
if (gItem instanceof GroupItem groupItem) {
excludeItems.addAll(groupItem.getAllMembers());
}
} catch (ItemNotFoundException e) {
logger.debug("Item group '{}' does not exist.", groupName);
}
}
}
items.removeAll(excludeItems);
return items;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import org.openhab.core.persistence.config.PersistenceAllConfig;
import org.openhab.core.persistence.config.PersistenceConfig;
import org.openhab.core.persistence.config.PersistenceGroupConfig;
import org.openhab.core.persistence.config.PersistenceGroupExcludeConfig;
import org.openhab.core.persistence.config.PersistenceItemConfig;
import org.openhab.core.persistence.config.PersistenceItemExcludeConfig;
import org.openhab.core.persistence.dto.PersistenceCronStrategyDTO;
import org.openhab.core.persistence.dto.PersistenceFilterDTO;
import org.openhab.core.persistence.dto.PersistenceItemConfigurationDTO;
Expand Down Expand Up @@ -113,8 +115,14 @@ private static PersistenceConfig stringToPersistenceConfig(String string) {
if ("*".equals(string)) {
return new PersistenceAllConfig();
} else if (string.endsWith("*")) {
if (string.startsWith("!")) {
return new PersistenceGroupExcludeConfig(string.substring(1, string.length() - 1));
}
return new PersistenceGroupConfig(string.substring(0, string.length() - 1));
} else {
if (string.startsWith("!")) {
return new PersistenceItemExcludeConfig(string.substring(1, string.length() - 1));
}
return new PersistenceItemConfig(string);
}
}
Expand Down Expand Up @@ -149,6 +157,10 @@ private static String persistenceConfigToString(PersistenceConfig config) {
return persistenceGroupConfig.getGroup() + "*";
} else if (config instanceof PersistenceItemConfig persistenceItemConfig) {
return persistenceItemConfig.getItem();
} else if (config instanceof PersistenceGroupExcludeConfig persistenceGroupExcludeConfig) {
return "!" + persistenceGroupExcludeConfig.getGroup() + "*";
} else if (config instanceof PersistenceItemExcludeConfig persistenceItemExcludeConfig) {
return "!" + persistenceItemExcludeConfig.getItem();
}
throw new IllegalArgumentException("Unknown persistence config class " + config.getClass());
}
Expand Down

0 comments on commit a35041a

Please sign in to comment.