Search in sources :

Example 46 with DataPointVO

use of com.serotonin.m2m2.vo.DataPointVO in project ma-modules-public by infiniteautomation.

the class PointValueRestController method getPointStatistics.

@ApiOperation(value = "Get Point Statistics", notes = "From time inclusive, To time exclusive")
@RequestMapping(method = RequestMethod.GET, value = "/{xid}/statistics", produces = { "application/json" })
public ResponseEntity<StatisticsStream> getPointStatistics(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", required = false, allowMultiple = false) @RequestParam(value = "timezone", required = false) String timezone, @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) {
    RestProcessResult<StatisticsStream> result = new RestProcessResult<StatisticsStream>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        if (dateTimeFormat != null) {
            try {
                DateTimeFormatter.ofPattern(dateTimeFormat);
            } catch (IllegalArgumentException e) {
                RestValidationResult vr = new RestValidationResult();
                vr.addError("validate.invalidValue", "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);
            }
        }
        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);
                }
                StatisticsStream stream = new StatisticsStream(vo, useRendered, unitConversion, from.getMillis(), to.getMillis(), dateTimeFormat, timezone);
                return result.createResponseEntity(stream);
            } 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) User(com.serotonin.m2m2.vo.User) ValidationFailedRestException(com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException) 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) StatisticsStream(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.statistics.StatisticsStream) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 47 with DataPointVO

use of com.serotonin.m2m2.vo.DataPointVO in project ma-modules-public by infiniteautomation.

the class PointValueRestController method latestPointValuesForMultiplePointsAsMultipleArrays.

private ResponseEntity<ObjectStream<Map<String, List<PointValueTime>>>> latestPointValuesForMultiplePointsAsMultipleArrays(HttpServletRequest request, String[] xids, boolean useRendered, boolean unitConversion, int limit, boolean useCache, String dateTimeFormat, 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()) {
        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);
            }
        }
        Map<Integer, DataPointVO> pointIdMap = new HashMap<Integer, DataPointVO>(xids.length);
        DataPointVO vo;
        for (String xid : xids) {
            vo = DataPointDao.instance.getByXid(xid);
            if (vo != null) {
                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) 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)

Example 48 with DataPointVO

use of com.serotonin.m2m2.vo.DataPointVO in project ma-modules-public by infiniteautomation.

the class PointValueRestController method pointValuesForMultiplePointsAsMultipleArrays.

public ResponseEntity<ObjectStream<Map<String, List<PointValueTime>>>> pointValuesForMultiplePointsAsMultipleArrays(HttpServletRequest request, String[] xids, boolean useRendered, boolean unitConversion, DateTime from, DateTime to, RollupEnum rollup, TimePeriodType timePeriodType, Integer timePeriods, String timezone, Integer limit, String dateTimeFormat) {
    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()) {
        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);
            }
        }
        Map<Integer, DataPointVO> pointIdMap = new HashMap<Integer, DataPointVO>(xids.length);
        DataPointVO vo;
        for (String xid : xids) {
            vo = DataPointDao.instance.getByXid(xid);
            if (vo != null) {
                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 {
            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);
            }
            // Are we using rollup
            if ((rollup != null) && (rollup != RollupEnum.NONE)) {
                if (rollup == RollupEnum.FFT) {
                // Special Rollup for FFT's with no time rollup action
                // TODO Need a way to return frequency or period values
                // IdPointValueFftCalculator calc = new
                // IdPointValueFftCalculator(pointIdMap, from.getTime(), to.getTime(),
                // true);
                // return result.createResponseEntity(calc);
                } else {
                    TimePeriod timePeriod = null;
                    if ((timePeriodType != null) && (timePeriods != null)) {
                        timePeriod = new TimePeriod(timePeriods, timePeriodType);
                    }
                    XidPointValueMapRollupCalculator calc = new XidPointValueMapRollupCalculator(pointIdMap, useRendered, unitConversion, rollup, timePeriod, from, to, limit, dateTimeFormat, timezone);
                    return result.createResponseEntity(calc);
                }
                return result.createResponseEntity();
            } else {
                XidPointValueTimeMapDatabaseStream pvtDatabaseStream = new XidPointValueTimeMapDatabaseStream(pointIdMap, useRendered, unitConversion, from.getMillis(), to.getMillis(), this.dao, limit, 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) User(com.serotonin.m2m2.vo.User) XidPointValueTimeMapDatabaseStream(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.XidPointValueTimeMapDatabaseStream) ValidationFailedRestException(com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException) HashMap(java.util.HashMap) TimePeriod(com.serotonin.m2m2.web.mvc.rest.v1.model.time.TimePeriod) 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) 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) XidPointValueMapRollupCalculator(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.XidPointValueMapRollupCalculator) Map(java.util.Map) HashMap(java.util.HashMap)

Example 49 with DataPointVO

use of com.serotonin.m2m2.vo.DataPointVO 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 50 with DataPointVO

use of com.serotonin.m2m2.vo.DataPointVO in project ma-modules-public by infiniteautomation.

the class UserAccessRestController method getDataPointAccess.

@ApiOperation(value = "Get Data Point Access List", notes = "Returns a list of users and thier access")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json", "text/csv" }, value = "/data-point/{xid}")
public ResponseEntity<List<UserAccessModel>> getDataPointAccess(@ApiParam(value = "Valid data point xid", required = true, allowMultiple = false) @PathVariable String xid, HttpServletRequest request) {
    RestProcessResult<List<UserAccessModel>> result = new RestProcessResult<List<UserAccessModel>>(HttpStatus.OK);
    this.checkUser(request, result);
    if (result.isOk()) {
        DataPointVO vo = DataPointDao.instance.getByXid(xid);
        if (vo != null) {
            List<UserAccessModel> models = new ArrayList<UserAccessModel>();
            List<User> allUsers = UserDao.instance.getUsers();
            int accessType;
            for (User mangoUser : allUsers) {
                accessType = Permissions.getDataPointAccessType(mangoUser, vo);
                if (accessType != Permissions.DataPointAccessTypes.NONE) {
                    models.add(new UserAccessModel(Permissions.ACCESS_TYPE_CODES.getCode(accessType), new UserModel(mangoUser)));
                }
            }
            return result.createResponseEntity(models);
        }
    }
    return result.createResponseEntity();
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) UserModel(com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) UserAccessModel(com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserAccessModel) User(com.serotonin.m2m2.vo.User) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

DataPointVO (com.serotonin.m2m2.vo.DataPointVO)196 User (com.serotonin.m2m2.vo.User)62 ArrayList (java.util.ArrayList)53 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)48 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)40 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)40 HashMap (java.util.HashMap)35 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)33 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)32 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)30 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)29 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)28 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)26 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)21 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)21 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)16 AbstractPointEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)15 List (java.util.List)15 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)14 IOException (java.io.IOException)12