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

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



class asyncHuaweiASR {
    constructor(options) {
        //配置项
        this.endpoint = options.token.asr.endpoint||config.huawei.endpoint,
            this.projectId = options.token.asr.projectId||config.huawei.projectId,
            this.token = options.token.asr.token||config.huawei.token;
            this.config=options;
            delete options.token;
        console.log("【Huawei shortVoiceASR】options:>>",options);
    }

    async speechRecognition(data) {

        //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": this.config.audio_format||"pcm16k16bit",
                    "property": this.config.property||"chinese_16k_common",
                    "add_punc": this.config.add_punc||"no",
                    "digit_norm":this.config.digit_norm ||"yes"

                },
                "data": data
            }
        };
        var result = await axios(options)
        return result;
    } catch(e) {
        console.error("【Huawei ASR】初始化接口内部错误", e);
    }
}

export default asyncHuaweiASR;