Search in sources :

Example 1 with RealTimeModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.RealTimeModel in project ma-modules-public by infiniteautomation.

the class RealTimeDataRestController method query.

/**
 * Query the User's Real Time Data
 * @param request
 * @param limit
 * @return
 */
@ApiOperation(value = "Query realtime values", notes = "Check the status member to ensure the point is OK not DISABLED or UNRELIABLE")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" })
public ResponseEntity<List<RealTimeModel>> query(HttpServletRequest request) {
    RestProcessResult<List<RealTimeModel>> result = new RestProcessResult<List<RealTimeModel>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        ASTNode model;
        try {
            model = parseRQLtoAST(request.getQueryString());
            if (model == null) {
                result.addRestMessage(new RestMessage(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("common.default", "Query Required")));
                return result.createResponseEntity();
            }
            List<RealTimeDataPointValue> values = RealTimeDataPointValueCache.instance.getUserView(user);
            values = model.accept(new RQLToObjectListQuery<RealTimeDataPointValue>(), values);
            List<RealTimeModel> models = new ArrayList<RealTimeModel>();
            UriComponentsBuilder imageServletBuilder = UriComponentsBuilder.fromPath("/imageValue/{ts}_{id}.jpg");
            for (RealTimeDataPointValue value : values) {
                if (value.getDataTypeId() == DataTypes.IMAGE) {
                    models.add(new RealTimeModel(value, imageServletBuilder.buildAndExpand(value.getTimestamp(), value.getDataPointId()).toUri()));
                } else {
                    models.add(new RealTimeModel(value, value.getValue()));
                }
            }
            return result.createResponseEntity(models);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            result.addRestMessage(getInternalServerErrorMessage(e.getMessage()));
            return result.createResponseEntity();
        }
    }
    return result.createResponseEntity();
}
Also used : User(com.serotonin.m2m2.vo.User) RealTimeDataPointValue(com.serotonin.m2m2.rt.dataImage.RealTimeDataPointValue) ArrayList(java.util.ArrayList) RealTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.RealTimeModel) RQLToObjectListQuery(com.infiniteautomation.mango.db.query.pojo.RQLToObjectListQuery) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) RestMessage(com.serotonin.m2m2.web.mvc.rest.v1.message.RestMessage) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) ASTNode(net.jazdw.rql.parser.ASTNode) ArrayList(java.util.ArrayList) List(java.util.List) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with RealTimeModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.RealTimeModel in project ma-modules-public by infiniteautomation.

the class RealTimeDataRestController method get.

@ApiOperation(value = "Get realtime value of point based on XID", notes = "Check the status member to ensure the point is OK not DISABLED or UNRELIABLE")
@RequestMapping(method = RequestMethod.GET, value = "/by-xid/{xid}", produces = { "application/json" })
public ResponseEntity<RealTimeModel> get(@PathVariable String xid, HttpServletRequest request) {
    RestProcessResult<RealTimeModel> result = new RestProcessResult<RealTimeModel>(HttpStatus.OK);
    // Check the user
    User user = checkUser(request, result);
    // If no messages then go for it
    if (result.isOk()) {
        List<RealTimeDataPointValue> values = RealTimeDataPointValueCache.instance.getUserView(user);
        ASTNode root = new ASTNode("eq", "xid", xid);
        values = root.accept(new RQLToObjectListQuery<RealTimeDataPointValue>(), values);
        if (values.size() == 0) {
            LOG.debug("Attempted access of Real time point that DNE.");
            result.addRestMessage(HttpStatus.NOT_FOUND, new TranslatableMessage("common.default", "Point doesn't exist or is not enabled."));
            return result.createResponseEntity();
        }
        RealTimeModel model;
        RealTimeDataPointValue value = values.get(0);
        if (value.getDataTypeId() != DataTypes.IMAGE)
            model = new RealTimeModel(value, value.getValue());
        else {
            UriComponentsBuilder imageServletBuilder = UriComponentsBuilder.fromPath("/imageValue/{ts}_{id}.jpg");
            model = new RealTimeModel(value, imageServletBuilder.buildAndExpand(value.getTimestamp(), value.getDataPointId()).toUri());
        }
        return result.createResponseEntity(model);
    } else {
        return result.createResponseEntity();
    }
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) RealTimeDataPointValue(com.serotonin.m2m2.rt.dataImage.RealTimeDataPointValue) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) ASTNode(net.jazdw.rql.parser.ASTNode) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) RealTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.RealTimeModel) RQLToObjectListQuery(com.infiniteautomation.mango.db.query.pojo.RQLToObjectListQuery) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with RealTimeModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.RealTimeModel in project ma-modules-public by infiniteautomation.

the class RealTimeDataRestController method getAll.

/**
 * Get all of the Users Real Time Data
 * @param request
 * @param limit
 * @return
 */
@ApiOperation(value = "List realtime values", notes = "Check the status member to ensure the point is OK not DISABLED or UNRELIABLE")
@RequestMapping(method = RequestMethod.GET, value = "/list", produces = { "application/json" })
public ResponseEntity<List<RealTimeModel>> getAll(HttpServletRequest request, @ApiParam(value = "Limit the number of results", required = false) @RequestParam(value = "limit", required = false, defaultValue = "100") int limit) {
    RestProcessResult<List<RealTimeModel>> result = new RestProcessResult<List<RealTimeModel>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        List<RealTimeDataPointValue> values = RealTimeDataPointValueCache.instance.getUserView(user);
        ASTNode root = new ASTNode("limit", limit);
        values = root.accept(new RQLToObjectListQuery<RealTimeDataPointValue>(), values);
        List<RealTimeModel> models = new ArrayList<RealTimeModel>();
        UriComponentsBuilder imageServletBuilder = UriComponentsBuilder.fromPath("/imageValue/{ts}_{id}.jpg");
        for (RealTimeDataPointValue value : values) {
            if (value.getDataTypeId() == DataTypes.IMAGE) {
                models.add(new RealTimeModel(value, imageServletBuilder.buildAndExpand(value.getTimestamp(), value.getDataPointId()).toUri()));
            } else {
                models.add(new RealTimeModel(value, value.getValue()));
            }
        }
        return result.createResponseEntity(models);
    }
    return result.createResponseEntity();
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) RealTimeDataPointValue(com.serotonin.m2m2.rt.dataImage.RealTimeDataPointValue) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) ASTNode(net.jazdw.rql.parser.ASTNode) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) RealTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.RealTimeModel) RQLToObjectListQuery(com.infiniteautomation.mango.db.query.pojo.RQLToObjectListQuery) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

RQLToObjectListQuery (com.infiniteautomation.mango.db.query.pojo.RQLToObjectListQuery)3 RealTimeDataPointValue (com.serotonin.m2m2.rt.dataImage.RealTimeDataPointValue)3 User (com.serotonin.m2m2.vo.User)3 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)3 RealTimeModel (com.serotonin.m2m2.web.mvc.rest.v1.model.RealTimeModel)3 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)3 ASTNode (net.jazdw.rql.parser.ASTNode)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)3 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 RestMessage (com.serotonin.m2m2.web.mvc.rest.v1.message.RestMessage)1