Search in sources :

Example 16 with PointValueTime

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

the class PointLinksDwr method validateScript.

@DwrPermission(user = true)
public ProcessResult validateScript(String script, int sourcePointId, int targetPointId, ScriptPermissions permissions, int logLevel) {
    ProcessResult response = new ProcessResult();
    TranslatableMessage message;
    DataPointRT source = Common.runtimeManager.getDataPoint(sourcePointId);
    if (source == null) {
        DataPointVO sourceVo = DataPointDao.instance.getDataPoint(sourcePointId, false);
        if (sourceVo == null) {
            message = new TranslatableMessage("pointLinks.validate.sourceRequired");
            response.addMessage("script", message);
            return response;
        }
        if (sourceVo.getDefaultCacheSize() == 0)
            sourceVo.setDefaultCacheSize(1);
        source = new DataPointRT(sourceVo, sourceVo.getPointLocator().createRuntime(), DataSourceDao.instance.getDataSource(sourceVo.getDataSourceId()), null);
        source.resetValues();
    }
    DataPointRT target = Common.runtimeManager.getDataPoint(targetPointId);
    if (target == null) {
        DataPointVO targetVo = DataPointDao.instance.getDataPoint(targetPointId, false);
        if (targetVo == null) {
            message = new TranslatableMessage("pointLinks.validate.targetRequired");
            response.addMessage("script", message);
            return response;
        }
        if (targetVo.getDefaultCacheSize() == 0)
            targetVo.setDefaultCacheSize(1);
        target = new DataPointRT(targetVo, targetVo.getPointLocator().createRuntime(), DataSourceDao.instance.getDataSource(targetVo.getDataSourceId()), null);
        target.resetValues();
    }
    Map<String, IDataPointValueSource> context = new HashMap<String, IDataPointValueSource>();
    context.put(PointLinkRT.CONTEXT_SOURCE_VAR_NAME, source);
    context.put(PointLinkRT.CONTEXT_TARGET_VAR_NAME, target);
    int targetDataType = target.getDataTypeId();
    final StringWriter scriptOut = new StringWriter();
    final PrintWriter scriptWriter = new PrintWriter(scriptOut);
    ScriptLog scriptLog = new ScriptLog(scriptWriter, logLevel);
    final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/YYY HH:mm:ss");
    ScriptPointValueSetter loggingSetter = new ScriptPointValueSetter(permissions) {

        @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, null, System.currentTimeMillis(), targetDataType, -1, permissions, scriptWriter, scriptLog, loggingSetter, null, true);
        if (pvt.getValue() == null)
            message = new TranslatableMessage("event.pointLink.nullResult");
        else if (pvt.getValue() == CompiledScriptExecutor.UNCHANGED)
            message = new TranslatableMessage("pointLinks.validate.successNoValue");
        else if (pvt.getTime() == -1)
            message = new TranslatableMessage("pointLinks.validate.success", pvt.getValue());
        else
            message = new TranslatableMessage("pointLinks.validate.successTs", pvt.getValue(), Functions.getTime(pvt.getTime()));
        // Add the script logging output
        response.addData("out", scriptOut.toString().replaceAll("\n", "<br/>"));
    } catch (ScriptException e) {
        message = new TranslatableMessage("pointLinks.validate.scriptError", e.getMessage());
    } catch (ResultTypeException e) {
        message = e.getTranslatableMessage();
    }
    response.addMessage("script", 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) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) ScriptLog(com.serotonin.m2m2.rt.script.ScriptLog) Date(java.util.Date) ScriptException(javax.script.ScriptException) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) StringWriter(java.io.StringWriter) 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) SimpleDateFormat(java.text.SimpleDateFormat) PrintWriter(java.io.PrintWriter) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 17 with PointValueTime

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

the class DataImportController method importCsv.

/**
 * The file needs to be in the format:
 *
 * Data Point XID, Device Name, Point name, Time, Value, Rendered, Annotation, Modify(Not used yet)
 *
 * @param reader
 * @param model
 * @return
 * @throws IOException
 * @throws TranslatableException
 */
private void importCsv(CSVReader csvReader, Map<String, Object> model, Translations translations, List<String> errorMessages) throws IOException, TranslatableException {
    DataPointDao dataPointDao = DataPointDao.instance;
    PointValueDao pointValueDao = Common.databaseProxy.newPointValueDao();
    int rowErrors = 0;
    int rowsImported = 0;
    int rowsDeleted = 0;
    // Basic validation of header
    String[] nextLine = csvReader.readNext();
    if (nextLine == null) {
        errorMessages.add(new TranslatableMessage("dataImport.import.noData").translate(translations));
        return;
    }
    if (nextLine.length != ExportCsvStreamer.columns) {
        errorMessages.add(new TranslatableMessage("dataImport.import.invalidHeaders", nextLine.length, ExportCsvStreamer.columns).translate(translations));
        return;
    }
    // Map of XIDs to non-running data points
    Map<String, DataPointVO> voMap = new HashMap<String, DataPointVO>();
    // Map of XIDs to running data points
    Map<String, DataPointRT> rtMap = new HashMap<String, DataPointRT>();
    // Read in all the rows
    int row = 1;
    String xid;
    DataPointVO vo;
    DataPointRT rt;
    long time;
    DataValue value;
    PointValueTime pvt;
    while ((nextLine = csvReader.readNext()) != null) {
        if (nextLine.length != ExportCsvStreamer.columns) {
            errorMessages.add(new TranslatableMessage("dataImport.import.invalidLength", row).translate(translations));
            rowErrors++;
            row++;
            continue;
        }
        // Check XID
        xid = nextLine[0];
        if (StringUtils.isBlank(xid)) {
            errorMessages.add(new TranslatableMessage("dataImport.import.badXid", xid, row).translate(translations));
            rowErrors++;
            row++;
            continue;
        }
        // First Check to see if we already have a point
        vo = voMap.get(xid);
        rt = rtMap.get(xid);
        // We will always have the vo in the map but the RT may be null if the point isn't running
        if (vo == null) {
            vo = dataPointDao.getDataPoint(xid);
            if (vo == null) {
                errorMessages.add(new TranslatableMessage("dataImport.import.xidNotFound", xid, row).translate(translations));
                rowErrors++;
                row++;
                continue;
            }
            rt = Common.runtimeManager.getDataPoint(vo.getId());
            rtMap.put(xid, rt);
            voMap.put(xid, vo);
        }
        // Add or delete or nothing
        String modify = nextLine[7];
        if (StringUtils.equalsIgnoreCase("add", modify)) {
            // Going to insert some data
            time = ExportCsvStreamer.dtf.parseDateTime(nextLine[3]).getMillis();
            value = DataValue.stringToValue(nextLine[4], vo.getPointLocator().getDataTypeId());
            // Get Annotation
            String annotation = nextLine[6];
            if (annotation != null)
                pvt = new AnnotatedPointValueTime(value, time, new TranslatableMessage("common.default", annotation));
            else
                pvt = new PointValueTime(value, time);
            if (rt == null) {
                // Insert Via DAO
                pointValueDao.savePointValueAsync(vo.getId(), pvt, null);
            } else {
                // Insert Via RT
                rt.savePointValueDirectToCache(pvt, null, true, true);
            }
            rowsImported++;
        } else if (StringUtils.equalsIgnoreCase("delete", modify)) {
            // Delete this entry
            time = ExportCsvStreamer.dtf.parseDateTime(nextLine[3]).getMillis();
            rowsDeleted += Common.runtimeManager.purgeDataPointValue(vo.getId(), time);
        }
        row++;
    }
    // Setup results
    model.put("rowsImported", rowsImported);
    model.put("rowsDeleted", rowsDeleted);
    model.put("rowsWithErrors", rowErrors);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) HashMap(java.util.HashMap) DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) PointValueDao(com.serotonin.m2m2.db.dao.PointValueDao) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 18 with PointValueTime

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

the class WatchListDwr method createWatchListState.

/**
 * Convenience method for creating a populated view state.
 */
private WatchListState createWatchListState(HttpServletRequest request, DataPointVO pointVO, RuntimeManager rtm, Map<String, Object> model, User user) {
    // Get the data point status from the data image.
    DataPointRT point = rtm.getDataPoint(pointVO.getId());
    WatchListState state = new WatchListState();
    state.setId(Integer.toString(pointVO.getId()));
    PointValueTime pointValue = prepareBasePointState(Integer.toString(pointVO.getId()), state, pointVO, point, model);
    setEvents(pointVO, user, model, pointEventsLimit);
    if (pointValue != null && pointValue.getValue() instanceof ImageValue) {
        // Text renderers don't help here. Create a thumbnail.
        setImageText(request, state, pointVO, model, pointValue);
    } else
        setPrettyText(state, pointVO, model, pointValue);
    if (pointVO.isSettable())
        setChange(pointVO, state, point, request, model, user);
    if (state.getValue() != null)
        setChart(pointVO, state, request, model);
    setMessages(state, request, getModule().getWebPath() + "/web/snippet/watchListMessages.jsp", model);
    return state;
}
Also used : DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) ImageValue(com.serotonin.m2m2.rt.dataImage.types.ImageValue)

Example 19 with PointValueTime

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

the class MobileWatchListHandler method createState.

private MobileWatchListState createState(HttpServletRequest request, DataPointVO pointVO) {
    MobileWatchListState state = new MobileWatchListState();
    state.setId(Integer.toString(pointVO.getId()));
    state.setName(pointVO.getExtendedName());
    // Get the data point status from the data image.
    DataPointRT pointRT = Common.runtimeManager.getDataPoint(pointVO.getId());
    if (pointRT == null)
        state.setDisabled(true);
    else {
        PointValueTime pvt = pointRT.getPointValue();
        state.setTime(Functions.getTime(pvt));
        if (pvt != null && pvt.getValue() instanceof ImageValue) {
            // Text renderers don't help here. Create a thumbnail.
            Map<String, Object> model = new HashMap<String, Object>();
            model.put("point", pointVO);
            model.put("pointValue", pvt);
            state.setValue(BaseDwr.generateContent(request, "imageValueThumbnail.jsp", model));
        } else
            state.setValue(Functions.getHtmlText(pointVO, pvt));
    }
    return state;
}
Also used : HashMap(java.util.HashMap) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) ImageValue(com.serotonin.m2m2.rt.dataImage.types.ImageValue)

Example 20 with PointValueTime

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

the class VirtualDataSourceRT method doPoll.

@Override
public void doPoll(long time) {
    if (delay > 0) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
    }
    for (DataPointRT dataPoint : dataPoints) {
        VirtualPointLocatorRT locator = dataPoint.getPointLocator();
        // Change the point values according to their definitions.
        locator.change();
        dataPoint.updatePointValue(new PointValueTime(locator.getCurrentValue(), time));
    }
}
Also used : DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime)

Aggregations

PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)104 ArrayList (java.util.ArrayList)33 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)31 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)29 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)24 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)23 IdPointValueTime (com.serotonin.m2m2.rt.dataImage.IdPointValueTime)23 IOException (java.io.IOException)18 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)17 HashMap (java.util.HashMap)17 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)15 ImageValue (com.serotonin.m2m2.rt.dataImage.types.ImageValue)12 PointValueFacade (com.serotonin.m2m2.rt.dataImage.PointValueFacade)11 ScriptException (javax.script.ScriptException)10 PointValueDao (com.serotonin.m2m2.db.dao.PointValueDao)9 IDataPointValueSource (com.serotonin.m2m2.rt.dataImage.IDataPointValueSource)9 LogStopWatch (com.serotonin.log.LogStopWatch)8 AlphanumericValue (com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue)8 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)8 ResultTypeException (com.serotonin.m2m2.rt.script.ResultTypeException)8