Search in sources :

Example 1 with Synchronizer

use of com.serotonin.timer.sync.Synchronizer in project ma-core-public by infiniteautomation.

the class AsyncImageChartServlet method getImageData.

private byte[] getImageData(String imageInfo, HttpServletRequest request) throws IOException {
    // Hex colour definitions need to be prefixed with '0x' instead of '#'.
    try {
        // Remove the / and the .png
        imageInfo = imageInfo.substring(1, imageInfo.length() - 4);
        // Split by underscore.
        String[] imageBits = imageInfo.split("_");
        // Get the data.
        long from, to;
        int pointIdStart;
        if (imageBits[0].equals("ft")) {
            from = Long.parseLong(imageBits[2]);
            to = Long.parseLong(imageBits[3]);
            pointIdStart = 4;
        } else {
            from = Common.timer.currentTimeMillis() - Long.parseLong(imageBits[1]);
            to = -1;
            pointIdStart = 2;
        }
        int width = getIntRequestParameter(request, "w", 200);
        int height = getIntRequestParameter(request, "h", 100);
        TimeZone timeZone = Common.getUserTimeZone(Common.getUser(request));
        // Create the datasets
        Synchronizer<PointDataRetriever> tasks = new Synchronizer<PointDataRetriever>();
        List<Integer> dataPointIds = new ArrayList<Integer>();
        for (int i = pointIdStart; i < imageBits.length; i++) {
            if (imageBits[i].startsWith("w"))
                width = NumberUtils.toInt(imageBits[i].substring(1), width);
            else if (imageBits[i].startsWith("h"))
                height = NumberUtils.toInt(imageBits[i].substring(1), height);
            else {
                String dataPointStr = imageBits[i];
                Color colour = null;
                int dataPointId;
                int pipe = dataPointStr.indexOf('|');
                if (pipe == -1)
                    dataPointId = Integer.parseInt(dataPointStr);
                else {
                    try {
                        String colourStr = dataPointStr.substring(pipe + 1);
                        if (colourStr.startsWith("0x"))
                            colourStr = "#" + colourStr.substring(2);
                        colour = ColorUtils.toColor(colourStr);
                    } catch (InvalidArgumentException e) {
                        throw new IOException(e);
                    }
                    dataPointId = Integer.parseInt(dataPointStr.substring(0, pipe));
                }
                dataPointIds.add(dataPointId);
                PointDataRetriever pdr = new PointDataRetriever(dataPointId, colour, width * 3, timeZone);
                tasks.addTask(pdr);
            }
        }
        if (tasks.getSize() == 0)
            return null;
        long start = from;
        long end = to;
        if (from == -1 && to == -1) {
            LongPair sae = pointValueDao.getStartAndEndTime(dataPointIds);
            start = sae.getL1();
            end = sae.getL2();
        } else if (from == -1)
            start = pointValueDao.getStartTime(dataPointIds);
        else if (to == -1)
            end = pointValueDao.getEndTime(dataPointIds);
        for (PointDataRetriever pdr : tasks.getTasks()) pdr.setRange(start, end);
        // Get the timer
        tasks.executeAndWait(Providers.get(TimerProvider.class).getTimer());
        PointTimeSeriesCollection ptsc = new PointTimeSeriesCollection(timeZone);
        for (PointDataRetriever pdr : tasks.getTasks()) pdr.addToCollection(ptsc);
        return ImageChartUtils.getChartData(ptsc, width, height, from, to);
    } catch (StringIndexOutOfBoundsException e) {
    // no op
    } catch (NumberFormatException e) {
    // no op
    } catch (ArrayIndexOutOfBoundsException e) {
    // no op
    }
    return null;
}
Also used : Synchronizer(com.serotonin.timer.sync.Synchronizer) LongPair(com.serotonin.m2m2.vo.pair.LongPair) PointTimeSeriesCollection(com.serotonin.m2m2.util.chart.PointTimeSeriesCollection) Color(java.awt.Color) ArrayList(java.util.ArrayList) IOException(java.io.IOException) TimeZone(java.util.TimeZone) InvalidArgumentException(com.serotonin.InvalidArgumentException)

Aggregations

InvalidArgumentException (com.serotonin.InvalidArgumentException)1 PointTimeSeriesCollection (com.serotonin.m2m2.util.chart.PointTimeSeriesCollection)1 LongPair (com.serotonin.m2m2.vo.pair.LongPair)1 Synchronizer (com.serotonin.timer.sync.Synchronizer)1 Color (java.awt.Color)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 TimeZone (java.util.TimeZone)1