Search in sources :

Example 16 with AlarmLevels

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

the class EventInstanceService method getActiveSummary.

/**
 * Get the active summary of events for a user
 */
public List<UserEventLevelSummary> getActiveSummary() throws PermissionException {
    PermissionHolder user = Common.getUser();
    this.permissionService.ensurePermission(user, eventsViewPermission.getPermission());
    Map<AlarmLevels, UserEventLevelSummary> summaries = new EnumMap<>(AlarmLevels.class);
    for (AlarmLevels level : AlarmLevels.values()) {
        if (level == AlarmLevels.IGNORE) {
            continue;
        }
        summaries.put(level, new UserEventLevelSummary(level));
    }
    for (EventInstance event : this.getAllActiveUserEvents()) {
        UserEventLevelSummary summary = summaries.get(event.getAlarmLevel());
        summary.increment(event);
    }
    return new ArrayList<>(summaries.values());
}
Also used : EventInstance(com.serotonin.m2m2.rt.event.EventInstance) ArrayList(java.util.ArrayList) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) UserEventLevelSummary(com.serotonin.m2m2.rt.event.UserEventLevelSummary) EnumMap(java.util.EnumMap) AlarmLevels(com.serotonin.m2m2.rt.event.AlarmLevels)

Example 17 with AlarmLevels

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

the class MailingListService method getAlarmAddresses.

/**
 *  Get any addresses for mailing lists that are mailed on alarm level up to and including 'alarmLevel'
 *
 * @param time of gathering addresses used to determine if a list is inactive
 * @param types for types of entries to return
 */
public Set<String> getAlarmAddresses(AlarmLevels alarmLevel, long time, RecipientListEntryType... types) {
    PermissionHolder user = Common.getUser();
    this.permissionService.ensureAdminRole(user);
    List<MailingList> result = new ArrayList<>();
    // TODO Mango 4.0 this is only weakly consistent
    cache.asMap().forEach((id, ml) -> {
        if (ml.getReceiveAlarmEmails().value() >= 0 && ml.getReceiveAlarmEmails().value() <= alarmLevel.value()) {
            result.add(ml);
        }
    });
    Set<String> addresses = new HashSet<>();
    for (MailingList list : result) {
        addresses.addAll(getActiveRecipients(list.getEntries(), time, types));
    }
    return addresses;
}
Also used : MailingList(com.serotonin.m2m2.vo.mailingList.MailingList) ArrayList(java.util.ArrayList) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) HashSet(java.util.HashSet)

Example 18 with AlarmLevels

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

the class AbstractDataSourceModel method getAlarmLevels.

@JsonGetter(value = "alarmLevels")
public Map<String, String> getAlarmLevels() {
    ExportCodes eventCodes = this.data.getEventCodes();
    Map<String, String> alarmCodeLevels = new HashMap<>();
    if (eventCodes != null && eventCodes.size() > 0) {
        for (int i = 0; i < eventCodes.size(); i++) {
            int eventId = eventCodes.getId(i);
            int level = this.data.getAlarmLevel(eventId, AlarmLevels.URGENT);
            alarmCodeLevels.put(eventCodes.getCode(eventId), AlarmLevels.CODES.getCode(level));
        }
    }
    return alarmCodeLevels;
}
Also used : ExportCodes(com.serotonin.m2m2.util.ExportCodes) HashMap(java.util.HashMap) JsonGetter(com.fasterxml.jackson.annotation.JsonGetter)

Example 19 with AlarmLevels

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

the class AbstractDataSourceModel method setAlarmLevels.

@JsonSetter(value = "alarmLevels")
public void setAlarmLevels(Map<String, String> alarmCodeLevels) throws TranslatableJsonException {
    if (alarmCodeLevels != null) {
        ExportCodes eventCodes = this.data.getEventCodes();
        if (eventCodes != null && eventCodes.size() > 0) {
            for (String code : alarmCodeLevels.keySet()) {
                int eventId = eventCodes.getId(code);
                if (!eventCodes.isValidId(eventId))
                    throw new TranslatableJsonException("emport.error.eventCode", code, eventCodes.getCodeList());
                String text = alarmCodeLevels.get(code);
                int level = AlarmLevels.CODES.getId(text);
                if (!AlarmLevels.CODES.isValidId(level))
                    throw new TranslatableJsonException("emport.error.alarmLevel", text, code, AlarmLevels.CODES.getCodeList());
                this.data.setAlarmLevel(eventId, level);
            }
        }
    }
}
Also used : ExportCodes(com.serotonin.m2m2.util.ExportCodes) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonSetter(com.fasterxml.jackson.annotation.JsonSetter)

Example 20 with AlarmLevels

use of com.serotonin.m2m2.rt.event.AlarmLevels in project ma-modules-public by infiniteautomation.

the class EventsWebSocketHandler method handleTextMessage.

@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) {
    try {
        JsonNode tree = this.jacksonMapper.readTree(message.getPayload());
        if (!WebSocketMessageType.REQUEST.messageTypeMatches(tree) || tree.get("requestType") == null) {
            return;
        }
        EventsWebsocketRequest request = this.jacksonMapper.treeToValue(tree, EventsWebsocketRequest.class);
        request.ensureValid();
        if (request instanceof EventsSubscriptionRequest) {
            EventsSubscriptionRequest subscription = (EventsSubscriptionRequest) request;
            Set<AlarmLevels> levels = subscription.getLevels();
            if (levels == null) {
                levels = Collections.emptySet();
            }
            Set<EventActionEnum> actions = subscription.getActions();
            if (actions == null) {
                actions = EnumSet.noneOf(EventActionEnum.class);
            }
            boolean emptySubscriptions = levels.isEmpty() || actions.isEmpty();
            synchronized (this.lock) {
                // Configure listener
                Boolean subscribed = (Boolean) session.getAttributes().get(SUBSCRIPTION_ATTRIBUTE);
                if (subscribed == null || subscribed == Boolean.FALSE) {
                    if (!emptySubscriptions) {
                        changeLevels(levels);
                        changeActions(actions);
                        initialize();
                        session.getAttributes().put(SUBSCRIPTION_ATTRIBUTE, Boolean.TRUE);
                    }
                } else {
                    if (emptySubscriptions) {
                        terminate();
                        session.getAttributes().put(SUBSCRIPTION_ATTRIBUTE, Boolean.FALSE);
                    } else {
                        changeActions(actions);
                        changeLevels(levels);
                    }
                }
            }
            EventsSubscriptionResponse response = new EventsSubscriptionResponse();
            if (subscription.isSendActiveSummary()) {
                List<UserEventLevelSummary> summaries = service.getActiveSummary();
                List<EventLevelSummaryModel> models = summaries.stream().map(s -> {
                    EventInstanceModel instanceModel = s.getLatest() != null ? modelMapper.map(s.getLatest(), EventInstanceModel.class, user) : null;
                    return new EventLevelSummaryModel(s.getAlarmLevel(), s.getCount(), instanceModel);
                }).collect(Collectors.toList());
                response.setActiveSummary(models);
            }
            if (subscription.isSendUnacknowledgedSummary()) {
                List<UserEventLevelSummary> summaries = service.getUnacknowledgedSummary();
                List<EventLevelSummaryModel> models = summaries.stream().map(s -> {
                    EventInstanceModel instanceModel = s.getLatest() != null ? modelMapper.map(s.getLatest(), EventInstanceModel.class, user) : null;
                    return new EventLevelSummaryModel(s.getAlarmLevel(), s.getCount(), instanceModel);
                }).collect(Collectors.toList());
                response.setUnacknowledgedSummary(models);
            }
            this.sendRawMessage(session, new WebSocketResponse<>(request.getSequenceNumber(), response));
        } else if (request instanceof EventsDataPointSummaryRequest) {
            EventsDataPointSummaryRequest query = (EventsDataPointSummaryRequest) request;
            WebSocketResponse<List<DataPointEventSummaryModel>> response = new WebSocketResponse<>(request.getSequenceNumber());
            Collection<DataPointEventLevelSummary> summaries = service.getDataPointEventSummaries(query.getDataPointXids());
            List<DataPointEventSummaryModel> models = summaries.stream().map(s -> new DataPointEventSummaryModel(s.getXid(), s.getCounts())).collect(Collectors.toList());
            response.setPayload(models);
            this.sendRawMessage(session, response);
        } else if (request instanceof AllActiveEventsRequest) {
            WebSocketResponse<List<EventInstanceModel>> response = new WebSocketResponse<>(request.getSequenceNumber());
            List<EventInstance> active = service.getAllActiveUserEvents();
            List<EventInstanceModel> models = new ArrayList<>(active.size());
            for (EventInstance vo : active) {
                models.add(modelMapper.map(vo, EventInstanceModel.class, user));
            }
            response.setPayload(models);
            this.sendRawMessage(session, response);
        } else if (request instanceof ActiveEventsQuery) {
            List<EventInstance> active = service.getAllActiveUserEvents();
            List<EventInstanceModel> models = new ArrayList<>(active.size());
            for (EventInstance vo : active) {
                models.add(modelMapper.map(vo, EventInstanceModel.class, user));
            }
            String query = ((ActiveEventsQuery) request).getQuery();
            ASTNode rql = RQLUtils.parseRQLtoAST(query);
            WebSocketResponse<ArrayWithTotal<Stream<EventInstanceModel>>> response = new WebSocketResponse<>(request.getSequenceNumber());
            response.setPayload(new FilteredStreamWithTotal<>(models, rql, user.getTranslations()));
            this.sendRawMessage(session, response);
        }
    } catch (NotFoundException e) {
        try {
            this.sendErrorMessage(session, MangoWebSocketErrorType.NOT_FOUND, e.getTranslatableMessage());
        } catch (Exception e1) {
            log.error(e.getMessage(), e);
        }
    } catch (PermissionException e) {
        try {
            this.sendErrorMessage(session, MangoWebSocketErrorType.PERMISSION_DENIED, e.getTranslatableMessage());
        } catch (Exception e1) {
            log.error(e.getMessage(), e);
        }
    } catch (Exception e) {
        try {
            this.sendErrorMessage(session, MangoWebSocketErrorType.SERVER_ERROR, new TranslatableMessage("rest.error.serverError", e.getMessage()));
        } catch (Exception e1) {
            log.error(e.getMessage(), e);
        }
    }
}
Also used : FilteredStreamWithTotal(com.infiniteautomation.mango.rest.latest.model.FilteredStreamWithTotal) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) EventInstance(com.serotonin.m2m2.rt.event.EventInstance) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Id(com.fasterxml.jackson.annotation.JsonTypeInfo.Id) ArrayWithTotal(com.infiniteautomation.mango.rest.latest.model.ArrayWithTotal) DataPointEventSummaryModel(com.infiniteautomation.mango.rest.latest.model.event.DataPointEventSummaryModel) WebSocketSession(org.springframework.web.socket.WebSocketSession) CloseStatus(org.springframework.web.socket.CloseStatus) ArrayList(java.util.ArrayList) RestModelMapper(com.infiniteautomation.mango.rest.latest.model.RestModelMapper) UserEventListener(com.serotonin.m2m2.rt.event.UserEventListener) TextMessage(org.springframework.web.socket.TextMessage) As(com.fasterxml.jackson.annotation.JsonTypeInfo.As) DataPointEventLevelSummary(com.serotonin.m2m2.rt.event.DataPointEventLevelSummary) JsonNode(com.fasterxml.jackson.databind.JsonNode) Validatable(com.serotonin.m2m2.vo.Validatable) EnumSet(java.util.EnumSet) Logger(org.slf4j.Logger) JsonSubTypes(com.fasterxml.jackson.annotation.JsonSubTypes) Common(com.serotonin.m2m2.Common) Collection(java.util.Collection) EventInstanceModel(com.infiniteautomation.mango.rest.latest.model.event.EventInstanceModel) RQLUtils(com.infiniteautomation.mango.util.RQLUtils) Set(java.util.Set) EventInstanceService(com.infiniteautomation.mango.spring.service.EventInstanceService) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) UserEventLevelSummary(com.serotonin.m2m2.rt.event.UserEventLevelSummary) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) List(java.util.List) Stream(java.util.stream.Stream) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) ASTNode(net.jazdw.rql.parser.ASTNode) EventLevelSummaryModel(com.infiniteautomation.mango.rest.latest.model.event.EventLevelSummaryModel) EventActionEnum(com.infiniteautomation.mango.rest.latest.model.event.EventActionEnum) AlarmLevels(com.serotonin.m2m2.rt.event.AlarmLevels) Collections(java.util.Collections) User(com.serotonin.m2m2.vo.User) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) Type(com.fasterxml.jackson.annotation.JsonSubTypes.Type) EventInstance(com.serotonin.m2m2.rt.event.EventInstance) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) EventActionEnum(com.infiniteautomation.mango.rest.latest.model.event.EventActionEnum) EventLevelSummaryModel(com.infiniteautomation.mango.rest.latest.model.event.EventLevelSummaryModel) ArrayList(java.util.ArrayList) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) JsonNode(com.fasterxml.jackson.databind.JsonNode) EventInstanceModel(com.infiniteautomation.mango.rest.latest.model.event.EventInstanceModel) ASTNode(net.jazdw.rql.parser.ASTNode) ArrayList(java.util.ArrayList) List(java.util.List) Stream(java.util.stream.Stream) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) FilteredStreamWithTotal(com.infiniteautomation.mango.rest.latest.model.FilteredStreamWithTotal) UserEventLevelSummary(com.serotonin.m2m2.rt.event.UserEventLevelSummary) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) DataPointEventSummaryModel(com.infiniteautomation.mango.rest.latest.model.event.DataPointEventSummaryModel) Collection(java.util.Collection) AlarmLevels(com.serotonin.m2m2.rt.event.AlarmLevels)

Aggregations

AlarmLevels (com.serotonin.m2m2.rt.event.AlarmLevels)19 ExportCodes (com.serotonin.m2m2.util.ExportCodes)14 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)11 ArrayList (java.util.ArrayList)11 EventInstance (com.serotonin.m2m2.rt.event.EventInstance)9 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)8 UserEventLevelSummary (com.serotonin.m2m2.rt.event.UserEventLevelSummary)7 HashMap (java.util.HashMap)7 EventTypeVO (com.serotonin.m2m2.vo.event.EventTypeVO)6 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)5 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)5 User (com.serotonin.m2m2.vo.User)5 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)4 SystemEventType (com.serotonin.m2m2.rt.event.type.SystemEventType)4 EnumMap (java.util.EnumMap)4 Test (org.junit.Test)4 JsonObject (com.serotonin.json.type.JsonObject)3 UserEventListener (com.serotonin.m2m2.rt.event.UserEventListener)3 Collections (java.util.Collections)3 HashSet (java.util.HashSet)3