package com.gyee.alarm.controller;


import com.gyee.alarm.websocket.PushParams;
import com.gyee.alarm.websocket.WebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.websocket.EncodeException;
import java.io.IOException;

/**
 * 功能描述:
 * 建立WebSocket连接
 * @Author: LXD
 * @Date: 2022-12-01 09:55:00
 * @since: 1.0.0
 */
@RestController
@RequestMapping("/webSocketPush")
public class WebSocketController {
    @Autowired
    private WebSocket webSocket;


    @RequestMapping("/sentMessage")
    public void sentMessage(String userId,String message){
        try {
            webSocket.sendMessageByUserId(userId,message);
        } catch (IOException | EncodeException e) {
            e.printStackTrace();
        }

    }

    @RequestMapping("/sentObjectMessage")
    public void sentObjectMessage(String userId){
        try {
            webSocket.sendMessageByUserId(userId,"111111");
        } catch (IOException | EncodeException e) {
            e.printStackTrace();
        }

    }

    /***
     * 功能描述:
     * 根据用户ID更新ws推送的参数
     * @Author: LXD
     * @Date: 2022-12-01 09:21:25
     * @Param  userId: WS中的用户ID
     * @Param pushParams: 推送参数
     * @return: void
     * @since: 1.0.0
     */
    @RequestMapping("/changeWsParams")
    public void changeWsParams(String userId, PushParams pushParams){
        try {
            webSocket.changeParamsByUserId(userId,pushParams);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (EncodeException e) {
            e.printStackTrace();
        }

    }

}
