package com.its.pis.websocket;

import com.its.pis.entity.TbPisInfr;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;

@Slf4j
@Data
public class PisWebSocketSession {

    private WebSocketSession session;
    private TbPisInfr obj;
    private String groupId;

    public PisWebSocketSession(TbPisInfr obj, WebSocketSession session) {
        this.obj = obj;
        this.session = session;
    }

    public void sendMessage(String command, TextMessage message) {

        if (this.session != null && this.session.isOpen()) {
            synchronized (this.session) {
                try {
                    this.session.sendMessage(message);
                } catch (IOException e) {
                    log.error("sendMessage: command: {}, session: {}", command, session);
                }
            }
        }
        else {
            log.error("Websocket Session is null or already closed, command={}, message={}", command, message);
        }
    }
}
