Search in sources :

Example 1 with DataPointRT

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

the class RuntimeManagerImpl method stopDataPoint.

private void stopDataPoint(int dataPointId) {
    synchronized (dataPoints) {
        // Remove this point from the data image if it is there. If not, just quit.
        DataPointRT p = dataPoints.remove(dataPointId);
        // Remove it from the data source, and terminate it.
        if (p != null) {
            try {
                getRunningDataSource(p.getDataSourceId()).removeDataPoint(p);
            } catch (Exception e) {
                LOG.error("Failed to stop point RT with ID: " + dataPointId + " stopping point.", e);
            }
            DataPointListener l = getDataPointListeners(dataPointId);
            if (l != null)
                l.pointTerminated();
            p.terminate();
        }
    }
}
Also used : DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) DataPointListener(com.serotonin.m2m2.rt.dataImage.DataPointListener) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException)

Example 2 with DataPointRT

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

the class RuntimeManagerImpl method restartDataPoint.

/* (non-Javadoc)
     * @see com.serotonin.m2m2.rt.RuntimeManager#restartDataPoint(com.serotonin.m2m2.vo.DataPointVO)
     */
@Override
public void restartDataPoint(DataPointVO vo) {
    boolean restarted = false;
    synchronized (dataPoints) {
        // Remove this point from the data image if it is there. If not, just quit.
        DataPointRT p = dataPoints.remove(vo.getId());
        // Remove it from the data source, and terminate it.
        if (p != null) {
            try {
                getRunningDataSource(p.getDataSourceId()).removeDataPoint(p);
            } catch (Exception e) {
                LOG.error("Failed to stop point RT with ID: " + vo.getId() + " stopping point.", e);
            }
            DataPointListener l = getDataPointListeners(vo.getId());
            if (l != null)
                l.pointTerminated();
            p.terminate();
            this.startDataPoint(p.getVO(), null);
            restarted = true;
        }
    }
    if (!restarted) {
        // The data poit wasn't really running. Ensure the event detectors and enable
        if (vo.getEventDetectors() == null)
            DataPointDao.instance.setEventDetectors(vo);
        vo.setEnabled(true);
        startDataPoint(vo, null);
        DataPointDao.instance.saveEnabledColumn(vo);
    }
}
Also used : DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) DataPointListener(com.serotonin.m2m2.rt.dataImage.DataPointListener) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException)

Example 3 with DataPointRT

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

the class EventHandlersDwr method validateScript.

@DwrPermission(user = true)
public ProcessResult validateScript(String script, Integer targetPointId, int type, List<IntStringPair> additionalContext, ScriptPermissions scriptPermissions) {
    ProcessResult response = new ProcessResult();
    TranslatableMessage message;
    Map<String, IDataPointValueSource> context = new HashMap<String, IDataPointValueSource>();
    int targetDataType;
    if (type == SetPointEventHandlerDefinition.ACTIVE_SCRIPT_TYPE || type == SetPointEventHandlerDefinition.INACTIVE_SCRIPT_TYPE) {
        DataPointRT target = targetPointId == null ? null : Common.runtimeManager.getDataPoint(targetPointId.intValue());
        if (target == null) {
            DataPointVO targetVo = targetPointId == null ? null : DataPointDao.instance.getDataPoint(targetPointId.intValue(), false);
            if (targetVo == null) {
                if (// These are passed in the validateScript of eventHandlers.jsp
                type == SetPointEventHandlerDefinition.ACTIVE_SCRIPT_TYPE)
                    response.addMessage("activeScript", new TranslatableMessage("eventHandlers.noTargetPoint"));
                else if (type == SetPointEventHandlerDefinition.INACTIVE_SCRIPT_TYPE)
                    response.addMessage("inactiveScript", new TranslatableMessage("eventHandlers.noTargetPoint"));
                return response;
            }
            if (targetVo.getDefaultCacheSize() == 0)
                targetVo.setDefaultCacheSize(1);
            target = new DataPointRT(targetVo, targetVo.getPointLocator().createRuntime(), DataSourceDao.instance.getDataSource(targetVo.getDataSourceId()), null);
            target.resetValues();
            context.put(SetPointEventHandlerVO.TARGET_CONTEXT_KEY, target);
        }
        targetDataType = target.getDataTypeId();
    } else {
        targetDataType = DataTypes.ALPHANUMERIC;
    }
    for (IntStringPair cxt : additionalContext) {
        DataPointRT dprt = Common.runtimeManager.getDataPoint(cxt.getKey());
        if (dprt == null) {
            DataPointVO dpvo = DataPointDao.instance.getDataPoint(cxt.getKey(), false);
            if (dpvo == null) {
                if (type == SetPointEventHandlerDefinition.ACTIVE_SCRIPT_TYPE)
                    response.addMessage("activeScript", new TranslatableMessage("event.script.contextPointMissing", cxt.getValue(), cxt.getKey()));
                else if (type == SetPointEventHandlerDefinition.INACTIVE_SCRIPT_TYPE)
                    response.addMessage("inactiveScript", new TranslatableMessage("event.script.contextPointMissing", cxt.getValue(), cxt.getKey()));
                else if (type == EmailEventHandlerDefinition.EMAIL_SCRIPT_TYPE)
                    response.addMessage("emailScript", new TranslatableMessage("event.script.contextPointMissing", cxt.getValue(), cxt.getKey()));
                return response;
            }
            if (dpvo.getDefaultCacheSize() == 0)
                dpvo.setDefaultCacheSize(1);
            dprt = new DataPointRT(dpvo, dpvo.getPointLocator().createRuntime(), DataSourceDao.instance.getDataSource(dpvo.getDataSourceId()), null);
            dprt.resetValues();
        }
        context.put(cxt.getValue(), dprt);
    }
    Map<String, Object> otherContext = new HashMap<String, Object>();
    otherContext.put(SetPointEventHandlerVO.EVENT_CONTEXT_KEY, getTestEvent());
    if (type == EmailEventHandlerDefinition.EMAIL_SCRIPT_TYPE)
        otherContext.put("model", new HashMap<String, Object>());
    final StringWriter scriptOut = new StringWriter();
    final PrintWriter scriptWriter = new PrintWriter(scriptOut);
    final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/YYY HH:mm:ss");
    ScriptPointValueSetter loggingSetter = new ScriptPointValueSetter(scriptPermissions) {

        @Override
        public void set(IDataPointValueSource point, Object value, long timestamp, String annotation) {
            DataPointRT dprt = (DataPointRT) point;
            if (!dprt.getVO().getPointLocator().isSettable()) {
                scriptOut.append("Point " + dprt.getVO().getExtendedName() + " not settable.");
                return;
            }
            if (!Permissions.hasPermission(dprt.getVO().getSetPermission(), permissions.getDataPointSetPermissions())) {
                scriptOut.write(new TranslatableMessage("pointLinks.setTest.permissionDenied", dprt.getVO().getXid()).translate(Common.getTranslations()));
                return;
            }
            scriptOut.append("Setting point " + dprt.getVO().getName() + " to " + value + " @" + sdf.format(new Date(timestamp)) + "\r\n");
        }

        @Override
        protected void setImpl(IDataPointValueSource point, Object value, long timestamp, String annotation) {
        // not really setting
        }
    };
    try {
        CompiledScript compiledScript = CompiledScriptExecutor.compile(script);
        PointValueTime pvt = CompiledScriptExecutor.execute(compiledScript, context, otherContext, System.currentTimeMillis(), targetDataType, System.currentTimeMillis(), scriptPermissions, scriptWriter, new ScriptLog(SetPointHandlerRT.NULL_WRITER, LogLevel.FATAL), loggingSetter, null, true);
        if (pvt.getValue() == null)
            message = new TranslatableMessage("eventHandlers.script.nullResult");
        else if (CompiledScriptExecutor.UNCHANGED == pvt.getValue())
            message = new TranslatableMessage("eventHandlers.script.successUnchanged");
        else
            message = new TranslatableMessage("eventHandlers.script.success", pvt.getValue());
        // Add the script logging output
        response.addData("out", scriptOut.toString().replaceAll("\n", "<br/>"));
    } catch (ScriptPermissionsException e) {
        message = e.getTranslatableMessage();
    } catch (ScriptException e) {
        message = new TranslatableMessage("eventHandlers.script.failure", e.getMessage());
    } catch (ResultTypeException e) {
        message = e.getTranslatableMessage();
    }
    if (type == SetPointEventHandlerDefinition.ACTIVE_SCRIPT_TYPE)
        response.addMessage("activeScript", message);
    else if (type == SetPointEventHandlerDefinition.INACTIVE_SCRIPT_TYPE)
        response.addMessage("inactiveScript", message);
    else if (type == EmailEventHandlerDefinition.EMAIL_SCRIPT_TYPE)
        response.addMessage("emailScript", message);
    return response;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) CompiledScript(javax.script.CompiledScript) ScriptPointValueSetter(com.serotonin.m2m2.rt.script.ScriptPointValueSetter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IntStringPair(com.serotonin.db.pair.IntStringPair) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) Date(java.util.Date) ScriptLog(com.serotonin.m2m2.rt.script.ScriptLog) ScriptException(javax.script.ScriptException) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) StringWriter(java.io.StringWriter) ScriptPermissionsException(com.serotonin.m2m2.rt.script.ScriptPermissionsException) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) SimpleDateFormat(java.text.SimpleDateFormat) PrintWriter(java.io.PrintWriter) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 4 with DataPointRT

use of com.serotonin.m2m2.rt.dataImage.DataPointRT 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 5 with DataPointRT

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

Aggregations

DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)47 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)28 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)23 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)19 HashMap (java.util.HashMap)12 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)11 IDataPointValueSource (com.serotonin.m2m2.rt.dataImage.IDataPointValueSource)9 IOException (java.io.IOException)8 ResultTypeException (com.serotonin.m2m2.rt.script.ResultTypeException)7 ArrayList (java.util.ArrayList)7 ScriptException (javax.script.ScriptException)7 ScriptLog (com.serotonin.m2m2.rt.script.ScriptLog)6 ScriptPermissionsException (com.serotonin.m2m2.rt.script.ScriptPermissionsException)5 SerialPortException (com.infiniteautomation.mango.io.serial.SerialPortException)4 SerialPointLocatorVO (com.infiniteautomation.serial.vo.SerialPointLocatorVO)4 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)4 DataPointListener (com.serotonin.m2m2.rt.dataImage.DataPointListener)4 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)4 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)4 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)4