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