Search in sources :

Example 1 with QueryablePersistenceService

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);
                        }
                    }
                }
            }
        }
    }
}
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 QueryablePersistenceService

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;
}
Also used : PersistenceService(org.eclipse.smarthome.core.persistence.PersistenceService) QueryablePersistenceService(org.eclipse.smarthome.core.persistence.QueryablePersistenceService) ModifiablePersistenceService(org.eclipse.smarthome.core.persistence.ModifiablePersistenceService) QueryablePersistenceService(org.eclipse.smarthome.core.persistence.QueryablePersistenceService) PersistenceServiceDTO(org.eclipse.smarthome.core.persistence.dto.PersistenceServiceDTO) ModifiablePersistenceService(org.eclipse.smarthome.core.persistence.ModifiablePersistenceService) ArrayList(java.util.ArrayList)

Example 3 with QueryablePersistenceService

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(), "");
}
Also used : PersistenceService(org.eclipse.smarthome.core.persistence.PersistenceService) QueryablePersistenceService(org.eclipse.smarthome.core.persistence.QueryablePersistenceService) ModifiablePersistenceService(org.eclipse.smarthome.core.persistence.ModifiablePersistenceService) QueryablePersistenceService(org.eclipse.smarthome.core.persistence.QueryablePersistenceService)

Example 4 with QueryablePersistenceService

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;
    }
}
Also used : QueryablePersistenceService(org.eclipse.smarthome.core.persistence.QueryablePersistenceService) PersistenceService(org.eclipse.smarthome.core.persistence.PersistenceService) QueryablePersistenceService(org.eclipse.smarthome.core.persistence.QueryablePersistenceService) FilterCriteria(org.eclipse.smarthome.core.persistence.FilterCriteria) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem)

Example 5 with QueryablePersistenceService

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;
    }
}
Also used : QueryablePersistenceService(org.eclipse.smarthome.core.persistence.QueryablePersistenceService) PersistenceService(org.eclipse.smarthome.core.persistence.PersistenceService) QueryablePersistenceService(org.eclipse.smarthome.core.persistence.QueryablePersistenceService) FilterCriteria(org.eclipse.smarthome.core.persistence.FilterCriteria) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem) ZonedDateTime(java.time.ZonedDateTime) DateTime(org.joda.time.DateTime)

Aggregations

QueryablePersistenceService (org.eclipse.smarthome.core.persistence.QueryablePersistenceService)9 PersistenceService (org.eclipse.smarthome.core.persistence.PersistenceService)8 FilterCriteria (org.eclipse.smarthome.core.persistence.FilterCriteria)6 HistoricItem (org.eclipse.smarthome.core.persistence.HistoricItem)6 ModifiablePersistenceService (org.eclipse.smarthome.core.persistence.ModifiablePersistenceService)3 ZonedDateTime (java.time.ZonedDateTime)2 ArrayList (java.util.ArrayList)2 BasicStroke (java.awt.BasicStroke)1 Graphics2D (java.awt.Graphics2D)1 BufferedImage (java.awt.image.BufferedImage)1 Date (java.util.Date)1 Entry (java.util.Map.Entry)1 GenericItem (org.eclipse.smarthome.core.items.GenericItem)1 GroupItem (org.eclipse.smarthome.core.items.GroupItem)1 Item (org.eclipse.smarthome.core.items.Item)1 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)1 OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)1 OpenClosedType (org.eclipse.smarthome.core.library.types.OpenClosedType)1 PersistenceServiceConfiguration (org.eclipse.smarthome.core.persistence.PersistenceServiceConfiguration)1 SimpleItemConfiguration (org.eclipse.smarthome.core.persistence.SimpleItemConfiguration)1