Search in sources :

Example 96 with PointValueTime

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

the class RuntimeManagerImpl method initializeDataSourceStartup.

/**
 * Only to be used at startup as the synchronization has been reduced for performance
 * @param vo
 * @return
 */
@Override
public boolean initializeDataSourceStartup(DataSourceVO<?> vo) {
    long startTime = System.nanoTime();
    // If the data source is already running, just quit.
    if (isDataSourceRunning(vo.getId()))
        return false;
    // Ensure that the data source is enabled.
    Assert.isTrue(vo.isEnabled(), "Data source not enabled.");
    // Create and initialize the runtime version of the data source.
    DataSourceRT<? extends DataSourceVO<?>> dataSource = vo.createDataSourceRT();
    dataSource.initialize();
    // Add it to the list of running data sources.
    synchronized (runningDataSources) {
        runningDataSources.put(dataSource.getId(), dataSource);
    }
    // Add the enabled points to the data source.
    List<DataPointVO> dataSourcePoints = DataPointDao.instance.getDataPointsForDataSourceStart(vo.getId());
    Map<Integer, List<PointValueTime>> latestValuesMap = null;
    PointValueDao pvDao = Common.databaseProxy.newPointValueDao();
    if (pvDao instanceof EnhancedPointValueDao) {
        // Find the maximum cache size for all point in the datasource
        // This number of values will be retrieved for all points in the datasource
        // If even one point has a high cache size this *may* cause issues
        int maxCacheSize = 0;
        for (DataPointVO dataPoint : dataSourcePoints) {
            if (dataPoint.getDefaultCacheSize() > maxCacheSize)
                maxCacheSize = dataPoint.getDefaultCacheSize();
        }
        try {
            latestValuesMap = ((EnhancedPointValueDao) pvDao).getLatestPointValuesForDataSource(vo, maxCacheSize);
        } catch (Exception e) {
            LOG.error("Failed to get latest point values for datasource " + vo.getXid() + ". Mango will try to retrieve latest point values per point which will take longer.", e);
        }
    }
    for (DataPointVO dataPoint : dataSourcePoints) {
        if (dataPoint.isEnabled()) {
            List<PointValueTime> latestValuesForPoint = null;
            if (latestValuesMap != null) {
                latestValuesForPoint = latestValuesMap.get(dataPoint.getId());
                if (latestValuesForPoint == null) {
                    latestValuesForPoint = new ArrayList<>();
                }
            }
            try {
                startDataPointStartup(dataPoint, latestValuesForPoint);
            } catch (Exception e) {
                LOG.error("Failed to start data point " + dataPoint.getXid(), e);
            }
        }
    }
    LOG.info("Data source '" + vo.getName() + "' initialized");
    long endTime = System.nanoTime();
    long duration = endTime - startTime;
    LOG.info("Data source '" + vo.getName() + "' took " + (double) duration / (double) 1000000 + "ms to start");
    return true;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) PointValueDao(com.serotonin.m2m2.db.dao.PointValueDao) EnhancedPointValueDao(com.serotonin.m2m2.db.dao.EnhancedPointValueDao) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) EnhancedPointValueDao(com.serotonin.m2m2.db.dao.EnhancedPointValueDao)

Example 97 with PointValueTime

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

the class DataPointRT method scheduleTimeoutImpl.

public void scheduleTimeoutImpl(long fireTime) {
    synchronized (intervalLoggingLock) {
        DataValue value;
        if (vo.getLoggingType() == DataPointVO.LoggingTypes.INTERVAL) {
            if (vo.getIntervalLoggingType() == DataPointVO.IntervalLoggingTypes.INSTANT)
                value = PointValueTime.getValue(pointValue);
            else if (vo.getIntervalLoggingType() == DataPointVO.IntervalLoggingTypes.MAXIMUM || vo.getIntervalLoggingType() == DataPointVO.IntervalLoggingTypes.MINIMUM) {
                value = PointValueTime.getValue(intervalValue);
                intervalValue = pointValue;
            } else if (vo.getIntervalLoggingType() == DataPointVO.IntervalLoggingTypes.AVERAGE) {
                // If we don't have enough averaging values then we will bail and wait for more
                if (vo.isOverrideIntervalLoggingSamples() && (averagingValues.size() != vo.getIntervalLoggingSampleWindowSize()))
                    return;
                if (vo.getPointLocator().getDataTypeId() == DataTypes.MULTISTATE) {
                    StartsAndRuntimeList stats = new StartsAndRuntimeList(intervalStartTime, fireTime, intervalValue, averagingValues);
                    double maxProportion = -1;
                    Object valueAtMax = null;
                    for (StartsAndRuntime sar : stats.getData()) {
                        if (sar.getProportion() > maxProportion) {
                            maxProportion = sar.getProportion();
                            valueAtMax = sar.getValue();
                        }
                    }
                    if (valueAtMax != null)
                        value = new MultistateValue(DataValue.objectToValue(valueAtMax).getIntegerValue());
                    else
                        value = null;
                } else {
                    AnalogStatistics stats = new AnalogStatistics(intervalStartTime, fireTime, intervalValue, averagingValues);
                    if (stats.getAverage() == null || (stats.getAverage() == Double.NaN && stats.getCount() == 0))
                        value = null;
                    else if (vo.getPointLocator().getDataTypeId() == DataTypes.NUMERIC)
                        value = new NumericValue(stats.getAverage());
                    else if (vo.getPointLocator().getDataTypeId() == DataTypes.BINARY)
                        value = new BinaryValue(stats.getAverage() >= 0.5);
                    else
                        throw new ShouldNeverHappenException("Unsupported average interval logging data type.");
                }
                // Compute the center point of our average data, starting by finding where our period started
                long sampleWindowStartTime;
                if (vo.isOverrideIntervalLoggingSamples())
                    sampleWindowStartTime = averagingValues.get(0).getTime();
                else
                    sampleWindowStartTime = intervalStartTime;
                intervalStartTime = fireTime;
                // Fix to simulate center tapped filter (un-shift the average)
                fireTime = sampleWindowStartTime + (fireTime - sampleWindowStartTime) / 2L;
                intervalValue = pointValue;
                if (!vo.isOverrideIntervalLoggingSamples())
                    averagingValues.clear();
            } else
                throw new ShouldNeverHappenException("Unknown interval logging type: " + vo.getIntervalLoggingType());
        } else if (vo.getLoggingType() == DataPointVO.LoggingTypes.ON_CHANGE_INTERVAL) {
            // Okay, no changes rescheduled the timer. Get a value, reschedule
            if (pointValue != null) {
                value = pointValue.getValue();
                if (vo.getPointLocator().getDataTypeId() == DataTypes.NUMERIC)
                    toleranceOrigin = pointValue.getDoubleValue();
            } else
                value = null;
            rescheduleChangeInterval(Common.getMillis(vo.getIntervalLoggingPeriodType(), vo.getIntervalLoggingPeriod()));
        } else
            value = null;
        if (value != null) {
            PointValueTime newValue = new PointValueTime(value, fireTime);
            valueCache.logPointValueAsync(newValue, null);
            // Fire logged Events
            fireEvents(null, newValue, null, false, false, true, false, false);
        }
    }
}
Also used : StartsAndRuntime(com.infiniteautomation.mango.statistics.StartsAndRuntime) DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) StartsAndRuntimeList(com.infiniteautomation.mango.statistics.StartsAndRuntimeList) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) AnalogStatistics(com.infiniteautomation.mango.statistics.AnalogStatistics) BinaryValue(com.serotonin.m2m2.rt.dataImage.types.BinaryValue) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue) MultistateValue(com.serotonin.m2m2.rt.dataImage.types.MultistateValue)

Example 98 with PointValueTime

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

the class AnalogChangeDetectorRT method scheduleTimeoutImpl.

/* 
	 * Call to check changes and see if we have changed enough
	 * (non-Javadoc)
	 * @see com.serotonin.m2m2.rt.event.detectors.TimeoutDetectorRT#scheduleTimeoutImpl(long)
	 */
@Override
protected void scheduleTimeoutImpl(long fireTime) {
    synchronized (periodValues) {
        PointValueTime lastValue = null;
        if (periodValues.size() > 0)
            lastValue = periodValues.get(periodValues.size() - 1);
        periodValues.clear();
        if (lastValue != null) {
            periodValues.add(lastValue);
            min = max = lastValue.getDoubleValue();
        } else {
            max = Double.NEGATIVE_INFINITY;
            min = Double.POSITIVE_INFINITY;
        }
        returnToNormal(fireTime);
        eventActive = false;
    }
}
Also used : PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime)

Example 99 with PointValueTime

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

the class DifferenceDetectorRT method initializeState.

@Override
public void initializeState() {
    // Get historical data for the point out of the database.
    int pointId = vo.njbGetDataPoint().getId();
    PointValueTime latest = Common.runtimeManager.getDataPoint(pointId).getPointValue();
    if (latest != null)
        lastChange = latest.getTime();
    else
        // The point may be new or not logged, so don't go active immediately.
        lastChange = Common.timer.currentTimeMillis();
    if (lastChange + getDurationMS() < Common.timer.currentTimeMillis())
        // Nothing has happened in the time frame, so set the event active.
        setEventActive(true);
    else
        // Otherwise, set the timeout.
        scheduleJob();
}
Also used : PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime)

Example 100 with PointValueTime

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

the class Interpolation method test1.

private static void test1() {
    List<PointValueTime> pvts = new ArrayList<PointValueTime>();
    pvts.add(new PointValueTime(2, 1000000));
    pvts.add(new PointValueTime(4, 1001000));
    pvts.add(new PointValueTime(5, 1002000));
    pvts.add(new PointValueTime(6, 1004000));
    pvts.add(new PointValueTime(3, 1005000));
    pvts.add(new PointValueTime(2, 1006000));
    pvts.add(new PointValueTime(3, 1010000));
    pvts.add(new PointValueTime(3, 1011000));
    pvts.add(new PointValueTime(8, 1011500));
    pvts.add(new PointValueTime(9, 1011600));
    pvts.add(new PointValueTime(7, 1015500));
    pvts.add(new PointValueTime(4, 1018000));
    List<PointValueTime> filled = interpolate(1000000, 1020000, 1000, new PointValueTime(3, 999000), pvts, new PointValueTime(3, 1025000));
    ensure(filled, 0, 2, 1000000);
    ensure(filled, 1, 4, 1001000);
    ensure(filled, 2, 5, 1002000);
    ensure(filled, 3, 5.5, 1003000);
    ensure(filled, 4, 6, 1004000);
    ensure(filled, 5, 3, 1005000);
    ensure(filled, 6, 2, 1006000);
    ensure(filled, 7, 2.25, 1007000);
    ensure(filled, 8, 2.5, 1008000);
    ensure(filled, 9, 2.75, 1009000);
    ensure(filled, 10, 3, 1010000);
    ensure(filled, 11, 3, 1011000);
    ensure(filled, 12, 8.794871794871796, 1012000);
    ensure(filled, 13, 8.282051282051283, 1013000);
    ensure(filled, 14, 7.769230769230769, 1014000);
    ensure(filled, 15, 7.256410256410256, 1015000);
    ensure(filled, 16, 6.4, 1016000);
    ensure(filled, 17, 5.2, 1017000);
    ensure(filled, 18, 4, 1018000);
    ensure(filled, 19, 3.857142857142857, 1019000);
}
Also used : PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) ArrayList(java.util.ArrayList)

Aggregations

PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)104 ArrayList (java.util.ArrayList)33 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)31 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)29 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)24 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)23 IdPointValueTime (com.serotonin.m2m2.rt.dataImage.IdPointValueTime)23 IOException (java.io.IOException)18 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)17 HashMap (java.util.HashMap)17 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)15 ImageValue (com.serotonin.m2m2.rt.dataImage.types.ImageValue)12 PointValueFacade (com.serotonin.m2m2.rt.dataImage.PointValueFacade)11 ScriptException (javax.script.ScriptException)10 PointValueDao (com.serotonin.m2m2.db.dao.PointValueDao)9 IDataPointValueSource (com.serotonin.m2m2.rt.dataImage.IDataPointValueSource)9 LogStopWatch (com.serotonin.log.LogStopWatch)8 AlphanumericValue (com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue)8 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)8 ResultTypeException (com.serotonin.m2m2.rt.script.ResultTypeException)8