import constants
import versions
-dev_type = "G10"
-#dev_type = "7PIN"
+#dev_type = "G10"
+dev_type = "7PIN"
class AI_led(Abstract):
def __init__(self):
self.led_flicker_timer.start(1500, 1, self.led_flicker_timer_cb)
def led_flicker_timer_cb(self, *args, **kwargs):
- EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_dev_timer_event})
+ EventMesh.publish("publish_AI_btnInfo",{"func":self.led_flicker_timer_cb_})
def led_flicker_timer_cb_(self):
if self.led_flicker_flag == 0:
self.led_flicker_timer.stop()
self.dev_res_timer.start(10000, 1, self.dev_timer_cb)
self.dev_res_timerCount = 0
self.ud_token_timer = osTimer()
- self.ud_token_timer.start(30000, 1, self.ud_token_timer_cb)
+ self.ud_token_timer.start(10000, 1, self.ud_token_timer_cb)
self.ud_token_timer_Count = 0
self.dev_token = None
self.res_error_count = 0
try:
#print("ud_token_timer_!!!!")
self.ud_token_timer_Count+=1
- if self.ud_token_timer_Count >= 2000: #定时30*2000s时更新token
+ if self.ud_token_timer_Count >= 720: #定时10*3000s时更新token
self.ud_token_timer_Count = 0
- EventMesh.publish_async("update_dev_token",thread_size = 8192)
+ EventMesh.publish_async("dev_sign_in",thread_size = 8192)
except Exception as e:
usys.print_exception(e)
def get_sim_Status(self):
dev_pass = ""
if len(dev_pass) == 0:#没有密码
return
- if token == None or token == "":
- if 1: #返回结果成功
- #获取本地保存的密码
- decrypt_data=EventMesh.publish("get_storage_config","dev_pass")
- # decrypt_data = self.aes_CBC_Decrypt(password, self.key, iv) #对服务器下发的密码进行解密
- print("decrypt_data {}".format(decrypt_data))
- #随机生成16个字符串 设备生成做加密使用
- random_string = self.generate_random_string(16) #随机的16个字符串
- enc_iv = random_string #加密的IV偏移量
- #设备端进行密码加密
- encrypt_data = self.aes_CBC_Encrypt(decrypt_data, self.key, enc_iv)
- print("encrypt_data {}".format(encrypt_data))
-
- #登入数据
- deviceId = self.deviceId
- sign_in_data = {
- "deviceId":"{}".format(deviceId), #设备ID
- "password":"{}".format(encrypt_data), #加密的数据
- "nonce":"{}".format(random_string) #随机的16个字符串
- }
- print(ujson.dumps(sign_in_data))
- sign_url = "http://dmp.raisound.com/bz-api/dev/auth/sign_in" #登入地址
- headers_T ={"Content-Type":"application/json"}
+ #if token == None or token == "":
+ if 1: #返回结果成功
+ #获取本地保存的密码
+ decrypt_data=EventMesh.publish("get_storage_config","dev_pass")
+ # decrypt_data = self.aes_CBC_Decrypt(password, self.key, iv) #对服务器下发的密码进行解密
+ print("decrypt_data {}".format(decrypt_data))
+ #随机生成16个字符串 设备生成做加密使用
+ random_string = self.generate_random_string(16) #随机的16个字符串
+ enc_iv = random_string #加密的IV偏移量
+ #设备端进行密码加密
+ encrypt_data = self.aes_CBC_Encrypt(decrypt_data, self.key, enc_iv)
+ print("encrypt_data {}".format(encrypt_data))
+
+ #登入数据
+ deviceId = self.deviceId
+ sign_in_data = {
+ "deviceId":"{}".format(deviceId), #设备ID
+ "password":"{}".format(encrypt_data), #加密的数据
+ "nonce":"{}".format(random_string) #随机的16个字符串
+ }
+ print(ujson.dumps(sign_in_data))
+ sign_url = "http://dmp.raisound.com/bz-api/dev/auth/sign_in" #登入地址
+ headers_T ={"Content-Type":"application/json"}
+ response_result = 0
+ response = 0
+ try:
+ response = request.post(sign_url,data=ujson.dumps(sign_in_data),headers = headers_T,timeout=5)
+ response_result = 1
+ except Exception as e:
+ usys.print_exception(e)
+ print("post error!!!!!\r\n")
response_result = 0
- response = 0
- try:
- response = request.post(sign_url,data=ujson.dumps(sign_in_data),headers = headers_T,timeout=5)
- response_result = 1
- except Exception as e:
- usys.print_exception(e)
- print("post error!!!!!\r\n")
- response_result = 0
- # {'msg': '', 'code': 20001, 'success': True,
- # 'data': {'deviceId': '876226002022574',
- # 'token': '95436a9e09fd0ff89feb8825257dca982f'}
- # }
-
- #self.dev_token = ""
- if response_result == 1:
- if response.status_code == 200:
- content = response.content
- c = next(content)
- data = ujson.loads(c)
- print("data-{}".format(data))
- if data != None:
- if "success" in data:
- if data['success'] == True: #登入成功
- if "data" in data:
- data_tmp = data["data"]
- if "token" in data_tmp: #token
- self.dev_token = data_tmp["token"]
- if data["code"] == 50003:#时间错误
- self.ntptime_flag = 0
- print("token:{}\r\n".format(self.dev_token))
- if response != None:
- response.close()
- pass
+ # {'msg': '', 'code': 20001, 'success': True,
+ # 'data': {'deviceId': '876226002022574',
+ # 'token': '95436a9e09fd0ff89feb8825257dca982f'}
+ # }
+
+ #self.dev_token = ""
+ if response_result == 1:
+ if response.status_code == 200:
+ content = response.content
+ c = next(content)
+ data = ujson.loads(c)
+ print("data-{}".format(data))
+ if data != None:
+ if "success" in data:
+ if data['success'] == True: #登入成功
+ if "data" in data:
+ data_tmp = data["data"]
+ if "token" in data_tmp: #token
+ self.dev_token = data_tmp["token"]
+ if data["code"] == 50003:#时间错误
+ self.ntptime_flag = 0
+ print("token:{}\r\n".format(self.dev_token))
+ if response != None:
+ response.close()
+ pass
except Exception as e:
print("post error!!!!!\r\n")
response_result = 0
# AI_client_tts_num = 2
AI_client_VC_num = 3
AI_client_asr_num = 4
+
+
class AI_VC(Abstract):
# AI
self.play_fd = None
self.is_VC_ACK = 0 #是否开启ack应答模式
self.ai_OTA = OTA()
-
+ self.is_auth = 1 #是否开启dmp鉴权
#self.VC_mode = 0 #0:短按连续问答模式 1:长按录音模式
self.VC_mode = EventMesh.publish("get_storage_config","VC_mode")#
if self.VC_mode == None :
self.VC_mode = 0
self.VC_server = 2 #0正式服务器,1:测试服务器1,2:测试服务器2
+
self.server_ip = EventMesh.publish("get_storage_config","server_ip")#
if self.server_ip == None:
if self.VC_server == 1:#
"bot_name":"柒先生",
"voice":"huoshan::zh_male_sunwukong_mars_bigtts" ,
"lang":"zh-cn"
+ },
+ "英语":{
+ "bot_name":"柒先生",
+ "voice":"huoshan::en_male_smith_mars_bigtts" ,
+ "lang":"zh-cn"
}
+
}
self.AI_ASR_send_data_queue = Queue(200)
self.AI_VC_send_data_queue = Queue(100)
self.AI_audio_queue = Queue(100)
+ self.AI_WS_connect_Queue = Queue(100) #释放用
self.AI_TTS_str = ""
self.AI_ASR_str = ""
self.AI_ASR_flag = 0
self.Audio_buf_size = 16*1024
self.record = audio.Record()
#self.record.end_callback(self.record_callback)
- self.record.gain_set(4,10)
+ #self.record.gain_set(4,10)
+ self.record.gain_set(4,12)
self.aud = audio.Audio(0)
self.aud.setCallback(self.audio_cb)
#self.aud.setVolume(self.VC_TTS_volume //10)
self.tts_in_data_len = 0
self.tts_out_data_len = 0
self.VC_satrt_flag = 0
+ self.session_id = ""
EventMesh.subscribe("AI_key_press", self.AI_key_press)
EventMesh.subscribe("AI_key_release", self.AI_key_release)
EventMesh.subscribe("AI_key_long_press", self.AI_key_long_press)
print("not lang!!!!!!!!!!!!!!!!!!!!!!")
except Exception as e:
usys.print_exception(e)
- req_str = "asr/ws?is_auth=0{}{}".format(platform,lang)
+ req_str = "asr/ws?is_auth={}&{}&{}".format(self.is_auth,platform,lang)
print("req_str:{}".format(req_str))
token = EventMesh.publish("get_dev_token")
print("token:{}".format(token))
url = 'ws://{}/{}&dt={}'.format(self.server_ip,req_str,token)
print("websocket_connect_asr URL:{}".format(url))
self.AI_client_asr = uwebsocket.Client.connect(url, debug=True)#
-
+ self.AI_WS_connect_Queue.put(self.AI_client_asr)
except Exception as e: #网络连接异常
usys.print_exception(e)
print("[Errno 22] EINVAL!!!\r\n")
print("[Errno 22] EINVAL!!!\r\n")
print("[Errno 22] EINVAL!!!\r\n")
- EventMesh.publish("set_storage_config",["powerRestart",1])#调试临时关闭
- utime.sleep_ms(1000)
- self.aud.stopPlayStream()
- self.aud.stopAll()
- Power.powerRestart()
+ # EventMesh.publish("set_storage_config",["powerRestart",1])#调试临时关闭
+ # utime.sleep_ms(1000)
+ # self.aud.stopPlayStream()
+ # self.aud.stopAll()
+ # Power.powerRestart()
self.websocket_error()
return
self.AI_client_asr_debug+=1
utime.sleep_ms(10)
- self.AI_client_asr.settimeout(6)
+ #self.AI_client_asr.settimeout(6)
#开启发送线程
if self.AI_ASR_send_data_queue_thread_id == 0:
_thread.stack_size(self.Audio_buf_size+8*1024)
#self.AI_ASR_send_data_queue_thread_close()#关闭发送线程
self.AI_client_asr_recv_thread_close()#关闭读线程
if self.AI_client_asr_isconnect == 1 or self.AI_client_asr!= None:
- self.AI_client_asr_isconnect = 0
- count = 500
- while True:#等待发完数据
- count-=1
- if count <=0 or self.AI_ASR_send_data_queue_thread_lock == 0:
- break
- print("等待发送完成:{}".format(count))
- if self.AI_ASR_send_data_queue.empty()!=True:
- self.AI_ASR_send_data_queue.clear()
- utime.sleep_ms(1)
-
try:
- self.AI_client_asr_StopTranscription_requset()#发送停止ASR
- self.AI_client_asr.close()
-
- #self.AI_client_asr = None
- except Exception as e:
- usys.print_exception(e)
- utime.sleep_ms(200)#再尝试关闭一次
+ self.AI_client_asr_isconnect = 0
+ count = 500
+ while True:#等待发完数据
+ count-=1
+ if count <=0 or self.AI_ASR_send_data_queue_thread_lock == 0:
+ break
+ print("等待发送完成:{}".format(count))
+ if self.AI_ASR_send_data_queue.empty()!=True:
+ self.AI_ASR_send_data_queue.clear()
+ utime.sleep_ms(1)
+
try:
+ self.AI_client_asr_StopTranscription_requset()#发送停止ASR
self.AI_client_asr.close()
- #self.AI_client_asr = None
except Exception as e:
usys.print_exception(e)
- self.AI_client_asr_debug-=1
+ utime.sleep_ms(200)#再尝试关闭一次
+ try:
+ self.AI_client_asr.close()
+
+ except Exception as e:
+ usys.print_exception(e)
+ self.AI_client_asr_debug-=1
+ try:
+ self.AI_client_asr._close()
+ except Exception as e:
+ usys.print_exception(e)
+ self.AI_client_asr = None
+ except Exception as e:
+ usys.print_exception(e)
print("websocket_deconnect_asr {}!!!!!!!".format(self.AI_client_asr_isconnect))
#websocket发送数据
usys.print_exception(e)
self.AI_client_asr_recv_id = 0
- #Ai VC websocket返回数据
+ #Ai ASR websocket返回数据
def AI_client_asr_recv(self,cli):
while True:
try:
except Exception as e:
error_str = str(e)
print("ASR ASR:e:{}".format(error_str))
- usys.print_exception(e)
+ #usys.print_exception(e)
if error_str.find("[Errno 110]")!= -1:
#print("ASR ASR:recv TIMEDOUT 正常")
pass
print("AI_client_asr_recv END!!!!!!!!!!!!!!!!!!!!!!!!!!!")
#ASR开始会话
def AI_client_asr_start_requset(self):
-
+ self.session_id = self.get_session_id()
json_data = \
{
"header":{
"payload":{
"format":"pcm",
"sample_rate":self.VC_record_samplerate,
+ "session_id": self.session_id ,
}
}
except Exception as e:
usys.print_exception(e)
if dev_type == "G10":
- #platform = "platform=huoshan&is_bot=1"
- platform = "&platform=raisound"
+ platform = "platform=huoshan&is_boot=1"
+ #platform = "platform=raisound"
+ #platform = "platform=aliyun"
+
else:
- platform ="platform=huoshan"# "platform=raisound"
-
+ platform = "platform=huoshan"
+ #platform = "platform=huoshan&is_boot=1"
#女,男,童,粤,川
self.bot_name = "柒先生"
try:
# self.AI_client_VC = uwebsocket.Client.connect('wss://nls-gateway-cn-beijing.aliyuncs.com/ws/v1',headers=Headers, debug=False)
token = EventMesh.publish("get_dev_token")
print("token:{}".format(token))
- req_str = "voice-chat/ws?is_auth=0&{}&{}".format(platform,lang)
+ req_str = "voice-chat/ws?is_auth={}&{}&{}".format(self.is_auth,platform,lang)
url = 'ws://{}/{}&dt={}'.format(self.server_ip,req_str,token)
print("VC VC:url{}".format(url))
self.AI_client_VC = uwebsocket.Client.connect(url, debug=True)
+ self.AI_WS_connect_Queue.put(self.AI_client_VC)
# if self.VC_server == 1:#
# #柒牌服务器地址sevenpin.qipai.com.cn
# #北科服务器118.145.158.207:22082
print("[Errno 22] EINVAL!!!\r\n")
print("[Errno 22] EINVAL!!!\r\n")
self.websocket_error()
- EventMesh.publish("set_storage_config",["powerRestart",1])#调试临时关闭
- utime.sleep_ms(1000)
- self.aud.stopPlayStream()
- self.aud.stopAll()
- Power.powerRestart()
+ # EventMesh.publish("set_storage_config",["powerRestart",1])#调试临时关闭
+ # utime.sleep_ms(1000)
+ # self.aud.stopPlayStream()
+ # self.aud.stopAll()
+ # Power.powerRestart()
self.websocket_error()
return
return
self.AI_client_VC_debug+=1
utime.sleep_ms(10)
- self.AI_client_VC.settimeout(6)
+ #self.AI_client_VC.settimeout(6)
#self.AI_VC_send_data_queue_thread_close()#关闭发送线程
if self.AI_VC_send_data_queue_thread_id == 0:
_thread.stack_size(self.Audio_buf_size+8*1024)
def websocket_deconnect_VC(self):
- print("websocket_deconnect_VC {}!!!!!!!".format(self.AI_client_VC_isconnect))
+ #print("websocket_deconnect_VC {}!!!!!!!".format(self.AI_client_VC_isconnect))
print("websocket_deconnect_VC!!!!!!!")
self.AI_client_VC_recv_thread_close()#关闭读线程
self.AI_client_VC_recv_id = 0
if self.AI_client_VC_isconnect == 1 or self.AI_client_VC!=None:
- self.AI_client_VC_isconnect = 0
- count = 500
- while True:#等待发完数据
- count-=1
- if count <=0 or self.AI_VC_send_data_queue_thread_lock == 0:
- break
- utime.sleep_ms(1)
-
try:
- self.AI_client_VC.close()
- self.AI_client_VC_debug -=1
- self.AI_client_VC=None
- except Exception as e:
- usys.print_exception(e)
- try :
+ self.AI_client_VC_isconnect = 0
+ count = 500
+ while True:#等待发完数据
+ count-=1
+ if count <=0 or self.AI_VC_send_data_queue_thread_lock == 0:
+ break
+ utime.sleep_ms(1)
+
+ try:
self.AI_client_VC.close()
self.AI_client_VC_debug -=1
- self.AI_client_VC=None
+ #self.AI_client_VC=None
+ except Exception as e:
+ usys.print_exception(e)
+ try :
+ self.AI_client_VC.close()
+ self.AI_client_VC_debug -=1
+ #self.AI_client_VC=None
+ except Exception as e:
+ usys.print_exception(e)
+ try :
+ self.AI_client_VC._close()
except Exception as e:
usys.print_exception(e)
+ self.AI_client_VC = None
+ except Exception as e:
+ usys.print_exception(e)
print("websocket_deconnect_VC {}!!!!!!!".format(self.AI_client_VC_isconnect))
#websocket发送ping
def AI_client_VC_ping(self):
#print("VC: send_data len:{}".format(len(data[1])))
if self.AI_client_VC != None and self.AI_client_VC_isconnect == 1:
self.AI_client_VC.send(data[1])
+ else:
+ print("VC VC: send_data:ERROR:{}".format(data[1]))
#print("VC: send_data end")
#print("VC: send_data end")
self.LocalRespondingEnded_timerout = 10 #最长倒数10秒发送音频结束或者终止对话
if self.VC_mode == 0 :#连续问答模式
#EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_Speech_End})#调试临时打开
- EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_Speech_Start})#开始录音
+ #EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_Speech_Start})#开始录音 调试临时关闭
+ pass
if self.play_data_save_flag == 1:
if self.play_fd !=None:
self.play_fd.close()
else:#下发音频数据
#self.AI_ASR_str = ""#下发音频后清掉asr结果
- print("下发音频数据1:{}".format(len(recv_data)))
+ #print("下发音频数据1:{}".format(len(recv_data)))
if self.play_data_save_flag == 1:
if self.play_fd !=None:
#print("下发音频数据1:{}".format(len(data[0])))
ret=self.AI_audio_queue.put(data)
self.tts_in_data_len += len(data[0])
- print("下发音频数据2:{},{}".format(len(data[0]),self.tts_in_data_len))
+ #print("下发音频数据2:{},{}".format(len(data[0]),self.tts_in_data_len))
def check_text(self,text):
if len(text)>60:
return -1
#self.aud.play(1,0,'U:/static/peizhichenggong.mp3')#切换成功
self.AI_TTS_str = "更改成功"
return 0
+ if text.find("英语") != -1 :
+ if "英语" in self.VC_TTS_voice_type:
+ self.VC_TTS_voice_key = "英语"
+ EventMesh.publish("set_storage_config",["VC_TTS_voice_key",self.VC_TTS_voice_key])
+ # EventMesh.publish("audio_montage_play",["peizhichenggong.mp3"])
+ #self.aud.play(1,0,'U:/static/peizhichenggong.mp3')#切换成功
+ self.AI_TTS_str = "更改成功"
+ return 0
self.AI_TTS_str = "更改失败,没找到对应声音"
return 0
if text.find("切换对话模式") != -1:
sign_data = ubinascii.hexlify(res)
sign_data = sign_data.decode('ascii')
return sign_data
+
+ def get_session_id(self,data = "session_id"):
+ t = utime.mktime(utime.localtime())
+ t = t*1000+urandom.randint(0, 1000)
+ md5_data = "{}{}".format(data,t)#md5签名
+ sign_data = ""
+ hash_obj = uhashlib.md5(md5_data)
+ res = hash_obj.digest()
+
+ # 转成十六进制表示
+ sign_data = ubinascii.hexlify(res)
+ sign_data = sign_data.decode('ascii')
+ return sign_data
+
def get_new_message_id(self):
message_id = self.get_trace_id()+"aaa"+self.get_trace_id()
self.VC_message_id=message_id[:32]
"voice":voice,#self.VC_TTS_voice_type[str(self.VC_TTS_voice_key)],
"voice_detection":"enabled",
"simple_response":True,
+ "session_id": self.session_id ,
"custom_variables":{
"bot_name":"{}".format(self.bot_name)
}
},
"payload":
{
- "dialog_id":self.VC_dialog_id
+ "dialog_id":self.VC_dialog_id,
+ "session_id": self.session_id ,
}
}
self.AI_client_VC_send(ujson.dumps(json_data))
"event":"StopSpeech"
},
"payload":{
- "dialog_id":self.VC_dialog_id
+ "dialog_id":self.VC_dialog_id,
+ "session_id": self.session_id ,
}
}
self.AI_client_VC_send(ujson.dumps(json_data))
"event":"LocalRespondingStarted"
},
"payload":{
- "dialog_id":self.VC_dialog_id
+ "dialog_id":self.VC_dialog_id,
+ "session_id": self.session_id ,
}
}
self.AI_client_VC_send(ujson.dumps(json_data))
"event":"LocalRespondingEnded"
},
"payload":{
- "dialog_id":self.VC_dialog_id
+ "dialog_id":self.VC_dialog_id,
+ "session_id": self.session_id ,
}
}
EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_init})
EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_VC_send,"data":ujson.dumps(json_data)})
+ json_data = \
+ {
+ "header":{
+ "trace_id":self.get_trace_id(),
+ "event":"AAAAAAAAAAAAAA1"
+ },
+ "payload":{
+ "dialog_id":self.VC_dialog_id,
+ "session_id": self.session_id ,
+ }
+ }
+ EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_VC_send,"data":ujson.dumps(json_data)})
# utime.sleep_ms(1)
# self.AI_client_VC_send(ujson.dumps(json_data))
self.VC_state = VC_State_End
"event":"RequestToSpeak"
},
"payload":{
- "dialog_id":self.VC_dialog_id
+ "dialog_id":self.VC_dialog_id,
+ "session_id": self.session_id ,
}
}
print("AI_client_VC_RequestToSpeak!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
},
"payload":{
"dialog_id":self.VC_dialog_id,
+ "session_id": self.session_id ,
"type":"transcript",#prompt是跳过asr,transcript是跳过asr和大模型
"text":""
}
if self.AI_ASR_send_data_queue.empty()!=True:
self.AI_ASR_send_data_queue.clear()
self.AI_ASR_send_buf_len = 0
+ #self.AI_WS_connect_Queue.put(self.AI_client_VC)
+
+ try:
+ while True:
+ if self.AI_WS_connect_Queue.empty()!=True:
+ AI_WS_connect = self.AI_WS_connect_Queue.get()
+ if AI_WS_connect :
+ try:
+ AI_WS_connect._close()
+ AI_WS_connect = None
+ except Exception as e:
+ usys.print_exception(e)
+ else:
+ print("websocket链接已为空")
+ else:
+ break
+ except Exception as e:
+ usys.print_exception(e)
def VC_init2(self):
self.VC_init()