use of org.eclipse.smarthome.core.library.types.OpenClosedType in project smarthome by eclipse.
the class DefaultChartProvider method addItem.
boolean addItem(Chart chart, QueryablePersistenceService service, Date timeBegin, Date timeEnd, Item item, int seriesCounter, ChartTheme chartTheme, int dpi) {
Color color = chartTheme.getLineColor(seriesCounter);
// Get the item label
String label = null;
if (itemUIRegistry != null) {
// Get the item label
label = itemUIRegistry.getLabel(item.getName());
if (label != null && label.contains("[") && label.contains("]")) {
label = label.substring(0, label.indexOf('['));
}
}
if (label == null) {
label = item.getName();
}
Iterable<HistoricItem> result;
FilterCriteria filter;
// Generate data collections
List<Date> xData = new ArrayList<Date>();
List<Number> yData = new ArrayList<Number>();
// Declare state here so it will hold the last value at the end of the process
State state = null;
// 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.setEndDate(ZonedDateTime.ofInstant(timeBegin.toInstant(), timeZoneProvider.getTimeZone()));
filter.setItemName(item.getName());
filter.setPageSize(1);
filter.setOrdering(Ordering.DESCENDING);
result = service.query(filter);
if (result.iterator().hasNext()) {
HistoricItem historicItem = result.iterator().next();
state = historicItem.getState();
xData.add(timeBegin);
yData.add(convertData(state));
}
// Now, get all the data between the start and end time
filter.setBeginDate(ZonedDateTime.ofInstant(timeBegin.toInstant(), timeZoneProvider.getTimeZone()));
filter.setEndDate(ZonedDateTime.ofInstant(timeEnd.toInstant(), timeZoneProvider.getTimeZone()));
filter.setPageSize(Integer.MAX_VALUE);
filter.setOrdering(Ordering.ASCENDING);
// Get the data from the persistence store
result = service.query(filter);
Iterator<HistoricItem> it = result.iterator();
// Iterate through the data
while (it.hasNext()) {
HistoricItem historicItem = it.next();
// to avoid diagonal lines
if (state instanceof OnOffType || state instanceof OpenClosedType) {
Calendar cal = Calendar.getInstance();
cal.setTime(historicItem.getTimestamp());
cal.add(Calendar.MILLISECOND, -1);
xData.add(cal.getTime());
yData.add(convertData(state));
}
state = historicItem.getState();
xData.add(historicItem.getTimestamp());
yData.add(convertData(state));
}
// Lastly, add the final state at the endtime
if (state != null) {
xData.add(timeEnd);
yData.add(convertData(state));
}
// The chart engine will throw an exception if there's no data
if (xData.size() == 0) {
return false;
}
// If there's only 1 data point, plot it again!
if (xData.size() == 1) {
xData.add(xData.iterator().next());
yData.add(yData.iterator().next());
}
Series series = chart.addSeries(label, xData, yData);
float lineWidth = (float) chartTheme.getLineWidth(dpi);
series.setLineStyle(new BasicStroke(lineWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER));
series.setMarker(SeriesMarker.NONE);
series.setLineColor(color);
// We use this to decide whether to put the legend in the top or bottom corner.
if (yData.iterator().next().floatValue() > ((series.getYMax() - series.getYMin()) / 2 + series.getYMin())) {
legendPosition++;
} else {
legendPosition--;
}
return true;
}
use of org.eclipse.smarthome.core.library.types.OpenClosedType 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