Search in sources :

Example 1 with ModifiablePersistenceService

use of org.eclipse.smarthome.core.persistence.ModifiablePersistenceService 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 2 with ModifiablePersistenceService

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

Example 3 with ModifiablePersistenceService

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

the class PersistenceResource method putItemState.

private Response putItemState(String serviceId, String itemName, String value, String time) {
    // If serviceId is null, then use the default service
    PersistenceService service = null;
    String effectiveServiceId = serviceId != null ? serviceId : persistenceServiceRegistry.getDefaultId();
    service = persistenceServiceRegistry.get(effectiveServiceId);
    if (service == null) {
        logger.warn("Persistence service not found '{}'.", effectiveServiceId);
        return JSONResponse.createErrorResponse(Status.BAD_REQUEST, "Persistence service not found: " + effectiveServiceId);
    }
    Item item;
    try {
        if (itemRegistry == null) {
            logger.warn("Item registry not set.");
            return JSONResponse.createErrorResponse(Status.CONFLICT, "Item registry not set.");
        }
        item = itemRegistry.getItem(itemName);
    } catch (ItemNotFoundException e) {
        logger.warn("Item not found '{}'.", itemName);
        return JSONResponse.createErrorResponse(Status.BAD_REQUEST, "Item not found: " + itemName);
    }
    // Try to parse a State from the input
    State state = TypeParser.parseState(item.getAcceptedDataTypes(), value);
    if (state == null) {
        // State could not be parsed
        logger.warn("Can't persist item {} with invalid state '{}'.", itemName, value);
        return JSONResponse.createErrorResponse(Status.BAD_REQUEST, "State could not be parsed: " + value);
    }
    ZonedDateTime dateTime = null;
    if (time != null && time.length() != 0) {
        dateTime = convertTime(time);
    }
    if (dateTime == null || dateTime.toEpochSecond() == 0) {
        logger.warn("Error with persistence store to {}. Time badly formatted {}.", itemName, time);
        return JSONResponse.createErrorResponse(Status.BAD_REQUEST, "Time badly formatted.");
    }
    if (!(service instanceof ModifiablePersistenceService)) {
        logger.warn("Persistence service not modifiable '{}'.", effectiveServiceId);
        return JSONResponse.createErrorResponse(Status.BAD_REQUEST, "Persistence service not modifiable: " + effectiveServiceId);
    }
    ModifiablePersistenceService mService = (ModifiablePersistenceService) service;
    mService.store(item, Date.from(dateTime.toInstant()), state);
    return Response.status(Status.OK).build();
}
Also used : PersistenceService(org.eclipse.smarthome.core.persistence.PersistenceService) QueryablePersistenceService(org.eclipse.smarthome.core.persistence.QueryablePersistenceService) ModifiablePersistenceService(org.eclipse.smarthome.core.persistence.ModifiablePersistenceService) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem) Item(org.eclipse.smarthome.core.items.Item) ZonedDateTime(java.time.ZonedDateTime) ModifiablePersistenceService(org.eclipse.smarthome.core.persistence.ModifiablePersistenceService) State(org.eclipse.smarthome.core.types.State) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Aggregations

ModifiablePersistenceService (org.eclipse.smarthome.core.persistence.ModifiablePersistenceService)3 PersistenceService (org.eclipse.smarthome.core.persistence.PersistenceService)3 QueryablePersistenceService (org.eclipse.smarthome.core.persistence.QueryablePersistenceService)3 ZonedDateTime (java.time.ZonedDateTime)2 ArrayList (java.util.ArrayList)1 Item (org.eclipse.smarthome.core.items.Item)1 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)1 FilterCriteria (org.eclipse.smarthome.core.persistence.FilterCriteria)1 HistoricItem (org.eclipse.smarthome.core.persistence.HistoricItem)1 PersistenceServiceDTO (org.eclipse.smarthome.core.persistence.dto.PersistenceServiceDTO)1 State (org.eclipse.smarthome.core.types.State)1