Search in sources :

Example 1 with EventRegistrationModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.events.EventRegistrationModel in project ma-modules-public by infiniteautomation.

the class EventsWebSocketHandler method handleTextMessage.

@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) {
    try {
        User user = this.getUser(session);
        if (user == null) {
            return;
        }
        EventRegistrationModel model = this.jacksonMapper.readValue(message.getPayload(), EventRegistrationModel.class);
        Set<String> levels = model.getLevels();
        if (levels == null) {
            levels = Collections.emptySet();
        }
        Set<EventEventTypeEnum> events = model.getEventTypes();
        if (events == null) {
            events = EnumSet.noneOf(EventEventTypeEnum.class);
        }
        boolean emptySubscriptions = levels.isEmpty() || events.isEmpty();
        synchronized (this.lock) {
            if (!this.connectionClosed) {
                if (this.listener != null) {
                    if (emptySubscriptions) {
                        this.listener.terminate();
                        this.listener = null;
                    } else {
                        this.listener.changeLevels(levels);
                        this.listener.changeEvents(events);
                    }
                } else if (!emptySubscriptions) {
                    this.listener = new EventsWebSocketListener(user, levels, events);
                    this.listener.initialize();
                }
            }
        }
    } 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);
        }
    }
    if (log.isDebugEnabled())
        log.debug(message.getPayload());
}
Also used : User(com.serotonin.m2m2.vo.User) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) EventRegistrationModel(com.serotonin.m2m2.web.mvc.rest.v1.model.events.EventRegistrationModel) EventEventTypeEnum(com.serotonin.m2m2.web.mvc.rest.v1.model.events.EventEventTypeEnum)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 User (com.serotonin.m2m2.vo.User)1 EventEventTypeEnum (com.serotonin.m2m2.web.mvc.rest.v1.model.events.EventEventTypeEnum)1 EventRegistrationModel (com.serotonin.m2m2.web.mvc.rest.v1.model.events.EventRegistrationModel)1