Search in sources :

Example 51 with DataPointWithEventDetectors

use of com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors in project ma-core-public by MangoAutomation.

the class DataPointDao method getDataPointsForDataSourceStart.

/**
 * Get points for runtime in an efficient manner by joining with the event detectors and only returning
 *  data points that are enabled
 */
public List<DataPointWithEventDetectors> getDataPointsForDataSourceStart(int dataSourceId) {
    List<Field<?>> fields = new ArrayList<>(this.getSelectFields());
    fields.addAll(Arrays.asList(eventDetectors.fields()));
    Select<Record> select = this.joinTables(this.getSelectQuery(fields), null).leftOuterJoin(eventDetectors).on(table.id.eq(eventDetectors.dataPointId)).where(table.dataSourceId.eq(dataSourceId).and(table.enabled.eq(boolToChar(true))));
    Map<Integer, DataPointWithEventDetectors> result = new HashMap<>();
    try (Cursor<Record> cursor = select.fetchLazy()) {
        for (Record record : cursor) {
            int id = record.get(table.id);
            DataPointWithEventDetectors dp = result.computeIfAbsent(id, (i) -> {
                DataPointVO dpvo = this.mapRecord(record);
                loadRelationalData(dpvo);
                return new DataPointWithEventDetectors(dpvo, new ArrayList<>());
            });
            AbstractPointEventDetectorVO detector = eventDetectorDao.mapPointEventDetector(record, dp.getDataPoint());
            if (detector != null) {
                dp.getEventDetectors().add(detector);
            }
        }
    }
    return new ArrayList<>(result.values());
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) HashMap(java.util.HashMap) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) ArrayList(java.util.ArrayList) DataPointWithEventDetectors(com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors) Field(org.jooq.Field) SortField(org.jooq.SortField) DataPointsRecord(com.infiniteautomation.mango.db.tables.records.DataPointsRecord) Record(org.jooq.Record)

Example 52 with DataPointWithEventDetectors

use of com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors in project ma-core-public by MangoAutomation.

the class DataPointRTTest method testIntervalOnChangeLogging.

/**
 * Test Interval Logged Values w/ On Change Option
 */
@Test
public void testIntervalOnChangeLogging() {
    MockDataSourceVO dsVo = new MockDataSourceVO();
    MockDataSourceRT dataSource = dsVo.createDataSourceRT();
    dataSource.initialize(false);
    PointValueDao dao = Common.getBean(PointValueDao.class);
    MockPointLocatorVO plVo = new MockPointLocatorVO(DataType.NUMERIC, true);
    DataPointVO dpVo = new DataPointVO();
    dpVo.setId(1);
    dpVo.setDataSourceId(dsVo.getId());
    // 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);
    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();
    DataPointWithEventDetectors dp = new DataPointWithEventDetectors(dpVo, new ArrayList<>());
    DataPointRT rt = new DataPointRT(dp, plRt, dataSource, initialCache, dao, Common.getBean(PointValueCache.class), timer);
    rt.initialize(false);
    // Test no changes
    timer.fastForwardTo(5001);
    PointValueTime value = dao.getLatestPointValue(dpVo).orElse(null);
    // 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).orElse(null);
    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).orElse(null);
    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).orElse(null);
    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) SimulationTimer(com.serotonin.timer.SimulationTimer) ArrayList(java.util.ArrayList) DataPointWithEventDetectors(com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors) MockPointLocatorVO(com.serotonin.m2m2.vo.dataPoint.MockPointLocatorVO) PointValueDao(com.serotonin.m2m2.db.dao.PointValueDao) BasicSQLPointValueDao(com.serotonin.m2m2.db.dao.BasicSQLPointValueDao) MockPointLocatorRT(com.serotonin.m2m2.rt.dataSource.MockPointLocatorRT) MockDataSourceRT(com.serotonin.m2m2.rt.dataSource.MockDataSourceRT) PointValueCache(com.infiniteautomation.mango.pointvaluecache.PointValueCache) Test(org.junit.Test)

Example 53 with DataPointWithEventDetectors

use of com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors in project ma-core-public by MangoAutomation.

the class RateOfChangeDetectorTest method testOneSecondPeriodTwoInitialValuesTwoValuesOutOfRangeForOneSecond.

@Test
public void testOneSecondPeriodTwoInitialValuesTwoValuesOutOfRangeForOneSecond() {
    DataPointWithEventDetectors dp = createDisabledPoint(1.0, null, TimePeriods.SECONDS, false, CalculationMode.INSTANTANEOUS, 0, TimePeriods.SECONDS, ComparisonMode.GREATER_THAN_OR_EQUALS, 0, TimePeriods.SECONDS);
    // Save some values
    PointValueDao dao = Common.getBean(PointValueDao.class);
    dao.savePointValueSync(dp.getDataPoint(), new PointValueTime(0.1, 0));
    dao.savePointValueSync(dp.getDataPoint(), new PointValueTime(1.101, 100));
    timer.fastForwardTo(200);
    dp.getDataPoint().setEnabled(true);
    Common.runtimeManager.startDataPoint(dp);
    DataPointRT rt = Common.runtimeManager.getDataPoint(dp.getDataPoint().getId());
    ensureSetPointValue(rt, new PointValueTime(1.5, timer.currentTimeMillis()));
    timer.fastForwardTo(500);
    ensureSetPointValue(rt, new PointValueTime(2.5, timer.currentTimeMillis()));
    // 1s after the change of grater than 1
    timer.fastForwardTo(1100);
    assertEquals(1, listener.raised.size());
    assertEquals(200, listener.raised.get(0).getActiveTimestamp());
    assertEquals(0, listener.rtn.size());
    ensureSetPointValue(rt, new PointValueTime(2.2, timer.currentTimeMillis()));
    // Well after we have had no changes and our RoC has dropped to RTN the event
    timer.fastForwardTo(4500);
    assertEquals(1, listener.raised.size());
    assertEquals(1, listener.rtn.size());
    assertEquals(1100, (long) listener.rtn.get(0).getRtnTimestamp());
}
Also used : PointValueDao(com.serotonin.m2m2.db.dao.PointValueDao) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) DataPointWithEventDetectors(com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors) Test(org.junit.Test)

Example 54 with DataPointWithEventDetectors

use of com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors in project ma-core-public by MangoAutomation.

the class RateOfChangeDetectorTest method testNoValuesSetTwoInitialValuesAverage.

@Test
public void testNoValuesSetTwoInitialValuesAverage() {
    DataPointWithEventDetectors dp = createDisabledPoint(1.0, 1.1, TimePeriods.SECONDS, false, CalculationMode.AVERAGE, 1, TimePeriods.SECONDS, ComparisonMode.LESS_THAN, 0, TimePeriods.SECONDS);
    // Save some values
    PointValueDao dao = Common.getBean(PointValueDao.class);
    dao.savePointValueSync(dp.getDataPoint(), new PointValueTime(0.0, 0));
    dao.savePointValueSync(dp.getDataPoint(), new PointValueTime(0.5, 500));
    timer.fastForwardTo(1000);
    dp.getDataPoint().setEnabled(true);
    Common.runtimeManager.startDataPoint(dp);
    DataPointRT rt = Common.runtimeManager.getDataPoint(dp.getDataPoint().getId());
    timer.fastForwardTo(1499);
    assertEquals(1, listener.raised.size());
    assertEquals(1000, listener.raised.get(0).getActiveTimestamp());
    assertEquals(0, listener.rtn.size());
    ensureSetPointValue(rt, new PointValueTime(2.2, timer.currentTimeMillis()));
    timer.fastForwardTo(2000);
    assertEquals(1, listener.raised.size());
    assertEquals(1, listener.rtn.size());
    assertEquals(1499, (long) listener.rtn.get(0).getRtnTimestamp());
}
Also used : PointValueDao(com.serotonin.m2m2.db.dao.PointValueDao) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) DataPointWithEventDetectors(com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors) Test(org.junit.Test)

Example 55 with DataPointWithEventDetectors

use of com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors in project ma-core-public by MangoAutomation.

the class RateOfChangeDetectorTest method testOneSecondPeriodTwoInitialValuesTwoValuesOutOfRangeResetAverage.

@Test
public void testOneSecondPeriodTwoInitialValuesTwoValuesOutOfRangeResetAverage() {
    DataPointWithEventDetectors dp = createDisabledPoint(1.0, 0.9, TimePeriods.SECONDS, false, CalculationMode.AVERAGE, 1, TimePeriods.SECONDS, ComparisonMode.GREATER_THAN, 0, TimePeriods.SECONDS);
    // Save some values
    PointValueDao dao = Common.getBean(PointValueDao.class);
    dao.savePointValueSync(dp.getDataPoint(), new PointValueTime(0.0, 0));
    dao.savePointValueSync(dp.getDataPoint(), new PointValueTime(1.1, 100));
    timer.fastForwardTo(1000);
    dp.getDataPoint().setEnabled(true);
    Common.runtimeManager.startDataPoint(dp);
    DataPointRT rt = Common.runtimeManager.getDataPoint(dp.getDataPoint().getId());
    timer.fastForwardTo(1500);
    assertEquals(1, listener.raised.size());
    assertEquals(1000, listener.raised.get(0).getActiveTimestamp());
    assertEquals(1, listener.rtn.size());
    assertEquals(1100, (long) listener.rtn.get(0).getRtnTimestamp());
    ensureSetPointValue(rt, new PointValueTime(0.5, timer.currentTimeMillis()));
    timer.fastForwardTo(2000);
    ensureSetPointValue(rt, new PointValueTime(0.9, timer.currentTimeMillis()));
    timer.fastForwardTo(4500);
    assertEquals(1, listener.rtn.size());
    assertEquals(1, listener.raised.size());
}
Also used : PointValueDao(com.serotonin.m2m2.db.dao.PointValueDao) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) DataPointWithEventDetectors(com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors) Test(org.junit.Test)

Aggregations

DataPointWithEventDetectors (com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors)54 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)29 PointValueDao (com.serotonin.m2m2.db.dao.PointValueDao)25 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)24 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)22 Test (org.junit.Test)22 ArrayList (java.util.ArrayList)20 AbstractPointEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)18 PointValueCache (com.infiniteautomation.mango.pointvaluecache.PointValueCache)7 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)6 JsonException (com.serotonin.json.JsonException)6 HashMap (java.util.HashMap)5 NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)4 JsonArray (com.serotonin.json.type.JsonArray)4 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)4 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)4 IDataPointValueSource (com.serotonin.m2m2.rt.dataImage.IDataPointValueSource)4 MockPointLocatorVO (com.serotonin.m2m2.vo.dataPoint.MockPointLocatorVO)4 SerialPointLocatorVO (com.infiniteautomation.serial.vo.SerialPointLocatorVO)3 DataPointChangeDefinition (com.serotonin.m2m2.module.definitions.dataPoint.DataPointChangeDefinition)3