Search in sources :

Example 46 with DataPointRT

use of com.serotonin.m2m2.rt.dataImage.DataPointRT in project ma-core-public by infiniteautomation.

the class RuntimeManagerImpl method stopDataPointShutdown.

/**
 * Only to be used at shutdown as synchronization has been reduced for performance
 */
private void stopDataPointShutdown(int dataPointId) {
    DataPointRT p = null;
    synchronized (dataPoints) {
        // Remove this point from the data image if it is there. If not, just quit.
        p = dataPoints.remove(dataPointId);
    }
    // Remove it from the data source, and terminate it.
    if (p != null) {
        try {
            getRunningDataSource(p.getDataSourceId()).removeDataPoint(p);
        } catch (Exception e) {
            LOG.error("Failed to stop point RT with ID: " + dataPointId + " stopping point.", e);
        }
        DataPointListener l = getDataPointListeners(dataPointId);
        if (l != null)
            l.pointTerminated();
        p.terminate();
    }
}
Also used : DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) DataPointListener(com.serotonin.m2m2.rt.dataImage.DataPointListener) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException)

Example 47 with DataPointRT

use of com.serotonin.m2m2.rt.dataImage.DataPointRT in project ma-core-public by infiniteautomation.

the class RuntimeManagerImpl method stopDataSourceShutdown.

/**
 * Should only be called at Shutdown as synchronization has been reduced for performance
 */
@Override
public void stopDataSourceShutdown(int id) {
    DataSourceRT<? extends DataSourceVO<?>> dataSource = getRunningDataSource(id);
    if (dataSource == null)
        return;
    try {
        // Stop the data points.
        for (DataPointRT p : dataPoints.values()) {
            if (p.getDataSourceId() == id)
                stopDataPointShutdown(p.getId());
        }
        synchronized (runningDataSources) {
            runningDataSources.remove(dataSource.getId());
        }
        dataSource.terminate();
        dataSource.joinTermination();
        LOG.info("Data source '" + dataSource.getName() + "' stopped");
    } catch (Exception e) {
        LOG.error("Data source '" + dataSource.getName() + "' failed proper termination.", e);
    }
}
Also used : DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException)

Example 48 with DataPointRT

use of com.serotonin.m2m2.rt.dataImage.DataPointRT in project ma-core-public by infiniteautomation.

the class RuntimeManagerImpl method setDataPointValue.

/* (non-Javadoc)
     * @see com.serotonin.m2m2.rt.RuntimeManager#setDataPointValue(int, com.serotonin.m2m2.rt.dataImage.PointValueTime, com.serotonin.m2m2.rt.dataImage.SetPointSource)
     */
@Override
public void setDataPointValue(int dataPointId, PointValueTime valueTime, SetPointSource source) {
    DataPointRT dataPoint = dataPoints.get(dataPointId);
    if (dataPoint == null)
        throw new RTException("Point is not enabled");
    if (!dataPoint.getPointLocator().isSettable())
        throw new RTException("Point is not settable");
    // Tell the data source to set the value of the point.
    DataSourceRT<? extends DataSourceVO<?>> ds = getRunningDataSource(dataPoint.getDataSourceId());
    // The data source may have been disabled. Just make sure.
    if (ds != null)
        ds.setPointValue(dataPoint, valueTime, source);
}
Also used : DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT)

Example 49 with DataPointRT

use of com.serotonin.m2m2.rt.dataImage.DataPointRT in project ma-core-public by infiniteautomation.

the class ImageValueServlet method doGet.

/**
 * @TODO(security): Validate the point access against the user. If anonymous, make sure the view allows public
 *                  access to the point.
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String imageInfo = request.getPathInfo();
    try {
        // Remove the / and the extension
        int dot = imageInfo.indexOf('.');
        String extension = imageInfo.substring(dot + 1, imageInfo.length()).toLowerCase();
        imageInfo = imageInfo.substring(1, dot);
        // Split by underscore.
        String[] imageBits = imageInfo.split("_");
        // Get the data.
        String timestamp = imageBits[0];
        int dataPointId = Integer.parseInt(imageBits[1]);
        int scalePercent = getIntRequestParameter(request, "p", -1);
        int width = getIntRequestParameter(request, "w", -1);
        int height = getIntRequestParameter(request, "h", -1);
        // DataPointRT dp = Common.ctx.getRuntimeManager().getDataPoint(dataPointId);
        // Permissions.ensureDataPointReadPermission(Common.getUser(request), dp.getVO());
        PointValueFacade pointValueFacade = new PointValueFacade(dataPointId);
        PointValueTime pvt = null;
        if (timestamp.startsWith(historyPrefix)) {
            // Find the point with the given timestamp
            long time = Long.parseLong(timestamp.substring(historyPrefix.length()));
            pvt = pointValueFacade.getPointValueAt(time);
        } else
            // Use the latest value
            pvt = pointValueFacade.getPointValue();
        if (pvt == null || pvt.getValue() == null || !(pvt.getValue() instanceof ImageValue)) {
            LOG.warn("Invalid pvt: " + pvt);
            response.sendError(HttpStatus.SC_NOT_FOUND);
        } else {
            ImageValue imageValue = (ImageValue) pvt.getValue();
            byte[] data = imageValue.getImageData();
            if (scalePercent != -1) {
                // Definitely going to be JPEG
                response.setContentType(MediaType.IMAGE_JPEG_VALUE);
                // Scale the image
                PercentScaledImage scaler = new PercentScaledImage(((float) scalePercent) / 100);
                data = ImageUtils.scaleImage(scaler, data, new JpegImageFormat(0.85f));
            } else if (width != -1 && height != -1) {
                // Definitely going to be JPEG
                response.setContentType(MediaType.IMAGE_JPEG_VALUE);
                // Scale the image
                BoxScaledImage scaler = new BoxScaledImage(width, height);
                data = ImageUtils.scaleImage(scaler, data, new JpegImageFormat(0.85f));
            } else {
                // Use the Image extension to se the Content Type
                if ("jpg".equals(extension))
                    response.setContentType(MediaType.IMAGE_JPEG_VALUE);
                else if ("png".equals(extension))
                    response.setContentType(MediaType.IMAGE_PNG_VALUE);
                else if ("gif".equals(extension))
                    response.setContentType(MediaType.IMAGE_GIF_VALUE);
            }
            response.getOutputStream().write(data);
        }
    } catch (FileNotFoundException e) {
        LOG.warn("", e);
    } catch (InterruptedException e) {
        LOG.warn("", e);
    } catch (StringIndexOutOfBoundsException e) {
        LOG.warn("", e);
    } catch (NumberFormatException e) {
        LOG.warn("", e);
    } catch (ArrayIndexOutOfBoundsException e) {
        LOG.warn("", e);
    } catch (IllegalArgumentException e) {
        LOG.warn("", e);
    }
}
Also used : PointValueFacade(com.serotonin.m2m2.rt.dataImage.PointValueFacade) FileNotFoundException(java.io.FileNotFoundException) JpegImageFormat(com.serotonin.util.image.JpegImageFormat) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) BoxScaledImage(com.serotonin.util.image.BoxScaledImage) ImageValue(com.serotonin.m2m2.rt.dataImage.types.ImageValue) PercentScaledImage(com.serotonin.util.image.PercentScaledImage)

Example 50 with DataPointRT

use of com.serotonin.m2m2.rt.dataImage.DataPointRT in project ma-core-public by infiniteautomation.

the class DataPointRTTest method testIntervalOnChangeLogging.

/**
 * Test Interval Logged Values w/ On Change Option
 */
@Test
public void testIntervalOnChangeLogging() {
    PointValueDao dao = Common.databaseProxy.newPointValueDao();
    MockPointLocatorVO plVo = new MockPointLocatorVO(DataTypes.NUMERIC, true);
    DataPointVO dpVo = new DataPointVO();
    dpVo.setId(1);
    // Configure Interval on change logging
    dpVo.setLoggingType(DataPointVO.LoggingTypes.ON_CHANGE_INTERVAL);
    dpVo.setTolerance(0.5);
    dpVo.setIntervalLoggingPeriod(5);
    dpVo.setIntervalLoggingPeriodType(TimePeriods.SECONDS);
    dpVo.setPointLocator(plVo);
    dpVo.setLoggingType(DataPointVO.LoggingTypes.ON_CHANGE_INTERVAL);
    MockDataSourceVO dsVo = new MockDataSourceVO();
    MockPointLocatorRT plRt = new MockPointLocatorRT(plVo);
    // Setup some initial data
    List<PointValueTime> initialCache = new ArrayList<>();
    initialCache.add(new PointValueTime(1.0, 0));
    SimulationTimer timer = new SimulationTimer();
    DataPointRT rt = new DataPointRT(dpVo, plRt, dsVo, initialCache, timer);
    rt.initialize();
    rt.initializeIntervalLogging(0, false);
    // Test no changes
    timer.fastForwardTo(5001);
    PointValueTime value = dao.getLatestPointValue(dpVo.getId());
    // Ensure database has interval logged value
    assertEquals(1.0, value.getDoubleValue(), 0.0001);
    assertEquals(5000, value.getTime());
    // Ensure cache does not have interval logged value
    assertEquals(1.0, rt.getPointValue().getDoubleValue(), 0.0001);
    assertEquals(0, rt.getPointValue().getTime());
    // Next interval
    timer.fastForwardTo(6000);
    rt.setPointValue(new PointValueTime(2.0, 6000), null);
    // Check Log On Change
    value = dao.getLatestPointValue(dpVo.getId());
    assertEquals(2.0, value.getDoubleValue(), 0.0001);
    assertEquals(6000, value.getTime());
    assertEquals(2.0, rt.getPointValue().getDoubleValue(), 0.0001);
    assertEquals(6000, rt.getPointValue().getTime());
    // Interval is reset for 5000ms from now
    timer.fastForwardTo(11001);
    // Check Interval Log
    value = dao.getLatestPointValue(dpVo.getId());
    assertEquals(2.0, value.getDoubleValue(), 0.0001);
    assertEquals(11000, value.getTime());
    assertEquals(2.0, rt.getPointValue().getDoubleValue(), 0.0001);
    assertEquals(6000, rt.getPointValue().getTime());
    // Test Tolerance (Should not get logged)
    timer.fastForwardTo(12000);
    rt.setPointValue(new PointValueTime(2.20, 12000), null);
    // Check Log On Change
    value = dao.getLatestPointValue(dpVo.getId());
    assertEquals(2.0, value.getDoubleValue(), 0.0001);
    assertEquals(11000, value.getTime());
    // Cache will have the set value
    assertEquals(2.2, rt.getPointValue().getDoubleValue(), 0.0001);
    assertEquals(12000, rt.getPointValue().getTime());
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) MockDataSourceVO(com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO) PointValueDao(com.serotonin.m2m2.db.dao.PointValueDao) SimulationTimer(com.serotonin.timer.SimulationTimer) MockPointLocatorRT(com.serotonin.m2m2.rt.dataSource.MockPointLocatorRT) ArrayList(java.util.ArrayList) MockPointLocatorVO(com.serotonin.m2m2.vo.dataPoint.MockPointLocatorVO) Test(org.junit.Test)

Aggregations

DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)47 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)28 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)23 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)19 HashMap (java.util.HashMap)12 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)11 IDataPointValueSource (com.serotonin.m2m2.rt.dataImage.IDataPointValueSource)9 IOException (java.io.IOException)8 ResultTypeException (com.serotonin.m2m2.rt.script.ResultTypeException)7 ArrayList (java.util.ArrayList)7 ScriptException (javax.script.ScriptException)7 ScriptLog (com.serotonin.m2m2.rt.script.ScriptLog)6 ScriptPermissionsException (com.serotonin.m2m2.rt.script.ScriptPermissionsException)5 SerialPortException (com.infiniteautomation.mango.io.serial.SerialPortException)4 SerialPointLocatorVO (com.infiniteautomation.serial.vo.SerialPointLocatorVO)4 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)4 DataPointListener (com.serotonin.m2m2.rt.dataImage.DataPointListener)4 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)4 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)4 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)4