Search in sources :

Example 6 with DataPointModel

use of com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel in project ma-modules-public by infiniteautomation.

the class DataPointRestController method getDataPoint.

@ApiOperation(value = "Get data point by XID", notes = "Only points that user has read permission to are returned")
@RequestMapping(method = RequestMethod.GET, value = "/{xid}")
public DataPointModel getDataPoint(@ApiParam(value = "Valid Data Point XID", required = true, allowMultiple = false) @PathVariable String xid, @AuthenticationPrincipal User user) {
    DataPointVO dataPoint = DataPointDao.instance.getByXid(xid);
    if (dataPoint == null) {
        throw new NotFoundRestException();
    }
    DataPointDao.instance.loadPartialRelationalData(dataPoint);
    Permissions.ensureDataPointReadPermission(user, dataPoint);
    return new DataPointModel(dataPoint);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) DataPointModel(com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with DataPointModel

use of com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel in project ma-modules-public by infiniteautomation.

the class DataPointRestController method deleteDataPoint.

@ApiOperation(value = "Delete a data point")
@RequestMapping(method = RequestMethod.DELETE, value = "/{xid}")
public DataPointModel deleteDataPoint(@ApiParam(value = "Valid Data Point XID", required = true, allowMultiple = false) @PathVariable String xid, @AuthenticationPrincipal User user) {
    DataPointVO dataPoint = DataPointDao.instance.getByXid(xid);
    if (dataPoint == null) {
        throw new NotFoundRestException();
    }
    Permissions.ensureDataPointReadPermission(user, dataPoint);
    Common.runtimeManager.deleteDataPoint(dataPoint);
    return new DataPointModel(dataPoint);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) DataPointModel(com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with DataPointModel

use of com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel in project ma-modules-public by infiniteautomation.

the class DataPointRestController method getDataPointById.

@ApiOperation(value = "Get data point by ID", notes = "Only points that user has read permission to are returned")
@RequestMapping(method = RequestMethod.GET, value = "/by-id/{id}")
public DataPointModel getDataPointById(@ApiParam(value = "Valid Data Point ID", required = true, allowMultiple = false) @PathVariable int id, @AuthenticationPrincipal User user) {
    DataPointVO dataPoint = DataPointDao.instance.get(id);
    if (dataPoint == null) {
        throw new NotFoundRestException();
    }
    DataPointDao.instance.loadPartialRelationalData(dataPoint);
    Permissions.ensureDataPointReadPermission(user, dataPoint);
    return new DataPointModel(dataPoint);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) DataPointModel(com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with DataPointModel

use of com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel in project ma-modules-public by infiniteautomation.

the class DataPointRestController method doIndividualRequest.

private DataPointIndividualResponse doIndividualRequest(DataPointIndividualRequest request, VoAction defaultAction, DataPointModel defaultBody, User user, UriComponentsBuilder builder) {
    DataPointIndividualResponse result = new DataPointIndividualResponse();
    try {
        String xid = request.getXid();
        VoAction action = request.getAction() == null ? defaultAction : request.getAction();
        if (action == null) {
            throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "action"));
        }
        result.setAction(action);
        DataPointModel body = request.getBody() == null ? defaultBody : request.getBody();
        switch(action) {
            case GET:
                if (xid == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "xid"));
                }
                result.setBody(this.getDataPoint(xid, user));
                break;
            case CREATE:
                if (body == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "body"));
                }
                result.setBody(this.createDataPoint(body, user, builder).getBody());
                break;
            case UPDATE:
                if (xid == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "xid"));
                }
                if (body == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "body"));
                }
                result.setBody(this.updateDataPoint(xid, body, user, builder).getBody());
                break;
            case DELETE:
                if (xid == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "xid"));
                }
                result.setBody(this.deleteDataPoint(xid, user));
                break;
        }
    } catch (Exception e) {
        result.exceptionCaught(e);
    }
    return result;
}
Also used : VoAction(com.infiniteautomation.mango.rest.v2.bulk.VoAction) DataPointModel(com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) AccessDeniedException(com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)

Example 10 with DataPointModel

use of com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel in project ma-modules-public by infiniteautomation.

the class DataSourceRestController method enableDisable.

@ApiOperation(value = "Enable/disable/restart a data source")
@RequestMapping(method = RequestMethod.PUT, value = "/enable-disable/{xid}")
public ResponseEntity<DataPointModel> enableDisable(@AuthenticationPrincipal User user, @PathVariable String xid, @ApiParam(value = "Enable or disable the data source", required = true, allowMultiple = false) @RequestParam(required = true) boolean enabled, @ApiParam(value = "Restart the data source, enabled must equal true", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean restart) {
    DataSourceVO<?> dsvo = DataSourceDao.instance.getByXid(xid);
    if (dsvo == null)
        throw new NotFoundRestException();
    try {
        Permissions.ensureDataSourcePermission(user, dsvo);
    } catch (PermissionException e) {
        throw new AccessDeniedException("User does not have permission to edit the data source", e);
    }
    if (enabled && restart) {
        dsvo.setEnabled(true);
        // saving will restart it
        Common.runtimeManager.saveDataSource(dsvo);
    } else if (dsvo.isEnabled() != enabled) {
        dsvo.setEnabled(enabled);
        Common.runtimeManager.saveDataSource(dsvo);
    }
    return new ResponseEntity<>(HttpStatus.OK);
}
Also used : PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) ResponseEntity(org.springframework.http.ResponseEntity) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

DataPointModel (com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel)8 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)8 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)6 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)6 BadRequestException (com.infiniteautomation.mango.rest.v2.exception.BadRequestException)4 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)4 ResponseEntity (org.springframework.http.ResponseEntity)4 HttpHeaders (org.springframework.http.HttpHeaders)3 VoAction (com.infiniteautomation.mango.rest.v2.bulk.VoAction)2 AbstractRestV2Exception (com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception)2 DataPointPropertiesTemplateVO (com.serotonin.m2m2.vo.template.DataPointPropertiesTemplateVO)2 URI (java.net.URI)2 ConditionSortLimitWithTagKeys (com.infiniteautomation.mango.db.query.ConditionSortLimitWithTagKeys)1 AccessDeniedException (com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException)1 InvalidRQLRestException (com.infiniteautomation.mango.rest.v2.exception.InvalidRQLRestException)1 StreamedVOQueryWithTotal (com.infiniteautomation.mango.rest.v2.model.StreamedVOQueryWithTotal)1 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)1 User (com.serotonin.m2m2.vo.User)1 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)1