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