Search in sources :

Example 1 with QueryablePersistenceService

use of org.openhab.core.persistence.QueryablePersistenceService in project openhab-addons by openhab.

the class DeviceHistoryHandler method handle.

public HistoryList handle(HttpServletRequest req, Matcher urlMatcher) {
    String deviceId, field;
    long start, end;
    try {
        deviceId = URLDecoder.decode(urlMatcher.group(1), StandardCharsets.UTF_8);
        field = URLDecoder.decode(urlMatcher.group(2), StandardCharsets.UTF_8);
        start = Long.parseLong(urlMatcher.group(3));
        end = Long.parseLong(urlMatcher.group(4));
    } catch (NumberFormatException e) {
        throw new RuntimeException("Could not decode request params", e);
    }
    logger.debug("History request for device {}, field {}: {}-{}", deviceId, field, start, end);
    AbstractDevice device = deviceRegistry.getDevice(deviceId);
    if (device == null) {
        logger.warn("Received history request for unknown device: {}", urlMatcher.group(0));
        return null;
    }
    PersistenceService persistence = persistenceServiceRegistry.getDefault();
    if (persistence == null) {
        logger.warn("Could not retrieve default persistence service; can't serve history request");
        return null;
    }
    if (!(persistence instanceof QueryablePersistenceService)) {
        logger.warn("Default persistence service is not queryable; can't serve history request");
        return null;
    }
    return serveHistory(device, (QueryablePersistenceService) persistence, start, end);
}
Also used : QueryablePersistenceService(org.openhab.core.persistence.QueryablePersistenceService) PersistenceService(org.openhab.core.persistence.PersistenceService) QueryablePersistenceService(org.openhab.core.persistence.QueryablePersistenceService) AbstractDevice(org.openhab.io.imperihome.internal.model.device.AbstractDevice)

Example 2 with QueryablePersistenceService

use of org.openhab.core.persistence.QueryablePersistenceService in project openhab-core by openhab.

the class PersistenceExtensions method getAllStatesSince.

private static Iterable<HistoricItem> getAllStatesSince(Item item, ZonedDateTime timestamp, String serviceId) {
    PersistenceService service = getService(serviceId);
    if (service instanceof QueryablePersistenceService) {
        QueryablePersistenceService qService = (QueryablePersistenceService) service;
        FilterCriteria filter = new FilterCriteria();
        filter.setBeginDate(timestamp);
        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();
    }
}
Also used : QueryablePersistenceService(org.openhab.core.persistence.QueryablePersistenceService) PersistenceService(org.openhab.core.persistence.PersistenceService) QueryablePersistenceService(org.openhab.core.persistence.QueryablePersistenceService) FilterCriteria(org.openhab.core.persistence.FilterCriteria)

Example 3 with QueryablePersistenceService

use of org.openhab.core.persistence.QueryablePersistenceService in project openhab-core by openhab.

the class DefaultChartProvider method createChart.

@Override
public BufferedImage createChart(@Nullable String serviceId, @Nullable String theme, ZonedDateTime startTime, ZonedDateTime endTime, int height, int width, @Nullable String items, @Nullable String groups, @Nullable Integer dpiValue, @Nullable String yAxisDecimalPattern, @Nullable Boolean legend) throws ItemNotFoundException, IllegalArgumentException {
    logger.debug("Rendering chart: service: '{}', theme: '{}', startTime: '{}', endTime: '{}', width: '{}', height: '{}', items: '{}', groups: '{}', dpi: '{}', yAxisDecimalPattern: '{}', legend: '{}'", serviceId, theme, startTime, endTime, width, height, items, groups, dpiValue, yAxisDecimalPattern, legend);
    // If a persistence service is specified, find the provider, or use the default provider
    PersistenceService service = (serviceId == null) ? persistenceServiceRegistry.getDefault() : persistenceServiceRegistry.get(serviceId);
    // Did we find a service?
    QueryablePersistenceService persistenceService = (service instanceof QueryablePersistenceService) ? (QueryablePersistenceService) service : (QueryablePersistenceService) // 
    persistenceServiceRegistry.getAll().stream().filter(// 
    it -> it instanceof QueryablePersistenceService).findFirst().orElseThrow(() -> new IllegalArgumentException("No Persistence service found."));
    int seriesCounter = 0;
    // get theme
    ChartTheme chartTheme = theme == null ? CHART_THEME_DEFAULT : CHART_THEMES.getOrDefault(theme, CHART_THEME_DEFAULT);
    // get DPI
    int dpi = dpiValue != null && dpiValue > 0 ? dpiValue : DPI_DEFAULT;
    // Create Chart
    XYChart chart = new XYChartBuilder().width(width).height(height).build();
    // Define the time axis - the defaults are not very nice
    Duration period = Duration.between(startTime, endTime);
    String pattern;
    if (period.compareTo(TEN_MINUTES) <= 0) {
        pattern = "mm:ss";
    } else if (period.compareTo(ONE_DAY) <= 0) {
        pattern = "HH:mm";
    } else if (period.compareTo(ONE_WEEK) <= 0) {
        pattern = "EEE d";
    } else {
        pattern = "d MMM";
    }
    XYStyler styler = chart.getStyler();
    styler.setDatePattern(pattern);
    // axis
    styler.setAxisTickLabelsFont(chartTheme.getAxisTickLabelsFont(dpi));
    styler.setAxisTickLabelsColor(chartTheme.getAxisTickLabelsColor());
    styler.setXAxisMin((double) startTime.toInstant().toEpochMilli());
    styler.setXAxisMax((double) endTime.toInstant().toEpochMilli());
    int yAxisSpacing = Math.max(height / 10, chartTheme.getAxisTickLabelsFont(dpi).getSize());
    if (yAxisDecimalPattern != null) {
        styler.setYAxisDecimalPattern(yAxisDecimalPattern);
    }
    styler.setYAxisTickMarkSpacingHint(yAxisSpacing);
    styler.setYAxisLabelAlignment(Styler.TextAlignment.Right);
    // chart
    styler.setChartBackgroundColor(chartTheme.getChartBackgroundColor());
    styler.setChartFontColor(chartTheme.getChartFontColor());
    styler.setChartPadding(chartTheme.getChartPadding(dpi));
    styler.setPlotBackgroundColor(chartTheme.getPlotBackgroundColor());
    float plotGridLinesDash = (float) chartTheme.getPlotGridLinesDash(dpi);
    float[] plotGridLinesDashArray = { plotGridLinesDash, plotGridLinesDash };
    styler.setPlotGridLinesStroke(new BasicStroke((float) chartTheme.getPlotGridLinesWidth(dpi), BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 10, plotGridLinesDashArray, 0));
    styler.setPlotGridLinesColor(chartTheme.getPlotGridLinesColor());
    // legend
    styler.setLegendBackgroundColor(chartTheme.getLegendBackgroundColor());
    styler.setLegendFont(chartTheme.getLegendFont(dpi));
    styler.setLegendSeriesLineLength(chartTheme.getLegendSeriesLineLength(dpi));
    LegendPositionDecider legendPositionDecider = new LegendPositionDecider();
    // Loop through all the items
    if (items != null) {
        String[] itemNames = items.split(",");
        for (String itemName : itemNames) {
            Item item = itemUIRegistry.getItem(itemName);
            if (addItem(chart, persistenceService, startTime, endTime, item, seriesCounter, chartTheme, dpi, legendPositionDecider)) {
                seriesCounter++;
            }
        }
    }
    // Loop through all the groups and add each item from each group
    if (groups != null) {
        String[] groupNames = groups.split(",");
        for (String groupName : groupNames) {
            Item item = itemUIRegistry.getItem(groupName);
            if (item instanceof GroupItem) {
                GroupItem groupItem = (GroupItem) item;
                for (Item member : groupItem.getMembers()) {
                    if (addItem(chart, persistenceService, startTime, endTime, member, seriesCounter, chartTheme, dpi, legendPositionDecider)) {
                        seriesCounter++;
                    }
                }
            } else {
                throw new ItemNotFoundException("Item '" + item.getName() + "' defined in groups is not a group.");
            }
        }
    }
    Boolean showLegend = null;
    // If there are no series, render a blank chart
    if (seriesCounter == 0) {
        // always hide the legend
        showLegend = false;
        List<Date> xData = new ArrayList<>();
        List<Number> yData = new ArrayList<>();
        xData.add(Date.from(startTime.toInstant()));
        yData.add(0);
        xData.add(Date.from(endTime.toInstant()));
        yData.add(0);
        XYSeries series = chart.addSeries("NONE", xData, yData);
        series.setMarker(new None());
        series.setLineStyle(new BasicStroke(0f));
    }
    // if the legend is not already hidden, check if legend parameter is supplied, or calculate a sensible value
    if (showLegend == null) {
        if (legend == null) {
            // more than one series, show the legend. otherwise hide it.
            showLegend = seriesCounter > 1;
        } else {
            // take value from supplied legend parameter
            showLegend = legend;
        }
    }
    // This won't be perfect, but it's a good compromise
    if (showLegend) {
        styler.setLegendPosition(legendPositionDecider.getLegendPosition());
    } else {
        // hide the whole legend
        styler.setLegendVisible(false);
    }
    // Write the chart as a PNG image
    BufferedImage lBufferedImage = new BufferedImage(chart.getWidth(), chart.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D lGraphics2D = lBufferedImage.createGraphics();
    chart.paint(lGraphics2D, chart.getWidth(), chart.getHeight());
    return lBufferedImage;
}
Also used : BasicStroke(java.awt.BasicStroke) XYSeries(org.knowm.xchart.XYSeries) ArrayList(java.util.ArrayList) BufferedImage(java.awt.image.BufferedImage) QueryablePersistenceService(org.openhab.core.persistence.QueryablePersistenceService) HistoricItem(org.openhab.core.persistence.HistoricItem) GroupItem(org.openhab.core.items.GroupItem) Item(org.openhab.core.items.Item) GroupItem(org.openhab.core.items.GroupItem) Duration(java.time.Duration) XYStyler(org.knowm.xchart.style.XYStyler) Date(java.util.Date) Graphics2D(java.awt.Graphics2D) QueryablePersistenceService(org.openhab.core.persistence.QueryablePersistenceService) PersistenceService(org.openhab.core.persistence.PersistenceService) XYChart(org.knowm.xchart.XYChart) XYChartBuilder(org.knowm.xchart.XYChartBuilder) None(org.knowm.xchart.style.markers.None) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Example 4 with QueryablePersistenceService

use of org.openhab.core.persistence.QueryablePersistenceService in project openhab-core by openhab.

the class PersistenceResource method getServiceItemList.

private Response getServiceItemList(@Nullable String serviceId) {
    // If serviceId is null, then use the default service
    PersistenceService service = null;
    if (serviceId == null) {
        service = persistenceServiceRegistry.getDefault();
    } else {
        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 QueryablePersistenceService)) {
        logger.debug("Persistence service not queryable '{}'.", serviceId);
        return JSONResponse.createErrorResponse(Status.BAD_REQUEST, "Persistence service not queryable: " + serviceId);
    }
    QueryablePersistenceService qService = (QueryablePersistenceService) service;
    return JSONResponse.createResponse(Status.OK, qService.getItemInfo(), "");
}
Also used : ModifiablePersistenceService(org.openhab.core.persistence.ModifiablePersistenceService) PersistenceService(org.openhab.core.persistence.PersistenceService) QueryablePersistenceService(org.openhab.core.persistence.QueryablePersistenceService) QueryablePersistenceService(org.openhab.core.persistence.QueryablePersistenceService)

Example 5 with QueryablePersistenceService

use of org.openhab.core.persistence.QueryablePersistenceService in project openhab-core by openhab.

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<>();
    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;
}
Also used : ModifiablePersistenceService(org.openhab.core.persistence.ModifiablePersistenceService) PersistenceService(org.openhab.core.persistence.PersistenceService) QueryablePersistenceService(org.openhab.core.persistence.QueryablePersistenceService) QueryablePersistenceService(org.openhab.core.persistence.QueryablePersistenceService) PersistenceServiceDTO(org.openhab.core.persistence.dto.PersistenceServiceDTO) ModifiablePersistenceService(org.openhab.core.persistence.ModifiablePersistenceService) ArrayList(java.util.ArrayList)

Aggregations

QueryablePersistenceService (org.openhab.core.persistence.QueryablePersistenceService)12 PersistenceService (org.openhab.core.persistence.PersistenceService)11 FilterCriteria (org.openhab.core.persistence.FilterCriteria)7 HistoricItem (org.openhab.core.persistence.HistoricItem)7 Nullable (org.eclipse.jdt.annotation.Nullable)4 ModifiablePersistenceService (org.openhab.core.persistence.ModifiablePersistenceService)3 ZonedDateTime (java.time.ZonedDateTime)2 ArrayList (java.util.ArrayList)2 State (org.openhab.core.types.State)2 BasicStroke (java.awt.BasicStroke)1 Graphics2D (java.awt.Graphics2D)1 BufferedImage (java.awt.image.BufferedImage)1 Duration (java.time.Duration)1 Date (java.util.Date)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 XYChart (org.knowm.xchart.XYChart)1 XYChartBuilder (org.knowm.xchart.XYChartBuilder)1 XYSeries (org.knowm.xchart.XYSeries)1 XYStyler (org.knowm.xchart.style.XYStyler)1 None (org.knowm.xchart.style.markers.None)1