Search in sources :

Example 1 with AbstractPointEventDetectorVO

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

the class EventDetectorRestV2Controller method getForDataSource.

@ApiOperation(value = "Get all Event Detectors for a given data source", notes = "Must have permission for all data points", response = AbstractEventDetectorModel.class, responseContainer = "List")
@RequestMapping(method = RequestMethod.GET, value = "/data-source/{xid}")
public ResponseEntity<List<AbstractEventDetectorModel<?>>> getForDataSource(@AuthenticationPrincipal User user, @ApiParam(value = "Valid Data Source XID", required = true, allowMultiple = false) @PathVariable String xid, HttpServletRequest request) {
    DataSourceVO<?> ds = DataSourceDao.instance.getByXid(xid);
    if (ds == null)
        throw new NotFoundRestException();
    List<DataPointVO> points = DataPointDao.instance.getDataPoints(ds.getId(), null, false);
    List<AbstractEventDetectorModel<?>> models = new ArrayList<AbstractEventDetectorModel<?>>();
    for (DataPointVO dp : points) {
        // Check permissions
        if (!user.isAdmin())
            Permissions.ensureDataPointReadPermission(user, dp);
        DataPointDao.instance.setEventDetectors(dp);
        for (AbstractPointEventDetectorVO<?> ped : dp.getEventDetectors()) models.add(ped.asModel());
    }
    return new ResponseEntity<>(models, HttpStatus.OK);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) ResponseEntity(org.springframework.http.ResponseEntity) ArrayList(java.util.ArrayList) AbstractEventDetectorModel(com.serotonin.m2m2.web.mvc.rest.v1.model.events.detectors.AbstractEventDetectorModel) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with AbstractPointEventDetectorVO

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

use of com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO 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 4 with AbstractPointEventDetectorVO

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

the class InternalMenuItem method maybeCreatePoints.

/**
 */
private void maybeCreatePoints(boolean safe, DataSourceVO<?> vo) {
    Map<String, ValueMonitor<?>> monitors = getAllHomePageMonitors();
    Iterator<String> it = monitors.keySet().iterator();
    while (it.hasNext()) {
        String xid = it.next();
        ValueMonitor<?> monitor = monitors.get(xid);
        if (monitor != null) {
            DataPointVO dp = DataPointDao.instance.getByXid(xid);
            if (dp == null) {
                InternalPointLocatorVO pl = new InternalPointLocatorVO();
                pl.setMonitorId(monitor.getId());
                dp = new DataPointVO();
                dp.setXid(xid);
                dp.setName(monitor.getName().translate(Common.getTranslations()));
                dp.setDataSourceId(vo.getId());
                dp.setDeviceName(vo.getName());
                dp.setEventDetectors(new ArrayList<AbstractPointEventDetectorVO<?>>(0));
                dp.defaultTextRenderer();
                dp.setEnabled(true);
                dp.setChartColour("");
                dp.setPointLocator(pl);
                // Use default template
                DataPointPropertiesTemplateVO template = TemplateDao.instance.getDefaultDataPointTemplate(pl.getDataTypeId());
                if (template != null) {
                    template.updateDataPointVO(dp);
                    dp.setTemplateId(template.getId());
                }
                // If we are numeric then we want to log on change
                switch(pl.getDataTypeId()) {
                    case DataTypes.NUMERIC:
                        if (SYSTEM_UPTIME_POINT_XID.equals(xid)) {
                            // This changes every time, so just do an interval instant
                            dp.setLoggingType(LoggingTypes.INTERVAL);
                            dp.setIntervalLoggingPeriodType(Common.TimePeriods.MINUTES);
                            dp.setIntervalLoggingPeriod(5);
                            dp.setIntervalLoggingType(DataPointVO.IntervalLoggingTypes.INSTANT);
                        } else {
                            // Setup to Log on Change
                            dp.setLoggingType(LoggingTypes.ON_CHANGE);
                        }
                        if (dp.getTextRenderer() instanceof AnalogRenderer && !dp.getXid().equals(SYSTEM_UPTIME_POINT_XID)) {
                            // This are count points, no need for decimals.
                            ((AnalogRenderer) dp.getTextRenderer()).setFormat("0");
                        }
                        // No template in use here
                        dp.setTemplateId(null);
                        break;
                }
                ProcessResult result = new ProcessResult();
                dp.validate(result);
                if (!result.getHasMessages()) {
                    if (safe)
                        DataPointDao.instance.saveDataPoint(dp);
                    else
                        Common.runtimeManager.saveDataPoint(dp);
                } else {
                    for (ProcessMessage message : result.getMessages()) {
                        LOG.error(message.toString(Common.getTranslations()));
                    }
                }
            }
        }
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) DataPointPropertiesTemplateVO(com.serotonin.m2m2.vo.template.DataPointPropertiesTemplateVO) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) AnalogRenderer(com.serotonin.m2m2.view.text.AnalogRenderer) ProcessMessage(com.serotonin.m2m2.i18n.ProcessMessage) ValueMonitor(com.infiniteautomation.mango.monitor.ValueMonitor)

Example 5 with AbstractPointEventDetectorVO

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

Aggregations

DataPointVO (com.serotonin.m2m2.vo.DataPointVO)21 AbstractPointEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)17 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)8 ArrayList (java.util.ArrayList)7 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)6 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)5 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)5 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)3 User (com.serotonin.m2m2.vo.User)3 DataPointPropertiesTemplateVO (com.serotonin.m2m2.vo.template.DataPointPropertiesTemplateVO)3 ResponseEntity (org.springframework.http.ResponseEntity)3 ServerErrorException (com.infiniteautomation.mango.rest.v2.exception.ServerErrorException)2 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)2 JsonException (com.serotonin.json.JsonException)2 JsonArray (com.serotonin.json.type.JsonArray)2 LicenseViolatedException (com.serotonin.m2m2.LicenseViolatedException)2 ProcessMessage (com.serotonin.m2m2.i18n.ProcessMessage)2 DataSourceVO (com.serotonin.m2m2.vo.dataSource.DataSourceVO)2