Search in sources :

Example 51 with PointValueTime

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;
}
Also used : ScriptException(javax.script.ScriptException) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine)

Example 52 with PointValueTime

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();
}
Also used : PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime)

Example 53 with PointValueTime

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();
}
Also used : PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime)

Example 54 with PointValueTime

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;
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) FileOutputStream(java.io.FileOutputStream) ImageSaveException(com.serotonin.m2m2.ImageSaveException) ImageValue(com.serotonin.m2m2.rt.dataImage.types.ImageValue) File(java.io.File)

Example 55 with PointValueTime

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;
}
Also used : ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) AnnotatedIdPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedIdPointValueTime)

Aggregations

PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)104 ArrayList (java.util.ArrayList)33 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)31 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)29 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)24 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)23 IdPointValueTime (com.serotonin.m2m2.rt.dataImage.IdPointValueTime)23 IOException (java.io.IOException)18 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)17 HashMap (java.util.HashMap)17 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)15 ImageValue (com.serotonin.m2m2.rt.dataImage.types.ImageValue)12 PointValueFacade (com.serotonin.m2m2.rt.dataImage.PointValueFacade)11 ScriptException (javax.script.ScriptException)10 PointValueDao (com.serotonin.m2m2.db.dao.PointValueDao)9 IDataPointValueSource (com.serotonin.m2m2.rt.dataImage.IDataPointValueSource)9 LogStopWatch (com.serotonin.log.LogStopWatch)8 AlphanumericValue (com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue)8 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)8 ResultTypeException (com.serotonin.m2m2.rt.script.ResultTypeException)8