Search in sources :

Example 1 with PointValueFacade

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

the class StatisticsChartRenderer method addDataToModel.

@Override
public void addDataToModel(Map<String, Object> model, DataPointVO point) {
    long startTime = getStartTime();
    long endTime = startTime + getDuration();
    PointValueFacade pointValueFacade = new PointValueFacade(point.getId());
    List<PointValueTime> values = pointValueFacade.getPointValuesBetween(startTime, endTime);
    PointValueTime startVT = null;
    if (!values.isEmpty()) {
        startVT = pointValueFacade.getPointValueBefore(startTime);
    }
    // Generate statistics on the values.
    int dataTypeId = point.getPointLocator().getDataTypeId();
    if (values.size() > 0) {
        if (dataTypeId == DataTypes.BINARY || dataTypeId == DataTypes.MULTISTATE) {
            // Runtime stats
            StartsAndRuntimeList stats = new StartsAndRuntimeList(startTime, endTime, startVT, values);
            model.put("start", startVT != null ? startTime : stats.getFirstTime());
            model.put("end", endTime);
            model.put("startsAndRuntimes", stats.getData());
        } else if (dataTypeId == DataTypes.NUMERIC) {
            AnalogStatistics stats = new AnalogStatistics(startTime, endTime, startVT, values);
            model.put("start", startVT != null ? startTime : stats.getFirstTime());
            model.put("end", endTime);
            model.put("minimum", stats.getMinimumValue());
            model.put("minTime", stats.getMinimumTime());
            model.put("maximum", stats.getMaximumValue());
            model.put("maxTime", stats.getMaximumTime());
            model.put("average", stats.getAverage());
            if (includeSum)
                model.put("sum", stats.getSum());
            model.put("count", stats.getCount());
            model.put("noData", stats.getAverage() == null);
            model.put("integral", stats.getIntegral());
        } else if (dataTypeId == DataTypes.ALPHANUMERIC) {
            ValueChangeCounter stats = new ValueChangeCounter(startTime, endTime, startVT, values);
            model.put("changeCount", stats.getChanges());
        }
    }
    model.put("logEntries", values.size());
}
Also used : PointValueFacade(com.serotonin.m2m2.rt.dataImage.PointValueFacade) StartsAndRuntimeList(com.serotonin.m2m2.view.stats.StartsAndRuntimeList) ValueChangeCounter(com.serotonin.m2m2.view.stats.ValueChangeCounter) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) AnalogStatistics(com.serotonin.m2m2.view.stats.AnalogStatistics)

Example 2 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.fasterxml.jackson.core.JsonGenerator)
	 */
@Override
public void streamData(JsonGenerator jgen) {
    Iterator<Integer> it = this.pointMap.keySet().iterator();
    while (it.hasNext()) {
        try {
            DataPointVO vo = this.pointMap.get(it.next());
            PointValueFacade pointValueFacade = new PointValueFacade(vo.getId(), useCache);
            PointValueTimeJsonStreamCallback callback = new PointValueTimeJsonStreamCallback(jgen, vo, useRendered, unitConversion, null, dateTimeFormat, timezone);
            jgen.writeArrayFieldStart(vo.getXid());
            List<PointValueTime> pvts = pointValueFacade.getLatestPointValues(limit);
            for (int i = 0; i < pvts.size(); i++) callback.row(pvts.get(i), i);
            jgen.writeEndArray();
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
        }
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PointValueFacade(com.serotonin.m2m2.rt.dataImage.PointValueFacade) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) IOException(java.io.IOException)

Example 3 with PointValueFacade

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

the class IdPointValueTimeLatestPointValueFacadeStream 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) {
    IdPointValueTimeJsonStreamCallback callback = new IdPointValueTimeJsonStreamCallback(jgen, pointMap, useRendered, unitConversion, null, dateTimeFormat, timezone);
    // Sadly in this scenario we must collect all the data and then order it
    List<IdPointValueTime> ipvts = new ArrayList<IdPointValueTime>();
    Iterator<Integer> it = this.pointMap.keySet().iterator();
    while (it.hasNext()) {
        DataPointVO vo = this.pointMap.get(it.next());
        PointValueFacade pointValueFacade = new PointValueFacade(vo.getId(), useCache);
        List<PointValueTime> pvts = pointValueFacade.getLatestPointValues(limit);
        for (PointValueTime pvt : pvts) ipvts.add(new IdPointValueTime(vo.getId(), pvt.getValue(), pvt.getTime()));
    }
    // Sort it all
    Collections.sort(ipvts, new Comparator<IdPointValueTime>() {

        // 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(IdPointValueTime o1, IdPointValueTime o2) {
            if (o1.getTime() < o2.getTime())
                return 1;
            if (o1.getTime() > o2.getTime())
                return -1;
            return 0;
        }
    });
    for (int i = 0; i < ipvts.size(); i++) callback.row(ipvts.get(i), i);
    callback.finish();
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PointValueFacade(com.serotonin.m2m2.rt.dataImage.PointValueFacade) ArrayList(java.util.ArrayList) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime)

Example 4 with PointValueFacade

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

the class IdPointValueTimeLatestPointValueFacadeStream method streamData.

/* (non-Javadoc)
	 * @see com.serotonin.m2m2.web.mvc.rest.v1.model.QueryArrayStream#streamData(com.serotonin.m2m2.web.mvc.rest.v1.csv.CSVPojoWriter)
	 */
@Override
public void streamData(CSVPojoWriter<PointValueTimeModel> writer) throws IOException {
    IdPointValueTimeCsvStreamCallback callback = new IdPointValueTimeCsvStreamCallback(writer.getWriter(), pointMap, useRendered, unitConversion, null, dateTimeFormat, timezone);
    // Sadly in this scenario we must collect all the data and then order it
    List<IdPointValueTime> ipvts = new ArrayList<IdPointValueTime>();
    Iterator<Integer> it = this.pointMap.keySet().iterator();
    while (it.hasNext()) {
        DataPointVO vo = this.pointMap.get(it.next());
        PointValueFacade pointValueFacade = new PointValueFacade(vo.getId(), useCache);
        List<PointValueTime> pvts = pointValueFacade.getLatestPointValues(limit);
        for (PointValueTime pvt : pvts) ipvts.add(new IdPointValueTime(vo.getId(), pvt.getValue(), pvt.getTime()));
    }
    // Sort it all
    Collections.sort(ipvts, new Comparator<IdPointValueTime>() {

        @Override
        public int compare(IdPointValueTime o1, IdPointValueTime o2) {
            return Long.compare(o1.getTime(), o2.getTime());
        }
    });
    for (int i = 0; i < ipvts.size(); i++) callback.row(ipvts.get(i), i);
    callback.finish();
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PointValueFacade(com.serotonin.m2m2.rt.dataImage.PointValueFacade) ArrayList(java.util.ArrayList) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime)

Example 5 with PointValueFacade

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

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