Search in sources :

Example 6 with PointValueFacade

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

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

the class XidPointValueTimeLatestPointFacadeStream method streamData.

/*
	 * (non-Javadoc)
	 * @see com.serotonin.m2m2.web.mvc.rest.v1.model.ObjectStream#streamData(com.serotonin.m2m2.web.mvc.rest.v1.csv.CSVPojoWriter)
	 */
@Override
public void streamData(CSVPojoWriter<Map<String, List<PointValueTime>>> writer) throws IOException {
    Iterator<Integer> it = this.pointMap.keySet().iterator();
    boolean writeHeaders = true;
    while (it.hasNext()) {
        DataPointVO vo = this.pointMap.get(it.next());
        PointValueFacade pointValueFacade = new PointValueFacade(vo.getId(), useCache);
        PointValueTimeCsvStreamCallback callback = new PointValueTimeCsvStreamCallback(writer.getWriter(), vo, useRendered, unitConversion, true, writeHeaders, null, dateTimeFormat, timezone);
        List<PointValueTime> pvts = pointValueFacade.getLatestPointValues(limit);
        for (int i = 0; i < pvts.size(); i++) callback.row(pvts.get(i), i);
        writeHeaders = false;
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PointValueFacade(com.serotonin.m2m2.rt.dataImage.PointValueFacade) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime)

Example 8 with PointValueFacade

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

the class StatisticsStream method streamData.

/* (non-Javadoc)
	 * @see com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.PointValueTimeStream#streamData(com.fasterxml.jackson.core.JsonGenerator)
	 */
@Override
public void streamData(JsonGenerator jgen) throws IOException {
    // TODO Can't use the Facade as there is no way to perform the callback integrated with the PointValueCache
    // PointValueFacade pointValueFacade = new PointValueFacade(this.dataPointId);
    // First find the start value
    PointValueDao pvd = Common.databaseProxy.newPointValueDao();
    PointValueTime startPvt = pvd.getPointValueBefore(vo.getId(), from);
    DataValue startValue = null;
    if (startPvt != null)
        startValue = startPvt.getValue();
    StatisticsCalculator calculator = new StatisticsCalculator(jgen, vo, useRendered, unitConversion, this.from, this.to, startValue, dateTimeFormat, timezone);
    // Do the main work
    pvd.getPointValuesBetween(vo.getId(), from, to, calculator);
    // Finish
    calculator.done();
}
Also used : PointValueDao(com.serotonin.m2m2.db.dao.PointValueDao) DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime)

Example 9 with PointValueFacade

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

the class DataPointDetailsDwr method getHistoryTableData.

@DwrPermission(user = true)
public ProcessResult getHistoryTableData(int limit, boolean useCache) {
    DataPointVO pointVO = Common.getUser().getEditPoint();
    PointValueFacade facade = new PointValueFacade(pointVO.getId(), useCache);
    List<PointValueTime> rawData = facade.getLatestPointValues(limit);
    List<RenderedPointValueTime> renderedData = new ArrayList<RenderedPointValueTime>(rawData.size());
    for (PointValueTime pvt : rawData) {
        RenderedPointValueTime rpvt = new RenderedPointValueTime();
        rpvt.setValue(Functions.getHtmlText(pointVO, pvt));
        rpvt.setTime(Functions.getTime(pvt));
        if (pvt.isAnnotated()) {
            AnnotatedPointValueTime apvt = (AnnotatedPointValueTime) pvt;
            rpvt.setAnnotation(apvt.getAnnotation(getTranslations()));
        }
        renderedData.add(rpvt);
    }
    ProcessResult response = new ProcessResult();
    response.addData("history", renderedData);
    addAsof(response);
    return response;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PointValueFacade(com.serotonin.m2m2.rt.dataImage.PointValueFacade) RenderedPointValueTime(com.serotonin.m2m2.web.dwr.beans.RenderedPointValueTime) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) RenderedPointValueTime(com.serotonin.m2m2.web.dwr.beans.RenderedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) ArrayList(java.util.ArrayList) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 10 with PointValueFacade

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

the class DataPointDetailsDwr method getFlipbookData.

@DwrPermission(user = true)
public ProcessResult getFlipbookData(int limit) {
    HttpServletRequest request = WebContextFactory.get().getHttpServletRequest();
    DataPointVO vo = Common.getUser(request).getEditPoint();
    PointValueFacade facade = new PointValueFacade(vo.getId());
    List<PointValueTime> values = facade.getLatestPointValues(limit);
    Collections.reverse(values);
    List<ImageValueBean> result = new ArrayList<ImageValueBean>();
    for (PointValueTime pvt : values) {
        ImageValue imageValue = (ImageValue) pvt.getValue();
        String uri = ImageValueServlet.servletPath + ImageValueServlet.historyPrefix + pvt.getTime() + "_" + vo.getId() + "." + imageValue.getTypeExtension();
        result.add(new ImageValueBean(Functions.getTime(pvt), uri));
    }
    ProcessResult response = new ProcessResult();
    response.addData("images", result);
    addAsof(response);
    return response;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PointValueFacade(com.serotonin.m2m2.rt.dataImage.PointValueFacade) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) RenderedPointValueTime(com.serotonin.m2m2.web.dwr.beans.RenderedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) ArrayList(java.util.ArrayList) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) ImageValueBean(com.serotonin.m2m2.vo.bean.ImageValueBean) ImageValue(com.serotonin.m2m2.rt.dataImage.types.ImageValue) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Aggregations

PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)13 PointValueFacade (com.serotonin.m2m2.rt.dataImage.PointValueFacade)11 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)9 ArrayList (java.util.ArrayList)7 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)4 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)3 RenderedPointValueTime (com.serotonin.m2m2.web.dwr.beans.RenderedPointValueTime)3 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)3 List (java.util.List)3 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)2 IdPointValueTime (com.serotonin.m2m2.rt.dataImage.IdPointValueTime)2 ImageValue (com.serotonin.m2m2.rt.dataImage.types.ImageValue)2 User (com.serotonin.m2m2.vo.User)2 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)2 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)2 RecentPointValueTimeModel (com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.RecentPointValueTimeModel)2 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ServletUriComponentsBuilder (org.springframework.web.servlet.support.ServletUriComponentsBuilder)2 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)2