use of org.eclipse.smarthome.core.persistence.FilterCriteria in project smarthome by eclipse.
the class PersistenceExtensions method previousState.
/**
* Returns the previous state of a given <code>item</code>.
* The {@link PersistenceService} identified by the <code>serviceId</code> is used.
*
* @param item the item to get the previous state value for
* @param skipEqual if <code>true</code>, skips equal state values and searches the first state not equal the
* current state
* @param serviceId the name of the {@link PersistenceService} to use
* @return the previous state or <code>null</code> if no previous state could be found, or if the given
* <code>serviceId</code> is not available or does not refer to a {@link QueryablePersistenceService}
*/
public static HistoricItem previousState(Item item, boolean skipEqual, String serviceId) {
PersistenceService service = getService(serviceId);
if (service instanceof QueryablePersistenceService) {
QueryablePersistenceService qService = (QueryablePersistenceService) service;
FilterCriteria filter = new FilterCriteria();
filter.setItemName(item.getName());
filter.setOrdering(Ordering.DESCENDING);
filter.setPageSize(skipEqual ? 1000 : 1);
int startPage = 0;
filter.setPageNumber(startPage);
Iterable<HistoricItem> items = qService.query(filter);
while (items != null) {
Iterator<HistoricItem> itemIterator = items.iterator();
int itemCount = 0;
while (itemIterator.hasNext()) {
HistoricItem historicItem = itemIterator.next();
itemCount++;
if (!skipEqual || (skipEqual && !historicItem.getState().equals(item.getState()))) {
return historicItem;
}
}
if (itemCount == filter.getPageSize()) {
filter.setPageNumber(++startPage);
items = qService.query(filter);
} else {
items = null;
}
}
return null;
} else {
LoggerFactory.getLogger(PersistenceExtensions.class).warn("There is no queryable persistence service registered with the id '{}'", serviceId);
return null;
}
}
use of org.eclipse.smarthome.core.persistence.FilterCriteria in project smarthome by eclipse.
the class PersistenceExtensions method getAllStatesSince.
private static Iterable<HistoricItem> getAllStatesSince(Item item, AbstractInstant timestamp, String serviceId) {
PersistenceService service = getService(serviceId);
if (service instanceof QueryablePersistenceService) {
QueryablePersistenceService qService = (QueryablePersistenceService) service;
FilterCriteria filter = new FilterCriteria();
filter.setBeginDate(ZonedDateTime.ofInstant(timestamp.toDate().toInstant(), timeZoneProvider.getTimeZone()));
filter.setItemName(item.getName());
filter.setOrdering(Ordering.ASCENDING);
return qService.query(filter);
} else {
LoggerFactory.getLogger(PersistenceExtensions.class).warn("There is no queryable persistence service registered with the id '{}'", serviceId);
return Collections.emptySet();
}
}
use of org.eclipse.smarthome.core.persistence.FilterCriteria in project smarthome by eclipse.
the class PersistenceResource method getItemHistoryDTO.
private Response getItemHistoryDTO(String serviceId, String itemName, String timeBegin, String timeEnd, int pageNumber, int pageLength, boolean boundary) {
// Benchmarking timer...
long timerStart = System.currentTimeMillis();
// 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.debug("Persistence service not found '{}'.", effectiveServiceId);
return JSONResponse.createErrorResponse(Status.BAD_REQUEST, "Persistence service not found: " + effectiveServiceId);
}
if (!(service instanceof QueryablePersistenceService)) {
logger.debug("Persistence service not queryable '{}'.", effectiveServiceId);
return JSONResponse.createErrorResponse(Status.BAD_REQUEST, "Persistence service not queryable: " + effectiveServiceId);
}
QueryablePersistenceService qService = (QueryablePersistenceService) service;
ZonedDateTime dateTimeBegin = ZonedDateTime.now();
ZonedDateTime dateTimeEnd = dateTimeBegin;
if (timeBegin != null) {
dateTimeBegin = convertTime(timeBegin);
}
if (timeEnd != null) {
dateTimeEnd = convertTime(timeEnd);
}
// End now...
if (dateTimeEnd.toEpochSecond() == 0) {
dateTimeEnd = ZonedDateTime.of(LocalDateTime.now(), timeZoneProvider.getTimeZone());
}
if (dateTimeBegin.toEpochSecond() == 0) {
// Default to 1 days data if the times are the same or the start time is newer
// than the end time
dateTimeBegin = ZonedDateTime.of(dateTimeEnd.toLocalDateTime().plusDays(-1), timeZoneProvider.getTimeZone());
}
// than the end time
if (dateTimeBegin.isAfter(dateTimeEnd) || dateTimeBegin.isEqual(dateTimeEnd)) {
dateTimeBegin = ZonedDateTime.of(dateTimeEnd.toLocalDateTime().plusDays(-1), timeZoneProvider.getTimeZone());
}
FilterCriteria filter;
Iterable<HistoricItem> result;
State state = null;
Long quantity = 0l;
ItemHistoryDTO dto = new ItemHistoryDTO();
dto.name = itemName;
filter = new FilterCriteria();
filter.setItemName(itemName);
// (or not at all if there's no change during the graph period)
if (boundary) {
// Get the value before the start time.
filter.setEndDate(dateTimeBegin);
filter.setPageSize(1);
filter.setOrdering(Ordering.DESCENDING);
result = qService.query(filter);
if (result != null && result.iterator().hasNext()) {
dto.addData(dateTimeBegin.toInstant().toEpochMilli(), result.iterator().next().getState());
quantity++;
}
}
if (pageLength == 0) {
filter.setPageNumber(0);
filter.setPageSize(Integer.MAX_VALUE);
} else {
filter.setPageNumber(pageNumber);
filter.setPageSize(pageLength);
}
filter.setBeginDate(dateTimeBegin);
filter.setEndDate(dateTimeEnd);
filter.setOrdering(Ordering.ASCENDING);
result = qService.query(filter);
if (result != null) {
Iterator<HistoricItem> it = result.iterator();
// Iterate through the data
while (it.hasNext()) {
HistoricItem historicItem = it.next();
state = historicItem.getState();
// to avoid diagonal lines
if (state instanceof OnOffType || state instanceof OpenClosedType) {
dto.addData(historicItem.getTimestamp().getTime(), state);
}
dto.addData(historicItem.getTimestamp().getTime(), state);
quantity++;
}
}
if (boundary) {
// Get the value after the end time.
filter.setBeginDate(dateTimeEnd);
filter.setPageSize(1);
filter.setOrdering(Ordering.ASCENDING);
result = qService.query(filter);
if (result != null && result.iterator().hasNext()) {
dto.addData(dateTimeEnd.toInstant().toEpochMilli(), result.iterator().next().getState());
quantity++;
}
}
dto.datapoints = Long.toString(quantity);
logger.debug("Persistence returned {} rows in {}ms", dto.datapoints, System.currentTimeMillis() - timerStart);
return JSONResponse.createResponse(Status.OK, dto, "");
}
Aggregations