use of org.eclipse.smarthome.core.persistence.PersistenceService 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.PersistenceService 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.PersistenceService 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.PersistenceService in project smarthome by eclipse.
the class PersistenceResource method deletePersistenceItemData.
private Response deletePersistenceItemData(String serviceId, String itemName, String timeBegin, String timeEnd) {
// For deleting, we must specify a service id - don't use the default service
if (serviceId == null || serviceId.length() == 0) {
logger.debug("Persistence service must be specified for delete operations.");
return JSONResponse.createErrorResponse(Status.BAD_REQUEST, "Persistence service must be specified for delete operations.");
}
PersistenceService 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 ModifiablePersistenceService)) {
logger.warn("Persistence service not modifiable '{}'.", serviceId);
return JSONResponse.createErrorResponse(Status.BAD_REQUEST, "Persistence service not modifiable: " + serviceId);
}
ModifiablePersistenceService mService = (ModifiablePersistenceService) service;
if (timeBegin == null | timeEnd == null) {
return JSONResponse.createErrorResponse(Status.BAD_REQUEST, "The start and end time must be set");
}
ZonedDateTime dateTimeBegin = convertTime(timeBegin);
ZonedDateTime dateTimeEnd = convertTime(timeEnd);
if (dateTimeEnd.isBefore(dateTimeBegin)) {
return JSONResponse.createErrorResponse(Status.BAD_REQUEST, "Start time must be earlier than end time");
}
FilterCriteria filter;
// First, get the value at the start time.
// This is necessary for values that don't change often otherwise data will start after the start of the graph
// (or not at all if there's no change during the graph period)
filter = new FilterCriteria();
filter.setBeginDate(dateTimeBegin);
filter.setEndDate(dateTimeEnd);
filter.setItemName(itemName);
try {
mService.remove(filter);
} catch (IllegalArgumentException e) {
return JSONResponse.createErrorResponse(Status.BAD_REQUEST, "Invalid filter parameters.");
}
return Response.status(Status.OK).build();
}
use of org.eclipse.smarthome.core.persistence.PersistenceService 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;
}
}
Aggregations