Search in sources :

Example 6 with FireEvents

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

the class PointValueModificationRestController method importPointValues.

@ApiOperation(value = "Import Point Values for one or many Data Points", notes = "Data Point must exist and user must have write access")
@RequestMapping(method = RequestMethod.POST, value = "/import")
@Async
public CompletableFuture<List<PointValueTimeImportResult>> importPointValues(@ApiParam(value = "Shall data point listeners be notified, default is NEVER") @RequestParam(defaultValue = "NEVER") FireEvents fireEvents, @RequestBody Stream<XidPointValueTimeModel> stream, @AuthenticationPrincipal PermissionHolder user) {
    return CompletableFuture.supplyAsync(() -> {
        try {
            Map<String, PointValueTimeImport> results = new LinkedHashMap<>();
            stream.forEachOrdered((pvt) -> {
                var entry = results.computeIfAbsent(pvt.getXid(), (xidKey) -> new PointValueTimeImport(pvt.getXid(), fireEvents, user));
                entry.saveValue(pvt.getValue(), pvt.getTimestamp(), pvt.getAnnotation());
            });
            return results.values().stream().map(v -> new PointValueTimeImportResult(v.xid, v.totalProcessed, v.totalSkipped, v.result)).collect(Collectors.toList());
        } catch (Exception e) {
            throw new CompletionException(e);
        }
    }, executorService);
}
Also used : Async(org.springframework.scheduling.annotation.Async) RequestParam(org.springframework.web.bind.annotation.RequestParam) BinaryValue(com.serotonin.m2m2.rt.dataImage.types.BinaryValue) PointValueDao(com.serotonin.m2m2.db.dao.PointValueDao) ZonedDateTime(java.time.ZonedDateTime) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) XidPointValueTimeModel(com.infiniteautomation.mango.rest.latest.model.pointValue.XidPointValueTimeModel) ApiParam(io.swagger.annotations.ApiParam) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) PointValueTimeDeleteResult(com.infiniteautomation.mango.rest.latest.model.pointValue.emport.PointValueTimeDeleteResult) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) StringUtils(org.apache.commons.lang3.StringUtils) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PointValueTimeImportResult(com.infiniteautomation.mango.rest.latest.model.pointValue.emport.PointValueTimeImportResult) DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) LinkedHashMap(java.util.LinkedHashMap) RequestBody(org.springframework.web.bind.annotation.RequestBody) DataType(com.serotonin.m2m2.DataType) FireEvents(com.serotonin.m2m2.rt.dataImage.DataPointRT.FireEvents) ApiOperation(io.swagger.annotations.ApiOperation) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue) Map(java.util.Map) AlphanumericValue(com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) Api(io.swagger.annotations.Api) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) MultistateValue(com.serotonin.m2m2.rt.dataImage.types.MultistateValue) ExecutorService(java.util.concurrent.ExecutorService) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) RuntimeManager(com.serotonin.m2m2.rt.RuntimeManager) CompletionException(java.util.concurrent.CompletionException) Collectors(java.util.stream.Collectors) RestController(org.springframework.web.bind.annotation.RestController) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) List(java.util.List) Stream(java.util.stream.Stream) AuthenticationPrincipal(org.springframework.security.core.annotation.AuthenticationPrincipal) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) CompletionException(java.util.concurrent.CompletionException) PointValueTimeImportResult(com.infiniteautomation.mango.rest.latest.model.pointValue.emport.PointValueTimeImportResult) CompletionException(java.util.concurrent.CompletionException) LinkedHashMap(java.util.LinkedHashMap) Async(org.springframework.scheduling.annotation.Async) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with FireEvents

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

the class DataPointRT method scheduleTimeoutImpl.

public void scheduleTimeoutImpl(long fireTime) {
    synchronized (intervalLoggingLock) {
        DataValue value;
        if (vo.getLoggingType() == LoggingTypes.INTERVAL) {
            if (vo.getIntervalLoggingType() == IntervalLoggingTypes.INSTANT)
                value = PointValueTime.getValue(pointValue.get());
            else if (vo.getIntervalLoggingType() == IntervalLoggingTypes.MAXIMUM || vo.getIntervalLoggingType() == IntervalLoggingTypes.MINIMUM) {
                value = PointValueTime.getValue(intervalValue);
                intervalValue = pointValue.get();
            } else if (vo.getIntervalLoggingType() == 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().getDataType() == DataType.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().getDataType() == DataType.NUMERIC)
                        value = new NumericValue(stats.getAverage());
                    else if (vo.getPointLocator().getDataType() == DataType.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.get();
                if (!vo.isOverrideIntervalLoggingSamples())
                    averagingValues.clear();
            } else
                throw new ShouldNeverHappenException("Unknown interval logging type: " + vo.getIntervalLoggingType());
        } else if (vo.getLoggingType() == LoggingTypes.ON_CHANGE_INTERVAL) {
            // Okay, no changes rescheduled the timer. Get a value,
            if (pointValue.get() != null) {
                value = pointValue.get().getValue();
                if (vo.getPointLocator().getDataType() == DataType.NUMERIC)
                    toleranceOrigin = pointValue.get().getDoubleValue();
            } else
                value = null;
            if (intervalStartTime != Long.MIN_VALUE) {
                if (// ...and reschedule
                this.timer == null)
                    intervalLoggingTask = new TimeoutTask(new OneTimeTrigger(Common.getMillis(vo.getIntervalLoggingPeriodType(), vo.getIntervalLoggingPeriod())), createIntervalLoggingTimeoutClient());
                else
                    intervalLoggingTask = new TimeoutTask(new OneTimeTrigger(Common.getMillis(vo.getIntervalLoggingPeriodType(), vo.getIntervalLoggingPeriod())), createIntervalLoggingTimeoutClient(), timer);
            }
        } else
            value = null;
        if (value != null) {
            PointValueTime newValue = new PointValueTime(value, fireTime);
            // Check if this value qualifies for discardation.
            if (discardUnwantedValues(newValue)) {
                return;
            }
            // Save the new value and get a point value time back that has the id and annotations set, as appropriate.
            valueCache.savePointValueAsync(newValue);
            // Fire logged Events
            fireEvents(null, newValue, null, false, false, true, false, false);
        }
    }
}
Also used : OneTimeTrigger(com.serotonin.timer.OneTimeTrigger) DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) AnalogStatistics(com.infiniteautomation.mango.statistics.AnalogStatistics) BinaryValue(com.serotonin.m2m2.rt.dataImage.types.BinaryValue) MultistateValue(com.serotonin.m2m2.rt.dataImage.types.MultistateValue) TimeoutTask(com.serotonin.m2m2.util.timeout.TimeoutTask) StartsAndRuntime(com.infiniteautomation.mango.statistics.StartsAndRuntime) StartsAndRuntimeList(com.infiniteautomation.mango.statistics.StartsAndRuntimeList) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue)

Example 8 with FireEvents

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

the class DataPointRT method initializeIntervalLogging.

public void initializeIntervalLogging(long nextPollTime, boolean quantize) {
    if (!isIntervalLogging() || intervalLoggingTask != null)
        return;
    // double checked lock
    synchronized (intervalLoggingLock) {
        // earlier in response to an event.
        if (intervalLoggingTask != null)
            return;
        long loggingPeriodMillis = Common.getMillis(vo.getIntervalLoggingPeriodType(), vo.getIntervalLoggingPeriod());
        long delay = loggingPeriodMillis;
        if (quantize) {
            // Quantize the start.
            // Compute delay only if we are offset from the next poll time
            long nextPollOffset = (nextPollTime % loggingPeriodMillis);
            if (nextPollOffset != 0)
                delay = loggingPeriodMillis - nextPollOffset;
            if (log.isDebugEnabled()) {
                log.debug(String.format("First interval log should be at: %s (%d)", new Date(nextPollTime + delay), nextPollTime + delay));
            }
        }
        Date startTime = new Date(nextPollTime + delay);
        if (vo.getLoggingType() == LoggingTypes.INTERVAL) {
            intervalValue = pointValue.get();
            if (vo.getIntervalLoggingType() == IntervalLoggingTypes.AVERAGE) {
                intervalStartTime = timer == null ? Common.timer.currentTimeMillis() : timer.currentTimeMillis();
                if (averagingValues.size() > 0) {
                    AnalogStatistics stats = new AnalogStatistics(intervalStartTime - loggingPeriodMillis, intervalStartTime, null, averagingValues);
                    PointValueTime newValue = new PointValueTime(stats.getAverage(), intervalStartTime);
                    // Save the new value and get a point value time back that has the id and annotations set, as appropriate.
                    valueCache.savePointValueAsync(newValue);
                    // Fire logged Events
                    fireEvents(null, newValue, null, false, false, true, false, false);
                    averagingValues.clear();
                }
            }
            // Are we using a custom timer?
            if (this.timer == null)
                intervalLoggingTask = new TimeoutTask(new FixedRateTrigger(startTime, loggingPeriodMillis), createIntervalLoggingTimeoutClient());
            else
                intervalLoggingTask = new TimeoutTask(new FixedRateTrigger(startTime, loggingPeriodMillis), createIntervalLoggingTimeoutClient(), this.timer);
        } else if (vo.getLoggingType() == LoggingTypes.ON_CHANGE_INTERVAL) {
            if (this.timer == null)
                intervalLoggingTask = new TimeoutTask(new OneTimeTrigger(startTime), createIntervalLoggingTimeoutClient());
            else
                intervalLoggingTask = new TimeoutTask(new OneTimeTrigger(startTime), createIntervalLoggingTimeoutClient(), timer);
        }
    }
}
Also used : OneTimeTrigger(com.serotonin.timer.OneTimeTrigger) FixedRateTrigger(com.serotonin.timer.FixedRateTrigger) AnalogStatistics(com.infiniteautomation.mango.statistics.AnalogStatistics) Date(java.util.Date) TimeoutTask(com.serotonin.m2m2.util.timeout.TimeoutTask)

Aggregations

NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)6 BinaryValue (com.serotonin.m2m2.rt.dataImage.types.BinaryValue)5 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)5 MultistateValue (com.serotonin.m2m2.rt.dataImage.types.MultistateValue)5 AnalogStatistics (com.infiniteautomation.mango.statistics.AnalogStatistics)4 TimeoutTask (com.serotonin.m2m2.util.timeout.TimeoutTask)4 OneTimeTrigger (com.serotonin.timer.OneTimeTrigger)4 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)3 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)3 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)3 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)3 AlphanumericValue (com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue)3 XidPointValueTimeModel (com.infiniteautomation.mango.rest.latest.model.pointValue.XidPointValueTimeModel)2 PointValueTimeDeleteResult (com.infiniteautomation.mango.rest.latest.model.pointValue.emport.PointValueTimeDeleteResult)2 PointValueTimeImportResult (com.infiniteautomation.mango.rest.latest.model.pointValue.emport.PointValueTimeImportResult)2 PermissionService (com.infiniteautomation.mango.spring.service.PermissionService)2 StartsAndRuntime (com.infiniteautomation.mango.statistics.StartsAndRuntime)2 StartsAndRuntimeList (com.infiniteautomation.mango.statistics.StartsAndRuntimeList)2 DataType (com.serotonin.m2m2.DataType)2 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)2