Search in sources :

Example 71 with PointValueTime

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

the class ThumbnailComponent method addDataToModel.

@Override
public void addDataToModel(Map<String, Object> model, PointValueTime pointValue) {
    if (pointValue == null || pointValue.getValue() == null) {
        model.put("error", "common.noData");
        return;
    }
    if (!(pointValue.getValue() instanceof ImageValue)) {
        model.put("error", "common.thumb.invalidValue");
        return;
    }
    ImageValue imageValue = (ImageValue) pointValue.getValue();
    model.put("imageType", imageValue.getTypeExtension());
    model.put("scalePercent", scalePercent);
}
Also used : ImageValue(com.serotonin.m2m2.rt.dataImage.types.ImageValue)

Example 72 with PointValueTime

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

the class PointValueRestController method firstAndLastPointValues.

@ApiOperation(value = "First and last point values", notes = "Retrieves the first and last point values within a time range, used to read accumulators")
@RequestMapping(method = RequestMethod.GET, value = "/{xid}/first-last", produces = { "application/json", "text/csv" })
public ResponseEntity<List<PointValueTimeModel>> firstAndLastPointValues(HttpServletRequest request, @ApiParam(value = "Point xid", required = true, allowMultiple = false) @PathVariable String xid, @ApiParam(value = "Return rendered value as String", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean useRendered, @ApiParam(value = "Return converted value using displayed unit", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean unitConversion, @ApiParam(value = "From time", required = false, allowMultiple = false) @RequestParam(value = "from", required = false) @DateTimeFormat(iso = ISO.DATE_TIME) DateTime from, @ApiParam(value = "To time", required = false, allowMultiple = false) @RequestParam(value = "to", required = false) @DateTimeFormat(iso = ISO.DATE_TIME) DateTime to, @ApiParam(value = "Time zone of output, used if formatted times are returned", required = false, allowMultiple = false) @RequestParam(value = "timezone", required = false) String timezone) {
    RestProcessResult<List<PointValueTimeModel>> result = new RestProcessResult<List<PointValueTimeModel>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        if (timezone != null) {
            try {
                ZoneId.of(timezone);
            } catch (Exception e) {
                RestValidationResult vr = new RestValidationResult();
                vr.addError("validate.invalidValue", "timezone");
                throw new ValidationFailedRestException(vr);
            }
        }
        DataPointVO vo = DataPointDao.instance.getByXid(xid);
        if (vo == null) {
            result.addRestMessage(getDoesNotExistMessage());
            return result.createResponseEntity();
        }
        try {
            if (Permissions.hasDataPointReadPermission(user, vo)) {
                long current = Common.timer.currentTimeMillis();
                if (from == null)
                    from = new DateTime(current);
                if (to == null)
                    to = new DateTime(current);
                // better not to for RESTfulness
                if (timezone != null) {
                    DateTimeZone zone = DateTimeZone.forID(timezone);
                    from = from.withZone(zone);
                    to = to.withZone(zone);
                }
                PointValueFacade pointValueFacade = new PointValueFacade(vo.getId(), false);
                PointValueTime first = pointValueFacade.getPointValueAfter(from.getMillis());
                PointValueTime last = pointValueFacade.getPointValueBefore(to.getMillis());
                List<PointValueTimeModel> models = new ArrayList<PointValueTimeModel>(2);
                if (useRendered) {
                    if (first != null) {
                        PointValueTimeModel model = new PointValueTimeModel();
                        model.setType(DataTypeEnum.convertTo(first.getValue().getDataType()));
                        model.setValue(Functions.getRenderedText(vo, first));
                        model.setTimestamp(first.getTime());
                        if (first.isAnnotated())
                            model.setAnnotation(((AnnotatedPointValueTime) first).getAnnotation(Common.getTranslations()));
                        models.add(model);
                    }
                    if (last != null) {
                        PointValueTimeModel model = new PointValueTimeModel();
                        model.setType(DataTypeEnum.convertTo(last.getValue().getDataType()));
                        model.setValue(Functions.getRenderedText(vo, last));
                        model.setTimestamp(last.getTime());
                        if (last.isAnnotated())
                            model.setAnnotation(((AnnotatedPointValueTime) last).getAnnotation(Common.getTranslations()));
                        models.add(model);
                    }
                } else if (unitConversion) {
                    if (first != null) {
                        PointValueTimeModel model = new PointValueTimeModel();
                        model.setType(DataTypeEnum.convertTo(first.getValue().getDataType()));
                        model.setValue(vo.getUnit().getConverterTo(vo.getRenderedUnit()).convert(first.getValue().getDoubleValue()));
                        model.setTimestamp(first.getTime());
                        if (first.isAnnotated())
                            model.setAnnotation(((AnnotatedPointValueTime) first).getAnnotation(Common.getTranslations()));
                        models.add(model);
                    }
                    if (last != null) {
                        PointValueTimeModel model = new PointValueTimeModel();
                        model.setType(DataTypeEnum.convertTo(last.getValue().getDataType()));
                        model.setValue(vo.getUnit().getConverterTo(vo.getRenderedUnit()).convert(last.getValue().getDoubleValue()));
                        model.setTimestamp(last.getTime());
                        if (last.isAnnotated())
                            model.setAnnotation(((AnnotatedPointValueTime) last).getAnnotation(Common.getTranslations()));
                        models.add(model);
                    }
                } else {
                    models.add(first == null ? null : new PointValueTimeModel(first));
                    models.add(last == null ? null : new PointValueTimeModel(last));
                }
                if (vo.getPointLocator().getDataTypeId() == DataTypes.IMAGE) {
                    // If we are an image type we should build the URLS
                    UriComponentsBuilder imageServletBuilder = UriComponentsBuilder.fromPath("/imageValue/hst{ts}_{id}.jpg");
                    for (PointValueTimeModel model : models) {
                        model.setValue(imageServletBuilder.buildAndExpand(model.getTimestamp(), vo.getId()).toUri());
                    }
                }
                return result.createResponseEntity(models);
            } else {
                result.addRestMessage(getUnauthorizedMessage());
                return result.createResponseEntity();
            }
        } catch (PermissionException e) {
            LOG.error(e.getMessage(), e);
            result.addRestMessage(getUnauthorizedMessage());
            return result.createResponseEntity();
        }
    } else {
        return result.createResponseEntity();
    }
}
Also used : RestValidationResult(com.infiniteautomation.mango.rest.v2.model.RestValidationResult) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) PointValueFacade(com.serotonin.m2m2.rt.dataImage.PointValueFacade) User(com.serotonin.m2m2.vo.User) XidPointValueTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.XidPointValueTimeModel) RecentPointValueTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.RecentPointValueTimeModel) PointValueTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.PointValueTimeModel) ValidationFailedRestException(com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException) ArrayList(java.util.ArrayList) RestValidationFailedException(com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException) ValidationFailedRestException(com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException) RTException(com.serotonin.m2m2.rt.RTException) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) DateTime(org.joda.time.DateTime) DateTimeZone(org.joda.time.DateTimeZone) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) ServletUriComponentsBuilder(org.springframework.web.servlet.support.ServletUriComponentsBuilder) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) List(java.util.List) ArrayList(java.util.ArrayList) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 73 with PointValueTime

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

the class PointValueRestController method setPointValue.

/**
 * Helper method for setting a point value
 *
 * @param xid
 * @param data
 * @param unitConversion
 * @return
 */
private RestProcessResult<PointValueTimeModel> setPointValue(User user, String xid, PointValueTimeModel model, boolean unitConversion, UriComponentsBuilder builder) {
    RestProcessResult<PointValueTimeModel> result = new RestProcessResult<PointValueTimeModel>(HttpStatus.OK);
    DataPointVO existingDp = DataPointDao.instance.getByXid(xid);
    if (existingDp == null) {
        result.addRestMessage(getDoesNotExistMessage());
        return result;
    }
    try {
        if (Permissions.hasDataPointSetPermission(user, existingDp)) {
            // Set the time to now if it is not present
            if (model.getTimestamp() == 0) {
                model.setTimestamp(Common.timer.currentTimeMillis());
            }
            // Validate the model's data type for compatibility
            if (DataTypeEnum.convertFrom(model.getType()) != existingDp.getPointLocator().getDataTypeId()) {
                result.addRestMessage(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("event.ds.dataType"));
                return result;
            }
            // Validate the timestamp for future dated
            if (model.getTimestamp() > Common.timer.currentTimeMillis() + SystemSettingsDao.getFutureDateLimit()) {
                result.addRestMessage(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("common.default", "Future dated points not acceptable."));
                return result;
            }
            // Are we converting from the rendered Unit?
            if (unitConversion) {
                if ((model.getType() == DataTypeEnum.NUMERIC) && (model.getValue() instanceof Number)) {
                    double value;
                    if (model.getValue() instanceof Integer) {
                        value = (double) ((Integer) model.getValue());
                    } else {
                        value = (double) ((Double) model.getValue());
                    }
                    model.setValue(existingDp.getRenderedUnit().getConverterTo(existingDp.getUnit()).convert(value));
                } else {
                    result.addRestMessage(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("common.default", "[" + xid + "]Cannot perform unit conversion on Non Numeric data types."));
                    return result;
                }
            }
            // to convert it
            if ((model.getType() == DataTypeEnum.MULTISTATE) && (model.getValue() instanceof String)) {
                try {
                    DataValue value = existingDp.getTextRenderer().parseText((String) model.getValue(), existingDp.getPointLocator().getDataTypeId());
                    model.setValue(value.getObjectValue());
                } catch (Exception e) {
                    // Lots can go wrong here so let the user know
                    result.addRestMessage(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("common.default", "[" + xid + "]Unable to convert Multistate String representation to any known value."));
                }
            }
            final PointValueTime pvt;
            try {
                pvt = model.getData();
            } catch (Exception e) {
                result.addRestMessage(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("common.default", "[" + xid + "]Invalid Format"));
                return result;
            }
            // one last check to ensure we are inserting the correct data type
            if (DataTypes.getDataType(pvt.getValue()) != existingDp.getPointLocator().getDataTypeId()) {
                result.addRestMessage(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("event.ds.dataType"));
                return result;
            }
            final int dataSourceId = existingDp.getDataSourceId();
            SetPointSource source = null;
            if (model.getAnnotation() != null) {
                source = new SetPointSource() {

                    @Override
                    public String getSetPointSourceType() {
                        return "REST";
                    }

                    @Override
                    public int getSetPointSourceId() {
                        return dataSourceId;
                    }

                    @Override
                    public TranslatableMessage getSetPointSourceMessage() {
                        return ((AnnotatedPointValueTime) pvt).getSourceMessage();
                    }

                    @Override
                    public void raiseRecursionFailureEvent() {
                        LOG.error("Recursive failure while setting point via REST");
                    }
                };
            }
            try {
                Common.runtimeManager.setDataPointValue(existingDp.getId(), pvt, source);
                // This URI may not always be accurate if the Data Source doesn't use the
                // provided time...
                URI location = builder.path("/v1/point-values/{xid}/{time}").buildAndExpand(xid, pvt.getTime()).toUri();
                result.addRestMessage(getResourceCreatedMessage(location));
                return result;
            } catch (RTException e) {
                // Ok its probably not enabled or settable
                result.addRestMessage(new RestMessage(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("common.default", "[" + xid + "]" + e.getMessage())));
                return result;
            } catch (Exception e) {
                LOG.error(e.getMessage(), e);
                result.addRestMessage(getInternalServerErrorMessage(e.getMessage()));
                return result;
            }
        } else {
            result.addRestMessage(getUnauthorizedMessage());
            return result;
        }
    } catch (PermissionException e) {
        LOG.error(e.getMessage(), e);
        result.addRestMessage(getUnauthorizedMessage());
        return result;
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) XidPointValueTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.XidPointValueTimeModel) RecentPointValueTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.RecentPointValueTimeModel) PointValueTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.PointValueTimeModel) DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) SetPointSource(com.serotonin.m2m2.rt.dataImage.SetPointSource) URI(java.net.URI) RestValidationFailedException(com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException) ValidationFailedRestException(com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException) RTException(com.serotonin.m2m2.rt.RTException) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) RTException(com.serotonin.m2m2.rt.RTException) RestMessage(com.serotonin.m2m2.web.mvc.rest.v1.message.RestMessage) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 74 with PointValueTime

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

the class PointValueRestController method getLatestPointValues.

/**
 * Get the latest point values for a point
 *
 * @param xid
 * @param limit
 * @return
 */
@ApiOperation(value = "Get Latest Point Values Directly from the Runtime Manager, this makes Cached and Intra-Interval data available.", notes = "Default limit 100, time descending order, Default to return cached data. For the most efficient use of this endpoint " + " the data point's default cache size should be the size that you will typically query the latest values of.")
@RequestMapping(method = RequestMethod.GET, value = "/{xid}/latest", produces = { "application/json", "text/csv" })
public ResponseEntity<List<RecentPointValueTimeModel>> getLatestPointValues(HttpServletRequest request, @ApiParam(value = "Point xid", required = true, allowMultiple = false) @PathVariable String xid, @ApiParam(value = "Return rendered value as String", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean useRendered, @ApiParam(value = "Return converted value using displayed unit", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean unitConversion, @ApiParam(value = "Limit results", allowMultiple = false, defaultValue = "100") @RequestParam(value = "limit", defaultValue = "100") int limit, @ApiParam(value = "Return cached data?", allowMultiple = false, defaultValue = "true") @RequestParam(value = "useCache", defaultValue = "true") boolean useCache) {
    RestProcessResult<List<RecentPointValueTimeModel>> result = new RestProcessResult<List<RecentPointValueTimeModel>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        DataPointVO vo = DataPointDao.instance.getByXid(xid);
        if (vo == null) {
            result.addRestMessage(getDoesNotExistMessage());
            return result.createResponseEntity();
        }
        try {
            if (Permissions.hasDataPointReadPermission(user, vo)) {
                // Check to see if we can convert (Must be a Numeric Value)
                if (unitConversion && (vo.getPointLocator().getDataTypeId() != DataTypes.NUMERIC)) {
                    result.addRestMessage(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("common.default", "Can't convert non-numeric types."));
                    return result.createResponseEntity();
                }
                // If we are an image type we should build the URLS
                UriComponentsBuilder imageServletBuilder = UriComponentsBuilder.fromPath("/imageValue/hst{ts}_{id}.jpg");
                List<RecentPointValueTimeModel> models;
                if (useCache) {
                    // In an effort not to expand the PointValueCache we avoid the
                    // PointValueFacade
                    DataPointRT rt = Common.runtimeManager.getDataPoint(vo.getId());
                    if (rt != null) {
                        List<PointValueTime> cache = rt.getCacheCopy();
                        if (limit < cache.size()) {
                            List<PointValueTime> pvts = cache.subList(0, limit);
                            models = new ArrayList<>(limit);
                            for (PointValueTime pvt : pvts) models.add(createRecentPointValueTimeModel(vo, pvt, imageServletBuilder, useRendered, unitConversion, true));
                        } else {
                            // We need to merge 2 lists
                            List<PointValueTime> disk = Common.databaseProxy.newPointValueDao().getLatestPointValues(vo.getId(), limit);
                            Set<RecentPointValueTimeModel> all = new HashSet<RecentPointValueTimeModel>(limit);
                            for (PointValueTime pvt : cache) all.add(createRecentPointValueTimeModel(vo, pvt, imageServletBuilder, useRendered, unitConversion, true));
                            for (PointValueTime pvt : disk) {
                                if (all.size() >= limit)
                                    break;
                                else
                                    all.add(createRecentPointValueTimeModel(vo, pvt, imageServletBuilder, useRendered, unitConversion, false));
                            }
                            models = new ArrayList<>(all);
                            // Override the comparison method
                            Collections.sort(models, new Comparator<RecentPointValueTimeModel>() {

                                // Compare such that data sets are returned in time
                                // descending order
                                // which turns out is opposite of compare to method for
                                // PointValueTime objects
                                @Override
                                public int compare(RecentPointValueTimeModel o1, RecentPointValueTimeModel o2) {
                                    if (o1.getTimestamp() < o2.getTimestamp())
                                        return 1;
                                    if (o1.getTimestamp() > o2.getTimestamp())
                                        return -1;
                                    return 0;
                                }
                            });
                        }
                    } else {
                        List<PointValueTime> pvts = Common.databaseProxy.newPointValueDao().getLatestPointValues(vo.getId(), limit);
                        models = new ArrayList<>(limit);
                        for (PointValueTime pvt : pvts) models.add(createRecentPointValueTimeModel(vo, pvt, imageServletBuilder, useRendered, unitConversion, false));
                    }
                } else {
                    models = new ArrayList<>(limit);
                    List<PointValueTime> pvts = Common.databaseProxy.newPointValueDao().getLatestPointValues(vo.getId(), limit);
                    for (PointValueTime pvt : pvts) models.add(createRecentPointValueTimeModel(vo, pvt, imageServletBuilder, useRendered, unitConversion, false));
                }
                return result.createResponseEntity(models);
            } else {
                result.addRestMessage(getUnauthorizedMessage());
                return result.createResponseEntity();
            }
        } catch (PermissionException e) {
            LOG.error(e.getMessage(), e);
            result.addRestMessage(getUnauthorizedMessage());
            return result.createResponseEntity();
        }
    } else {
        return result.createResponseEntity();
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) User(com.serotonin.m2m2.vo.User) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) RecentPointValueTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.RecentPointValueTimeModel) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) ServletUriComponentsBuilder(org.springframework.web.servlet.support.ServletUriComponentsBuilder) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) List(java.util.List) ArrayList(java.util.ArrayList) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) HashSet(java.util.HashSet) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 75 with PointValueTime

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

the class PointValueRestController method getLatestPointValuesForDataSourceAsMultipleArrays.

/**
 * Get the latest point values a set of points return as map of xid to array of values
 *
 * @param xid
 * @param limit
 * @return
 */
@ApiOperation(value = "Get Latest Point Values for all points on a data source directly from the Runtime Manager, this makes Cached and Intra-Interval data available.", notes = "Default limit 100, time descending order, Default to return cached data. Returns data as map of xid to values.")
@RequestMapping(method = RequestMethod.GET, value = "/{dataSourceXid}/latest-data-source-multiple-arrays", produces = { "application/json", "text/csv" })
public ResponseEntity<ObjectStream<Map<String, List<PointValueTime>>>> getLatestPointValuesForDataSourceAsMultipleArrays(HttpServletRequest request, @ApiParam(value = "Data source xid", required = true, allowMultiple = true) @PathVariable String dataSourceXid, @ApiParam(value = "Return rendered value as String", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean useRendered, @ApiParam(value = "Return converted value using displayed unit", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean unitConversion, @ApiParam(value = "Limit results", allowMultiple = false, defaultValue = "100") @RequestParam(value = "limit", defaultValue = "100") int limit, @ApiParam(value = "Return cached data?", allowMultiple = false, defaultValue = "true") @RequestParam(value = "useCache", defaultValue = "true") boolean useCache, @ApiParam(value = "Date Time format pattern for timestamps as strings, if not included epoch milli number is used", required = false, allowMultiple = false) @RequestParam(value = "dateTimeFormat", required = false) String dateTimeFormat, @ApiParam(value = "Time zone of output, used if formatted times are returned", required = false, allowMultiple = false) @RequestParam(value = "timezone", required = false) String timezone) {
    RestProcessResult<ObjectStream<Map<String, List<PointValueTime>>>> result = new RestProcessResult<ObjectStream<Map<String, List<PointValueTime>>>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        DataSourceVO<?> ds = DataSourceDao.instance.getByXid(dataSourceXid);
        if (ds == null)
            throw new NotFoundRestException();
        if (dateTimeFormat != null) {
            try {
                DateTimeFormatter.ofPattern(dateTimeFormat);
            } catch (IllegalArgumentException e) {
                RestValidationResult vr = new RestValidationResult();
                vr.addError("validate.invalid", "dateTimeFormat");
                throw new ValidationFailedRestException(vr);
            }
        }
        if (timezone != null) {
            try {
                ZoneId.of(timezone);
            } catch (Exception e) {
                RestValidationResult vr = new RestValidationResult();
                vr.addError("validate.invalidValue", "timezone");
                throw new ValidationFailedRestException(vr);
            }
        }
        List<DataPointVO> points = DataPointDao.instance.getDataPointsForDataSourceStart(ds.getId());
        Map<Integer, DataPointVO> pointIdMap = new HashMap<Integer, DataPointVO>(points.size());
        for (DataPointVO vo : points) {
            if (Permissions.hasDataPointReadPermission(user, vo))
                pointIdMap.put(vo.getId(), vo);
            else {
                // Abort, invalid permissions
                result.addRestMessage(getUnauthorizedMessage());
                return result.createResponseEntity();
            }
        }
        // Do we have any valid points?
        if (pointIdMap.size() == 0) {
            result.addRestMessage(getDoesNotExistMessage());
            return result.createResponseEntity();
        }
        try {
            XidPointValueTimeLatestPointFacadeStream pvtDatabaseStream = new XidPointValueTimeLatestPointFacadeStream(pointIdMap, useRendered, unitConversion, limit, useCache, dateTimeFormat, timezone);
            return result.createResponseEntity(pvtDatabaseStream);
        } catch (PermissionException e) {
            LOG.error(e.getMessage(), e);
            result.addRestMessage(getUnauthorizedMessage());
            return result.createResponseEntity();
        }
    } else {
        return result.createResponseEntity();
    }
}
Also used : RestValidationResult(com.infiniteautomation.mango.rest.v2.model.RestValidationResult) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) User(com.serotonin.m2m2.vo.User) ValidationFailedRestException(com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException) HashMap(java.util.HashMap) RestValidationFailedException(com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException) ValidationFailedRestException(com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException) RTException(com.serotonin.m2m2.rt.RTException) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) XidPointValueTimeLatestPointFacadeStream(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.XidPointValueTimeLatestPointFacadeStream) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) ObjectStream(com.serotonin.m2m2.web.mvc.rest.v1.model.ObjectStream) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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