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());
}
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());
}
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());
}
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());
}
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());
}
Aggregations