package com.ruoyi.web.controller.device;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.device.domain.model.TsbUserDeviceBind;
import com.ruoyi.device.websocket.TsbWebSocketService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 调试宝 WebSocket连接 辅助接口（连接前的检测接口）
 *
 * @author lwm
 */
@RestController
@RequestMapping("/tsb/ws")
public class TsbWebSocketController extends BaseController
{
    @Resource
    private TsbWebSocketService tsbWebSocketService;

    /**
     * WebSocket 连接预检（设备在线、操作权限、设备未被占用）
     */
    @GetMapping("/bindInfo")
    public AjaxResult bindInfo(@RequestParam Long deviceSn)
    {
        Long userId = SecurityUtils.getUserId();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String precheckMsg = tsbWebSocketService.precheckConnect(userId, deviceSn, loginUser);
        if (precheckMsg != null)
        {
            logger.warn("设备连接预检失败：{}, userId={}, deviceSn={}", precheckMsg, userId, deviceSn);
            return error(precheckMsg);
        }
        TsbUserDeviceBind bind = tsbWebSocketService.buildOperateBind(deviceSn, loginUser);
        return success(bind);
    }
}
