Search in sources :

Example 11 with PersistenceService

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

PersistenceService (org.eclipse.smarthome.core.persistence.PersistenceService)11 QueryablePersistenceService (org.eclipse.smarthome.core.persistence.QueryablePersistenceService)10 FilterCriteria (org.eclipse.smarthome.core.persistence.FilterCriteria)7 HistoricItem (org.eclipse.smarthome.core.persistence.HistoricItem)6 ModifiablePersistenceService (org.eclipse.smarthome.core.persistence.ModifiablePersistenceService)5 ZonedDateTime (java.time.ZonedDateTime)4 Item (org.eclipse.smarthome.core.items.Item)2 PersistenceServiceConfiguration (org.eclipse.smarthome.core.persistence.PersistenceServiceConfiguration)2 SimpleItemConfiguration (org.eclipse.smarthome.core.persistence.SimpleItemConfiguration)2 State (org.eclipse.smarthome.core.types.State)2 ArrayList (java.util.ArrayList)1 GenericItem (org.eclipse.smarthome.core.items.GenericItem)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 ItemHistoryDTO (org.eclipse.smarthome.core.persistence.dto.ItemHistoryDTO)1 PersistenceServiceDTO (org.eclipse.smarthome.core.persistence.dto.PersistenceServiceDTO)1 DateTime (org.joda.time.DateTime)1