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