Search in sources :

Example 6 with ScriptContextVariable

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

the class ScriptingTest method testAnalogStatistics.

@Test
public void testAnalogStatistics() {
    String script = "var a = p1.past(MINUTE,50);";
    script += "return a.average;";
    try {
        Map<String, IDataPointValueSource> context = new HashMap<String, IDataPointValueSource>();
        List<IDataPoint> vos = createMockDataPoints(1, true, new MangoPermission(), new MangoPermission());
        ScriptContextVariable p1 = new ScriptContextVariable();
        p1.setContextUpdate(true);
        p1.setDataPointId(vos.get(0).getId());
        p1.setVariableName("p1");
        ScriptingTestPointValueRT p1Rt = new ScriptingTestPointValueRT((DataPointVO) vos.get(0));
        context.put(p1.getVariableName(), p1Rt);
        final StringWriter scriptOut = new StringWriter();
        final PrintWriter scriptWriter = new PrintWriter(scriptOut);
        try (ScriptLog scriptLog = new ScriptLog("testScriptLogger", LogLevel.TRACE, scriptWriter)) {
            ScriptPointValueSetter setter = null;
            CompiledMangoJavaScript compiled = new CompiledMangoJavaScript(setter, scriptLog, new ArrayList<>(), admin);
            compiled.compile(script, true);
            compiled.initialize(context);
            MangoJavaScriptResult result = compiled.execute(Common.timer.currentTimeMillis(), Common.timer.currentTimeMillis(), DataType.NUMERIC);
            PointValueTime pvt = (PointValueTime) result.getResult();
            assertNotNull(pvt);
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : HashMap(java.util.HashMap) MangoJavaScriptResult(com.infiniteautomation.mango.util.script.MangoJavaScriptResult) IOException(java.io.IOException) StringWriter(java.io.StringWriter) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) CompiledMangoJavaScript(com.infiniteautomation.mango.util.script.CompiledMangoJavaScript) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 7 with ScriptContextVariable

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

the class ScriptContextVariable method jsonReadVarContext.

/**
 * Read in context,
 * @return if my XID is in the context, return the name it has to map into the VO otherwise return null
 */
public static String jsonReadVarContext(JsonObject json, List<ScriptContextVariable> context, boolean isContextUpdate) throws JsonException {
    JsonArray jsonContext = json.getJsonArray("context");
    if (jsonContext != null) {
        context.clear();
        DataPointDao dataPointDao = DataPointDao.getInstance();
        for (JsonValue jv : jsonContext) {
            JsonObject jo = jv.toJsonObject();
            String xid = jo.getString("dataPointXid");
            if (xid == null)
                throw new TranslatableJsonException("emport.error.context.missing", "dataPointXid");
            Integer dpid = dataPointDao.getIdByXid(xid);
            if (dpid == null) {
                // This can also happen if the point is in its own context (Bug from legacy systems).
                throw new TranslatableJsonException("emport.error.missingPoint", xid);
            }
            // For compatibility with varName and variableName json types
            String var = jo.getString("varName");
            if (var == null) {
                var = jo.getString("variableName");
                if (var == null)
                    throw new TranslatableJsonException("emport.error.context.missing", "varName");
            }
            // Default for legacy systems
            if (jo.containsKey("updateContext"))
                isContextUpdate = AbstractVO.getBoolean(jo, "updateContext");
            context.add(new ScriptContextVariable(dpid, var, isContextUpdate));
        }
    }
    return json.getString("variableName");
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) JsonValue(com.serotonin.json.type.JsonValue) JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonString(com.serotonin.json.type.JsonString)

Example 8 with ScriptContextVariable

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

the class ScriptContextVariable method jsonWriteVarContext.

public static void jsonWriteVarContext(ObjectWriter writer, List<ScriptContextVariable> context) throws IOException, JsonException {
    DataPointDao dataPointDao = DataPointDao.getInstance();
    JsonArray pointList = new JsonArray();
    for (ScriptContextVariable p : context) {
        String xid = dataPointDao.getXidById(p.getDataPointId());
        JsonObject point = new JsonObject();
        pointList.add(point);
        point.put("varName", new JsonString(p.getVariableName()));
        point.put("dataPointXid", new JsonString(xid));
        point.put("updateContext", new JsonBoolean(p.isContextUpdate()));
    }
    writer.writeEntry("context", pointList);
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) JsonObject(com.serotonin.json.type.JsonObject) JsonBoolean(com.serotonin.json.type.JsonBoolean) JsonString(com.serotonin.json.type.JsonString) JsonString(com.serotonin.json.type.JsonString)

Example 9 with ScriptContextVariable

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

the class ScriptContextVariable method contextToString.

public static String contextToString(List<ScriptContextVariable> context) {
    DataPointDao dataPointDao = DataPointDao.getInstance();
    StringBuilder sb = new StringBuilder();
    boolean first = true;
    for (ScriptContextVariable ivp : context) {
        DataPointVO dp = dataPointDao.get(ivp.getDataPointId());
        if (first) {
            first = false;
        } else
            sb.append(", ");
        sb.append("{ name=");
        if (dp == null)
            sb.append("?");
        else
            sb.append(dp.getExtendedName()).append(", varName=");
        sb.append(ivp.getVariableName()).append(", updateContext=").append(ivp.isContextUpdate()).append("}");
    }
    return sb.toString();
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao)

Example 10 with ScriptContextVariable

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

the class CompiledMangoJavaScript method initialize.

/**
 * Clear the engine scope and initialize it with an expandable context which is filled with the ScriptContextVariables and returned
 *
 * @throws ScriptPermissionsException - permission denied executing a command
 * @throws ScriptError - Execution failure, generally will have line and column number with message
 * @throws DataPointStateException - If a point is not enabled or missing (unless testRun is true, then a dummy point is created)
 */
public Map<String, IDataPointValueSource> initialize(List<ScriptContextVariable> variables) throws ScriptPermissionsException, ScriptError, DataPointStateException {
    Map<String, IDataPointValueSource> context = new HashMap<>();
    if (variables != null) {
        for (ScriptContextVariable variable : variables) {
            DataPointVO dpvo = DataPointDao.getInstance().get(variable.getDataPointId());
            if (dpvo != null) {
                DataPointRT dprt = Common.runtimeManager.getDataPoint(dpvo.getId());
                // So we can test with points disabled
                if (dprt == null) {
                    if (testRun) {
                        if (dpvo.getDefaultCacheSize() == 0) {
                            dpvo.setDefaultCacheSize(1);
                        }
                        // Generate some fake event detectors
                        DataPointWithEventDetectors dp = new DataPointWithEventDetectors(dpvo, new ArrayList<>());
                        DataSourceRT<? extends DataSourceVO> dataSource = DataSourceDao.getInstance().get(dpvo.getDataSourceId()).createDataSourceRT();
                        dprt = new DataPointRT(dp, dpvo.getPointLocator().createRuntime(), dataSource, null, pointValueDao, pointValueCache);
                    } else {
                        throw new DataPointStateException(variable.getDataPointId(), new TranslatableMessage("event.script.contextPointDisabled", variable.getVariableName(), dpvo.getXid()));
                    }
                }
                if (dprt != null)
                    context.put(variable.getVariableName(), dprt);
            } else {
                throw new DataPointStateException(variable.getDataPointId(), new TranslatableMessage("event.script.contextPointMissing", variable.getVariableName(), variable.getDataPointId()));
            }
        }
    }
    this.initialize(context);
    return context;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) HashMap(java.util.HashMap) DataPointStateException(com.serotonin.m2m2.rt.script.DataPointStateException) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) ScriptContextVariable(com.serotonin.m2m2.rt.script.ScriptContextVariable) DataPointWithEventDetectors(com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors) 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