Search in sources :

Example 1 with PersistenceServiceConfiguration

use of org.eclipse.smarthome.core.persistence.PersistenceServiceConfiguration in project smarthome by eclipse.

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
 */
private void initialize(Item item) {
    // get the last persisted state from the persistence service if no state is yet set
    if (item.getState().equals(UnDefType.NULL) && item instanceof GenericItem) {
        for (Entry<String, PersistenceServiceConfiguration> entry : persistenceServiceConfigs.entrySet()) {
            final String serviceName = entry.getKey();
            final PersistenceServiceConfiguration config = entry.getValue();
            for (SimpleItemConfiguration itemConfig : config.getConfigs()) {
                if (hasStrategy(serviceName, itemConfig, SimpleStrategy.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 = queryService.query(filter);
                            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);
                                logger.debug("Restored item state from '{}' for item '{}' -> '{}'", new Object[] { DateFormat.getDateTimeInstance().format(historicItem.getTimestamp()), item.getName(), historicItem.getState().toString() });
                                return;
                            }
                        } else if (service != null) {
                            logger.warn("Failed to restore item states as persistence service '{}' can not be queried.", serviceName);
                        }
                    }
                }
            }
        }
    }
}
Also used : QueryablePersistenceService(org.eclipse.smarthome.core.persistence.QueryablePersistenceService) PersistenceService(org.eclipse.smarthome.core.persistence.PersistenceService) QueryablePersistenceService(org.eclipse.smarthome.core.persistence.QueryablePersistenceService) GenericItem(org.eclipse.smarthome.core.items.GenericItem) SimpleItemConfiguration(org.eclipse.smarthome.core.persistence.SimpleItemConfiguration) FilterCriteria(org.eclipse.smarthome.core.persistence.FilterCriteria) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem) PersistenceServiceConfiguration(org.eclipse.smarthome.core.persistence.PersistenceServiceConfiguration)

Example 2 with PersistenceServiceConfiguration

use of org.eclipse.smarthome.core.persistence.PersistenceServiceConfiguration in project smarthome by eclipse.

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, PersistenceServiceConfiguration> entry : persistenceServiceConfigs.entrySet()) {
            final String serviceName = entry.getKey();
            final PersistenceServiceConfiguration config = entry.getValue();
            if (persistenceServices.containsKey(serviceName)) {
                for (SimpleItemConfiguration itemConfig : config.getConfigs()) {
                    if (hasStrategy(serviceName, itemConfig, onlyChanges ? SimpleStrategy.Globals.CHANGE : SimpleStrategy.Globals.UPDATE)) {
                        if (appliesToItem(itemConfig, item)) {
                            persistenceServices.get(serviceName).store(item, itemConfig.getAlias());
                        }
                    }
                }
            }
        }
    }
}
Also used : SimpleItemConfiguration(org.eclipse.smarthome.core.persistence.SimpleItemConfiguration) PersistenceServiceConfiguration(org.eclipse.smarthome.core.persistence.PersistenceServiceConfiguration)

Example 3 with PersistenceServiceConfiguration

use of org.eclipse.smarthome.core.persistence.PersistenceServiceConfiguration in project smarthome by eclipse.

the class PersistenceManagerImpl method startEventHandling.

@Override
public void startEventHandling(final String dbId) {
    synchronized (persistenceServiceConfigs) {
        final PersistenceServiceConfiguration config = persistenceServiceConfigs.get(dbId);
        if (config == null) {
            return;
        }
        if (itemRegistry != null) {
            for (SimpleItemConfiguration itemConfig : config.getConfigs()) {
                if (hasStrategy(dbId, itemConfig, SimpleStrategy.Globals.RESTORE)) {
                    for (Item item : getAllItems(itemConfig)) {
                        initialize(item);
                    }
                }
            }
        }
        createTimers(dbId, config.getStrategies());
    }
}
Also used : GroupItem(org.eclipse.smarthome.core.items.GroupItem) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) SimpleItemConfiguration(org.eclipse.smarthome.core.persistence.SimpleItemConfiguration) PersistenceServiceConfiguration(org.eclipse.smarthome.core.persistence.PersistenceServiceConfiguration)

Example 4 with PersistenceServiceConfiguration

use of org.eclipse.smarthome.core.persistence.PersistenceServiceConfiguration in project smarthome by eclipse.

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 (SimpleItemConfiguration 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.eclipse.smarthome.core.persistence.PersistenceService) Item(org.eclipse.smarthome.core.items.Item) SimpleItemConfiguration(org.eclipse.smarthome.core.persistence.SimpleItemConfiguration) PersistenceServiceConfiguration(org.eclipse.smarthome.core.persistence.PersistenceServiceConfiguration)

Example 5 with PersistenceServiceConfiguration

use of org.eclipse.smarthome.core.persistence.PersistenceServiceConfiguration in project smarthome by eclipse.

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())));
    }
}
Also used : PersistenceModel(org.eclipse.smarthome.model.persistence.persistence.PersistenceModel) PersistenceServiceConfiguration(org.eclipse.smarthome.core.persistence.PersistenceServiceConfiguration)

Aggregations

PersistenceServiceConfiguration (org.eclipse.smarthome.core.persistence.PersistenceServiceConfiguration)5 SimpleItemConfiguration (org.eclipse.smarthome.core.persistence.SimpleItemConfiguration)4 GenericItem (org.eclipse.smarthome.core.items.GenericItem)2 Item (org.eclipse.smarthome.core.items.Item)2 HistoricItem (org.eclipse.smarthome.core.persistence.HistoricItem)2 PersistenceService (org.eclipse.smarthome.core.persistence.PersistenceService)2 GroupItem (org.eclipse.smarthome.core.items.GroupItem)1 FilterCriteria (org.eclipse.smarthome.core.persistence.FilterCriteria)1 QueryablePersistenceService (org.eclipse.smarthome.core.persistence.QueryablePersistenceService)1 PersistenceModel (org.eclipse.smarthome.model.persistence.persistence.PersistenceModel)1