Search in sources :

Example 1 with DataPointStateException

use of com.serotonin.m2m2.rt.script.DataPointStateException in project ma-core-public by MangoAutomation.

the class MangoJavaScriptService method executeScript.

/**
 * The preferred way to execute a script
 */
public MangoJavaScriptResult executeScript(MangoJavaScript vo, ScriptPointValueSetter setter) throws ValidationException, PermissionException {
    PermissionHolder user = Common.getUser();
    ensureValid(vo);
    MangoJavaScriptResult result = new MangoJavaScriptResult();
    final Writer scriptOut;
    final PrintWriter scriptWriter;
    if (vo.isReturnLogOutput()) {
        scriptOut = new StringWriter();
        scriptWriter = new PrintWriter(scriptOut);
    } else {
        NullWriter writer = new NullWriter();
        scriptWriter = new NullPrintWriter(writer);
        scriptOut = writer;
    }
    try {
        try (ScriptLogExtender scriptLog = new ScriptLogExtender("scriptTest-" + user.getPermissionHolderName(), vo.getLogLevel(), scriptWriter, vo.getLog(), vo.isCloseLog())) {
            CompiledMangoJavaScript script = new CompiledMangoJavaScript(vo, setter, scriptLog, result, this, pointValueDao, pointValueCache);
            script.compile(vo.getScript(), vo.isWrapInFunction());
            script.initialize(vo.getContext());
            long time = Common.timer.currentTimeMillis();
            runAs.runAsCallable(script.getPermissionHolder(), () -> {
                if (vo.getResultDataType() != null) {
                    script.execute(time, time, vo.getResultDataType());
                } else {
                    script.execute(time, time);
                }
                return null;
            });
        }
    } catch (ScriptError e) {
        // The script exception should be clean as both compile() and execute() clean it
        result.addError(new MangoJavaScriptError(e.getTranslatableMessage(), e.getLineNumber(), e.getColumnNumber()));
    } catch (ResultTypeException | DataPointStateException e) {
        result.addError(new MangoJavaScriptError(e.getTranslatableMessage()));
    } catch (Exception e) {
        result.addError(new MangoJavaScriptError(e.getMessage()));
    } finally {
        if (vo.isReturnLogOutput())
            result.setScriptOutput(scriptOut.toString());
    }
    return result;
}
Also used : MangoJavaScriptError(com.infiniteautomation.mango.util.script.MangoJavaScriptError) MangoJavaScriptResult(com.infiniteautomation.mango.util.script.MangoJavaScriptResult) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) NullWriter(com.serotonin.io.NullWriter) ScriptException(javax.script.ScriptException) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) ScriptPermissionsException(com.serotonin.m2m2.rt.script.ScriptPermissionsException) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) DataPointStateException(com.serotonin.m2m2.rt.script.DataPointStateException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) MangoJavaScriptError(com.infiniteautomation.mango.util.script.MangoJavaScriptError) ScriptError(com.serotonin.m2m2.rt.script.ScriptError) StringWriter(java.io.StringWriter) NullPrintWriter(com.serotonin.m2m2.util.log.NullPrintWriter) DataPointStateException(com.serotonin.m2m2.rt.script.DataPointStateException) CompiledMangoJavaScript(com.infiniteautomation.mango.util.script.CompiledMangoJavaScript) NullWriter(com.serotonin.io.NullWriter) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) StringWriter(java.io.StringWriter) NullPrintWriter(com.serotonin.m2m2.util.log.NullPrintWriter) PrintWriter(java.io.PrintWriter) NullPrintWriter(com.serotonin.m2m2.util.log.NullPrintWriter)

Example 2 with DataPointStateException

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

the class MangoJavaScriptService method executeScript.

/**
 * The preferred way to execute a script
 */
public MangoJavaScriptResult executeScript(MangoJavaScript vo, ScriptPointValueSetter setter) throws ValidationException, PermissionException {
    PermissionHolder user = Common.getUser();
    ensureValid(vo);
    MangoJavaScriptResult result = new MangoJavaScriptResult();
    final Writer scriptOut;
    final PrintWriter scriptWriter;
    if (vo.isReturnLogOutput()) {
        scriptOut = new StringWriter();
        scriptWriter = new PrintWriter(scriptOut);
    } else {
        NullWriter writer = new NullWriter();
        scriptWriter = new NullPrintWriter(writer);
        scriptOut = writer;
    }
    try {
        try (ScriptLogExtender scriptLog = new ScriptLogExtender("scriptTest-" + user.getPermissionHolderName(), vo.getLogLevel(), scriptWriter, vo.getLog(), vo.isCloseLog())) {
            CompiledMangoJavaScript script = new CompiledMangoJavaScript(vo, setter, scriptLog, result, this, pointValueDao, pointValueCache);
            script.compile(vo.getScript(), vo.isWrapInFunction());
            script.initialize(vo.getContext());
            long time = Common.timer.currentTimeMillis();
            runAs.runAsCallable(script.getPermissionHolder(), () -> {
                if (vo.getResultDataType() != null) {
                    script.execute(time, time, vo.getResultDataType());
                } else {
                    script.execute(time, time);
                }
                return null;
            });
        }
    } catch (ScriptError e) {
        // The script exception should be clean as both compile() and execute() clean it
        result.addError(new MangoJavaScriptError(e.getTranslatableMessage(), e.getLineNumber(), e.getColumnNumber()));
    } catch (ResultTypeException | DataPointStateException e) {
        result.addError(new MangoJavaScriptError(e.getTranslatableMessage()));
    } catch (Exception e) {
        result.addError(new MangoJavaScriptError(e.getMessage()));
    } finally {
        if (vo.isReturnLogOutput())
            result.setScriptOutput(scriptOut.toString());
    }
    return result;
}
Also used : MangoJavaScriptError(com.infiniteautomation.mango.util.script.MangoJavaScriptError) MangoJavaScriptResult(com.infiniteautomation.mango.util.script.MangoJavaScriptResult) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) NullWriter(com.serotonin.io.NullWriter) ScriptException(javax.script.ScriptException) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) ScriptPermissionsException(com.serotonin.m2m2.rt.script.ScriptPermissionsException) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) DataPointStateException(com.serotonin.m2m2.rt.script.DataPointStateException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) MangoJavaScriptError(com.infiniteautomation.mango.util.script.MangoJavaScriptError) ScriptError(com.serotonin.m2m2.rt.script.ScriptError) StringWriter(java.io.StringWriter) NullPrintWriter(com.serotonin.m2m2.util.log.NullPrintWriter) DataPointStateException(com.serotonin.m2m2.rt.script.DataPointStateException) CompiledMangoJavaScript(com.infiniteautomation.mango.util.script.CompiledMangoJavaScript) NullWriter(com.serotonin.io.NullWriter) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) StringWriter(java.io.StringWriter) NullPrintWriter(com.serotonin.m2m2.util.log.NullPrintWriter) PrintWriter(java.io.PrintWriter) NullPrintWriter(com.serotonin.m2m2.util.log.NullPrintWriter)

Example 3 with DataPointStateException

use of com.serotonin.m2m2.rt.script.DataPointStateException 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 4 with DataPointStateException

use of com.serotonin.m2m2.rt.script.DataPointStateException 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)

Example 5 with DataPointStateException

use of com.serotonin.m2m2.rt.script.DataPointStateException 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

TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)4 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)4 IDataPointValueSource (com.serotonin.m2m2.rt.dataImage.IDataPointValueSource)4 DataPointStateException (com.serotonin.m2m2.rt.script.DataPointStateException)4 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)4 HashMap (java.util.HashMap)4 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)2 CompiledMangoJavaScript (com.infiniteautomation.mango.util.script.CompiledMangoJavaScript)2 MangoJavaScriptError (com.infiniteautomation.mango.util.script.MangoJavaScriptError)2 MangoJavaScriptResult (com.infiniteautomation.mango.util.script.MangoJavaScriptResult)2 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)2 NullWriter (com.serotonin.io.NullWriter)2 ResultTypeException (com.serotonin.m2m2.rt.script.ResultTypeException)2 ScriptContextVariable (com.serotonin.m2m2.rt.script.ScriptContextVariable)2 ScriptError (com.serotonin.m2m2.rt.script.ScriptError)2 ScriptPermissionsException (com.serotonin.m2m2.rt.script.ScriptPermissionsException)2 NullPrintWriter (com.serotonin.m2m2.util.log.NullPrintWriter)2 DataPointWithEventDetectors (com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors)2 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)2 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)2