Search in sources :

Example 1 with FireEvents

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

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 2 with FireEvents

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

the class DataPointRT method savePointValue.

private void savePointValue(PointValueTime newValue, SetPointSource source, boolean async, boolean saveToDatabase) {
    // Null values are not very nice, and since they don't have a specific meaning they are hereby ignored.
    if (newValue == null)
        return;
    // Check the data type of the value against that of the locator, just for fun.
    int valueDataType = DataTypes.getDataType(newValue.getValue());
    if (valueDataType != DataTypes.UNKNOWN && valueDataType != vo.getPointLocator().getDataTypeId())
        // to know how it happened, and the stack trace here provides the best information.
        throw new ShouldNeverHappenException("Data type mismatch between new value and point locator: newValue=" + DataTypes.getDataType(newValue.getValue()) + ", locator=" + vo.getPointLocator().getDataTypeId());
    // Check if this value qualifies for discardation.
    if (vo.isDiscardExtremeValues() && DataTypes.getDataType(newValue.getValue()) == DataTypes.NUMERIC) {
        double newd = newValue.getDoubleValue();
        // Discard if NaN
        if (Double.isNaN(newd))
            return;
        if (newd < vo.getDiscardLowLimit() || newd > vo.getDiscardHighLimit())
            // Discard the value
            return;
    }
    if (newValue.getTime() > Common.timer.currentTimeMillis() + SystemSettingsDao.getFutureDateLimit()) {
        // Too far future dated. Toss it. But log a message first.
        LOG.warn("Future dated value detected: pointId=" + vo.getId() + ", value=" + newValue.getValue().toString() + ", type=" + vo.getPointLocator().getDataTypeId() + ", ts=" + newValue.getTime(), new Exception());
        return;
    }
    boolean backdated = pointValue != null && newValue.getTime() < pointValue.getTime();
    // Determine whether the new value qualifies for logging.
    boolean logValue;
    // ... or even saving in the cache.
    boolean saveValue = true;
    switch(vo.getLoggingType()) {
        case DataPointVO.LoggingTypes.ON_CHANGE_INTERVAL:
        case DataPointVO.LoggingTypes.ON_CHANGE:
            if (pointValue == null)
                logValue = true;
            else if (backdated)
                // Backdated. Ignore it
                logValue = false;
            else {
                if (newValue.getValue() instanceof NumericValue) {
                    // Get the new double
                    double newd = newValue.getDoubleValue();
                    // See if the new value is outside of the tolerance.
                    double diff = toleranceOrigin - newd;
                    if (diff < 0)
                        diff = -diff;
                    if (diff > vo.getTolerance()) {
                        toleranceOrigin = newd;
                        logValue = true;
                    } else
                        logValue = false;
                } else if (newValue.getValue() instanceof ImageValue) {
                    logValue = !((ImageValue) newValue.getValue()).equalDigests(((ImageValue) pointValue.getValue()).getDigest());
                } else
                    logValue = !Objects.equals(newValue.getValue(), pointValue.getValue());
            }
            saveValue = logValue;
            break;
        case DataPointVO.LoggingTypes.ALL:
            logValue = true;
            break;
        case DataPointVO.LoggingTypes.ON_TS_CHANGE:
            if (pointValue == null)
                logValue = true;
            else if (backdated)
                // Backdated. Ignore it
                logValue = false;
            else
                logValue = newValue.getTime() != pointValue.getTime();
            saveValue = logValue;
            break;
        case DataPointVO.LoggingTypes.INTERVAL:
            if (!backdated)
                intervalSave(newValue);
        default:
            logValue = false;
    }
    if (!saveToDatabase)
        logValue = false;
    if (saveValue) {
        valueCache.savePointValue(newValue, source, logValue, async);
        if (vo.getLoggingType() == DataPointVO.LoggingTypes.ON_CHANGE_INTERVAL)
            rescheduleChangeInterval(Common.getMillis(vo.getIntervalLoggingPeriodType(), vo.getIntervalLoggingPeriod()));
    }
    // fetch the annotation
    if (source != null) {
        newValue = new AnnotatedPointValueTime(newValue.getValue(), newValue.getTime(), source.getSetPointSourceMessage());
    }
    // Ignore historical values.
    if (pointValue == null || newValue.getTime() >= pointValue.getTime()) {
        PointValueTime oldValue = pointValue;
        pointValue = newValue;
        fireEvents(oldValue, newValue, null, source != null, false, logValue, true, false);
    } else
        fireEvents(null, newValue, null, false, true, logValue, false, false);
}
Also used : ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue) ImageValue(com.serotonin.m2m2.rt.dataImage.types.ImageValue) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException)

Example 3 with FireEvents

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

the class PointValueModificationRestController method deletePointValues.

@ApiOperation(value = "Delete Point Values for one or many Data Points", notes = "Data Point must exist and user must have write access")
@RequestMapping(method = RequestMethod.DELETE, value = "/delete")
@Async
public CompletableFuture<List<PointValueTimeDeleteResult>> deletePointValues(@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, PointValueTimeDelete> results = new HashMap<>();
            stream.forEachOrdered((pvt) -> {
                var entry = results.computeIfAbsent(pvt.getXid(), (xidKey) -> new PointValueTimeDelete(pvt.getXid(), fireEvents, user));
                entry.deleteValue(pvt.getTimestamp());
            });
            return results.values().stream().map(v -> new PointValueTimeDeleteResult(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) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PointValueTimeDeleteResult(com.infiniteautomation.mango.rest.latest.model.pointValue.emport.PointValueTimeDeleteResult) CompletionException(java.util.concurrent.CompletionException) CompletionException(java.util.concurrent.CompletionException) Async(org.springframework.scheduling.annotation.Async) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with FireEvents

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

the class PointValueImportResult method saveValue.

public void saveValue(LegacyXidPointValueTimeModel model) {
    if (valid) {
        // Validate the model against our point
        long timestamp = model.getTimestamp();
        if (timestamp == 0)
            timestamp = Common.timer.currentTimeMillis();
        try {
            DataValue value;
            switch(vo.getPointLocator().getDataType()) {
                case ALPHANUMERIC:
                    value = new AlphanumericValue((String) model.getValue());
                    break;
                case BINARY:
                    value = new BinaryValue((Boolean) model.getValue());
                    break;
                case MULTISTATE:
                    if (model.getValue() instanceof String) {
                        try {
                            value = vo.getTextRenderer().parseText((String) model.getValue(), vo.getPointLocator().getDataType());
                        } catch (Exception e) {
                            // Lots can go wrong here so let the user know
                            result.addContextualMessage("value", "event.valueParse.textParse", e.getMessage());
                            return;
                        }
                    } else {
                        value = new MultistateValue(((Number) model.getValue()).intValue());
                    }
                    break;
                case NUMERIC:
                    value = new NumericValue(((Number) model.getValue()).doubleValue());
                    break;
                default:
                    result.addContextualMessage("dataType", "common.default", vo.getPointLocator().getDataType() + " data type not supported yet");
                    return;
            }
            PointValueTime pvt;
            if (model.getAnnotation() == null) {
                pvt = new PointValueTime(value, timestamp);
            } else {
                pvt = new AnnotatedPointValueTime(value, timestamp, new TranslatableMessage("common.default", model.getAnnotation()));
            }
            if (rt == null) {
                dao.savePointValueAsync(vo, pvt);
            } else {
                rt.savePointValueDirectToCache(pvt, null, true, true, fireEvents);
            }
            total++;
        } catch (Exception e) {
            if (e instanceof ClassCastException) {
                result.addContextualMessage("dataType", "event.ds.dataType");
            } else {
                result.addContextualMessage("value", "common.default", e.getMessage());
            }
        }
    }
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) BinaryValue(com.serotonin.m2m2.rt.dataImage.types.BinaryValue) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) MultistateValue(com.serotonin.m2m2.rt.dataImage.types.MultistateValue) AlphanumericValue(com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue)

Example 5 with FireEvents

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

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