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;
}
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();
}
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();
}
Aggregations