Search in sources :

Example 6 with AbstractEventDetectorModel

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

the class EventDetectorRestV2Controller method queryRQL.

@ApiOperation(value = "Query Event Detectors", notes = "Use RQL formatted query, filtered by data point permissions", response = AbstractEventDetectorModel.class, responseContainer = "List")
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<QueryDataPageStream<AbstractEventDetectorVO<?>>> queryRQL(@AuthenticationPrincipal User user, HttpServletRequest request) {
    ASTNode node = parseRQLtoAST(request.getQueryString());
    if (user.isAdmin()) {
        // admin users don't need to filter the results
        return new ResponseEntity<>(getPageStream(node), HttpStatus.OK);
    } else {
        EventDetectorStreamCallback callback = new EventDetectorStreamCallback(this, user);
        FilteredPageQueryStream<AbstractEventDetectorVO<?>, AbstractEventDetectorModel<?>, EventDetectorDao> stream = new FilteredPageQueryStream<AbstractEventDetectorVO<?>, AbstractEventDetectorModel<?>, EventDetectorDao>(EventDetectorDao.instance, this, node, callback);
        stream.setupQuery();
        return new ResponseEntity<>(stream, HttpStatus.OK);
    }
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) EventDetectorStreamCallback(com.serotonin.m2m2.web.mvc.rest.v1.model.eventDetector.EventDetectorStreamCallback) AbstractEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractEventDetectorVO) ASTNode(net.jazdw.rql.parser.ASTNode) AbstractEventDetectorModel(com.serotonin.m2m2.web.mvc.rest.v1.model.events.detectors.AbstractEventDetectorModel) EventDetectorDao(com.serotonin.m2m2.db.dao.EventDetectorDao) FilteredPageQueryStream(com.serotonin.m2m2.web.mvc.rest.v1.model.FilteredPageQueryStream) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with AbstractEventDetectorModel

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

the class EventDetectorRestV2Controller method delete.

@ApiOperation(value = "Delete an Event Detector", notes = "")
@RequestMapping(method = RequestMethod.DELETE, value = { "/{xid}" })
public ResponseEntity<AbstractEventDetectorModel<?>> delete(@ApiParam(value = "Valid Event Detector XID", required = true, allowMultiple = false) @PathVariable String xid, @AuthenticationPrincipal User user, UriComponentsBuilder builder, HttpServletRequest request) {
    // Check to see if it already exists
    AbstractEventDetectorVO<?> existing = this.dao.getByXid(xid);
    if (existing == null) {
        throw new NotFoundRestException();
    }
    // Check permission
    DataPointVO dp = DataPointDao.instance.get(existing.getSourceId());
    Permissions.ensureDataSourcePermission(user, dp.getDataSourceId());
    // TODO Fix this when we have other types of detectors
    AbstractPointEventDetectorVO<?> ped = (AbstractPointEventDetectorVO<?>) existing;
    ped.njbSetDataPoint(dp);
    // Remove it from the data point, if it isn't replaced we fail.
    boolean removed = false;
    DataPointDao.instance.setEventDetectors(dp);
    ListIterator<AbstractPointEventDetectorVO<?>> it = dp.getEventDetectors().listIterator();
    while (it.hasNext()) {
        AbstractPointEventDetectorVO<?> ed = it.next();
        if (ed.getId() == ped.getId()) {
            it.remove();
            removed = true;
            break;
        }
    }
    if (!removed)
        throw new ServerErrorException(new TranslatableMessage("rest.error.eventDetectorNotAssignedToThisPoint"));
    // Save the data point
    Common.runtimeManager.saveDataPoint(dp);
    return new ResponseEntity<>(existing.asModel(), HttpStatus.OK);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) ResponseEntity(org.springframework.http.ResponseEntity) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) ServerErrorException(com.infiniteautomation.mango.rest.v2.exception.ServerErrorException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with AbstractEventDetectorModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.events.detectors.AbstractEventDetectorModel in project ma-core-public by infiniteautomation.

the class AbstractEventDetectorModelDeserializer method deserialize.

/* (non-Javadoc)
	 * @see com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext)
	 */
@Override
public AbstractEventDetectorModel<?> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    ObjectMapper mapper = (ObjectMapper) jp.getCodec();
    JsonNode tree = jp.readValueAsTree();
    String typeName = tree.get("detectorType").asText();
    EventDetectorDefinition<?> def = ModuleRegistry.getEventDetectorDefinition(typeName);
    if (def == null)
        throw new ModelNotFoundException(typeName);
    AbstractEventDetectorModel<?> model = (AbstractEventDetectorModel<?>) mapper.treeToValue(tree, def.getModelClass());
    model.setDefinition(def);
    return model;
}
Also used : ModelNotFoundException(com.serotonin.m2m2.web.mvc.rest.v1.exception.ModelNotFoundException) JsonNode(com.fasterxml.jackson.databind.JsonNode) AbstractEventDetectorModel(com.serotonin.m2m2.web.mvc.rest.v1.model.events.detectors.AbstractEventDetectorModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ApiOperation (com.wordnik.swagger.annotations.ApiOperation)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)6 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)6 ResponseEntity (org.springframework.http.ResponseEntity)5 AbstractEventDetectorModel (com.serotonin.m2m2.web.mvc.rest.v1.model.events.detectors.AbstractEventDetectorModel)4 AbstractPointEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)3 ServerErrorException (com.infiniteautomation.mango.rest.v2.exception.ServerErrorException)2 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 AlreadyExistsRestException (com.infiniteautomation.mango.rest.v2.exception.AlreadyExistsRestException)1 EventDetectorDao (com.serotonin.m2m2.db.dao.EventDetectorDao)1 AbstractEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractEventDetectorVO)1 ModelNotFoundException (com.serotonin.m2m2.web.mvc.rest.v1.exception.ModelNotFoundException)1 FilteredPageQueryStream (com.serotonin.m2m2.web.mvc.rest.v1.model.FilteredPageQueryStream)1 EventDetectorStreamCallback (com.serotonin.m2m2.web.mvc.rest.v1.model.eventDetector.EventDetectorStreamCallback)1 ASTNode (net.jazdw.rql.parser.ASTNode)1