use of com.serotonin.m2m2.rt.event.type.EventType in project ma-core-public by infiniteautomation.
the class EventManagerImpl method raiseEvent.
//
//
// Basic event management.
//
/**
* Raise Event
* @param type
* @param time
* @param rtnApplicable - does this event return to normal?
* @param alarmLevel
* @param message
* @param context
*/
public void raiseEvent(EventType type, long time, boolean rtnApplicable, int alarmLevel, TranslatableMessage message, Map<String, Object> context) {
if (state != RUNNING)
return;
if (alarmLevel == AlarmLevels.IGNORE)
return;
// Check if there is an event for this type already active.
EventInstance dup = get(type);
if (dup != null) {
// Check the duplicate handling.
boolean discard = canDiscard(type, message);
if (discard)
return;
// Otherwise we just continue...
} else if (!rtnApplicable) {
// Check if we've already seen this type recently.
boolean recent = isRecent(type, message);
if (recent)
return;
}
// Determine if the event should be suppressed.
TranslatableMessage autoAckMessage = null;
for (EventManagerListenerDefinition l : listeners) {
autoAckMessage = l.autoAckEventWithMessage(type);
if (autoAckMessage != null)
break;
}
EventInstance evt = new EventInstance(type, time, rtnApplicable, alarmLevel, message, context);
if (autoAckMessage == null)
setHandlers(evt);
// Check to see if we are Not Logging these
if (alarmLevel != AlarmLevels.DO_NOT_LOG) {
eventDao.saveEvent(evt);
}
// Create user alarm records for all applicable users
List<Integer> eventUserIds = new ArrayList<Integer>();
Set<String> emailUsers = new HashSet<String>();
for (User user : userDao.getActiveUsers()) {
// user should be skipped.
if (type.excludeUser(user))
continue;
if (Permissions.hasEventTypePermission(user, type)) {
eventUserIds.add(user.getId());
if (user.getReceiveAlarmEmails() > AlarmLevels.IGNORE && alarmLevel >= user.getReceiveAlarmEmails() && !StringUtils.isEmpty(user.getEmail()))
emailUsers.add(user.getEmail());
// Notify All User Event Listeners of the new event
if ((alarmLevel != AlarmLevels.DO_NOT_LOG) && (!evt.getEventType().getEventType().equals(EventType.EventTypeNames.AUDIT))) {
for (UserEventListener l : this.userEventListeners) {
if (l.getUserId() == user.getId()) {
Common.backgroundProcessing.addWorkItem(new EventNotifyWorkItem(user, l, evt, true, false, false, false));
}
}
// Add to the UserEventCache if the user has recently accessed their events
this.userEventCache.addEvent(user.getId(), evt);
}
}
}
DateTime now = new DateTime(Common.timer.currentTimeMillis());
for (MailingList ml : MailingListDao.instance.getAlarmMailingLists(alarmLevel)) {
ml.appendAddresses(emailUsers, now);
}
// No Audit or Do Not Log events are User Events
if ((eventUserIds.size() > 0) && (alarmLevel != AlarmLevels.DO_NOT_LOG) && (!evt.getEventType().getEventType().equals(EventType.EventTypeNames.AUDIT))) {
eventDao.insertUserEvents(evt.getId(), eventUserIds, true);
if (autoAckMessage == null)
lastAlarmTimestamp = Common.timer.currentTimeMillis();
}
if (evt.isRtnApplicable()) {
activeEventsLock.writeLock().lock();
try {
activeEvents.add(evt);
} finally {
activeEventsLock.writeLock().unlock();
}
} else if (evt.getEventType().isRateLimited()) {
recentEventsLock.writeLock().lock();
try {
recentEvents.add(evt);
} finally {
recentEventsLock.writeLock().unlock();
}
}
if ((autoAckMessage != null) && (alarmLevel != AlarmLevels.DO_NOT_LOG) && (!evt.getEventType().getEventType().equals(EventType.EventTypeNames.AUDIT)))
this.acknowledgeEvent(evt, time, null, autoAckMessage);
else {
if (evt.isRtnApplicable()) {
if (alarmLevel > highestActiveAlarmLevel) {
int oldValue = highestActiveAlarmLevel;
highestActiveAlarmLevel = alarmLevel;
SystemEventType.raiseEvent(new SystemEventType(SystemEventType.TYPE_MAX_ALARM_LEVEL_CHANGED), time, false, getAlarmLevelChangeMessage("event.alarmMaxIncreased", oldValue));
}
}
// Call raiseEvent handlers.
handleRaiseEvent(evt, emailUsers);
if (log.isDebugEnabled())
log.trace("Event raised: type=" + type + ", message=" + message.translate(Common.getTranslations()));
}
}
use of com.serotonin.m2m2.rt.event.type.EventType in project ma-core-public by infiniteautomation.
the class EventHandlersDwr method saveProcessEventHandler.
@DwrPermission(user = true)
public ProcessResult saveProcessEventHandler(String eventType, String eventSubtype, int eventTypeRef1, int eventTypeRef2, int handlerId, String xid, String alias, boolean disabled, String activeProcessCommand, int activeProcessTimeout, String inactiveProcessCommand, int inactiveProcessTimeout) {
ProcessEventHandlerVO handler = new ProcessEventHandlerVO();
handler.setDefinition(ModuleRegistry.getEventHandlerDefinition(ProcessEventHandlerDefinition.TYPE_NAME));
handler.setActiveProcessCommand(activeProcessCommand);
handler.setActiveProcessTimeout(activeProcessTimeout);
handler.setInactiveProcessCommand(inactiveProcessCommand);
handler.setInactiveProcessTimeout(inactiveProcessTimeout);
return save(eventType, eventSubtype, eventTypeRef1, eventTypeRef2, handler, handlerId, xid, alias, disabled);
}
use of com.serotonin.m2m2.rt.event.type.EventType in project ma-core-public by infiniteautomation.
the class EventHandlerImporter method importImpl.
@Override
protected void importImpl() {
String xid = json.getString("xid");
if (StringUtils.isBlank(xid))
xid = ctx.getEventHandlerDao().generateUniqueXid();
AbstractEventHandlerVO<?> handler = ctx.getEventHandlerDao().getEventHandler(xid);
if (handler == null) {
String typeStr = json.getString("handlerType");
if (StringUtils.isBlank(typeStr))
addFailureMessage("emport.eventHandler.missingType", xid, ModuleRegistry.getEventHandlerDefinitionTypes());
else {
EventHandlerDefinition<?> def = ModuleRegistry.getEventHandlerDefinition(typeStr);
if (def == null)
addFailureMessage("emport.eventHandler.invalidType", xid, typeStr, ModuleRegistry.getEventHandlerDefinitionTypes());
else {
handler = def.baseCreateEventHandlerVO();
handler.setXid(xid);
}
}
}
JsonObject et = json.getJsonObject("eventType");
JsonArray ets = json.getJsonArray("eventTypes");
EventType eventType = null;
List<EventType> eventTypes = null;
try {
// Find the event type.
if (et != null)
eventType = ctx.getReader().read(EventType.class, et);
else if (ets != null) {
eventTypes = new ArrayList<EventType>(ets.size());
Iterator<JsonValue> iter = ets.iterator();
while (iter.hasNext()) eventTypes.add(ctx.getReader().read(EventType.class, iter.next()));
}
ctx.getReader().readInto(handler, json);
// Now validate it. Use a new response object so we can distinguish errors in this vo from other errors.
ProcessResult voResponse = new ProcessResult();
handler.validate(voResponse);
if (voResponse.getHasMessages())
setValidationMessages(voResponse, "emport.eventHandler.prefix", xid);
else {
// Sweet.
boolean isnew = handler.getId() == Common.NEW_ID;
// Save it.
if (et != null)
ctx.getEventHandlerDao().saveEventHandler(eventType, handler);
else
ctx.getEventHandlerDao().saveEventHandler(handler);
if (eventTypes != null)
for (EventType type : eventTypes) ctx.getEventHandlerDao().addEventHandlerMappingIfMissing(handler.getId(), type);
addSuccessMessage(isnew, "emport.eventHandler.prefix", xid);
}
} catch (TranslatableJsonException e) {
addFailureMessage("emport.eventHandler.prefix", xid, e.getMsg());
} catch (JsonException e) {
addFailureMessage("emport.eventHandler.prefix", xid, getJsonExceptionMessage(e));
}
}
use of com.serotonin.m2m2.rt.event.type.EventType in project ma-core-public by infiniteautomation.
the class EventHandlerDao method saveEventHandler.
private AbstractEventHandlerVO<?> saveEventHandler(final String typeName, final String subtypeName, final int typeRef1, final int typeRef2, final AbstractEventHandlerVO<?> handler) {
getTransactionTemplate().execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
if (handler.getId() == Common.NEW_ID)
insertEventHandler(typeName, subtypeName, typeRef1, typeRef2, handler);
else
updateEventHandler(handler);
if (handler.getAddedEventTypes() != null)
for (EventType et : handler.getAddedEventTypes()) EventHandlerDao.instance.addEventHandlerMappingIfMissing(handler.getId(), et);
}
});
return getEventHandler(handler.getId());
}
use of com.serotonin.m2m2.rt.event.type.EventType in project ma-core-public by infiniteautomation.
the class EventInstanceDao method createEventType.
static EventType createEventType(ResultSet rs, int offset) throws SQLException {
String typeName = rs.getString(offset);
String subtypeName = rs.getString(offset + 1);
EventType type;
if (typeName.equals(EventType.EventTypeNames.DATA_POINT))
type = new DataPointEventType(rs.getInt(offset + 2), rs.getInt(offset + 3));
else if (typeName.equals(EventType.EventTypeNames.DATA_SOURCE))
type = new DataSourceEventType(rs.getInt(offset + 2), rs.getInt(offset + 3));
else if (typeName.equals(EventType.EventTypeNames.SYSTEM))
type = new SystemEventType(subtypeName, rs.getInt(offset + 2));
else if (typeName.equals(EventType.EventTypeNames.PUBLISHER))
type = new PublisherEventType(rs.getInt(offset + 2), rs.getInt(offset + 3));
else if (typeName.equals(EventType.EventTypeNames.AUDIT))
throw new ShouldNeverHappenException("AUDIT events should not exist here. Consider running the SQL: DELETE FROM events WHERE typeName='AUDIT';");
else {
EventTypeDefinition def = ModuleRegistry.getEventTypeDefinition(typeName);
if (def == null) {
// Create Missing Event Type
type = new MissingEventType(typeName, null, rs.getInt(offset + 2), rs.getInt(offset + 3));
} else {
type = def.createEventType(subtypeName, rs.getInt(offset + 2), rs.getInt(offset + 3));
if (type == null) {
// Create Missing Event type
type = new MissingEventType(typeName, subtypeName, rs.getInt(offset + 2), rs.getInt(offset + 3));
}
}
}
return type;
}
Aggregations