Search in sources :

Example 1 with WebSocketSendException

use of com.infiniteautomation.mango.rest.latest.websocket.WebSocketSendException in project ma-modules-public by infiniteautomation.

the class PointValueWebSocketHandler method handleTextMessage.

@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) {
    try {
        PermissionHolder user = getUser(session);
        PointValueRegistrationModel model = this.jacksonMapper.readValue(message.getPayload(), PointValueRegistrationModel.class);
        DataPointVO vo;
        try {
            // This will check for not found and permissions
            vo = datapointService.get(model.getDataPointXid());
        } catch (NotFoundException e) {
            // send not found message back
            this.sendErrorMessage(session, MangoWebSocketErrorType.SERVER_ERROR, new TranslatableMessage("rest.error.pointNotFound", model.getDataPointXid()));
            return;
        } catch (PermissionException e) {
            // Send permission denied message here
            this.sendErrorMessage(session, MangoWebSocketErrorType.PERMISSION_DENIED, new TranslatableMessage("permission.exception.readDataPoint", user.getPermissionHolderName()));
            return;
        }
        Set<PointValueEventType> eventsTypes = model.getEventTypes();
        int dataPointId = vo.getId();
        synchronized (pointIdToListenerMap) {
            if (this.connectionClosed) {
                return;
            }
            PointValueWebSocketListener publisher = pointIdToListenerMap.get(dataPointId);
            if (publisher != null) {
                if (eventsTypes.isEmpty()) {
                    publisher.terminate();
                    pointIdToListenerMap.remove(dataPointId);
                } else {
                    publisher.setEventTypes(eventsTypes);
                }
            } else if (!eventsTypes.isEmpty()) {
                publisher = new PointValueWebSocketListener(vo, eventsTypes);
                publisher.initialize();
                // Immediately send the most recent Point Value and the status of the data point
                publisher.sendPointStatus();
                pointIdToListenerMap.put(dataPointId, publisher);
            }
        }
    } catch (WebSocketSendException e) {
        log.warn("Error sending websocket message", e);
    } catch (Exception e) {
        try {
            this.sendErrorMessage(session, MangoWebSocketErrorType.SERVER_ERROR, new TranslatableMessage("rest.error.serverError", e.getMessage()));
        } catch (Exception e1) {
            log.error("An error occurred", e);
        }
    }
    if (log.isDebugEnabled()) {
        log.debug(message.getPayload());
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) WebSocketSendException(com.infiniteautomation.mango.rest.latest.websocket.WebSocketSendException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) WebSocketSendException(com.infiniteautomation.mango.rest.latest.websocket.WebSocketSendException)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 WebSocketSendException (com.infiniteautomation.mango.rest.latest.websocket.WebSocketSendException)1 NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)1 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)1 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)1