Jelajahi Sumber

讯飞asr测试,debug

Davidliu 1 bulan lalu
induk
melakukan
fd0b73db71
1 mengubah file dengan 18 tambahan dan 11 penghapusan
  1. 18 11
      src/core/voip/asr.py

+ 18 - 11
src/core/voip/asr.py

@@ -199,11 +199,21 @@ class TestSt:
 class XfAsr:
 
     def __init__(self, tid, logger, message_receiver=None):
+        self.end_tag = "{\"end\": true}"
+        self.tid = tid
+        self.logger = logger
+        self.message_receiver = message_receiver
+        self.ws = self.new_connection()
+        self.logger.info(f"xunfei.Asr: ws.connected:{self.ws.connected}")
+        self.trecv = threading.Thread(target=self.recv)
+
+    def new_connection(self, base_url = "ws://rtasr.xfyun.cn/v1/ws", app_id = "1ec1097b", api_key = "60b7d2d8d172b065b1c3e723e5ba0696"):
         import hashlib
         import hmac
         import base64
         # from socket import *
         # import json, time, threading
+
         from websocket import create_connection
         # import websocket
         from urllib.parse import quote
@@ -211,10 +221,6 @@ class XfAsr:
 
         logging.basicConfig()
 
-        app_id = "1ec1097b"
-        api_key = "60b7d2d8d172b065b1c3e723e5ba0696"
-        base_url = "ws://rtasr.xfyun.cn/v1/ws"
-
         ts = str(int(time.time()))
         tt = (app_id + ts).encode('utf-8')
         md5 = hashlib.md5()
@@ -227,13 +233,14 @@ class XfAsr:
         signa = base64.b64encode(signa)
         signa = str(signa, 'utf-8')
 
-        self.end_tag = "{\"end\": true}"
-        self.tid = tid
-        self.logger = logger
-        self.message_receiver = message_receiver
-        self.ws = create_connection(base_url + "?appid=" + app_id + "&ts=" + ts + "&signa=" + quote(signa))
-        self.logger.info(f"xunfei.Asr: ws.connected:{self.ws.connected}")
-        self.trecv = threading.Thread(target=self.recv)
+        count = 10
+        while count > 0:
+            try:
+                return create_connection(base_url + "?appid=" + app_id + "&ts=" + ts + "&signa=" + quote(signa))
+            except Exception as e:
+                count -= 1
+                self.logger.info("new_connection:exception, count=%s, message=%s", count, e)
+                time.sleep(.010)
 
     def start(self):
         self.trecv.start()