Search in sources :

Example 11 with DataPointRT

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

the class VirtualDataSourceRT method forcePointRead.

@Override
public void forcePointRead(DataPointRT dataPoint) {
    VirtualPointLocatorRT locator = dataPoint.getPointLocator();
    locator.change();
    dataPoint.updatePointValue(new PointValueTime(locator.getCurrentValue(), System.currentTimeMillis()));
}
Also used : PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime)

Example 12 with DataPointRT

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

the class RuntimeManagerRestController method relinquish.

@ApiOperation(value = "Relinquish the value of a data point", notes = "Only BACnet data points allow this", response = Void.class)
@RequestMapping(method = RequestMethod.POST, value = "/relinquish/{xid}")
public void relinquish(@ApiParam(value = "Valid Data Point XID", required = true, allowMultiple = false) @PathVariable String xid, @AuthenticationPrincipal User user, HttpServletRequest request) {
    DataPointVO dataPoint = DataPointDao.instance.getByXid(xid);
    if (dataPoint == null)
        throw new NotFoundRestException();
    Permissions.ensureDataPointReadPermission(user, dataPoint);
    DataPointRT rt = Common.runtimeManager.getDataPoint(dataPoint.getId());
    if (rt == null)
        throw new GenericRestException(HttpStatus.BAD_REQUEST, new TranslatableMessage("rest.error.pointNotEnabled", xid));
    // Get the Data Source and Relinquish the point
    DataSourceRT<?> dsRt = Common.runtimeManager.getRunningDataSource(rt.getDataSourceId());
    if (dsRt == null)
        throw new GenericRestException(HttpStatus.BAD_REQUEST, new TranslatableMessage("rest.error.dataSourceNotEnabled", xid));
    dsRt.relinquish(rt);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) GenericRestException(com.infiniteautomation.mango.rest.v2.exception.GenericRestException) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 13 with DataPointRT

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

the class ScriptUtilRestController method testScript.

@PreAuthorize("isAdmin()")
@ApiOperation(value = "Test a script")
@ApiResponses({ @ApiResponse(code = 401, message = "Unauthorized user access", response = ResponseEntity.class), @ApiResponse(code = 500, message = "Error processing request", response = ResponseEntity.class) })
@RequestMapping(method = RequestMethod.POST, value = { "/test" }, consumes = { "application/json" }, produces = { "application/json" })
public ResponseEntity<ScriptRestResult> testScript(@AuthenticationPrincipal User user, @RequestBody ScriptRestModel scriptModel) {
    if (LOG.isDebugEnabled())
        LOG.debug("Testing script for: " + user.getName());
    Map<String, IDataPointValueSource> context = convertContextModel(scriptModel.getContext(), true);
    try {
        CompiledScript script = CompiledScriptExecutor.compile(scriptModel.getScript());
        final StringWriter scriptOut = new StringWriter();
        final PrintWriter scriptWriter = new PrintWriter(scriptOut);
        int logLevel = ScriptLog.LogLevel.FATAL;
        if (StringUtils.isEmpty(scriptModel.getLogLevel())) {
            int levelId = ScriptLog.LOG_LEVEL_CODES.getId(scriptModel.getLogLevel());
            if (levelId == -1)
                throw new GenericRestException(HttpStatus.INTERNAL_SERVER_ERROR, new TranslatableMessage("rest.script.error.unknownLogLevel", scriptModel.getLogLevel()));
            else
                logLevel = levelId;
        }
        ScriptLog scriptLog = new ScriptLog(scriptWriter, logLevel);
        final ScriptPermissions permissions = scriptModel.getPermissions().toPermissions();
        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 {
            PointValueTime pvt = CompiledScriptExecutor.execute(script, context, new HashMap<String, Object>(), Common.timer.currentTimeMillis(), DataTypes.ALPHANUMERIC, Common.timer.currentTimeMillis(), permissions, scriptWriter, scriptLog, loggingSetter, null, true);
            if (LOG.isDebugEnabled())
                LOG.debug("Script output: " + scriptOut.toString());
            return new ResponseEntity<>(new ScriptRestResult(scriptOut.toString(), new PointValueTimeModel(pvt)), HttpStatus.OK);
        } catch (ResultTypeException e) {
            throw new GenericRestException(HttpStatus.INTERNAL_SERVER_ERROR, e);
        }
    } catch (ScriptException e) {
        throw new GenericRestException(HttpStatus.INTERNAL_SERVER_ERROR, e);
    }
}
Also used : CompiledScript(javax.script.CompiledScript) ScriptPointValueSetter(com.serotonin.m2m2.rt.script.ScriptPointValueSetter) PointValueTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.PointValueTimeModel) ScriptLog(com.serotonin.m2m2.rt.script.ScriptLog) ScriptPermissions(com.serotonin.m2m2.rt.script.ScriptPermissions) Date(java.util.Date) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) ScriptException(javax.script.ScriptException) ResponseEntity(org.springframework.http.ResponseEntity) StringWriter(java.io.StringWriter) 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) GenericRestException(com.infiniteautomation.mango.rest.v2.exception.GenericRestException) PrintWriter(java.io.PrintWriter) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) ApiResponses(com.wordnik.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 14 with DataPointRT

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

the class MultiPointLatestDatabaseStream method buildCache.

/**
 * Build the cache based on our Query Info
 * @param voMap
 * @param limit
 * @return
 */
protected Map<Integer, List<IdPointValueTime>> buildCache() {
    Map<Integer, List<IdPointValueTime>> map = new HashMap<>();
    for (Integer id : voMap.keySet()) {
        DataPointRT rt = Common.runtimeManager.getDataPoint(id);
        if (rt != null) {
            List<PointValueTime> cache;
            if (info.getLimit() != null)
                cache = rt.getCacheCopy(info.getLimit());
            else
                cache = rt.getCacheCopy();
            List<IdPointValueTime> idPvtCache = new ArrayList<>(cache.size());
            for (PointValueTime pvt : cache) {
                if (includeCachedPoint(pvt)) {
                    if (pvt.isAnnotated())
                        idPvtCache.add(new AnnotatedIdPointValueTime(id, pvt.getValue(), pvt.getTime(), ((AnnotatedPointValueTime) pvt).getSourceMessage()));
                    else
                        idPvtCache.add(new IdPointValueTime(id, pvt.getValue(), pvt.getTime()));
                }
            }
            if (!idPvtCache.isEmpty()) {
                sortCache(idPvtCache);
                map.put(id, idPvtCache);
            }
        }
    }
    return map;
}
Also used : HashMap(java.util.HashMap) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) AnnotatedIdPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedIdPointValueTime) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) AnnotatedIdPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedIdPointValueTime) AnnotatedIdPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedIdPointValueTime)

Example 15 with DataPointRT

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

the class MBusDataSourceRT method setValue.

protected boolean setValue(DataBlock db, boolean pointError, long time, DataPointRT point, final MBusPointLocatorRT locatorRT, final MBusPointLocatorVO locatorVo) {
    try {
        if ((db instanceof BcdValue) && ((BcdValue) db).isBcdError()) {
            pointError = true;
            LOG.fatal("BCD Error : " + ((BcdValue) db).getBcdError());
            raiseEvent(POINT_READ_EXCEPTION_EVENT, time, true, new TranslatableMessage("event.exception2", point.getVO().getExtendedName(), "BCD error value: " + ((BcdValue) db).getBcdError()));
            pointError = true;
        } else if (db instanceof ByteDataBlock) {
            point.updatePointValue(new PointValueTime(locatorRT.calcCorrectedValue(((ByteDataBlock) db).getValue(), db.getCorrectionExponent(locatorVo.effectiveSiPrefix()), db.getCorrectionConstant()), time));
        } else if (db instanceof ShortDataBlock) {
            point.updatePointValue(new PointValueTime(locatorRT.calcCorrectedValue(((ShortDataBlock) db).getValue(), db.getCorrectionExponent(locatorVo.effectiveSiPrefix()), db.getCorrectionConstant()), time));
        } else if (db instanceof IntegerDataBlock) {
            point.updatePointValue(new PointValueTime(locatorRT.calcCorrectedValue(((IntegerDataBlock) db).getValue(), db.getCorrectionExponent(locatorVo.effectiveSiPrefix()), db.getCorrectionConstant()), time));
        } else if (db instanceof LongDataBlock) {
            point.updatePointValue(new PointValueTime(locatorRT.calcCorrectedValue(((LongDataBlock) db).getValue(), db.getCorrectionExponent(locatorVo.effectiveSiPrefix()), db.getCorrectionConstant()), time));
        } else if (db instanceof RealDataBlock) {
            point.updatePointValue(new PointValueTime(locatorRT.calcCorrectedValue(((RealDataBlock) db).getValue(), db.getCorrectionExponent(locatorVo.effectiveSiPrefix()), db.getCorrectionConstant()), time));
        } else if (db instanceof BigDecimalDataBlock) {
            point.updatePointValue(new PointValueTime(locatorRT.calcCorrectedValue(((BigDecimalDataBlock) db).getValue().doubleValue(), db.getCorrectionExponent(locatorVo.effectiveSiPrefix()), db.getCorrectionConstant()), time));
        } else if (db instanceof StringDataBlock) {
            point.updatePointValue(new PointValueTime(((StringDataBlock) db).getValue(), time));
        } else {
            LOG.fatal("Dont know how to save: " + point.getVO().getExtendedName());
            raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.exception2", point.getVO().getExtendedName(), "Dont know how to save"));
            pointError = true;
        }
    } catch (Exception ex) {
        LOG.fatal("Error during saving: " + vo.getName(), ex);
        raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.exception2", point.getVO().getExtendedName(), "Ex: " + ex));
        pointError = true;
    }
    return pointError;
}
Also used : ShortDataBlock(net.sf.mbus4j.dataframes.datablocks.ShortDataBlock) IntegerDataBlock(net.sf.mbus4j.dataframes.datablocks.IntegerDataBlock) BigDecimalDataBlock(net.sf.mbus4j.dataframes.datablocks.BigDecimalDataBlock) StringDataBlock(net.sf.mbus4j.dataframes.datablocks.StringDataBlock) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) BcdValue(net.sf.mbus4j.dataframes.datablocks.BcdValue) RealDataBlock(net.sf.mbus4j.dataframes.datablocks.RealDataBlock) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) LongDataBlock(net.sf.mbus4j.dataframes.datablocks.LongDataBlock) ByteDataBlock(net.sf.mbus4j.dataframes.datablocks.ByteDataBlock) IOException(java.io.IOException)

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