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());
}
}
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;
}
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);
}
}
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;
}
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;
}
Aggregations