Search in sources :

Example 6 with IDataPointValueSource

use of com.serotonin.m2m2.rt.dataImage.IDataPointValueSource in project ma-core-public by infiniteautomation.

the class CompiledScriptExecutor method execute.

/**
 * Execute the script on the common engine
 * @param script
 * @param context
 * @param additionalContext
 * @param runtime
 * @param dataTypeId
 * @param timestamp
 * @param permissions
 * @param scriptWriter
 * @return
 * @throws ScriptException
 * @throws ResultTypeException
 */
public static PointValueTime execute(CompiledScript script, Map<String, IDataPointValueSource> context, Map<String, Object> additionalContext, long runtime, int dataTypeId, long timestamp, ScriptPermissions permissions, PrintWriter scriptWriter, ScriptLog log, ScriptPointValueSetter setter, List<JsonImportExclusion> importExclusions, boolean testRun) throws ScriptException, ResultTypeException {
    // StopWatch stopWatch = new Log4JStopWatch();
    // stopWatch.start();
    ensureInit();
    // Create the wrapper object context.
    ScriptEngine engine = script.getEngine();
    // Prepare the Engine
    Bindings engineScope = prepareEngine(engine, context, additionalContext, runtime, timestamp, permissions, scriptWriter, log, setter, importExclusions, testRun);
    // Execute.
    Object result;
    try {
        result = script.eval(engineScope);
    } catch (ScriptException e) {
        throw prettyScriptMessage(e);
    }
    PointValueTime value = getResult(engine, result, dataTypeId, timestamp);
    // stopWatch.stop("execute()");
    return value;
}
Also used : ScriptException(javax.script.ScriptException) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine)

Example 7 with IDataPointValueSource

use of com.serotonin.m2m2.rt.dataImage.IDataPointValueSource in project ma-core-public by infiniteautomation.

the class ScriptPointValueSetter method set.

// Ensure points are settable and the setter has permissions
public void set(IDataPointValueSource point, Object value, long timestamp, String annotation) {
    DataPointRT dprt = (DataPointRT) point;
    if (!dprt.getVO().getPointLocator().isSettable())
        return;
    if (permissions != null && !Permissions.hasPermission(dprt.getVO().getSetPermission(), permissions.getDataPointSetPermissions()))
        throw new ScriptPermissionsException(new TranslatableMessage("script.set.permissionDenied", dprt.getVO().getXid()));
    setImpl(point, value, timestamp, annotation);
}
Also used : DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 8 with IDataPointValueSource

use of com.serotonin.m2m2.rt.dataImage.IDataPointValueSource in project ma-core-public by infiniteautomation.

the class SetPointHandlerRT method eventInactive.

@Override
public void eventInactive(EventInstance evt) {
    if (vo.getInactiveAction() == SetPointEventHandlerVO.SET_ACTION_NONE)
        return;
    // Validate that the target point is available.
    DataPointRT targetPoint = Common.runtimeManager.getDataPoint(vo.getTargetPointId());
    if (targetPoint == null) {
        raiseFailureEvent(new TranslatableMessage("event.setPoint.targetPointMissing"), evt.getEventType());
        return;
    }
    if (!targetPoint.getPointLocator().isSettable()) {
        raiseFailureEvent(new TranslatableMessage("event.setPoint.targetNotSettable"), evt.getEventType());
        return;
    }
    int targetDataType = targetPoint.getVO().getPointLocator().getDataTypeId();
    DataValue value;
    if (vo.getInactiveAction() == SetPointEventHandlerVO.SET_ACTION_POINT_VALUE) {
        // Get the source data point.
        DataPointRT sourcePoint = Common.runtimeManager.getDataPoint(vo.getInactivePointId());
        if (sourcePoint == null) {
            raiseFailureEvent(new TranslatableMessage("event.setPoint.inactivePointMissing"), evt.getEventType());
            return;
        }
        PointValueTime valueTime = sourcePoint.getPointValue();
        if (valueTime == null) {
            raiseFailureEvent(new TranslatableMessage("event.setPoint.inactivePointValue"), evt.getEventType());
            return;
        }
        if (DataTypes.getDataType(valueTime.getValue()) != targetDataType) {
            raiseFailureEvent(new TranslatableMessage("event.setPoint.inactivePointDataType"), evt.getEventType());
            return;
        }
        value = valueTime.getValue();
    } else if (vo.getInactiveAction() == SetPointEventHandlerVO.SET_ACTION_STATIC_VALUE)
        value = DataValue.stringToValue(vo.getInactiveValueToSet(), targetDataType);
    else if (vo.getInactiveAction() == SetPointEventHandlerVO.SET_ACTION_SCRIPT_VALUE) {
        if (inactiveScript == null) {
            raiseFailureEvent(new TranslatableMessage("eventHandlers.invalidInactiveScript"), evt.getEventType());
            return;
        }
        Map<String, IDataPointValueSource> context = new HashMap<String, IDataPointValueSource>();
        context.put("target", targetPoint);
        try {
            PointValueTime pvt = CompiledScriptExecutor.execute(inactiveScript, context, new HashMap<String, Object>(), evt.getRtnTimestamp(), targetPoint.getDataTypeId(), evt.getRtnTimestamp(), vo.getScriptPermissions(), NULL_WRITER, new ScriptLog(NULL_WRITER, LogLevel.FATAL), setCallback, importExclusions, false);
            value = pvt.getValue();
        } catch (ScriptPermissionsException e) {
            raiseFailureEvent(e.getTranslatableMessage(), evt.getEventType());
            return;
        } catch (ScriptException e) {
            raiseFailureEvent(new TranslatableMessage("eventHandlers.invalidInactiveScriptError", e.getCause().getMessage()), evt.getEventType());
            return;
        } catch (ResultTypeException e) {
            raiseFailureEvent(new TranslatableMessage("eventHandlers.invalidInactiveScriptError", e.getMessage()), evt.getEventType());
            return;
        }
    } else
        throw new ShouldNeverHappenException("Unknown active action: " + vo.getInactiveAction());
    Common.backgroundProcessing.addWorkItem(new SetPointWorkItem(vo.getTargetPointId(), new PointValueTime(value, evt.getRtnTimestamp()), this));
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) HashMap(java.util.HashMap) SetPointWorkItem(com.serotonin.m2m2.rt.maint.work.SetPointWorkItem) ScriptLog(com.serotonin.m2m2.rt.script.ScriptLog) ScriptException(javax.script.ScriptException) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) ScriptPermissionsException(com.serotonin.m2m2.rt.script.ScriptPermissionsException) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 9 with IDataPointValueSource

use of com.serotonin.m2m2.rt.dataImage.IDataPointValueSource in project ma-modules-public by infiniteautomation.

the class PointLinkRT method execute.

private void execute(PointValueTime newValue) {
    // Bail out if already running a point link operation
    synchronized (ready) {
        if (!ready) {
            SystemEventType.raiseEvent(alreadyRunningEvent, newValue.getTime(), true, new TranslatableMessage("event.pointLink.duplicateRuns"));
            return;
        } else {
            // Stop anyone else from using this
            ready = false;
            SystemEventType.returnToNormal(alreadyRunningEvent, System.currentTimeMillis());
        }
    }
    // Propagate the update to the target point. Validate that the target point is available.
    DataPointRT targetPoint = Common.runtimeManager.getDataPoint(vo.getTargetPointId());
    if (targetPoint == null) {
        raiseFailureEvent(newValue.getTime(), new TranslatableMessage("event.pointLink.targetUnavailable"));
        ready = true;
        return;
    }
    if (!targetPoint.getPointLocator().isSettable()) {
        raiseFailureEvent(newValue.getTime(), new TranslatableMessage("event.pointLink.targetNotSettable"));
        ready = true;
        return;
    }
    int targetDataType = targetPoint.getVO().getPointLocator().getDataTypeId();
    if (!StringUtils.isBlank(vo.getScript())) {
        Map<String, IDataPointValueSource> context = new HashMap<String, IDataPointValueSource>();
        context.put(CONTEXT_SOURCE_VAR_NAME, Common.runtimeManager.getDataPoint(vo.getSourcePointId()));
        context.put(CONTEXT_TARGET_VAR_NAME, Common.runtimeManager.getDataPoint(vo.getTargetPointId()));
        try {
            if (!compiled) {
                compiledScript = CompiledScriptExecutor.compile(vo.getScript());
                compiled = true;
            }
            PointValueTime pvt = CompiledScriptExecutor.execute(compiledScript, context, null, newValue.getTime(), targetDataType, newValue.getTime(), vo.getScriptPermissions(), new PrintWriter(new NullWriter()), scriptLog, setCallback, importExclusions, false);
            if (pvt.getValue() == null) {
                raiseFailureEvent(newValue.getTime(), new TranslatableMessage("event.pointLink.nullResult"));
                ready = true;
                return;
            } else if (pvt.getValue() == CompiledScriptExecutor.UNCHANGED) {
                ready = true;
                return;
            }
            newValue = pvt;
        } catch (ScriptException e) {
            raiseFailureEvent(newValue.getTime(), new TranslatableMessage("pointLinks.validate.scriptError", e.getMessage()));
            ready = true;
            return;
        } catch (ScriptPermissionsException e) {
            raiseFailureEvent(newValue.getTime(), e.getTranslatableMessage());
            ready = true;
            return;
        } catch (ResultTypeException e) {
            raiseFailureEvent(newValue.getTime(), e.getTranslatableMessage());
            ready = true;
            return;
        }
    }
    if (DataTypes.getDataType(newValue.getValue()) != targetDataType) {
        raiseFailureEvent(newValue.getTime(), new TranslatableMessage("event.pointLink.convertError"));
        ready = true;
        return;
    }
    // Queue a work item to perform the update.
    Common.backgroundProcessing.addWorkItem(new PointLinkSetPointWorkItem(vo.getTargetPointId(), newValue, this));
    returnToNormal();
}
Also used : HashMap(java.util.HashMap) NullWriter(org.apache.commons.io.output.NullWriter) ScriptException(javax.script.ScriptException) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) ScriptPermissionsException(com.serotonin.m2m2.rt.script.ScriptPermissionsException) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) PrintWriter(java.io.PrintWriter)

Example 10 with IDataPointValueSource

use of com.serotonin.m2m2.rt.dataImage.IDataPointValueSource in project ma-core-public by infiniteautomation.

the class SetPointHandlerRT method eventRaised.

@Override
public void eventRaised(EventInstance evt) {
    if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_NONE)
        return;
    // Validate that the target point is available.
    DataPointRT targetPoint = Common.runtimeManager.getDataPoint(vo.getTargetPointId());
    if (targetPoint == null) {
        raiseFailureEvent(new TranslatableMessage("event.setPoint.targetPointMissing"), evt.getEventType());
        return;
    }
    if (!targetPoint.getPointLocator().isSettable()) {
        raiseFailureEvent(new TranslatableMessage("event.setPoint.targetNotSettable"), evt.getEventType());
        return;
    }
    int targetDataType = targetPoint.getVO().getPointLocator().getDataTypeId();
    DataValue value;
    if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_POINT_VALUE) {
        // Get the source data point.
        DataPointRT sourcePoint = Common.runtimeManager.getDataPoint(vo.getActivePointId());
        if (sourcePoint == null) {
            raiseFailureEvent(new TranslatableMessage("event.setPoint.activePointMissing"), evt.getEventType());
            return;
        }
        PointValueTime valueTime = sourcePoint.getPointValue();
        if (valueTime == null) {
            raiseFailureEvent(new TranslatableMessage("event.setPoint.activePointValue"), evt.getEventType());
            return;
        }
        if (DataTypes.getDataType(valueTime.getValue()) != targetDataType) {
            raiseFailureEvent(new TranslatableMessage("event.setPoint.activePointDataType"), evt.getEventType());
            return;
        }
        value = valueTime.getValue();
    } else if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_STATIC_VALUE) {
        value = DataValue.stringToValue(vo.getActiveValueToSet(), targetDataType);
    } else if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_SCRIPT_VALUE) {
        if (activeScript == null) {
            raiseFailureEvent(new TranslatableMessage("eventHandlers.invalidActiveScript"), evt.getEventType());
            return;
        }
        Map<String, IDataPointValueSource> context = new HashMap<String, IDataPointValueSource>();
        context.put(SetPointEventHandlerVO.TARGET_CONTEXT_KEY, targetPoint);
        Map<String, Object> additionalContext = new HashMap<String, Object>();
        additionalContext.put(SetPointEventHandlerVO.EVENT_CONTEXT_KEY, new EventInstanceWrapper(evt));
        try {
            for (IntStringPair cxt : vo.getAdditionalContext()) {
                DataPointRT dprt = Common.runtimeManager.getDataPoint(cxt.getKey());
                if (dprt != null)
                    context.put(cxt.getValue(), dprt);
            }
            PointValueTime pvt = CompiledScriptExecutor.execute(activeScript, context, additionalContext, evt.getActiveTimestamp(), targetPoint.getDataTypeId(), evt.getActiveTimestamp(), vo.getScriptPermissions(), NULL_WRITER, new ScriptLog(NULL_WRITER, LogLevel.FATAL), setCallback, importExclusions, false);
            value = pvt.getValue();
        } catch (ScriptPermissionsException e) {
            raiseFailureEvent(e.getTranslatableMessage(), evt.getEventType());
            return;
        } catch (ScriptException e) {
            raiseFailureEvent(new TranslatableMessage("eventHandlers.invalidActiveScriptError", e.getCause().getMessage()), evt.getEventType());
            return;
        } catch (ResultTypeException e) {
            raiseFailureEvent(new TranslatableMessage("eventHandlers.invalidActiveScriptError", e.getMessage()), evt.getEventType());
            return;
        }
    } else
        throw new ShouldNeverHappenException("Unknown active action: " + vo.getActiveAction());
    // Queue a work item to perform the set point.
    if (CompiledScriptExecutor.UNCHANGED != value)
        Common.backgroundProcessing.addWorkItem(new SetPointWorkItem(vo.getTargetPointId(), new PointValueTime(value, evt.getActiveTimestamp()), this));
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) HashMap(java.util.HashMap) IntStringPair(com.serotonin.db.pair.IntStringPair) SetPointWorkItem(com.serotonin.m2m2.rt.maint.work.SetPointWorkItem) ScriptLog(com.serotonin.m2m2.rt.script.ScriptLog) ScriptException(javax.script.ScriptException) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) ScriptPermissionsException(com.serotonin.m2m2.rt.script.ScriptPermissionsException) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) EventInstanceWrapper(com.serotonin.m2m2.rt.script.EventInstanceWrapper)

Aggregations

IDataPointValueSource (com.serotonin.m2m2.rt.dataImage.IDataPointValueSource)12 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)11 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)10 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)10 HashMap (java.util.HashMap)9 ScriptException (javax.script.ScriptException)9 ResultTypeException (com.serotonin.m2m2.rt.script.ResultTypeException)8 ScriptLog (com.serotonin.m2m2.rt.script.ScriptLog)7 ScriptPermissionsException (com.serotonin.m2m2.rt.script.ScriptPermissionsException)6 PrintWriter (java.io.PrintWriter)6 CompiledScript (javax.script.CompiledScript)6 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)5 StringWriter (java.io.StringWriter)5 IntStringPair (com.serotonin.db.pair.IntStringPair)3 ScriptPointValueSetter (com.serotonin.m2m2.rt.script.ScriptPointValueSetter)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Date (java.util.Date)3 GenericRestException (com.infiniteautomation.mango.rest.v2.exception.GenericRestException)2 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)2 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)2