Search in sources :

Example 21 with AbstractPointEventDetectorVO

use of com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO in project ma-core-public by infiniteautomation.

the class ImageChartServlet 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);
        String useCacheString = request.getParameter("useCache");
        boolean useCache = false;
        if (useCacheString != null && Boolean.valueOf(useCacheString))
            useCache = true;
        TimeZone timeZone = Common.getUserTimeZone(Common.getUser(request));
        // Create the datasets
        DataPointVO markerPoint = null;
        int pointCount = 0;
        PointTimeSeriesCollection ptsc = new PointTimeSeriesCollection(timeZone);
        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));
                }
                // Get the data.
                DataPointVO dp = DataPointDao.instance.getDataPoint(dataPointId);
                if (dp != null && dp.getName() != null) {
                    pointCount++;
                    markerPoint = dp;
                    // Get the Color if there wasn't one provided
                    if (colour == null) {
                        try {
                            if (dp.getChartColour() != null)
                                colour = ColorUtils.toColor(dp.getChartColour());
                        } catch (InvalidArgumentException e) {
                        // Munch it
                        }
                    }
                    PointValueFacade pointValueFacade = new PointValueFacade(dataPointId, useCache);
                    List<PointValueTime> data;
                    if (from == -1 && to == -1)
                        data = pointValueFacade.getPointValuesBetween(0, Common.timer.currentTimeMillis(), true, true);
                    else if (from == -1)
                        data = pointValueFacade.getPointValuesBetween(0, to, true, true);
                    else if (to == -1)
                        data = pointValueFacade.getPointValuesBetween(from, Common.timer.currentTimeMillis(), true, true);
                    else
                        data = pointValueFacade.getPointValuesBetween(from, to, true, true);
                    if (dp.getPointLocator().getDataTypeId() == DataTypes.NUMERIC) {
                        TimeSeries ts;
                        if (dp.isUseRenderedUnit()) {
                            // This works because we enforce that all Units default to the ONE Unit if not used
                            UnitConverter converter = null;
                            if (dp.getRenderedUnit() != dp.getUnit())
                                converter = dp.getUnit().getConverterTo(dp.getRenderedUnit());
                            ts = new TimeSeries(dp.getExtendedName(), null, dp.getTextRenderer().getMetaText());
                            double value;
                            for (PointValueTime pv : data) {
                                if (pv.getValue() != null) {
                                    if (converter != null)
                                        value = converter.convert(pv.getDoubleValue());
                                    else
                                        value = pv.getDoubleValue();
                                    ImageChartUtils.addMillisecond(ts, pv.getTime(), value);
                                }
                            }
                        } else {
                            // No renderer, don't need it
                            ts = new TimeSeries(dp.getExtendedName(), null, dp.getTextRenderer().getMetaText());
                            for (PointValueTime pv : data) {
                                if (pv.getValue() != null)
                                    ImageChartUtils.addMillisecond(ts, pv.getTime(), pv.getValue().numberValue());
                            }
                        }
                        ptsc.addNumericTimeSeries(new NumericTimeSeries(dp.getPlotType(), ts, colour, null));
                    } else {
                        DiscreteTimeSeries ts = new DiscreteTimeSeries(dp.getExtendedName(), dp.getTextRenderer(), colour, null);
                        for (PointValueTime pv : data) if (pv.getValue() != null)
                            ts.addValueTime(pv);
                        ptsc.addDiscreteTimeSeries(ts);
                    }
                }
            }
        }
        if (pointCount == 1) {
            // Only one point. Check for limits to draw as markers.
            UnitConverter uc;
            if (markerPoint.getUnit() != null && markerPoint.getRenderedUnit() != null)
                uc = markerPoint.getUnit().getConverterTo(markerPoint.getRenderedUnit());
            else
                uc = Unit.ONE.getConverterTo(Unit.ONE);
            for (AbstractPointEventDetectorVO<?> ped : markerPoint.getEventDetectors()) {
                if (ped.getDefinition().getEventDetectorTypeName().equals(AnalogLowLimitEventDetectorDefinition.TYPE_NAME))
                    ptsc.addRangeMarker(new ValueMarker(uc.convert(((AnalogLowLimitDetectorVO) ped).getLimit()), lowLimitPaint, limitStroke));
                else if (ped.getDefinition().getEventDetectorTypeName().equals(AnalogHighLimitEventDetectorDefinition.TYPE_NAME))
                    ptsc.addRangeMarker(new ValueMarker(uc.convert(((AnalogHighLimitDetectorVO) ped).getLimit()), highLimitPaint, limitStroke));
            }
        }
        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 : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PointValueFacade(com.serotonin.m2m2.rt.dataImage.PointValueFacade) NumericTimeSeries(com.serotonin.m2m2.util.chart.NumericTimeSeries) TimeSeries(org.jfree.data.time.TimeSeries) DiscreteTimeSeries(com.serotonin.m2m2.util.chart.DiscreteTimeSeries) PointTimeSeriesCollection(com.serotonin.m2m2.util.chart.PointTimeSeriesCollection) Color(java.awt.Color) IOException(java.io.IOException) Paint(java.awt.Paint) TimeZone(java.util.TimeZone) InvalidArgumentException(com.serotonin.InvalidArgumentException) NumericTimeSeries(com.serotonin.m2m2.util.chart.NumericTimeSeries) UnitConverter(javax.measure.converter.UnitConverter) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) AnalogLowLimitDetectorVO(com.serotonin.m2m2.vo.event.detector.AnalogLowLimitDetectorVO) ArrayList(java.util.ArrayList) List(java.util.List) ValueMarker(org.jfree.chart.plot.ValueMarker) DiscreteTimeSeries(com.serotonin.m2m2.util.chart.DiscreteTimeSeries)

Example 22 with AbstractPointEventDetectorVO

use of com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO in project ma-core-public by infiniteautomation.

the class EventDetectorImporter method importImpl.

@Override
protected void importImpl() {
    String dataPointXid = json.getString("dataPointXid");
    DataPointVO dpvo;
    // Everyone is in the same thread so no synchronization on dataPointMap required.
    if (dataPointMap.containsKey(dataPointXid))
        dpvo = dataPointMap.get(dataPointXid);
    else if (StringUtils.isEmpty(dataPointXid) || (dpvo = DataPointDao.instance.getByXid(dataPointXid)) == null) {
        addFailureMessage("emport.error.missingPoint", dataPointXid);
        return;
    } else {
        dataPointMap.put(dataPointXid, dpvo);
        // We're only going to use this to house event detectors imported in the eventDetectors object.
        dpvo.setEventDetectors(new ArrayList<AbstractPointEventDetectorVO<?>>());
    }
    String typeStr = json.getString("type");
    if (typeStr == null)
        addFailureMessage("emport.error.ped.missingAttr", "type");
    EventDetectorDefinition<?> def = ModuleRegistry.getEventDetectorDefinition(typeStr);
    if (def == null) {
        addFailureMessage("emport.error.ped.invalid", "type", typeStr, ModuleRegistry.getEventDetectorDefinitionTypes());
        return;
    }
    JsonArray handlerXids = json.getJsonArray("handlers");
    if (handlerXids != null)
        for (int k = 0; k < handlerXids.size(); k += 1) {
            AbstractEventHandlerVO<?> eh = EventHandlerDao.instance.getByXid(handlerXids.getString(k));
            if (eh == null) {
                addFailureMessage("emport.eventHandler.missing", handlerXids.getString(k));
                return;
            }
        }
    AbstractEventDetectorVO<?> importing = def.baseCreateEventDetectorVO();
    importing.setDefinition(def);
    String xid = json.getString("xid");
    // Create a new one
    importing.setId(Common.NEW_ID);
    importing.setXid(xid);
    AbstractPointEventDetectorVO<?> dped = (AbstractPointEventDetectorVO<?>) importing;
    dped.njbSetDataPoint(dpvo);
    dpvo.getEventDetectors().add(dped);
    try {
        ctx.getReader().readInto(importing, json);
    // try {
    // if(Common.runtimeManager.getState() == RuntimeManager.RUNNING){
    // Common.runtimeManager.saveDataPoint(dpvo);
    // addSuccessMessage(isNew, "emport.eventDetector.prefix", xid);
    // }else{
    // addFailureMessage(new ProcessMessage("Runtime Manager not running point with xid: " + xid + " not saved."));
    // }
    // } catch(LicenseViolatedException e) {
    // addFailureMessage(new ProcessMessage(e.getErrorMessage()));
    // }
    } catch (TranslatableJsonException e) {
        addFailureMessage("emport.eventDetector.prefix", xid, e.getMsg());
    } catch (JsonException e) {
        addFailureMessage("emport.eventDetector.prefix", xid, getJsonExceptionMessage(e));
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) JsonArray(com.serotonin.json.type.JsonArray) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) ArrayList(java.util.ArrayList) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) AbstractEventHandlerVO(com.serotonin.m2m2.vo.event.AbstractEventHandlerVO)

Example 23 with AbstractPointEventDetectorVO

use of com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO in project ma-core-public by infiniteautomation.

the class DataPointDao method copy.

@Override
public int copy(final int existingId, final String newXid, final String newName) {
    TransactionCallback<Integer> callback = new TransactionCallback<Integer>() {

        @Override
        public Integer doInTransaction(TransactionStatus status) {
            DataPointVO dataPoint = get(existingId);
            // Copy the vo
            DataPointVO copy = dataPoint.copy();
            copy.setId(Common.NEW_ID);
            copy.setXid(generateUniqueXid());
            copy.setName(dataPoint.getName());
            copy.setDeviceName(dataPoint.getDeviceName());
            copy.setDataSourceId(dataPoint.getDataSourceId());
            copy.setEnabled(dataPoint.isEnabled());
            copy.getComments().clear();
            // Copy the event detectors
            List<AbstractEventDetectorVO<?>> existing = EventDetectorDao.instance.getWithSourceId(EventType.EventTypeNames.DATA_POINT, dataPoint.getId());
            List<AbstractPointEventDetectorVO<?>> detectors = new ArrayList<AbstractPointEventDetectorVO<?>>(existing.size());
            for (AbstractEventDetectorVO<?> ed : existing) {
                AbstractPointEventDetectorVO<?> ped = (AbstractPointEventDetectorVO<?>) ed;
                ped.setId(Common.NEW_ID);
                ped.njbSetDataPoint(copy);
            }
            copy.setEventDetectors(detectors);
            Common.runtimeManager.saveDataPoint(copy);
            // Copy permissions.
            return copy.getId();
        }
    };
    return getTransactionTemplate().execute(callback);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) TransactionCallback(org.springframework.transaction.support.TransactionCallback) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) AbstractEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractEventDetectorVO) ArrayList(java.util.ArrayList) TransactionStatus(org.springframework.transaction.TransactionStatus)

Aggregations

DataPointVO (com.serotonin.m2m2.vo.DataPointVO)21 AbstractPointEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)17 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)8 ArrayList (java.util.ArrayList)7 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)6 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)5 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)5 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)3 User (com.serotonin.m2m2.vo.User)3 DataPointPropertiesTemplateVO (com.serotonin.m2m2.vo.template.DataPointPropertiesTemplateVO)3 ResponseEntity (org.springframework.http.ResponseEntity)3 ServerErrorException (com.infiniteautomation.mango.rest.v2.exception.ServerErrorException)2 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)2 JsonException (com.serotonin.json.JsonException)2 JsonArray (com.serotonin.json.type.JsonArray)2 LicenseViolatedException (com.serotonin.m2m2.LicenseViolatedException)2 ProcessMessage (com.serotonin.m2m2.i18n.ProcessMessage)2 DataSourceVO (com.serotonin.m2m2.vo.dataSource.DataSourceVO)2