Search in sources :

Example 1 with PersistenceItemConfiguration

use of org.openhab.core.persistence.PersistenceItemConfiguration 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());
                        }
                    }
                }
            }
        }
    }
}
Also used : PersistenceServiceConfiguration(org.openhab.core.persistence.PersistenceServiceConfiguration) PersistenceItemConfiguration(org.openhab.core.persistence.PersistenceItemConfiguration)

Example 2 with PersistenceItemConfiguration

use of org.openhab.core.persistence.PersistenceItemConfiguration 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);
}
Also used : PersistenceStrategy(org.openhab.core.persistence.strategy.PersistenceStrategy) PersistenceServiceConfiguration(org.openhab.core.persistence.PersistenceServiceConfiguration) PersistenceItemConfiguration(org.openhab.core.persistence.PersistenceItemConfiguration) PersistenceAllConfig(org.openhab.core.persistence.config.PersistenceAllConfig) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 3 with PersistenceItemConfiguration

use of org.openhab.core.persistence.PersistenceItemConfiguration 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));
                    }
                }
            }
        }
    }
}
Also used : PersistenceService(org.openhab.core.persistence.PersistenceService) Item(org.openhab.core.items.Item) PersistenceServiceConfiguration(org.openhab.core.persistence.PersistenceServiceConfiguration) PersistenceItemConfiguration(org.openhab.core.persistence.PersistenceItemConfiguration)

Example 4 with PersistenceItemConfiguration

use of org.openhab.core.persistence.PersistenceItemConfiguration 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);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : PersistenceService(org.openhab.core.persistence.PersistenceService) QueryablePersistenceService(org.openhab.core.persistence.QueryablePersistenceService) QueryablePersistenceService(org.openhab.core.persistence.QueryablePersistenceService) GenericItem(org.openhab.core.items.GenericItem) FilterCriteria(org.openhab.core.persistence.FilterCriteria) HistoricItem(org.openhab.core.persistence.HistoricItem) PersistenceServiceConfiguration(org.openhab.core.persistence.PersistenceServiceConfiguration) PersistenceItemConfiguration(org.openhab.core.persistence.PersistenceItemConfiguration)

Aggregations

PersistenceItemConfiguration (org.openhab.core.persistence.PersistenceItemConfiguration)4 PersistenceServiceConfiguration (org.openhab.core.persistence.PersistenceServiceConfiguration)4 PersistenceService (org.openhab.core.persistence.PersistenceService)2 Nullable (org.eclipse.jdt.annotation.Nullable)1 GenericItem (org.openhab.core.items.GenericItem)1 Item (org.openhab.core.items.Item)1 FilterCriteria (org.openhab.core.persistence.FilterCriteria)1 HistoricItem (org.openhab.core.persistence.HistoricItem)1 QueryablePersistenceService (org.openhab.core.persistence.QueryablePersistenceService)1 PersistenceAllConfig (org.openhab.core.persistence.config.PersistenceAllConfig)1 PersistenceStrategy (org.openhab.core.persistence.strategy.PersistenceStrategy)1