Search in sources :

Example 1 with AbstractEventDetectorVO

use of com.serotonin.m2m2.vo.event.detector.AbstractEventDetectorVO in project ma-core-public by infiniteautomation.

the class DataPointVO method jsonRead.

@Override
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException {
    // Not reading XID so can't do this: super.jsonRead(reader, jsonObject);
    name = jsonObject.getString("name");
    enabled = jsonObject.getBoolean("enabled");
    String text = jsonObject.getString("loggingType");
    if (text != null) {
        loggingType = LOGGING_TYPE_CODES.getId(text);
        if (loggingType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "loggingType", text, LOGGING_TYPE_CODES.getCodeList());
    }
    text = jsonObject.getString("intervalLoggingPeriodType");
    if (text != null) {
        intervalLoggingPeriodType = Common.TIME_PERIOD_CODES.getId(text);
        if (intervalLoggingPeriodType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "intervalLoggingPeriodType", text, Common.TIME_PERIOD_CODES.getCodeList());
    }
    text = jsonObject.getString("intervalLoggingType");
    if (text != null) {
        intervalLoggingType = INTERVAL_LOGGING_TYPE_CODES.getId(text);
        if (intervalLoggingType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "intervalLoggingType", text, INTERVAL_LOGGING_TYPE_CODES.getCodeList());
    }
    text = jsonObject.getString("purgeType");
    if (text != null) {
        purgeType = Common.TIME_PERIOD_CODES.getId(text);
        if (purgeType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "purgeType", text, Common.TIME_PERIOD_CODES.getCodeList(TimePeriods.MILLISECONDS, TimePeriods.SECONDS, TimePeriods.MINUTES, TimePeriods.HOURS));
    }
    JsonObject locatorJson = jsonObject.getJsonObject("pointLocator");
    if (locatorJson != null)
        reader.readInto(pointLocator, locatorJson);
    JsonArray pedArray = jsonObject.getJsonArray("eventDetectors");
    if (pedArray != null) {
        for (JsonValue jv : pedArray) {
            JsonObject pedObject = jv.toJsonObject();
            String pedXid = pedObject.getString("xid");
            if (StringUtils.isBlank(pedXid))
                throw new TranslatableJsonException("emport.error.ped.missingAttr", "xid");
            // Use the ped xid to lookup an existing ped.
            AbstractEventDetectorVO<?> ped = null;
            for (AbstractPointEventDetectorVO<?> existing : eventDetectors) {
                if (StringUtils.equals(pedXid, existing.getXid())) {
                    ped = existing;
                    break;
                }
            }
            JsonArray handlerXids = pedObject.getJsonArray("handlers");
            if (handlerXids != null)
                for (int k = 0; k < handlerXids.size(); k += 1) {
                    AbstractEventHandlerVO<?> eh = EventHandlerDao.instance.getByXid(handlerXids.getString(k));
                    if (eh == null) {
                        throw new TranslatableJsonException("emport.eventHandler.missing", handlerXids.getString(k));
                    }
                }
            if (ped == null) {
                String typeStr = pedObject.getString("type");
                if (typeStr == null)
                    throw new TranslatableJsonException("emport.error.ped.missingAttr", "type");
                EventDetectorDefinition<?> def = ModuleRegistry.getEventDetectorDefinition(typeStr);
                if (def == null)
                    throw new TranslatableJsonException("emport.error.ped.invalid", "type", typeStr, ModuleRegistry.getEventDetectorDefinitionTypes());
                else {
                    ped = def.baseCreateEventDetectorVO();
                    ped.setDefinition(def);
                }
                // Create a new one
                ped.setId(Common.NEW_ID);
                ped.setXid(pedXid);
                AbstractPointEventDetectorVO<?> dped = (AbstractPointEventDetectorVO<?>) ped;
                dped.njbSetDataPoint(this);
                eventDetectors.add(dped);
            }
            reader.readInto(ped, pedObject);
        }
    }
    text = jsonObject.getString("unit");
    if (text != null) {
        unit = parseUnitString(text, "unit");
        unitString = UnitUtil.formatUcum(unit);
    }
    text = jsonObject.getString("integralUnit");
    if (text != null) {
        useIntegralUnit = true;
        integralUnit = parseUnitString(text, "integralUnit");
        integralUnitString = UnitUtil.formatUcum(integralUnit);
    }
    text = jsonObject.getString("renderedUnit");
    if (text != null) {
        useRenderedUnit = true;
        renderedUnit = parseUnitString(text, "renderedUnit");
        renderedUnitString = UnitUtil.formatUcum(renderedUnit);
    }
    text = jsonObject.getString("plotType");
    if (text != null) {
        plotType = PLOT_TYPE_CODES.getId(text);
        if (plotType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "plotType", text, PLOT_TYPE_CODES.getCodeList());
    }
    // Rollup
    text = jsonObject.getString("rollup");
    if (text != null) {
        rollup = Common.ROLLUP_CODES.getId(text);
        if (rollup == -1)
            throw new TranslatableJsonException("emport.error.chart.invalid", "rollup", text, Common.ROLLUP_CODES.getCodeList());
    }
    // Simplify
    text = jsonObject.getString("simplifyType");
    if (text != null) {
        simplifyType = SIMPLIFY_TYPE_CODES.getId(text);
        if (simplifyType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "simplifyType", text, SIMPLIFY_TYPE_CODES.getCodeList());
    }
    int simplifyTarget = jsonObject.getInt("simplifyTarget", Integer.MIN_VALUE);
    if (simplifyTarget != Integer.MIN_VALUE)
        this.simplifyTarget = simplifyTarget;
    double simplifyTolerance = jsonObject.getDouble("simplifyTolerance", Double.NaN);
    if (simplifyTolerance != Double.NaN)
        this.simplifyTolerance = simplifyTolerance;
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) JsonValue(com.serotonin.json.type.JsonValue) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonObject(com.serotonin.json.type.JsonObject) AbstractEventHandlerVO(com.serotonin.m2m2.vo.event.AbstractEventHandlerVO)

Example 2 with AbstractEventDetectorVO

use of com.serotonin.m2m2.vo.event.detector.AbstractEventDetectorVO in project ma-modules-public by infiniteautomation.

the class EventDetectorRestV2Controller method save.

@ApiOperation(value = "Create an Event Detector", notes = "Cannot already exist, must have data source permission for the point")
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<AbstractEventDetectorModel<?>> save(@ApiParam(value = "Event Detector", required = true) @RequestBody(required = true) AbstractEventDetectorModel<?> model, @AuthenticationPrincipal User user, UriComponentsBuilder builder, HttpServletRequest request) {
    AbstractEventDetectorVO<?> vo = model.getData();
    // Set XID if required
    if (StringUtils.isEmpty(vo.getXid())) {
        vo.setXid(EventDetectorDao.instance.generateUniqueXid());
    }
    // Check to see if it already exists
    AbstractEventDetectorVO<?> existing = this.dao.getByXid(vo.getXid());
    if (existing != null) {
        throw new AlreadyExistsRestException(vo.getXid());
    }
    // Check permission
    DataPointVO dp = DataPointDao.instance.get(vo.getSourceId());
    if (dp == null)
        throw new NotFoundRestException();
    Permissions.ensureDataSourcePermission(user, dp.getDataSourceId());
    // TODO Fix this when we have other types of detectors
    AbstractPointEventDetectorVO<?> ped = (AbstractPointEventDetectorVO<?>) vo;
    ped.njbSetDataPoint(dp);
    // Validate
    ped.ensureValid();
    // Add it to the data point
    DataPointDao.instance.setEventDetectors(dp);
    dp.getEventDetectors().add(ped);
    // Save the data point
    Common.runtimeManager.saveDataPoint(dp);
    // Put a link to the updated data in the header?
    URI location = builder.path("/v2/event-detectors/{xid}").buildAndExpand(vo.getXid()).toUri();
    return getResourceCreated(vo.asModel(), location);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) AlreadyExistsRestException(com.infiniteautomation.mango.rest.v2.exception.AlreadyExistsRestException) URI(java.net.URI) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with AbstractEventDetectorVO

use of com.serotonin.m2m2.vo.event.detector.AbstractEventDetectorVO in project ma-modules-public by infiniteautomation.

the class EventDetectorRestV2Controller method get.

@ApiOperation(value = "Get an Event Detector", notes = "", response = AbstractEventDetectorModel.class, responseContainer = "List")
@RequestMapping(method = RequestMethod.GET, value = "/{xid}")
public ResponseEntity<AbstractEventDetectorModel<?>> get(@AuthenticationPrincipal User user, @ApiParam(value = "Valid Event Detector XID", required = true, allowMultiple = false) @PathVariable String xid, HttpServletRequest request) {
    AbstractEventDetectorVO<?> vo = this.dao.getByXid(xid);
    if (vo == null)
        throw new NotFoundRestException();
    // Check permissions
    if (!user.isAdmin()) {
        DataPointVO dp = DataPointDao.instance.get(vo.getSourceId());
        Permissions.ensureDataPointReadPermission(user, dp);
    }
    return new ResponseEntity<>(vo.asModel(), HttpStatus.OK);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) ResponseEntity(org.springframework.http.ResponseEntity) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with AbstractEventDetectorVO

use of com.serotonin.m2m2.vo.event.detector.AbstractEventDetectorVO in project ma-modules-public by infiniteautomation.

the class EventDetectorRestV2Controller method update.

@ApiOperation(value = "Update an Event Detector", notes = "")
@RequestMapping(method = RequestMethod.PUT, value = { "/{xid}" })
public ResponseEntity<AbstractEventDetectorModel<?>> update(@ApiParam(value = "Valid Event Detector XID", required = true, allowMultiple = false) @PathVariable String xid, @ApiParam(value = "Event Detector", required = true) @RequestBody(required = true) AbstractEventDetectorModel<?> model, @AuthenticationPrincipal User user, UriComponentsBuilder builder, HttpServletRequest request) {
    AbstractEventDetectorVO<?> vo = model.getData();
    // Check to see if it already exists
    AbstractEventDetectorVO<?> existing = this.dao.getByXid(xid);
    if (existing == null) {
        throw new NotFoundRestException();
    } else {
        // Set the ID
        vo.setId(existing.getId());
    }
    // Check permission
    DataPointVO dp = DataPointDao.instance.get(vo.getSourceId());
    if (dp == null)
        throw new NotFoundRestException();
    Permissions.ensureDataSourcePermission(user, dp.getDataSourceId());
    // TODO Fix this when we have other types of detectors
    AbstractPointEventDetectorVO<?> ped = (AbstractPointEventDetectorVO<?>) vo;
    ped.njbSetDataPoint(dp);
    // Validate
    ped.ensureValid();
    // Replace it on the data point, if it isn't replaced we fail.
    boolean replaced = false;
    DataPointDao.instance.setEventDetectors(dp);
    ListIterator<AbstractPointEventDetectorVO<?>> it = dp.getEventDetectors().listIterator();
    while (it.hasNext()) {
        AbstractPointEventDetectorVO<?> ed = it.next();
        if (ed.getId() == ped.getId()) {
            it.set(ped);
            replaced = true;
            break;
        }
    }
    if (!replaced)
        throw new ServerErrorException(new TranslatableMessage("rest.error.eventDetectorNotAssignedToThisPoint"));
    // Save the data point
    Common.runtimeManager.saveDataPoint(dp);
    URI location = builder.path("/v2/event-detectors/{xid}").buildAndExpand(vo.getXid()).toUri();
    return getResourceUpdated(vo.asModel(), location);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) ServerErrorException(com.infiniteautomation.mango.rest.v2.exception.ServerErrorException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) URI(java.net.URI) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with AbstractEventDetectorVO

use of com.serotonin.m2m2.vo.event.detector.AbstractEventDetectorVO 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)

Aggregations

AbstractPointEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)7 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)6 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)4 ArrayList (java.util.ArrayList)3 ResponseEntity (org.springframework.http.ResponseEntity)3 ServerErrorException (com.infiniteautomation.mango.rest.v2.exception.ServerErrorException)2 JsonArray (com.serotonin.json.type.JsonArray)2 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)2 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)2 AbstractEventHandlerVO (com.serotonin.m2m2.vo.event.AbstractEventHandlerVO)2 AbstractEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractEventDetectorVO)2 URI (java.net.URI)2 AlreadyExistsRestException (com.infiniteautomation.mango.rest.v2.exception.AlreadyExistsRestException)1 StringStringPair (com.serotonin.db.pair.StringStringPair)1 JsonException (com.serotonin.json.JsonException)1 JsonObject (com.serotonin.json.type.JsonObject)1 JsonValue (com.serotonin.json.type.JsonValue)1 EventDetectorDao (com.serotonin.m2m2.db.dao.EventDetectorDao)1