self.is_up_error= 0 #是否上传错误id
self.Close_flag =0 #=1时,连续对话播放完音频就退出
self.AI_alarm = _ai_alarm.AI_alarm()
- self.buffer = bytearray(16384) #音频缓存区
+ self.buffer = bytearray(2048) #音频缓存区
#表情变化状态以及检索词
self.face_status = 0 # 0为正常,1为哭泣,2为愤怒,3为疑惑
self.normal_list = ["别生气"]
self.asr_pingping_timerout = 0
self.factory_timer = osTimer()
self._1S_timer = osTimer()
- self.Audio_buf_size = 16*1024
+ self.Audio_buf_size = 2*1024
self.record = audio.Record()
#self.record.end_callback(self.record_callback)
self.record.gain_set(4,10)
if self.Close_flag == 1:
self.Close_flag = 0
EventMesh.publish("publish_AI_btnInfo",{"func":self.Close})
+ screen_name = EventMesh.publish("get_screen_NAME")
if screen_name != "main_menu" and screen_name != "imei":
EventMesh.publish("load_screen", {"screen": "main_menu"})
EventMesh.publish("send_device_VC_state")#上传VC状态
self.bat_count = 0
self.reconnect_count = 0 #连续重连次数 默认十次 不过就不重连了 等下一个心跳重连
self._1S_timer = osTimer()
+ self._1scnt = 0
self._5S_timer = osTimer()
+ self._10Stimer = osTimer()
self.timer_id = 0
self.start_voice_flag = 0
self.IOT_sock = None
def send_unbind_dev_to_iot(self,event,msg):
self._5S_timer.start(30000,1,self._1M_timer_cb)
self._1S_timer.start(1000,1,self._1S_timer_cb)
+ # self._10Stimer.start(60000,1,self._10S_timer_cb)
def _1M_timer_cb(self,*argc):
EventMesh.publish("heart_IOT")#开启IOT心跳包
EventMesh.publish("send_device_information")#发送设备信息
if EventMesh.publish("get_voice_record_flag") == 1:
EventMesh.publish("stop_voice_record")
print("connect_iot_socket !!!!")
- # self.iot_close_therad()
stagecode, subcode = checknet.wait_network_connected(30)
if stagecode == 3 and subcode == 1:
_thread.stack_size(self.thread_buf_size+8*1024)
self.iot_protoclo_recv_id = _thread.start_new_thread(self.iot_protocol_recv, ())
_thread.stack_size(8*1024)
+
socket_log.info("self.IOT_sock thread init!!!!!!!!!")
- if self.IOT_sock != None:
+ if self.IOT_sock is not None:
self.start_voice_flag = 1
else:
self.start_voice_flag = 0
print("VC状态改变")
EventMesh.publish("send_device_VC_state")#上传VC状态
self.last_data = self.current_data # 保存上一次的数据状态
+ self._1scnt += 1
+ if self._1scnt >5:
+ if self.IOT_sock is not None:
+ if self.IOT_sock.getsocketsta() != 4:
+ print("iot断线重连")
+ self.iot_close_therad()
+ EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_socket})
+ EventMesh.publish("login_IOT")#登录IOT
+ else:
+ EventMesh.publish("login_IOT")#登录IOT
+ self._1scnt = 0
+
+
+ def _10S_timer_cb(self,*argc):
+ print("主动断线")
+ self.IOT_sock.close()
def socket_send_queue_thread(self):
while True:
self.reconnect_count = 0
else:
EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_socket})
- # print("socket关闭退出send线程")
- # return
- EventMesh.debug_log("socket:send data2 len:{}".format(len(data),self.IOT_sock))
+ print("socket关闭退出send线程")
+ return
+ EventMesh.debug_log("socket:send data2 len:{}".format(len(data),self.IOT_sock))
except Exception as e:
EventMesh.debug_log("socket:send error")
EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_socket})
break
-
+
def iot_protocol_recv(self):
# 配置超时参数,单位:秒
while True:
# 死循环接收数据
# with self.sock_lock: # 警告:不能加锁,不然会导致线程超长延时
- socket_flag = self.IOT_sock.getsocketsta()
- print("socket_flag = {}".format(socket_flag))
try:
data = self.IOT_sock.recv(256)
if isinstance(data, bytes) and len(data)>10:
playload_data(data)
- last_recv_time = utime.time() # 更新最后接收时间
+
socket_log.info("IOT_recv = {} ,len = {}".format(self.bytes_to_hex(data),len(data)))
#检查socket是否已关闭
if self.IOT_sock == None:
EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_socket})
return
#超时检查:如果超过设定时间未收到数据,触发重连
+ # print("接收时间差 = {}".format(utime.time() - last_recv_time))
if utime.time() - last_recv_time > RECV_TIMEOUT:
print("超过{}秒未收到数据,触发重连".format(RECV_TIMEOUT))
# 先关闭现有连接
self.IOT_sock.close()
# 触发重连
EventMesh.publish("publish_AI_btnInfo", {"func": self.connect_iot_socket})
- # 重置超时计时器
- last_recv_time = utime.time()
# 重连后短暂延迟再继续循环
utime.sleep(1)
+ # 重置超时计时器
+ last_recv_time = utime.time()
except Exception as e:
print("cli break")
print("异常是:{}".format(e))
def iot_send_queue_close(self):
if self.socket_send_queue_id != None:
+
if self.iot_soc_send_queue.empty()!=True:
self.iot_soc_send_queue.clear()
try:
_thread.stop_thread(self.socket_send_queue_id)
except Exception as e:
- print("iot_protoclo_recv_id !!!!!!!!")
- print("iot_protoclo_recv_id 1111!!!!!!!!")
+ print("iot_send_queue_close !!!!!!!!")
+ self.send_recv_flag = 0
+ print("iot_send_queue_close 1111!!!!!!!!")
self.socket_send_queue_id = None
def iot_protoclo_recv_close_therad(self):
try:
_thread.stop_thread(self.iot_protoclo_recv_id)
except Exception as e:
- print("self.iot_protoclo_recv_id !!!!!!!!")
+ print("iot_protoclo_recv_close_therad !!!!!!!!")
self.iot_protoclo_recv_id = None
if self.IOT_sock != None:
try:
return
else :
try:
- if self.start_voice_flag == 0: #如果关闭了就需要重新再
+ if self.start_voice_flag == 0: #如果关闭了就需要重新再连接
EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_socket})
return
if data != None: