import config from "../../config";

const sleep = (waitTimeInMs) => new Promise(resolve => setTimeout(resolve, waitTimeInMs))
const axios = require('axios');



class asyncHuaweiASR {
    constructor(options) {
        //配置项
        this.endpoint = config.huawei.endpoint,
            this.projectId = config.huawei.projectId,
            this.token = config.huawei.token;
    }

    async speechRecognition(data, param) {

        //Huawei ASR
        var requestUrl = "https://" + this.endpoint + "/v1/" + this.projectId + "/asr/short-audio";
        /**
       * 设置HTTPS请求头部
      */
        var httpHeaders = {
            'Content-type': 'application/json',
            'X-Auth-Token': this.token
        };

        var options = {
            url: requestUrl,
            method: 'POST',
            headers: httpHeaders,
            data: {
                "config": {
                    "audio_format": "pcm16k16bit",
                    "property": "chinese_16k_common",
                    "add_punc": "yes"
                },
                "data": data
            }
        };
        var result = await axios(options)
        return result;
    } catch(e) {
        console.error("【BAIDU ASR】初始化接口内部错误", e);
    }
}

export default asyncHuaweiASR;