use of org.eclipse.smarthome.core.persistence.SimpleItemConfiguration 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);
}
}
}
}
}
}
}
use of org.eclipse.smarthome.core.persistence.SimpleItemConfiguration 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());
}
}
}
}
}
}
}
use of org.eclipse.smarthome.core.persistence.SimpleItemConfiguration 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());
}
}
use of org.eclipse.smarthome.core.persistence.SimpleItemConfiguration 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));
}
}
}
}
}
}
Aggregations