Search in sources :

Example 1 with AbstractPointWrapper

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

the class MangoJavaScriptService method coerce.

/**
 * Coerce an object into a DataValue
 */
public DataValue coerce(Object input, DataType toDataType) throws ResultTypeException {
    DataValue value;
    if (input instanceof DataValue)
        return (DataValue) input;
    else if (input instanceof PointValueTime)
        return ((PointValueTime) input).getValue();
    if (input == null) {
        if (toDataType == DataType.BINARY)
            value = new BinaryValue(false);
        else if (toDataType == DataType.MULTISTATE)
            value = new MultistateValue(0);
        else if (toDataType == DataType.NUMERIC)
            value = new NumericValue(0);
        else if (toDataType == DataType.ALPHANUMERIC)
            value = new AlphanumericValue("");
        else
            value = null;
    } else if (input instanceof AbstractPointWrapper) {
        value = ((AbstractPointWrapper) input).getValueImpl();
        if ((value != null) && (value.getDataType() != toDataType))
            throw new ResultTypeException(new TranslatableMessage("event.script.convertError", input, toDataType.getDescription()));
    } else // See if the type matches.
    if (toDataType == DataType.BINARY && input instanceof Boolean)
        value = new BinaryValue((Boolean) input);
    else if (toDataType == DataType.MULTISTATE) {
        if (input instanceof Number)
            value = new MultistateValue(((Number) input).intValue());
        else if (input instanceof String) {
            try {
                value = new MultistateValue(Integer.parseInt((String) input));
            } catch (NumberFormatException e) {
                throw new ResultTypeException(new TranslatableMessage("event.script.convertError", input, toDataType.getDescription()));
            }
        } else
            throw new ResultTypeException(new TranslatableMessage("event.script.convertError", input, toDataType.getDescription()));
    } else if (toDataType == DataType.NUMERIC) {
        if (input instanceof Number)
            value = new NumericValue(((Number) input).doubleValue());
        else if (input instanceof String) {
            try {
                value = new NumericValue(Double.parseDouble((String) input));
            } catch (NumberFormatException e) {
                throw new ResultTypeException(new TranslatableMessage("event.script.convertError", input, toDataType.getDescription()));
            }
        } else
            throw new ResultTypeException(new TranslatableMessage("event.script.convertError", input, toDataType.getDescription()));
    } else if (toDataType == DataType.ALPHANUMERIC)
        value = new AlphanumericValue(input.toString());
    else
        // If not, ditch it.
        throw new ResultTypeException(new TranslatableMessage("event.script.convertError", input, toDataType.getDescription()));
    return value;
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) BinaryValue(com.serotonin.m2m2.rt.dataImage.types.BinaryValue) MultistateValue(com.serotonin.m2m2.rt.dataImage.types.MultistateValue) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) AlphanumericValue(com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) AbstractPointWrapper(com.serotonin.m2m2.rt.script.AbstractPointWrapper) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue)

Example 2 with AbstractPointWrapper

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

the class MangoJavaScriptService method initialize.

/**
 * Reset the engine scope of a script and initialize for running
 * @param context - if provided points will be wrapped with script's setter (alternatively use script.addToContext()
 */
public void initialize(CompiledMangoJavaScript script, Map<String, IDataPointValueSource> context) throws ScriptError {
    if (context == null) {
        context = new HashMap<>();
    }
    Bindings engineScope = script.getEngine().getBindings(ScriptContext.ENGINE_SCOPE);
    // TODO Clear engine scope completely?
    engineScope.put(MangoJavaScriptService.UNCHANGED_KEY, MangoJavaScriptService.UNCHANGED);
    Set<String> points = new HashSet<>();
    engineScope.put(MangoJavaScriptService.POINTS_CONTEXT_KEY, points);
    // Holder for modifying timestamps of meta points, in Engine Scope so it can be modified by all
    engineScope.put(MangoJavaScriptService.TIMESTAMP_CONTEXT_KEY, null);
    if (script.getPermissionHolder() != null) {
        script.getUtilities().clear();
        for (MangoJavascriptContextObjectDefinition def : ModuleRegistry.getMangoJavascriptContextObjectDefinitions()) {
            ScriptUtility util = def.initializeContextObject(script);
            util.setScriptLog(script.getLog());
            util.setResult(script.getResult());
            util.takeContext(script.getEngine(), engineScope, script.getSetter(), script.getImportExclusions(), script.isTestRun());
            engineScope.put(util.getContextKey(), util);
            script.getUtilities().add(util);
        }
        // Initialize additional utilities
        for (ScriptUtility util : script.getAdditionalUtilities()) {
            util.setScriptLog(script.getLog());
            util.setResult(script.getResult());
            util.takeContext(script.getEngine(), engineScope, script.getSetter(), script.getImportExclusions(), script.isTestRun());
            engineScope.put(util.getContextKey(), util);
        }
    }
    Set<Entry<String, Object>> entries = script.getAdditionalContext().entrySet();
    for (Entry<String, Object> entry : entries) engineScope.put(entry.getKey(), entry.getValue());
    String selfPointXid = (String) script.getAdditionalContext().get(SELF_POINT_XID_KEY);
    Map<String, AbstractPointWrapper> external = new HashMap<>();
    for (String varName : context.keySet()) {
        IDataPointValueSource point = context.get(varName);
        AbstractPointWrapper wrapped = wrapPoint(script.getEngine(), point, script.getSetter());
        engineScope.put(varName, wrapped);
        points.add(varName);
        if (!point.getVO().getXid().equals(selfPointXid)) {
            external.put(varName, wrapped);
        }
    }
    engineScope.put(EXTERNAL_POINTS_KEY, external);
    engineScope.put(EXTERNAL_POINTS_ARRAY_KEY, external.values());
    engineScope.put(POINTS_MAP_KEY, context);
    // Set the print writer and log
    script.getEngine().getContext().setWriter(script.getLog().getStdOutWriter());
    engineScope.put(ScriptLog.CONTEXT_KEY, script.getLog());
    try {
        script.getEngine().eval(getGlobalFunctions());
    } catch (ScriptException e) {
        throw ScriptError.create(e, script.isWrapInFunction());
    } catch (RuntimeException e) {
        // Nashorn seems to like to wrap exceptions in RuntimeException
        if (e.getCause() instanceof ScriptPermissionsException)
            throw (ScriptPermissionsException) e.getCause();
        else if (e.getCause() != null)
            throw ScriptError.createFromThrowable(e.getCause());
        else
            throw new ShouldNeverHappenException(e);
    }
}
Also used : MangoJavascriptContextObjectDefinition(com.serotonin.m2m2.module.MangoJavascriptContextObjectDefinition) HashMap(java.util.HashMap) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) ScriptException(javax.script.ScriptException) Entry(java.util.Map.Entry) ScriptPermissionsException(com.serotonin.m2m2.rt.script.ScriptPermissionsException) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) AbstractPointWrapper(com.serotonin.m2m2.rt.script.AbstractPointWrapper) ScriptUtility(com.infiniteautomation.mango.util.script.ScriptUtility) HashSet(java.util.HashSet)

Example 3 with AbstractPointWrapper

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

the class MangoJavaScriptService method addToContext.

/**
 * Add a data point to the engine scope bindings.
 *
 * Only to be called while script is not executing.
 */
@SuppressWarnings("unchecked")
public void addToContext(ScriptEngine engine, String varName, DataPointRT dprt, ScriptPointValueSetter setCallback) {
    Bindings engineBindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
    AbstractPointWrapper wrapper = wrapPoint(engine, dprt, setCallback);
    engineBindings.put(varName, wrapper);
    Map<String, IDataPointValueSource> context = (Map<String, IDataPointValueSource>) engineBindings.get(POINTS_MAP_KEY);
    context.put(varName, dprt);
    Set<String> points = (Set<String>) engineBindings.get(POINTS_CONTEXT_KEY);
    if (points != null) {
        points.remove(varName);
        points.add(varName);
    }
    String selfPointXid = (String) engineBindings.get(SELF_POINT_XID_KEY);
    Map<String, AbstractPointWrapper> external = (Map<String, AbstractPointWrapper>) engineBindings.get(EXTERNAL_POINTS_KEY);
    if (!dprt.getVO().getXid().equals(selfPointXid)) {
        external.put(varName, wrapper);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) AbstractPointWrapper(com.serotonin.m2m2.rt.script.AbstractPointWrapper) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with AbstractPointWrapper

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

the class MangoJavaScriptService method removeFromContext.

/**
 * Remove a data point from the engine scope bindings.
 *
 * Only to be called while the script is not executing.
 */
@SuppressWarnings("unchecked")
public void removeFromContext(ScriptEngine engine, String varName) {
    Bindings engineBindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
    Map<String, IDataPointValueSource> context = (Map<String, IDataPointValueSource>) engineBindings.get(POINTS_MAP_KEY);
    context.remove(varName);
    Set<String> points = (Set<String>) engineBindings.get(POINTS_CONTEXT_KEY);
    if (points != null) {
        points.remove(varName);
    }
    engineBindings.remove(varName);
    Map<String, AbstractPointWrapper> external = (Map<String, AbstractPointWrapper>) engineBindings.get(EXTERNAL_POINTS_KEY);
    external.remove(varName);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) AbstractPointWrapper(com.serotonin.m2m2.rt.script.AbstractPointWrapper) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with AbstractPointWrapper

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

the class DataPointQuery method byXid.

public DataPointWrapper byXid(String xid) {
    DataPointVO dp = DataPointDao.getInstance().getByXid(xid);
    if (dp == null)
        return null;
    if (permissionService.hasPermission(permissions, dp.getReadPermission())) {
        DataPointRT rt = null;
        AbstractPointWrapper wrapper = null;
        rt = Common.runtimeManager.getDataPoint(dp.getId());
        if (rt != null)
            wrapper = service.wrapPoint(engine, rt, setter);
        else
            wrapper = null;
        return new DataPointWrapper(dp, wrapper);
    } else
        return null;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT)

Aggregations

AbstractPointWrapper (com.serotonin.m2m2.rt.script.AbstractPointWrapper)8 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)7 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)7 IDataPointValueSource (com.serotonin.m2m2.rt.dataImage.IDataPointValueSource)6 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)6 Bindings (javax.script.Bindings)6 SimpleBindings (javax.script.SimpleBindings)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)4 Set (java.util.Set)4 AlphanumericValue (com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue)3 BinaryValue (com.serotonin.m2m2.rt.dataImage.types.BinaryValue)3 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)3 MultistateValue (com.serotonin.m2m2.rt.dataImage.types.MultistateValue)3 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)3 ConditionSortLimitWithTagKeys (com.infiniteautomation.mango.db.query.ConditionSortLimitWithTagKeys)2 ScriptUtility (com.infiniteautomation.mango.util.script.ScriptUtility)2 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)2 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)2