Search in sources :

Example 1 with ModifiablePersistenceService

use of org.openhab.core.persistence.ModifiablePersistenceService in project openhab-core by openhab.

the class PersistenceResource method deletePersistenceItemData.

private Response deletePersistenceItemData(@Nullable String serviceId, String itemName, @Nullable String timeBegin, @Nullable 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);
    }
    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");
    }
    // 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)
    FilterCriteria filter = new FilterCriteria();
    filter.setItemName(itemName);
    filter.setBeginDate(dateTimeBegin);
    filter.setEndDate(dateTimeEnd);
    ModifiablePersistenceService mService = (ModifiablePersistenceService) service;
    try {
        mService.remove(filter);
    } catch (IllegalArgumentException e) {
        return JSONResponse.createErrorResponse(Status.BAD_REQUEST, "Invalid filter parameters.");
    }
    return Response.status(Status.OK).build();
}
Also used : ModifiablePersistenceService(org.openhab.core.persistence.ModifiablePersistenceService) PersistenceService(org.openhab.core.persistence.PersistenceService) QueryablePersistenceService(org.openhab.core.persistence.QueryablePersistenceService) ModifiablePersistenceService(org.openhab.core.persistence.ModifiablePersistenceService) ZonedDateTime(java.time.ZonedDateTime) FilterCriteria(org.openhab.core.persistence.FilterCriteria)

Example 2 with ModifiablePersistenceService

use of org.openhab.core.persistence.ModifiablePersistenceService in project openhab-core by openhab.

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

Example 3 with ModifiablePersistenceService

use of org.openhab.core.persistence.ModifiablePersistenceService in project openhab-core by openhab.

the class PersistenceResource method putItemState.

private Response putItemState(@Nullable String serviceId, String itemName, String value, @Nullable String time) {
    // If serviceId is null, then use the default service
    String effectiveServiceId = serviceId != null ? serviceId : persistenceServiceRegistry.getDefaultId();
    PersistenceService 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);
    }
    if (!(service instanceof ModifiablePersistenceService)) {
        logger.warn("Persistence service not modifiable '{}'.", effectiveServiceId);
        return JSONResponse.createErrorResponse(Status.BAD_REQUEST, "Persistence service not modifiable: " + effectiveServiceId);
    }
    Item item;
    try {
        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.");
    }
    ModifiablePersistenceService mService = (ModifiablePersistenceService) service;
    mService.store(item, dateTime, state);
    return Response.status(Status.OK).build();
}
Also used : ModifiablePersistenceService(org.openhab.core.persistence.ModifiablePersistenceService) PersistenceService(org.openhab.core.persistence.PersistenceService) QueryablePersistenceService(org.openhab.core.persistence.QueryablePersistenceService) HistoricItem(org.openhab.core.persistence.HistoricItem) Item(org.openhab.core.items.Item) ModifiablePersistenceService(org.openhab.core.persistence.ModifiablePersistenceService) ZonedDateTime(java.time.ZonedDateTime) State(org.openhab.core.types.State) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Aggregations

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