mirror of
https://github.com/danieldemus/openhab-core.git
synced 2025-01-10 13:21:53 +01:00
[persistence] Exclude items and groups (#4468)
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
This commit is contained in:
parent
0c93cc0456
commit
a35041ac7b
@ -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])*)?)
|
||||
| ';')
|
||||
@ -75,6 +76,14 @@ GroupConfig:
|
||||
group=ID '*'
|
||||
;
|
||||
|
||||
ItemExcludeConfig:
|
||||
'!' itemExclude=ID
|
||||
;
|
||||
|
||||
GroupExcludeConfig:
|
||||
'!' groupExclude=ID '*'
|
||||
;
|
||||
|
||||
DECIMAL returns ecore::EBigDecimal :
|
||||
'-'? INT ('.' INT)?
|
||||
;
|
||||
|
@ -29,8 +29,10 @@ import org.openhab.core.model.persistence.persistence.CronStrategy;
|
||||
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;
|
||||
@ -43,7 +45,9 @@ import org.openhab.core.persistence.PersistenceService;
|
||||
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;
|
||||
@ -98,7 +102,11 @@ public class PersistenceModelManager extends AbstractProvider<PersistenceService
|
||||
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);
|
||||
|
||||
@ -153,6 +161,10 @@ public class PersistenceModelManager extends AbstractProvider<PersistenceService
|
||||
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()),
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
@ -57,7 +55,9 @@ import org.openhab.core.persistence.QueryablePersistenceService;
|
||||
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;
|
||||
@ -198,25 +198,39 @@ public class PersistenceManagerImpl implements ItemRegistryChangeListener, State
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -226,23 +240,19 @@ public class PersistenceManagerImpl implements ItemRegistryChangeListener, State
|
||||
* @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);
|
||||
@ -252,8 +262,26 @@ public class PersistenceManagerImpl implements ItemRegistryChangeListener, State
|
||||
} 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;
|
||||
}
|
||||
|
||||
|
@ -27,7 +27,9 @@ import org.openhab.core.persistence.PersistenceItemConfiguration;
|
||||
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;
|
||||
@ -113,8 +115,14 @@ public class PersistenceServiceConfigurationDTOMapper {
|
||||
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);
|
||||
}
|
||||
}
|
||||
@ -149,6 +157,10 @@ public class PersistenceServiceConfigurationDTOMapper {
|
||||
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());
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user