use of org.openhab.core.persistence.PersistenceServiceConfiguration in project openhab-core by openhab.
the class PersistenceManagerImpl method handleStateEvent.
/**
* Calls all persistence services which use change or update policy for the given item
*
* @param item the item to persist
* @param onlyChanges true, if it has the change strategy, false otherwise
*/
private void handleStateEvent(Item item, boolean onlyChanges) {
synchronized (persistenceServiceConfigs) {
for (Entry<String, @Nullable PersistenceServiceConfiguration> entry : persistenceServiceConfigs.entrySet()) {
final String serviceName = entry.getKey();
final PersistenceServiceConfiguration config = entry.getValue();
if (config != null && persistenceServices.containsKey(serviceName)) {
for (PersistenceItemConfiguration itemConfig : config.getConfigs()) {
if (hasStrategy(config, itemConfig, onlyChanges ? PersistenceStrategy.Globals.CHANGE : PersistenceStrategy.Globals.UPDATE)) {
if (appliesToItem(itemConfig, item)) {
persistenceServices.get(serviceName).store(item, itemConfig.getAlias());
}
}
}
}
}
}
}
use of org.openhab.core.persistence.PersistenceServiceConfiguration in project openhab-core by openhab.
the class PersistenceManagerImpl method getDefaultConfig.
@Nullable
private PersistenceServiceConfiguration getDefaultConfig(PersistenceService persistenceService) {
List<PersistenceStrategy> strategies = persistenceService.getDefaultStrategies();
List<PersistenceItemConfiguration> configs = List.of(new PersistenceItemConfiguration(List.of(new PersistenceAllConfig()), null, strategies, null));
return new PersistenceServiceConfiguration(configs, strategies, strategies);
}
use of org.openhab.core.persistence.PersistenceServiceConfiguration in project openhab-core by openhab.
the class PersistenceModelManager method addModel.
private void addModel(String modelName) {
final PersistenceModel model = (PersistenceModel) modelRepository.getModel(modelName);
if (model != null) {
String serviceName = serviceName(modelName);
manager.addConfig(serviceName, new PersistenceServiceConfiguration(mapConfigs(model.getConfigs()), mapStrategies(model.getDefaults()), mapStrategies(model.getStrategies())));
}
}
use of org.openhab.core.persistence.PersistenceServiceConfiguration in project openhab-core by openhab.
the class PersistItemsJob method run.
@Override
public void run() {
synchronized (manager.persistenceServiceConfigs) {
final PersistenceService persistenceService = manager.persistenceServices.get(dbId);
final PersistenceServiceConfiguration config = manager.persistenceServiceConfigs.get(dbId);
if (persistenceService != null) {
for (PersistenceItemConfiguration itemConfig : config.getConfigs()) {
if (hasStrategy(config.getDefaults(), itemConfig, strategyName)) {
for (Item item : manager.getAllItems(itemConfig)) {
long startTime = System.nanoTime();
persistenceService.store(item, itemConfig.getAlias());
logger.trace("Storing item '{}' with persistence service '{}' took {}ms", item.getName(), dbId, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime));
}
}
}
}
}
}
use of org.openhab.core.persistence.PersistenceServiceConfiguration in project openhab-core by openhab.
the class PersistenceManagerImpl method initialize.
/**
* Handles the "restoreOnStartup" strategy for the item.
* If the item state is still undefined when entering this method, all persistence configurations are checked,
* if they have the "restoreOnStartup" strategy configured for the item. If so, the item state will be set
* to its last persisted value.
*
* @param item the item to restore the state for
*/
@SuppressWarnings("null")
private void initialize(Item item) {
// get the last persisted state from the persistence service if no state is yet set
if (UnDefType.NULL.equals(item.getState()) && item instanceof GenericItem) {
for (Entry<String, @Nullable PersistenceServiceConfiguration> entry : persistenceServiceConfigs.entrySet()) {
final String serviceName = entry.getKey();
final PersistenceServiceConfiguration config = entry.getValue();
if (config != null) {
for (PersistenceItemConfiguration itemConfig : config.getConfigs()) {
if (hasStrategy(config, itemConfig, PersistenceStrategy.Globals.RESTORE)) {
if (appliesToItem(itemConfig, item)) {
PersistenceService service = persistenceServices.get(serviceName);
if (service instanceof QueryablePersistenceService) {
QueryablePersistenceService queryService = (QueryablePersistenceService) service;
FilterCriteria filter = new FilterCriteria().setItemName(item.getName()).setPageSize(1);
Iterable<HistoricItem> result = safeCaller.create(queryService, QueryablePersistenceService.class).onTimeout(() -> {
logger.warn("Querying persistence service '{}' takes more than {}ms.", queryService.getId(), SafeCaller.DEFAULT_TIMEOUT);
}).onException(e -> {
logger.error("Exception occurred while querying persistence service '{}': {}", queryService.getId(), e.getMessage(), e);
}).build().query(filter);
if (result != null) {
Iterator<HistoricItem> it = result.iterator();
if (it.hasNext()) {
HistoricItem historicItem = it.next();
GenericItem genericItem = (GenericItem) item;
genericItem.removeStateChangeListener(this);
genericItem.setState(historicItem.getState());
genericItem.addStateChangeListener(this);
if (logger.isDebugEnabled()) {
logger.debug("Restored item state from '{}' for item '{}' -> '{}'", DateTimeFormatter.ISO_ZONED_DATE_TIME.format(historicItem.getTimestamp()), item.getName(), historicItem.getState());
}
return;
}
}
} else if (service != null) {
logger.warn("Failed to restore item states as persistence service '{}' cannot be queried.", serviceName);
}
}
}
}
}
}
}
}
Aggregations