use of com.serotonin.m2m2.rt.script.ScriptContextVariable in project ma-core-public by MangoAutomation.
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());
}
}
use of com.serotonin.m2m2.rt.script.ScriptContextVariable in project ma-core-public by MangoAutomation.
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();
}
use of com.serotonin.m2m2.rt.script.ScriptContextVariable in project ma-core-public by MangoAutomation.
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");
}
use of com.serotonin.m2m2.rt.script.ScriptContextVariable in project ma-core-public by MangoAutomation.
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);
}
use of com.serotonin.m2m2.rt.script.ScriptContextVariable in project ma-core-public by MangoAutomation.
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);
}
}
Aggregations