Search in sources :

Example 31 with EventInstance

use of com.serotonin.m2m2.rt.event.EventInstance in project ma-core-public by infiniteautomation.

the class EventManagerImpl method deactivateEvents.

/**
 * Deactivate a group of simmilar events, these events should have been removed from the active events list already.
 *
 * @param evts
 * @param time
 * @param inactiveCause
 */
private void deactivateEvents(List<EventInstance> evts, long time, int inactiveCause) {
    List<User> activeUsers = userDao.getActiveUsers();
    List<Integer> eventIds = new ArrayList<Integer>();
    for (EventInstance evt : evts) {
        if (evt.isActive())
            eventIds.add(evt.getId());
        evt.returnToNormal(time, inactiveCause);
        for (User user : activeUsers) {
            // user should be skipped.
            if (evt.getEventType().excludeUser(user))
                continue;
            if (Permissions.hasEventTypePermission(user, evt.getEventType())) {
                // Notify All User Event Listeners of the new event
                for (UserEventListener l : this.userEventListeners) {
                    if (l.getUserId() == user.getId()) {
                        Common.backgroundProcessing.addWorkItem(new EventNotifyWorkItem(user, l, evt, false, false, true, false));
                    }
                }
            }
        }
        // Call inactiveEvent handlers.
        handleInactiveEvent(evt);
    }
    if (eventIds.size() > 0) {
        resetHighestAlarmLevel(time);
        eventDao.returnEventsToNormal(eventIds, time, inactiveCause);
    }
}
Also used : EventInstance(com.serotonin.m2m2.rt.event.EventInstance) User(com.serotonin.m2m2.vo.User) UserEventListener(com.serotonin.m2m2.rt.event.UserEventListener) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 32 with EventInstance

use of com.serotonin.m2m2.rt.event.EventInstance in project ma-core-public by infiniteautomation.

the class EventManagerImpl method purgeEventsBefore.

/**
 * Purge Events before time with a given type
 * @param time
 * @param typeName
 * @return
 */
public int purgeEventsBefore(final long time, final String typeName) {
    activeEventsLock.writeLock().lock();
    try {
        ListIterator<EventInstance> it = activeEvents.listIterator();
        while (it.hasNext()) {
            EventInstance e = it.next();
            if ((e.getActiveTimestamp() < time) && (e.getEventType().getEventType().equals(typeName)))
                it.remove();
        }
    } finally {
        activeEventsLock.writeLock().unlock();
    }
    recentEventsLock.writeLock().lock();
    try {
        ListIterator<EventInstance> it = recentEvents.listIterator();
        while (it.hasNext()) {
            EventInstance e = it.next();
            if ((e.getActiveTimestamp() < time) && (e.getEventType().getEventType().equals(typeName)))
                it.remove();
        }
    } finally {
        recentEventsLock.writeLock().unlock();
    }
    userEventCache.purgeEventsBefore(time, typeName);
    return eventDao.purgeEventsBefore(time, typeName);
}
Also used : EventInstance(com.serotonin.m2m2.rt.event.EventInstance)

Example 33 with EventInstance

use of com.serotonin.m2m2.rt.event.EventInstance in project ma-core-public by infiniteautomation.

the class EventManagerImpl method isRecent.

private boolean isRecent(EventType type, TranslatableMessage message) {
    long cutoff = Common.timer.currentTimeMillis() - RECENT_EVENT_PERIOD;
    recentEventsLock.writeLock().lock();
    try {
        for (int i = recentEvents.size() - 1; i >= 0; i--) {
            EventInstance evt = recentEvents.get(i);
            // event instance has expired or not.
            if (cutoff > evt.getActiveTimestamp())
                recentEvents.remove(i);
            else if (evt.getEventType().equals(type) && evt.getMessage().equals(message))
                return true;
        }
    } finally {
        recentEventsLock.writeLock().unlock();
    }
    return false;
}
Also used : EventInstance(com.serotonin.m2m2.rt.event.EventInstance)

Example 34 with EventInstance

use of com.serotonin.m2m2.rt.event.EventInstance in project ma-core-public by infiniteautomation.

the class PublisherEditController method handleRequestInternal.

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    User user = Common.getUser(request);
    Permissions.ensureAdmin(user);
    PublisherVO<? extends PublishedPointVO> publisherVO;
    // Get the id.
    String idStr = request.getParameter("pid");
    if (idStr == null) {
        // Adding a new data source? Get the type id.
        String typeId = request.getParameter("typeId");
        if (StringUtils.isBlank(typeId))
            return new ModelAndView(new RedirectView(errorViewName));
        // A new publisher
        PublisherDefinition def = ModuleRegistry.getPublisherDefinition(typeId);
        if (def == null)
            return new ModelAndView(new RedirectView(errorViewName));
        publisherVO = def.baseCreatePublisherVO();
        publisherVO.setXid(PublisherDao.instance.generateUniqueXid());
    } else {
        // An existing configuration.
        int id = Integer.parseInt(idStr);
        publisherVO = Common.runtimeManager.getPublisher(id);
        if (publisherVO == null)
            return new ModelAndView(new RedirectView(errorViewName));
    }
    // Set the id of the data source in the user object for the DWR.
    user.setEditPublisher(publisherVO);
    // Create the model.
    Map<String, Object> model = new HashMap<>();
    model.put("publisher", publisherVO);
    if (publisherVO.getId() != Common.NEW_ID) {
        List<EventInstance> events = EventDao.instance.getPendingEventsForPublisher(publisherVO.getId(), user.getId());
        List<EventInstanceBean> beans = new ArrayList<>();
        if (events != null) {
            Translations translations = ControllerUtils.getTranslations(request);
            for (EventInstance event : events) beans.add(new EventInstanceBean(event.isActive(), event.getAlarmLevel(), Functions.getTime(event.getActiveTimestamp()), event.getMessage().translate(translations)));
        }
        model.put("publisherEvents", beans);
    }
    publisherVO.addEditContext(model);
    return new ModelAndView(getViewName(), model);
}
Also used : EventInstance(com.serotonin.m2m2.rt.event.EventInstance) User(com.serotonin.m2m2.vo.User) HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) ArrayList(java.util.ArrayList) PublisherDefinition(com.serotonin.m2m2.module.PublisherDefinition) EventInstanceBean(com.serotonin.m2m2.web.dwr.beans.EventInstanceBean) RedirectView(org.springframework.web.servlet.view.RedirectView) Translations(com.serotonin.m2m2.i18n.Translations)

Example 35 with EventInstance

use of com.serotonin.m2m2.rt.event.EventInstance in project ma-core-public by infiniteautomation.

the class SetPointHandlerRT method eventRaised.

@Override
public void eventRaised(EventInstance evt) {
    if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_NONE)
        return;
    // Validate that the target point is available.
    DataPointRT targetPoint = Common.runtimeManager.getDataPoint(vo.getTargetPointId());
    if (targetPoint == null) {
        raiseFailureEvent(new TranslatableMessage("event.setPoint.targetPointMissing"), evt.getEventType());
        return;
    }
    if (!targetPoint.getPointLocator().isSettable()) {
        raiseFailureEvent(new TranslatableMessage("event.setPoint.targetNotSettable"), evt.getEventType());
        return;
    }
    int targetDataType = targetPoint.getVO().getPointLocator().getDataTypeId();
    DataValue value;
    if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_POINT_VALUE) {
        // Get the source data point.
        DataPointRT sourcePoint = Common.runtimeManager.getDataPoint(vo.getActivePointId());
        if (sourcePoint == null) {
            raiseFailureEvent(new TranslatableMessage("event.setPoint.activePointMissing"), evt.getEventType());
            return;
        }
        PointValueTime valueTime = sourcePoint.getPointValue();
        if (valueTime == null) {
            raiseFailureEvent(new TranslatableMessage("event.setPoint.activePointValue"), evt.getEventType());
            return;
        }
        if (DataTypes.getDataType(valueTime.getValue()) != targetDataType) {
            raiseFailureEvent(new TranslatableMessage("event.setPoint.activePointDataType"), evt.getEventType());
            return;
        }
        value = valueTime.getValue();
    } else if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_STATIC_VALUE) {
        value = DataValue.stringToValue(vo.getActiveValueToSet(), targetDataType);
    } else if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_SCRIPT_VALUE) {
        if (activeScript == null) {
            raiseFailureEvent(new TranslatableMessage("eventHandlers.invalidActiveScript"), evt.getEventType());
            return;
        }
        Map<String, IDataPointValueSource> context = new HashMap<String, IDataPointValueSource>();
        context.put(SetPointEventHandlerVO.TARGET_CONTEXT_KEY, targetPoint);
        Map<String, Object> additionalContext = new HashMap<String, Object>();
        additionalContext.put(SetPointEventHandlerVO.EVENT_CONTEXT_KEY, new EventInstanceWrapper(evt));
        try {
            for (IntStringPair cxt : vo.getAdditionalContext()) {
                DataPointRT dprt = Common.runtimeManager.getDataPoint(cxt.getKey());
                if (dprt != null)
                    context.put(cxt.getValue(), dprt);
            }
            PointValueTime pvt = CompiledScriptExecutor.execute(activeScript, context, additionalContext, evt.getActiveTimestamp(), targetPoint.getDataTypeId(), evt.getActiveTimestamp(), vo.getScriptPermissions(), NULL_WRITER, new ScriptLog(NULL_WRITER, LogLevel.FATAL), setCallback, importExclusions, false);
            value = pvt.getValue();
        } catch (ScriptPermissionsException e) {
            raiseFailureEvent(e.getTranslatableMessage(), evt.getEventType());
            return;
        } catch (ScriptException e) {
            raiseFailureEvent(new TranslatableMessage("eventHandlers.invalidActiveScriptError", e.getCause().getMessage()), evt.getEventType());
            return;
        } catch (ResultTypeException e) {
            raiseFailureEvent(new TranslatableMessage("eventHandlers.invalidActiveScriptError", e.getMessage()), evt.getEventType());
            return;
        }
    } else
        throw new ShouldNeverHappenException("Unknown active action: " + vo.getActiveAction());
    // Queue a work item to perform the set point.
    if (CompiledScriptExecutor.UNCHANGED != value)
        Common.backgroundProcessing.addWorkItem(new SetPointWorkItem(vo.getTargetPointId(), new PointValueTime(value, evt.getActiveTimestamp()), this));
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) HashMap(java.util.HashMap) IntStringPair(com.serotonin.db.pair.IntStringPair) SetPointWorkItem(com.serotonin.m2m2.rt.maint.work.SetPointWorkItem) ScriptLog(com.serotonin.m2m2.rt.script.ScriptLog) ScriptException(javax.script.ScriptException) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) ScriptPermissionsException(com.serotonin.m2m2.rt.script.ScriptPermissionsException) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) EventInstanceWrapper(com.serotonin.m2m2.rt.script.EventInstanceWrapper)

Aggregations

EventInstance (com.serotonin.m2m2.rt.event.EventInstance)30 ArrayList (java.util.ArrayList)14 User (com.serotonin.m2m2.vo.User)12 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)7 HashMap (java.util.HashMap)7 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)6 EventDao (com.serotonin.m2m2.db.dao.EventDao)5 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)5 UserEventListener (com.serotonin.m2m2.rt.event.UserEventListener)4 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)3 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)3 Translations (com.serotonin.m2m2.i18n.Translations)3 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)3 IDataPointValueSource (com.serotonin.m2m2.rt.dataImage.IDataPointValueSource)3 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)3 ResultTypeException (com.serotonin.m2m2.rt.script.ResultTypeException)3 ScriptLog (com.serotonin.m2m2.rt.script.ScriptLog)3 ScriptPermissionsException (com.serotonin.m2m2.rt.script.ScriptPermissionsException)3 EventInstanceBean (com.serotonin.m2m2.web.dwr.beans.EventInstanceBean)3 ScriptException (javax.script.ScriptException)3