package com.ruoyi.cms.handler;

import com.alibaba.nls.client.protocol.asr.SpeechRecognizer;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

@Component
@ServerEndpoint("/speech-recognition")
public class SpeechRecognitionWebSocketHandler {

    private SpeechRecognizerAI recognizerDemo;

    public SpeechRecognitionWebSocketHandler() {
        // 初始化语音识别器
        String appKey = "4lFYn2yPsQymwGu8";
        String id = "LTAI5t9hhSqdDHqwH3RjgyYj";
        String secret = "ni5aW3vxrWouMwcGqJPfh9Uu56PBuv";
        String url = System.getenv().getOrDefault("NLS_GATEWAY_URL", "wss://nls-gateway-cn-shanghai.aliyuncs.com/ws/v1");
        recognizerDemo = new SpeechRecognizerAI(appKey, id, secret, url);
    }

    /**
     * 连接建立成功调用的方法
     */
    @OnOpen
    public void onOpen(Session session) {
        System.out.println("WebSocket 连接建立成功，sessionId = " + session.getId());
    }

    /**
     * 收到客户端消息后调用的方法
     */
    @OnMessage(maxMessageSize=5242880)
    public void onMessage(ByteBuffer message, Session session) throws IOException {
        byte[] audioData = new byte[message.remaining()];
        message.get(audioData);

        // 处理音频数据
        recognizerDemo.processStream(session, new ByteArrayInputStream(audioData), 16000);
    }


    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose(Session session) {
        System.out.println("WebSocket 连接关闭，sessionId = " + session.getId());
    }

    /**
     * 发生错误时调用的方法
     */
    @OnError
    public void onError(Session session, Throwable error) {
        System.err.println("WebSocket 发生错误：" + error.getMessage());
        error.printStackTrace();
    }
}