Search in sources :

Example 1 with SimulationTimer

use of com.serotonin.timer.SimulationTimer 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

PointValueDao (com.serotonin.m2m2.db.dao.PointValueDao)1 MockPointLocatorRT (com.serotonin.m2m2.rt.dataSource.MockPointLocatorRT)1 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)1 MockPointLocatorVO (com.serotonin.m2m2.vo.dataPoint.MockPointLocatorVO)1 MockDataSourceVO (com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO)1 SimulationTimer (com.serotonin.timer.SimulationTimer)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1