use of org.eclipse.smarthome.core.persistence.QueryablePersistenceService 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.QueryablePersistenceService in project smarthome by eclipse.
the class PersistenceResource method getPersistenceServiceList.
/**
* Gets a list of persistence services currently configured in the system
*
* @return list of persistence services as {@link ServiceBean}
*/
private List<PersistenceServiceDTO> getPersistenceServiceList(Locale locale) {
List<PersistenceServiceDTO> dtoList = new ArrayList<PersistenceServiceDTO>();
for (PersistenceService service : persistenceServiceRegistry.getAll()) {
PersistenceServiceDTO serviceDTO = new PersistenceServiceDTO();
serviceDTO.id = service.getId();
serviceDTO.label = service.getLabel(locale);
if (service instanceof ModifiablePersistenceService) {
serviceDTO.type = MODIFYABLE;
} else if (service instanceof QueryablePersistenceService) {
serviceDTO.type = QUERYABLE;
} else {
serviceDTO.type = STANDARD;
}
dtoList.add(serviceDTO);
}
return dtoList;
}
use of org.eclipse.smarthome.core.persistence.QueryablePersistenceService in project smarthome by eclipse.
the class PersistenceResource method getServiceItemList.
private Response getServiceItemList(String serviceId) {
// If serviceId is null, then use the default service
PersistenceService service = null;
if (serviceId == null) {
service = persistenceServiceRegistry.getDefault();
} else {
service = persistenceServiceRegistry.get(serviceId);
}
if (service == null) {
logger.debug("Persistence service not found '{}'.", serviceId);
return JSONResponse.createErrorResponse(Status.BAD_REQUEST, "Persistence service not found: " + serviceId);
}
if (!(service instanceof QueryablePersistenceService)) {
logger.debug("Persistence service not queryable '{}'.", serviceId);
return JSONResponse.createErrorResponse(Status.BAD_REQUEST, "Persistence service not queryable: " + serviceId);
}
QueryablePersistenceService qService = (QueryablePersistenceService) service;
return JSONResponse.createResponse(Status.OK, qService.getItemInfo(), "");
}
use of org.eclipse.smarthome.core.persistence.QueryablePersistenceService in project smarthome by eclipse.
the class PersistenceExtensions method historicState.
/**
* Retrieves the historic item for a given <code>item</code> at a certain point in time through a
* {@link PersistenceService} identified by the <code>serviceId</code>.
*
* @param item the item for which to retrieve the historic item
* @param timestamp the point in time for which the historic item should be retrieved
* @param serviceId the name of the {@link PersistenceService} to use
* @return the historic item at the given point in time, or <code>null</code> if no historic item could be found or
* if the provided <code>serviceId</code> does not refer to an available
* {@link QueryablePersistenceService}
*/
public static HistoricItem historicState(Item item, AbstractInstant timestamp, String serviceId) {
PersistenceService service = getService(serviceId);
if (service instanceof QueryablePersistenceService) {
QueryablePersistenceService qService = (QueryablePersistenceService) service;
FilterCriteria filter = new FilterCriteria();
filter.setEndDate(ZonedDateTime.ofInstant(timestamp.toDate().toInstant(), timeZoneProvider.getTimeZone()));
filter.setItemName(item.getName());
filter.setPageSize(1);
filter.setOrdering(Ordering.DESCENDING);
Iterable<HistoricItem> result = qService.query(filter);
if (result.iterator().hasNext()) {
return result.iterator().next();
} else {
return null;
}
} else {
LoggerFactory.getLogger(PersistenceExtensions.class).warn("There is no queryable persistence service registered with the id '{}'", serviceId);
return null;
}
}
use of org.eclipse.smarthome.core.persistence.QueryablePersistenceService in project smarthome by eclipse.
the class PersistenceExtensions method lastUpdate.
/**
* Query for the last update time of a given <code>item</code>.
*
* @param item the item for which the last update time is to be returned
* @param serviceId the name of the {@link PersistenceService} to use
* @return last time <code>item</code> was updated, or <code>null</code> if there are no previously
* persisted updates or if persistence service given by <code>serviceId</code> does not refer to an
* available {@link QueryablePersistenceService}
*/
public static AbstractInstant lastUpdate(Item item, String serviceId) {
PersistenceService service = getService(serviceId);
if (service instanceof QueryablePersistenceService) {
QueryablePersistenceService qService = (QueryablePersistenceService) service;
FilterCriteria filter = new FilterCriteria();
filter.setItemName(item.getName());
filter.setOrdering(Ordering.DESCENDING);
filter.setPageSize(1);
Iterable<HistoricItem> result = qService.query(filter);
if (result.iterator().hasNext()) {
return new DateTime(result.iterator().next().getTimestamp());
} else {
return null;
}
} else {
LoggerFactory.getLogger(PersistenceExtensions.class).warn("There is no queryable persistence service registered with the id '{}'", serviceId);
return null;
}
}
Aggregations