use of com.serotonin.m2m2.rt.dataImage.PointValueTime in project ma-core-public by infiniteautomation.
the class CompiledScriptExecutor method execute.
/**
* Execute the script on the common engine
* @param script
* @param context
* @param additionalContext
* @param runtime
* @param dataTypeId
* @param timestamp
* @param permissions
* @param scriptWriter
* @return
* @throws ScriptException
* @throws ResultTypeException
*/
public static PointValueTime execute(CompiledScript script, Map<String, IDataPointValueSource> context, Map<String, Object> additionalContext, long runtime, int dataTypeId, long timestamp, ScriptPermissions permissions, PrintWriter scriptWriter, ScriptLog log, ScriptPointValueSetter setter, List<JsonImportExclusion> importExclusions, boolean testRun) throws ScriptException, ResultTypeException {
// StopWatch stopWatch = new Log4JStopWatch();
// stopWatch.start();
ensureInit();
// Create the wrapper object context.
ScriptEngine engine = script.getEngine();
// Prepare the Engine
Bindings engineScope = prepareEngine(engine, context, additionalContext, runtime, timestamp, permissions, scriptWriter, log, setter, importExclusions, testRun);
// Execute.
Object result;
try {
result = script.eval(engineScope);
} catch (ScriptException e) {
throw prettyScriptMessage(e);
}
PointValueTime value = getResult(engine, result, dataTypeId, timestamp);
// stopWatch.stop("execute()");
return value;
}
use of com.serotonin.m2m2.rt.dataImage.PointValueTime in project ma-core-public by infiniteautomation.
the class MultistatePointWrapper method ago.
public int ago(int periodType, int count) {
long from = DateUtils.minus(getContext().getRuntime(), periodType, count);
PointValueTime pvt = point.getPointValueBefore(from);
if (pvt == null)
return 0;
return pvt.getIntegerValue();
}
use of com.serotonin.m2m2.rt.dataImage.PointValueTime in project ma-core-public by infiniteautomation.
the class NumericPointWrapper method ago.
public double ago(int periodType, int count) {
long from = DateUtils.minus(getContext().getRuntime(), periodType, count);
PointValueTime pvt = point.getPointValueBefore(from);
if (pvt == null)
return 0;
return pvt.getDoubleValue();
}
use of com.serotonin.m2m2.rt.dataImage.PointValueTime in project ma-core-public by infiniteautomation.
the class PointValueDaoSQL method updatePointValueImpl.
long updatePointValueImpl(final int pointId, final PointValueTime pvt, final SetPointSource source, boolean async) {
DataValue value = pvt.getValue();
final int dataType = DataTypes.getDataType(value);
double dvalue = 0;
String svalue = null;
if (dataType == DataTypes.IMAGE) {
ImageValue imageValue = (ImageValue) value;
dvalue = imageValue.getType();
if (imageValue.isSaved())
svalue = Long.toString(imageValue.getId());
} else if (value.hasDoubleRepresentation())
dvalue = value.getDoubleValue();
else
svalue = value.getStringValue();
// Check if we need to create an annotation.
long id;
try {
if (svalue != null || source != null || dataType == DataTypes.IMAGE)
async = false;
id = updatePointValue(pointId, dataType, dvalue, pvt.getTime(), svalue, source, async);
} catch (ConcurrencyFailureException e) {
// Still failed to insert after all of the retries. Store the data
synchronized (UNSAVED_POINT_UPDATES) {
UNSAVED_POINT_UPDATES.add(new UnsavedPointUpdate(pointId, pvt, source));
}
return -1;
}
// Check if we need to save an image
if (dataType == DataTypes.IMAGE) {
ImageValue imageValue = (ImageValue) value;
if (!imageValue.isSaved()) {
imageValue.setId(id);
File file = new File(Common.getFiledataPath(), imageValue.getFilename());
// Write the file.
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
StreamUtils.transfer(new ByteArrayInputStream(imageValue.getData()), out);
} catch (IOException e) {
// Rethrow as an RTE
throw new ImageSaveException(e);
} finally {
try {
if (out != null)
out.close();
} catch (IOException e) {
// no op
}
}
// Allow the data to be GC'ed
imageValue.setData(null);
}
}
clearUnsavedPointUpdates();
return id;
}
use of com.serotonin.m2m2.rt.dataImage.PointValueTime in project ma-core-public by infiniteautomation.
the class PointValueDaoSQL method savePointValueSync.
/**
* Only the PointValueCache should call this method during runtime. Do not use.
*/
@Override
public PointValueTime savePointValueSync(int pointId, PointValueTime pointValue, SetPointSource source) {
long id = savePointValueImpl(pointId, pointValue, source, false);
PointValueTime savedPointValue;
int retries = 5;
while (true) {
try {
savedPointValue = getPointValue(id);
break;
} catch (ConcurrencyFailureException e) {
if (retries <= 0)
throw e;
retries--;
}
}
return savedPointValue;
}
Aggregations