Search in sources :

Example 11 with AbstractPointEventDetectorVO

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

the class ImportTask method processUpdatedDetectors.

private void processUpdatedDetectors(Map<String, DataPointVO> eventDetectorMap) {
    for (DataPointVO dpvo : eventDetectorMap.values()) {
        // We can't really guarantee that the import didnt' also ocntain event detectors on the point or that the
        // Data Point VO's we retrieved are still correct, so mix in the detectors to the point out of the database.
        boolean isNew = true;
        DataPointVO saved = DataPointDao.instance.getDataPoint(dpvo.getId(), false);
        DataPointDao.instance.setEventDetectors(saved);
        for (AbstractPointEventDetectorVO<?> eventDetector : dpvo.getEventDetectors()) {
            Iterator<AbstractPointEventDetectorVO<?>> iter = saved.getEventDetectors().iterator();
            while (iter.hasNext()) {
                AbstractPointEventDetectorVO<?> next = iter.next();
                if (eventDetector.getXid().equals(next.getXid())) {
                    // Same detector, replace it
                    eventDetector.setId(next.getId());
                    isNew = false;
                    iter.remove();
                    break;
                }
            }
            // Having removed the old versions, add the new.
            saved.getEventDetectors().add(eventDetector);
        }
        // Save the data point
        Common.runtimeManager.saveDataPoint(saved);
        for (AbstractPointEventDetectorVO<?> modified : dpvo.getEventDetectors()) importContext.addSuccessMessage(isNew, "emport.eventDetector.prefix", modified.getXid());
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)

Example 12 with AbstractPointEventDetectorVO

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

the class DataPointImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    DataPointVO vo = null;
    DataSourceVO<?> dsvo = null;
    if (StringUtils.isBlank(xid))
        xid = ctx.getDataPointDao().generateUniqueXid();
    else
        vo = ctx.getDataPointDao().getDataPoint(xid);
    if (vo == null) {
        // Locate the data source for the point.
        String dsxid = json.getString("dataSourceXid");
        dsvo = ctx.getDataSourceDao().getDataSource(dsxid);
        if (dsvo == null)
            addFailureMessage("emport.dataPoint.badReference", xid);
        else {
            vo = new DataPointVO();
            vo.setXid(xid);
            vo.setDataSourceId(dsvo.getId());
            vo.setDataSourceXid(dsxid);
            vo.setPointLocator(dsvo.createPointLocator());
            vo.setEventDetectors(new ArrayList<AbstractPointEventDetectorVO<?>>(0));
        // Not needed as it will be set via the template or JSON or it exists in the DB already: vo.setTextRenderer(new PlainRenderer());
        }
    }
    if (vo != null) {
        try {
            DataPointPropertiesTemplateVO template = null;
            if (json.containsKey("templateXid")) {
                String templateXid = json.getString("templateXid");
                if (!StringUtils.isEmpty(templateXid))
                    template = (DataPointPropertiesTemplateVO) TemplateDao.instance.getByXid(templateXid);
            }
            // Read into the VO to get all properties
            ctx.getReader().readInto(vo, json);
            // Override the settings if we need to
            if (template != null) {
                template.updateDataPointVO(vo);
            }
            // If the name is not provided, default to the XID
            if (StringUtils.isBlank(vo.getName()))
                vo.setName(xid);
            // If the chart colour is null provide default of '' to handle legacy code that sets colour to null
            if (vo.getChartColour() == null)
                vo.setChartColour("");
            // Now validate it. Use a new response object so we can distinguish errors in this vo from
            // other errors.
            ProcessResult voResponse = new ProcessResult();
            vo.validate(voResponse);
            if (voResponse.getHasMessages())
                setValidationMessages(voResponse, "emport.dataPoint.prefix", xid);
            else {
                // We will always override the DS Info with the one from the XID Lookup
                dsvo = ctx.getDataSourceDao().getDataSource(vo.getDataSourceXid());
                if (dsvo == null)
                    addFailureMessage("emport.dataPoint.badReference", xid);
                else {
                    // Compare this point to the existing point in DB to ensure
                    // that we aren't moving a point to a different type of Data Source
                    DataPointVO oldPoint = ctx.getDataPointDao().getDataPoint(vo.getId(), false);
                    // Does the old point have a different data source?
                    if (oldPoint != null && (oldPoint.getDataSourceId() != dsvo.getId())) {
                        vo.setDataSourceId(dsvo.getId());
                        vo.setDataSourceName(dsvo.getName());
                    }
                }
                boolean isNew = vo.isNew();
                try {
                    if (Common.runtimeManager.getState() == RuntimeManager.RUNNING) {
                        Common.runtimeManager.saveDataPoint(vo);
                        if (hierarchyList != null && json.containsKey(PATH)) {
                            String path = json.getString(PATH);
                            if (StringUtils.isNotEmpty(path))
                                hierarchyList.add(new DataPointSummaryPathPair(new DataPointSummary(vo), path));
                        }
                        addSuccessMessage(isNew, "emport.dataPoint.prefix", xid);
                    } else {
                        addFailureMessage(new ProcessMessage("Runtime Manager not running point with xid: " + xid + " not saved."));
                    }
                } catch (LicenseViolatedException e) {
                    addFailureMessage(new ProcessMessage(e.getErrorMessage()));
                }
            }
        } catch (TranslatableJsonException e) {
            addFailureMessage("emport.dataPoint.prefix", xid, e.getMsg());
        } catch (JsonException e) {
            addFailureMessage("emport.dataPoint.prefix", xid, getJsonExceptionMessage(e));
        }
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) DataPointSummary(com.serotonin.m2m2.vo.DataPointSummary) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) LicenseViolatedException(com.serotonin.m2m2.LicenseViolatedException) DataPointPropertiesTemplateVO(com.serotonin.m2m2.vo.template.DataPointPropertiesTemplateVO) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) ProcessMessage(com.serotonin.m2m2.i18n.ProcessMessage)

Example 13 with AbstractPointEventDetectorVO

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

the class InternalDataSourceRT method createMonitorPoint.

private void createMonitorPoint(ValueMonitor<?> monitor) {
    DataPointVO dpvo = new DataPointVO();
    InternalPointLocatorVO plvo = new InternalPointLocatorVO();
    plvo.setMonitorId(monitor.getId());
    dpvo.setPointLocator(plvo);
    dpvo.setDataSourceId(vo.getId());
    dpvo.setDataSourceTypeName(vo.getDefinition().getDataSourceTypeName());
    dpvo.setEventDetectors(new ArrayList<AbstractPointEventDetectorVO<?>>(0));
    dpvo.setEnabled(true);
    dpvo.setDeviceName(vo.getName());
    // Logging types are usually going to be ON_CHANGE, INTERVAL (MAXIMUM), INTERVAL (INSTANT) AND INTERVAL (MINIMUM)
    if (monitor.getId().startsWith("com.serotonin.m2m2.rt.dataSource.PollingDataSource")) {
        // Defaults for polling data source metrics
        TranslatableMessage name;
        if (monitor.getId().contains("SUCCESS")) {
            dpvo.setLoggingType(DataPointVO.LoggingTypes.ON_CHANGE);
            name = new TranslatableMessage("dsEdit.internal.autoCreate.names.pollingSuccess");
            dpvo.setRollup(Rollups.MAXIMUM);
            dpvo.setTextRenderer(getIntegerAnalogSuffixRenderer(""));
        } else {
            dpvo.setLoggingType(DataPointVO.LoggingTypes.INTERVAL);
            dpvo.setIntervalLoggingPeriod(5);
            dpvo.setIntervalLoggingPeriodType(TimePeriods.MINUTES);
            dpvo.setIntervalLoggingType(DataPointVO.IntervalLoggingTypes.MAXIMUM);
            if (monitor.getId().contains("PERCENTAGE")) {
                name = new TranslatableMessage("dsEdit.internal.autoCreate.names.pollingPercentage");
                dpvo.setRollup(Rollups.AVERAGE);
                dpvo.setTextRenderer(getIntegerAnalogSuffixRenderer("%"));
            } else {
                // must be duration
                name = new TranslatableMessage("dsEdit.internal.autoCreate.names.pollingDuration");
                dpvo.setRollup(Rollups.MAXIMUM);
                dpvo.setTextRenderer(getIntegerAnalogSuffixRenderer(" ms"));
            }
        }
        // Set the device name base on the XID in the monitor ID....
        String dsXid = monitor.getId().substring(monitor.getId().indexOf('_') + 1, monitor.getId().lastIndexOf('_'));
        defaultNewPointToDataSource(dpvo, dsXid);
        dpvo.setName(name.translate(Common.getTranslations()));
    } else if (monitor.getId().startsWith("com.serotonin.m2m2.persistent")) {
        // Defaults for persistent metrics
        // com.serotonin.m2m2.persistent.
        int dsXidIndex = 30;
        TranslatableMessage name;
        dpvo.setLoggingType(DataPointVO.LoggingTypes.INTERVAL);
        dpvo.setIntervalLoggingPeriod(5);
        dpvo.setIntervalLoggingPeriodType(TimePeriods.MINUTES);
        if (monitor.getId().contains("TOTAL_CONNECTION_TIME_MONITOR")) {
            dpvo.setIntervalLoggingType(DataPointVO.IntervalLoggingTypes.MINIMUM);
            name = new TranslatableMessage("dsEdit.internal.autoCreate.names.persistentConnectionTime");
            dsXidIndex += 30;
        } else if (monitor.getId().contains("CONNECTED_POINTS_MONITOR")) {
            dpvo.setIntervalLoggingType(DataPointVO.IntervalLoggingTypes.MINIMUM);
            name = new TranslatableMessage("dsEdit.internal.autoCreate.names.persistentConnectedPoints");
            dsXidIndex += 25;
        } else if (monitor.getId().contains("TOTAL_CONNECTIONS_MONITOR")) {
            dpvo.setIntervalLoggingType(DataPointVO.IntervalLoggingTypes.MAXIMUM);
            name = new TranslatableMessage("dsEdit.internal.autoCreate.names.persistentTotalConnections");
            dsXidIndex += 26;
        } else if (monitor.getId().contains("TOTAL_TIMEOUTS_MONITOR")) {
            dpvo.setIntervalLoggingType(DataPointVO.IntervalLoggingTypes.MAXIMUM);
            name = new TranslatableMessage("dsEdit.internal.autoCreate.names.persistentTimeouts");
            dsXidIndex += 23;
        } else if (monitor.getId().contains("RECIEVING_RATE_MONITOR")) {
            dpvo.setIntervalLoggingType(DataPointVO.IntervalLoggingTypes.AVERAGE);
            name = new TranslatableMessage("dsEdit.internal.autoCreate.names.persistentReceiveRate");
            dsXidIndex += 23;
        } else {
            // Nothing should get here currently...
            dpvo.setIntervalLoggingType(DataPointVO.IntervalLoggingTypes.INSTANT);
            name = new TranslatableMessage("common.literal", monitor.getId());
        }
        // Set the device name base on the XID in the monitor ID....
        if (dsXidIndex > 30) {
            String dsXid = monitor.getId().substring(dsXidIndex);
            defaultNewPointToDataSource(dpvo, dsXid);
        } else {
            // Will happen if new properties are added because the XID scheme isn't great.
            dpvo.setDeviceName(monitor.getId());
        }
        dpvo.setName(name.translate(Common.getTranslations()));
    } else {
        // Default others, including InternalPointLocatorRT.MONITOR_NAMES to ON_CHANGE
        dpvo.setLoggingType(DataPointVO.LoggingTypes.ON_CHANGE);
        dpvo.setDeviceName(vo.getName());
        dpvo.setName(plvo.getConfigurationDescription().translate(Common.getTranslations()));
    }
    dpvo.defaultTextRenderer();
    dpvo.setXid(Common.generateXid("DP_In_"));
    monitorMap.put(monitor.getId(), true);
    // Won't appear until next poll, but that's fine.
    Common.runtimeManager.saveDataPoint(dpvo);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 14 with AbstractPointEventDetectorVO

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

the class EventDetectorRestV2Controller method getForDataPoint.

@ApiOperation(value = "Get all Event Detectors for a given data point", notes = "", response = AbstractEventDetectorModel.class, responseContainer = "List")
@RequestMapping(method = RequestMethod.GET, value = "/data-point/{xid}")
public ResponseEntity<List<AbstractEventDetectorModel<?>>> getForDataPoint(@AuthenticationPrincipal User user, @ApiParam(value = "Valid Data Point XID", required = true, allowMultiple = false) @PathVariable String xid, HttpServletRequest request) {
    DataPointVO dp = DataPointDao.instance.getByXid(xid);
    if (dp == null)
        throw new NotFoundRestException();
    // Check permissions
    if (!user.isAdmin())
        Permissions.ensureDataPointReadPermission(user, dp);
    DataPointDao.instance.setEventDetectors(dp);
    List<AbstractEventDetectorModel<?>> models = new ArrayList<AbstractEventDetectorModel<?>>();
    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 15 with AbstractPointEventDetectorVO

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

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