Search in sources :

Example 41 with DataPointWithEventDetectors

use of com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors in project ma-core-public by infiniteautomation.

the class DataPointService method setDataPointState.

/**
 * Set the state helper method
 * @param vo - The point to restart
 * @param enabled - Enable or disable the data point
 * @param restart - Restart the data point, enabled must equal true (will start a stopped point)
 * @return - true if the state changed
 */
protected boolean setDataPointState(DataPointVO vo, boolean enabled, boolean restart) {
    vo.setEnabled(enabled);
    boolean dataSourceRunning = getRuntimeManager().isDataSourceRunning(vo.getDataSourceId());
    if (!dataSourceRunning) {
        // We must check its state in the DB
        boolean enabledInDB = dao.isEnabled(vo.getId());
        if (enabledInDB && !enabled) {
            dao.saveEnabledColumn(vo);
            return true;
        } else if (!enabledInDB && enabled) {
            dao.saveEnabledColumn(vo);
            return true;
        }
    } else {
        boolean running = getRuntimeManager().isDataPointRunning(vo.getId());
        if (running && !enabled) {
            // Running, so stop it
            getRuntimeManager().stopDataPoint(vo.getId());
            dao.saveEnabledColumn(vo);
            return true;
        } else if (!running && enabled) {
            // Not running, so start it
            List<AbstractPointEventDetectorVO> detectors = eventDetectorDao.getWithSource(vo.getId(), vo);
            DataPointWithEventDetectors dp = new DataPointWithEventDetectors(vo, detectors);
            dao.saveEnabledColumn(vo);
            getRuntimeManager().startDataPoint(dp);
            return true;
        } else if (enabled && restart) {
            // May be running or not, will either start or restart it (stopping a non running point will do nothing which is ok)
            getRuntimeManager().stopDataPoint(vo.getId());
            List<AbstractPointEventDetectorVO> detectors = eventDetectorDao.getWithSource(vo.getId(), vo);
            DataPointWithEventDetectors dp = new DataPointWithEventDetectors(vo, detectors);
            getRuntimeManager().startDataPoint(dp);
            return false;
        }
    }
    return false;
}
Also used : AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) DataPointWithEventDetectors(com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors) List(java.util.List) ArrayList(java.util.ArrayList)

Example 42 with DataPointWithEventDetectors

use of com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors in project ma-core-public by infiniteautomation.

the class DataPointService method insert.

@Override
public DataPointVO insert(DataPointVO vo) throws PermissionException, ValidationException {
    PermissionHolder user = Common.getUser();
    // Ensure they can create
    ensureCreatePermission(user, vo);
    // Ensure id is not set
    if (vo.getId() != Common.NEW_ID) {
        ProcessResult result = new ProcessResult();
        result.addContextualMessage("id", "validate.invalidValue");
        throw new ValidationException(result);
    }
    // Generate an Xid if necessary
    if (StringUtils.isEmpty(vo.getXid()))
        vo.setXid(dao.generateUniqueXid());
    for (DataPointChangeDefinition def : changeDefinitions) {
        def.preInsert(vo);
    }
    ensureValid(vo);
    dao.insert(vo);
    List<AbstractPointEventDetectorVO> detectors = new ArrayList<>();
    for (DataPointChangeDefinition def : changeDefinitions) {
        for (var detector : def.postInsert(vo)) {
            if (detector.isNew()) {
                log.warn("Detector added via postInsert hook was not saved");
            } else if (detector.getDataPoint().getId() != vo.getId()) {
                log.warn("Detector added via postInsert hook was for a different data point");
            } else {
                detectors.add(detector);
            }
        }
    }
    if (vo.isEnabled()) {
        // the data point cannot have detectors if it was just inserted, don't query for detectors
        getRuntimeManager().startDataPoint(new DataPointWithEventDetectors(vo, detectors));
    }
    return vo;
}
Also used : DataPointChangeDefinition(com.serotonin.m2m2.module.definitions.dataPoint.DataPointChangeDefinition) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) ArrayList(java.util.ArrayList) DataPointWithEventDetectors(com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder)

Example 43 with DataPointWithEventDetectors

use of com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors in project ma-core-public by infiniteautomation.

the class EmailHandlerRT method sendEmail.

private static void sendEmail(EventInstance evt, NotificationType notificationType, Set<String> addresses, String baseSubject, boolean includeSystemInfo, int pointValueCount, boolean includeLogs, String handlerXid, String customTemplate, List<IntStringPair> additionalContext, String script, SetCallback setCallback, ScriptPermissions permissions) {
    if (evt.getEventType().isSystemMessage()) {
        if (((SystemEventType) evt.getEventType()).getSystemEventType().equals(SystemEventType.TYPE_EMAIL_SEND_FAILURE)) {
            // Don't send email notifications about email send failures.
            LOG.info("Not sending email for event raised due to email failure");
            return;
        }
    }
    Translations translations = Common.getTranslations();
    if (StringUtils.isBlank(baseSubject)) {
        // Just set the subject to the message
        baseSubject = evt.getMessage().translate(translations);
        // Strip out the HTML and the &nbsp
        baseSubject = StringEscapeUtils.unescapeHtml4(baseSubject);
        // Since we have <br/> in the code and that isn't proper HTML we need to remove it by hand
        baseSubject = baseSubject.replace("<br/>", "\n");
    }
    // end if alias was blank
    // Determine the subject to use.
    TranslatableMessage subjectMsg;
    TranslatableMessage notifTypeMsg = new TranslatableMessage(notificationType.getKey());
    if (StringUtils.isBlank(baseSubject)) {
        // Make these more descriptive
        if (evt.getId() == Common.NEW_ID)
            subjectMsg = new TranslatableMessage("ftl.subject.default", notifTypeMsg);
        else
            subjectMsg = new TranslatableMessage("ftl.subject.default.id", notifTypeMsg, evt.getId());
    } else {
        if (evt.getId() == Common.NEW_ID)
            subjectMsg = new TranslatableMessage("ftl.subject.alias", baseSubject, notifTypeMsg);
        else
            subjectMsg = new TranslatableMessage("ftl.subject.alias.id", baseSubject, notifTypeMsg, evt.getId());
    }
    String alarmLevel = evt.getAlarmLevel().getDescription().translate(translations);
    String subject = alarmLevel + " - " + subjectMsg.translate(translations);
    // Trim the subject if its too long
    if (subject.length() > 200)
        subject = subject.substring(0, 200);
    try {
        String[] toAddrs;
        if (addresses == null)
            toAddrs = new String[0];
        else
            toAddrs = addresses.toArray(new String[0]);
        UsedImagesDirective inlineImages = new UsedImagesDirective();
        // Send the email.
        Map<String, Object> model = new HashMap<String, Object>();
        model.put(EventInstance.CONTEXT_KEY, evt);
        model.put(EventInstanceWrapper.CONTEXT_KEY, new EventInstanceWrapper(evt));
        if (evt.getContext() != null)
            model.putAll(evt.getContext());
        model.put("img", inlineImages);
        model.put("instanceDescription", SystemSettingsDao.getInstance().getValue(SystemSettingsDao.INSTANCE_DESCRIPTION));
        if (includeSystemInfo) {
            // Get the Work Items
            List<WorkItemInfo> highPriorityWorkItems = Common.backgroundProcessing.getHighPriorityServiceItems();
            model.put("highPriorityWorkItems", highPriorityWorkItems);
            List<WorkItemInfo> mediumPriorityWorkItems = Common.backgroundProcessing.getMediumPriorityServiceQueueItems();
            model.put("mediumPriorityWorkItems", mediumPriorityWorkItems);
            List<WorkItemInfo> lowPriorityWorkItems = Common.backgroundProcessing.getLowPriorityServiceQueueItems();
            model.put("lowPriorityWorkItems", lowPriorityWorkItems);
            model.put("threadList", getThreadsList());
        }
        int type = SystemSettingsDao.getInstance().getIntValue(SystemSettingsDao.EMAIL_CONTENT_TYPE);
        // If we are a point event then add the value
        if (evt.getEventType() instanceof DataPointEventType) {
            DataPointVO dp = (DataPointVO) evt.getContext().get("point");
            if (dp != null) {
                DataPointRT rt = Common.runtimeManager.getDataPoint(dp.getId());
                if (rt != null) {
                    List<PointValueTime> pointValues = null;
                    if (pointValueCount > 0)
                        pointValues = rt.getLatestPointValues(pointValueCount);
                    if ((pointValues != null) && (pointValues.size() > 0)) {
                        if (type == MangoEmailContent.CONTENT_TYPE_HTML || type == MangoEmailContent.CONTENT_TYPE_BOTH) {
                            List<RenderedPointValueTime> renderedPointValues = new ArrayList<RenderedPointValueTime>();
                            for (PointValueTime pvt : pointValues) {
                                RenderedPointValueTime rpvt = new RenderedPointValueTime();
                                rpvt.setValue(Functions.getHtmlText(rt.getVO(), pvt));
                                rpvt.setTime(Functions.getFullSecondTime(pvt.getTime()));
                                renderedPointValues.add(rpvt);
                            }
                            model.put("renderedHtmlPointValues", renderedPointValues);
                        }
                        if (type == MangoEmailContent.CONTENT_TYPE_TEXT || type == MangoEmailContent.CONTENT_TYPE_BOTH) {
                            List<RenderedPointValueTime> renderedPointValues = new ArrayList<RenderedPointValueTime>();
                            for (PointValueTime pvt : pointValues) {
                                RenderedPointValueTime rpvt = new RenderedPointValueTime();
                                rpvt.setValue(Functions.getRenderedText(rt.getVO(), pvt));
                                rpvt.setTime(Functions.getFullSecondTime(pvt.getTime()));
                                renderedPointValues.add(rpvt);
                            }
                            model.put("renderedPointValues", renderedPointValues);
                        }
                    }
                }
            }
        }
        // Build the additional context for the email model
        if (additionalContext == null || pointValueCount <= 0)
            model.put("additionalContext", new HashMap<>(0));
        else {
            Map<String, EmailPointWrapper> context = new HashMap<>();
            for (IntStringPair pair : additionalContext) {
                EmailPointWrapper point;
                DataPointRT rt = Common.runtimeManager.getDataPoint(pair.getKey());
                List<PointValueTime> pointValues;
                List<RenderedPointValueTime> renderedPointValues;
                DataPointVO dpvo;
                if (rt != null) {
                    dpvo = rt.getVO();
                    point = new EmailPointWrapper(dpvo);
                    pointValues = rt.getLatestPointValues(pointValueCount);
                    renderedPointValues = new ArrayList<RenderedPointValueTime>();
                    if (pointValues != null && pointValues.size() > 0)
                        for (PointValueTime pvt : pointValues) {
                            RenderedPointValueTime rpvt = new RenderedPointValueTime();
                            rpvt.setValue(Functions.getRenderedText(rt.getVO(), pvt));
                            rpvt.setTime(Functions.getFullSecondTime(pvt.getTime()));
                            renderedPointValues.add(rpvt);
                        }
                } else {
                    dpvo = DataPointDao.getInstance().get(pair.getKey());
                    if (dpvo == null)
                        continue;
                    point = new EmailPointWrapper(dpvo);
                    pointValues = Common.getBean(PointValueDao.class).getLatestPointValues(dpvo, pointValueCount);
                    renderedPointValues = new ArrayList<RenderedPointValueTime>();
                    for (PointValueTime pvt : pointValues) {
                        RenderedPointValueTime rpvt = new RenderedPointValueTime();
                        rpvt.setValue(Functions.getRenderedText(dpvo, pvt));
                        rpvt.setTime(Functions.getFullSecondTime(pvt.getTime()));
                        renderedPointValues.add(rpvt);
                    }
                }
                point.setRawValues(pointValues);
                point.setValues(renderedPointValues);
                point.setContextKey(pair.getValue());
                context.put(pair.getValue(), point);
            }
            model.put("additionalContext", context);
        }
        if (!StringUtils.isEmpty(script)) {
            // Okay, a script is defined, let's pass it the model so that it may add to it
            Map<String, Object> modelContext = new HashMap<String, Object>();
            modelContext.put("model", model);
            Map<String, IDataPointValueSource> context = new HashMap<String, IDataPointValueSource>();
            for (IntStringPair pair : additionalContext) {
                DataPointRT dprt = Common.runtimeManager.getDataPoint(pair.getKey());
                if (dprt == null) {
                    DataPointVO targetVo = DataPointDao.getInstance().get(pair.getKey());
                    if (targetVo == null) {
                        LOG.warn("Additional context point with ID: " + pair.getKey() + " and context name " + pair.getValue() + " could not be found.");
                        // Not worth aborting the email, just warn it
                        continue;
                    }
                    if (targetVo.getDefaultCacheSize() == 0)
                        targetVo.setDefaultCacheSize(1);
                    DataPointWithEventDetectors dp = new DataPointWithEventDetectors(targetVo, new ArrayList<>());
                    DataSourceRT<? extends DataSourceVO> dataSource = DataSourceDao.getInstance().get(targetVo.getDataSourceId()).createDataSourceRT();
                    dprt = new DataPointRT(dp, targetVo.getPointLocator().createRuntime(), dataSource, null, Common.getBean(PointValueDao.class), Common.getBean(PointValueCache.class));
                }
                context.put(pair.getValue(), dprt);
            }
            modelContext.put(DO_NOT_SEND_KEY, MangoJavaScriptService.UNCHANGED);
            List<JsonImportExclusion> importExclusions = new ArrayList<JsonImportExclusion>(1);
            importExclusions.add(new JsonImportExclusion("xid", handlerXid) {

                @Override
                public String getImporterType() {
                    return ConfigurationExportData.EVENT_HANDLERS;
                }
            });
            try (ScriptLog scriptLog = new ScriptLog("emailScript-" + evt.getId())) {
                MangoJavaScriptService service = Common.getBean(MangoJavaScriptService.class);
                long time = evt.isActive() || !evt.isRtnApplicable() ? evt.getActiveTimestamp() : evt.getRtnTimestamp();
                CompiledMangoJavaScript compiledScript = new CompiledMangoJavaScript(setCallback, scriptLog, modelContext, null, importExclusions, false, service, permissions);
                compiledScript.compile(script, true);
                compiledScript.initialize(context);
                MangoJavaScriptResult r = compiledScript.execute(Common.timer.currentTimeMillis(), time, DataType.ALPHANUMERIC);
                PointValueTime result = (PointValueTime) r.getResult();
                if (// The script cancelled the email
                result != null && result.getValue() == MangoJavaScriptService.UNCHANGED)
                    return;
            } catch (ScriptError | ResultTypeException e) {
                LOG.error("Exception running email handler script: " + e.getTranslatableMessage(), e);
            }
        }
        MangoEmailContent content;
        if (StringUtils.isEmpty(customTemplate))
            content = new MangoEmailContent(notificationType.getFile(), model, translations, subject, StandardCharsets.UTF_8);
        else
            content = new MangoEmailContent(handlerXid, customTemplate, model, translations, subject);
        PostEmailRunnable[] postEmail = null;
        if (includeLogs) {
            final File logZip = getZippedLogfile(content, new File(Common.getLogsDir(), "ma.log"));
            // Setup To delete the temp files from zip
            if (logZip != null) {
                // See that the temp file(s) gets deleted after the email is sent.
                PostEmailRunnable deleteTempFile = new PostEmailRunnable() {

                    @Override
                    public void run() {
                        if (!logZip.delete())
                            LOG.warn("Temp file " + logZip.getPath() + " not deleted");
                    // Set our state to email failed if necessary
                    // TODO Create an Event to notify of Failed Emails...
                    // if(!this.isSuccess()){}
                    }
                };
                postEmail = new PostEmailRunnable[] { deleteTempFile };
            }
        }
        for (Entry<Path, UUID> entry : inlineImages.getImageList().entrySet()) {
            content.addInline(new FileInline(entry.getValue().toString(), entry.getKey().toFile()));
        }
        if (toAddrs.length > 0)
            EmailWorkItem.queueEmail(toAddrs, content, postEmail);
    } catch (Exception e) {
        LOG.error("Error sending email", e);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonImportExclusion(com.serotonin.m2m2.rt.script.JsonImportExclusion) ScriptLog(com.serotonin.m2m2.rt.script.ScriptLog) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) RenderedPointValueTime(com.serotonin.m2m2.rt.dataImage.RenderedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) FileInline(com.serotonin.web.mail.EmailInline.FileInline) UsedImagesDirective(com.serotonin.m2m2.email.UsedImagesDirective) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) DataPointEventType(com.serotonin.m2m2.rt.event.type.DataPointEventType) UUID(java.util.UUID) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) MangoJavaScriptResult(com.infiniteautomation.mango.util.script.MangoJavaScriptResult) DataPointWithEventDetectors(com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors) PostEmailRunnable(com.serotonin.m2m2.email.PostEmailRunnable) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) MangoEmailContent(com.serotonin.m2m2.email.MangoEmailContent) CompiledMangoJavaScript(com.infiniteautomation.mango.util.script.CompiledMangoJavaScript) Translations(com.serotonin.m2m2.i18n.Translations) File(java.io.File) EventInstanceWrapper(com.serotonin.m2m2.rt.script.EventInstanceWrapper) MangoJavaScriptService(com.infiniteautomation.mango.spring.service.MangoJavaScriptService) ScriptError(com.serotonin.m2m2.rt.script.ScriptError) Path(java.nio.file.Path) IntStringPair(com.serotonin.db.pair.IntStringPair) WorkItemInfo(com.infiniteautomation.mango.util.WorkItemInfo) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) IOException(java.io.IOException) RenderedPointValueTime(com.serotonin.m2m2.rt.dataImage.RenderedPointValueTime)

Example 44 with DataPointWithEventDetectors

use of com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors in project ma-core-public by infiniteautomation.

the class DataPointRTTest method testIntervalOnChangeLogging.

/**
 * Test Interval Logged Values w/ On Change Option
 */
@Test
public void testIntervalOnChangeLogging() {
    MockDataSourceVO dsVo = new MockDataSourceVO();
    MockDataSourceRT dataSource = dsVo.createDataSourceRT();
    dataSource.initialize(false);
    PointValueDao dao = Common.getBean(PointValueDao.class);
    MockPointLocatorVO plVo = new MockPointLocatorVO(DataType.NUMERIC, true);
    DataPointVO dpVo = new DataPointVO();
    dpVo.setId(1);
    dpVo.setDataSourceId(dsVo.getId());
    // Configure Interval on change logging
    dpVo.setLoggingType(DataPointVO.LoggingTypes.ON_CHANGE_INTERVAL);
    dpVo.setTolerance(0.5);
    dpVo.setIntervalLoggingPeriod(5);
    dpVo.setIntervalLoggingPeriodType(TimePeriods.SECONDS);
    dpVo.setPointLocator(plVo);
    dpVo.setLoggingType(DataPointVO.LoggingTypes.ON_CHANGE_INTERVAL);
    MockPointLocatorRT plRt = new MockPointLocatorRT(plVo);
    // Setup some initial data
    List<PointValueTime> initialCache = new ArrayList<>();
    initialCache.add(new PointValueTime(1.0, 0));
    SimulationTimer timer = new SimulationTimer();
    DataPointWithEventDetectors dp = new DataPointWithEventDetectors(dpVo, new ArrayList<>());
    DataPointRT rt = new DataPointRT(dp, plRt, dataSource, initialCache, dao, Common.getBean(PointValueCache.class), timer);
    rt.initialize(false);
    // Test no changes
    timer.fastForwardTo(5001);
    PointValueTime value = dao.getLatestPointValue(dpVo).orElse(null);
    // Ensure database has interval logged value
    assertEquals(1.0, value.getDoubleValue(), 0.0001);
    assertEquals(5000, value.getTime());
    // Ensure cache does not have interval logged value
    assertEquals(1.0, rt.getPointValue().getDoubleValue(), 0.0001);
    assertEquals(0, rt.getPointValue().getTime());
    // Next interval
    timer.fastForwardTo(6000);
    rt.setPointValue(new PointValueTime(2.0, 6000), null);
    // Check Log On Change
    value = dao.getLatestPointValue(dpVo).orElse(null);
    assertEquals(2.0, value.getDoubleValue(), 0.0001);
    assertEquals(6000, value.getTime());
    assertEquals(2.0, rt.getPointValue().getDoubleValue(), 0.0001);
    assertEquals(6000, rt.getPointValue().getTime());
    // Interval is reset for 5000ms from now
    timer.fastForwardTo(11001);
    // Check Interval Log
    value = dao.getLatestPointValue(dpVo).orElse(null);
    assertEquals(2.0, value.getDoubleValue(), 0.0001);
    assertEquals(11000, value.getTime());
    assertEquals(2.0, rt.getPointValue().getDoubleValue(), 0.0001);
    assertEquals(6000, rt.getPointValue().getTime());
    // Test Tolerance (Should not get logged)
    timer.fastForwardTo(12000);
    rt.setPointValue(new PointValueTime(2.20, 12000), null);
    // Check Log On Change
    value = dao.getLatestPointValue(dpVo).orElse(null);
    assertEquals(2.0, value.getDoubleValue(), 0.0001);
    assertEquals(11000, value.getTime());
    // Cache will have the set value
    assertEquals(2.2, rt.getPointValue().getDoubleValue(), 0.0001);
    assertEquals(12000, rt.getPointValue().getTime());
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) MockDataSourceVO(com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO) SimulationTimer(com.serotonin.timer.SimulationTimer) ArrayList(java.util.ArrayList) DataPointWithEventDetectors(com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors) MockPointLocatorVO(com.serotonin.m2m2.vo.dataPoint.MockPointLocatorVO) PointValueDao(com.serotonin.m2m2.db.dao.PointValueDao) BasicSQLPointValueDao(com.serotonin.m2m2.db.dao.BasicSQLPointValueDao) MockPointLocatorRT(com.serotonin.m2m2.rt.dataSource.MockPointLocatorRT) MockDataSourceRT(com.serotonin.m2m2.rt.dataSource.MockDataSourceRT) PointValueCache(com.infiniteautomation.mango.pointvaluecache.PointValueCache) Test(org.junit.Test)

Example 45 with DataPointWithEventDetectors

use of com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors in project ma-core-public by infiniteautomation.

the class ImportTask method processUpdatedDetectors.

/**
 * Since detectors can be attached to a data point we will import them in bulk here.  This will
 *  remove any fully imported detectors and their container after there are no more detectors to import
 *  for that point.
 */
private void processUpdatedDetectors(Map<String, DataPointWithEventDetectors> eventDetectorMap) {
    Iterator<String> it = eventDetectorMap.keySet().iterator();
    while (it.hasNext()) {
        String key = it.next();
        DataPointWithEventDetectors dp = eventDetectorMap.get(key);
        // The content of the event detectors lists may have duplicates and the DataPointVO may be out of date,
        // but we can assume that all the event detectors for a point will exist in this list.
        ListIterator<AbstractPointEventDetectorVO> listIt = dp.getEventDetectors().listIterator();
        while (listIt.hasNext()) {
            AbstractPointEventDetectorVO ed = listIt.next();
            try {
                if (ed.isNew()) {
                    eventDetectorService.insertAndReload(ed, false);
                    importContext.addSuccessMessage(true, "emport.eventDetector.prefix", ed.getXid());
                } else {
                    eventDetectorService.updateAndReload(ed.getXid(), ed, false);
                    importContext.addSuccessMessage(false, "emport.eventDetector.prefix", ed.getXid());
                }
                // Reload into the RT
                dataPointService.reloadDataPoint(dp.getDataPoint().getXid());
            } catch (ValidationException e) {
                importContext.copyValidationMessages(e.getValidationResult(), "emport.eventDetector.prefix", ed.getXid());
            } catch (Exception e) {
                addException(e);
                LOG.error("Event detector import failed.", e);
            } finally {
                // To avoid being stuck in the loop, removing the item from the lists even if it caused an issue or not.
                listIt.remove();
            }
        }
        if (dp.getEventDetectors().size() == 0) {
            it.remove();
        }
    }
}
Also used : ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) DataPointWithEventDetectors(com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors) JsonException(com.serotonin.json.JsonException) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException)

Aggregations

DataPointWithEventDetectors (com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors)54 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)29 PointValueDao (com.serotonin.m2m2.db.dao.PointValueDao)25 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)24 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)22 Test (org.junit.Test)22 ArrayList (java.util.ArrayList)20 AbstractPointEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)18 PointValueCache (com.infiniteautomation.mango.pointvaluecache.PointValueCache)7 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)6 JsonException (com.serotonin.json.JsonException)6 HashMap (java.util.HashMap)5 NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)4 JsonArray (com.serotonin.json.type.JsonArray)4 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)4 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)4 IDataPointValueSource (com.serotonin.m2m2.rt.dataImage.IDataPointValueSource)4 MockPointLocatorVO (com.serotonin.m2m2.vo.dataPoint.MockPointLocatorVO)4 SerialPointLocatorVO (com.infiniteautomation.serial.vo.SerialPointLocatorVO)3 DataPointChangeDefinition (com.serotonin.m2m2.module.definitions.dataPoint.DataPointChangeDefinition)3