|
@@ -50,11 +50,12 @@ class TestSt:
|
|
|
def get_cached_token(cls):
|
|
|
# 检查是否已有缓存的Token且未过期s):
|
|
|
# # 检查是否已有缓存的Token且未
|
|
|
- if cls.token_cache["token"] and cls.token_cache["expire_time"]:
|
|
|
- current_time = int(time.time())
|
|
|
- if current_time < cls.token_cache["expire_time"]:
|
|
|
- print("使用缓存的Token")
|
|
|
- return cls.token_cache["token"]
|
|
|
+ current_time = int(time.time())
|
|
|
+ # if cls.token_cache["token"] and cls.token_cache["expire_time"]:
|
|
|
+ if cls.token_cache["token"] and cls.token_cache["expire_time"] - current_time > 60:
|
|
|
+ # if current_time < cls.token_cache["expire_time"]:
|
|
|
+ # print("使用缓存的Token")
|
|
|
+ return cls.token_cache["token"]
|
|
|
|
|
|
# 如果没有缓存Token或者Token已过期,重新获取
|
|
|
new_token, expire_time = get_token()
|
|
@@ -68,6 +69,9 @@ class TestSt:
|
|
|
return None
|
|
|
|
|
|
def __init__(self, tid, message_receiver=None):
|
|
|
+ self.is_closed = False
|
|
|
+ self.lock = threading.Lock()
|
|
|
+
|
|
|
self.__th = threading.Thread(target=self.__test_run)
|
|
|
self.__id = tid
|
|
|
self.message_receiver = message_receiver
|
|
@@ -79,10 +83,25 @@ class TestSt:
|
|
|
self.__th.start()
|
|
|
|
|
|
def send_audio(self, audio_data):
|
|
|
- if self.sr:
|
|
|
- # print("Sending audio data of length:", len(audio_data))
|
|
|
- self.sr.send_audio(audio_data)
|
|
|
+ # if self.sr:
|
|
|
+ # # print("Sending audio data of length:", len(audio_data))
|
|
|
+ # self.sr.send_audio(audio_data)
|
|
|
# print("Audio data sent.")
|
|
|
+ if self.sr and not self.is_closed:
|
|
|
+ with self.lock:
|
|
|
+ try:
|
|
|
+ self.sr.send_audio(audio_data)
|
|
|
+ except Exception as e:
|
|
|
+ print(f"Error sending audio: {e}")
|
|
|
+ self.close()
|
|
|
+ def close(self):
|
|
|
+ with self.lock:
|
|
|
+ if not self.is_closed:
|
|
|
+ self.is_closed = True
|
|
|
+ try:
|
|
|
+ self.sr.stop()
|
|
|
+ except Exception as e:
|
|
|
+ print(f"Error stopping ASR: {e}")
|
|
|
|
|
|
def __test_run(self):
|
|
|
print("Thread:{} start..".format(self.__id))
|