Search in sources :

Example 1 with ScriptContextVariable

use of com.serotonin.m2m2.rt.script.ScriptContextVariable in project ma-core-public by infiniteautomation.

the class RhinoScriptingTest method testAnalogStatistics.

@Test
public void testAnalogStatistics() {
    String script = "var a = p1.past(MINUTE,50);";
    script += "return a.average;";
    // String script = "TIMESTAMP = 100; return 9;";
    ScriptContextVariable p1 = new ScriptContextVariable();
    p1.setContextUpdate(true);
    p1.setDataPointId(1);
    p1.setVariableName("p1");
    try {
        Map<String, IDataPointValueSource> context = new HashMap<String, IDataPointValueSource>();
        RhinoScriptingTestPointValueRT p1Rt = new RhinoScriptingTestPointValueRT(p1.getDataPointId(), DataTypes.NUMERIC);
        context.put(p1.getVariableName(), p1Rt);
        ScriptPermissions permissions = new ScriptPermissions();
        permissions.setCustomPermissions("superadmin");
        permissions.setDataSourcePermissions("superadmin");
        permissions.setDataPointReadPermissions("superadmin");
        permissions.setDataPointSetPermissions("superadmin");
        final StringWriter scriptOut = new StringWriter();
        final PrintWriter scriptWriter = new PrintWriter(scriptOut);
        ScriptLog scriptLog = new ScriptLog(scriptWriter, ScriptLog.LogLevel.TRACE);
        CompiledScript s = CompiledScriptExecutor.compile(script);
        PointValueTime pvt = CompiledScriptExecutor.execute(s, context, null, Common.timer.currentTimeMillis(), DataTypes.NUMERIC, -1, permissions, scriptWriter, scriptLog, null, null, true);
        assertNotNull(pvt);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : CompiledScript(javax.script.CompiledScript) HashMap(java.util.HashMap) IOException(java.io.IOException) StringWriter(java.io.StringWriter) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 2 with ScriptContextVariable

use of com.serotonin.m2m2.rt.script.ScriptContextVariable in project ma-modules-public by infiniteautomation.

the class MangoJavaScriptModel method convertContext.

private List<ScriptContextVariable> convertContext() {
    List<ScriptContextVariable> result = new ArrayList<>();
    if (context != null)
        for (ScriptContextVariableModel variable : context) {
            ScriptContextVariable var = new ScriptContextVariable();
            DataPointVO dpvo = DataPointDao.getInstance().getByXid(variable.getXid());
            if (dpvo == null)
                var.setDataPointId(Common.NEW_ID);
            else
                var.setDataPointId(dpvo.getId());
            var.setVariableName(variable.getVariableName());
            result.add(var);
        }
    return result;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) ScriptContextVariable(com.serotonin.m2m2.rt.script.ScriptContextVariable) ArrayList(java.util.ArrayList)

Example 3 with ScriptContextVariable

use of com.serotonin.m2m2.rt.script.ScriptContextVariable in project ma-core-public by infiniteautomation.

the class MangoJavaScriptService method validateContext.

/**
 * Validate a script context
 */
public void validateContext(List<ScriptContextVariable> context, ProcessResult result) {
    PermissionHolder user = Common.getUser();
    // Validate the context, can we read all points and are the var names valid
    List<String> varNameSpace = new ArrayList<>();
    for (ScriptContextVariable var : context) {
        String varName = var.getVariableName();
        DataPointVO dp = DataPointDao.getInstance().get(var.getDataPointId());
        if (dp == null)
            result.addContextualMessage("context", "javascript.validate.missingContextPoint", varName);
        else {
            if (!dataPointService.hasReadPermission(user, dp))
                result.addContextualMessage("context", "javascript.validate.noReadPermissionOnContextPoint", varName);
        }
        if (StringUtils.isBlank(varName)) {
            result.addContextualMessage("context", "validate.allVarNames");
            continue;
        }
        if (!VarNames.validateVarName(varName)) {
            result.addContextualMessage("context", "validate.invalidVarName", varName);
            continue;
        }
        if (varNameSpace.contains(varName)) {
            result.addContextualMessage("context", "validate.duplicateVarName", varName);
            continue;
        }
        varNameSpace.add(varName);
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) ArrayList(java.util.ArrayList) ScriptContextVariable(com.serotonin.m2m2.rt.script.ScriptContextVariable) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder)

Example 4 with ScriptContextVariable

use of com.serotonin.m2m2.rt.script.ScriptContextVariable in project ma-core-public by infiniteautomation.

the class ScriptExecutor method convertScriptContextForScriptValidation.

public static Map<String, IDataPointValueSource> convertScriptContextForScriptValidation(List<ScriptContextVariable> context) throws DataPointStateException {
    Map<String, IDataPointValueSource> converted = new HashMap<String, IDataPointValueSource>();
    for (ScriptContextVariable contextEntry : context) {
        DataPointRT point = Common.runtimeManager.getDataPoint(contextEntry.getDataPointId());
        if (point == null) {
            DataPointVO vo = DataPointDao.instance.get(contextEntry.getDataPointId());
            if (vo == null)
                throw new DataPointStateException(contextEntry.getDataPointId(), new TranslatableMessage("event.script.contextPointMissing", contextEntry.getVariableName(), contextEntry.getDataPointId()));
            if (vo.getDefaultCacheSize() == 0)
                vo.setDefaultCacheSize(1);
            point = new DataPointRT(vo, vo.getPointLocator().createRuntime(), DataSourceDao.instance.getDataSource(vo.getDataSourceId()), null);
            point.resetValues();
        }
        converted.put(contextEntry.getVariableName(), point);
    }
    return converted;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) HashMap(java.util.HashMap) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 5 with ScriptContextVariable

use of com.serotonin.m2m2.rt.script.ScriptContextVariable in project ma-core-public by infiniteautomation.

the class ScriptExecutor method convertScriptContext.

/**
 * @param context
 * @return
 * @throws DataPointStateException
 */
public static Map<String, IDataPointValueSource> convertScriptContext(List<ScriptContextVariable> context) throws DataPointStateException {
    Map<String, IDataPointValueSource> converted = new HashMap<String, IDataPointValueSource>();
    for (ScriptContextVariable contextEntry : context) {
        DataPointRT point = Common.runtimeManager.getDataPoint(contextEntry.getDataPointId());
        if (point == null) {
            DataPointVO vo = DataPointDao.instance.get(contextEntry.getDataPointId());
            if (vo == null)
                throw new DataPointStateException(contextEntry.getDataPointId(), new TranslatableMessage("event.script.contextPointMissing", contextEntry.getVariableName(), contextEntry.getDataPointId()));
            else
                throw new DataPointStateException(contextEntry.getDataPointId(), new TranslatableMessage("event.script.contextPointDisabled", contextEntry.getVariableName(), contextEntry.getDataPointId()));
        }
        converted.put(contextEntry.getVariableName(), point);
    }
    return converted;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) HashMap(java.util.HashMap) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Aggregations

DataPointVO (com.serotonin.m2m2.vo.DataPointVO)9 IDataPointValueSource (com.serotonin.m2m2.rt.dataImage.IDataPointValueSource)7 HashMap (java.util.HashMap)7 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)6 ScriptContextVariable (com.serotonin.m2m2.rt.script.ScriptContextVariable)5 JsonArray (com.serotonin.json.type.JsonArray)4 JsonObject (com.serotonin.json.type.JsonObject)4 JsonString (com.serotonin.json.type.JsonString)4 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)4 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)4 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)3 IOException (java.io.IOException)3 PrintWriter (java.io.PrintWriter)3 StringWriter (java.io.StringWriter)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)2 CompiledMangoJavaScript (com.infiniteautomation.mango.util.script.CompiledMangoJavaScript)2 MangoJavaScriptResult (com.infiniteautomation.mango.util.script.MangoJavaScriptResult)2 JsonBoolean (com.serotonin.json.type.JsonBoolean)2