IOT断线重连解决,5S检查一次
authorazao <821478748@qq.com>
Thu, 18 Sep 2025 14:22:35 +0000 (22:22 +0800)
committerazao <821478748@qq.com>
Thu, 18 Sep 2025 14:22:35 +0000 (22:22 +0800)
2.sdk/project/K8/sdk/services/microPython/ports/quectel/core/ai_VC.py
2.sdk/project/K8/sdk/services/microPython/ports/quectel/core/protocol.py

index 3260fad..292a814 100644 (file)
@@ -1388,7 +1388,7 @@ class AI_VC(Abstract):
         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 = ["别生气"]
@@ -2030,7 +2030,7 @@ class AI_VC(Abstract):
         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)
@@ -4085,6 +4085,7 @@ class AI_VC(Abstract):
                     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状态
index aa04f5d..1125690 100644 (file)
@@ -529,7 +529,9 @@ class SockMangager(Abstract):
         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
@@ -568,6 +570,7 @@ class SockMangager(Abstract):
     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")#发送设备信息
@@ -615,7 +618,6 @@ class SockMangager(Abstract):
         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:
@@ -656,10 +658,11 @@ class SockMangager(Abstract):
                     _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
@@ -756,6 +759,22 @@ class SockMangager(Abstract):
             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:
@@ -769,14 +788,14 @@ class SockMangager(Abstract):
                             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):
         # 配置超时参数,单位:秒
@@ -785,13 +804,11 @@ class SockMangager(Abstract):
         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:
@@ -799,6 +816,7 @@ class SockMangager(Abstract):
                         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))
                         # 先关闭现有连接
@@ -806,10 +824,10 @@ class SockMangager(Abstract):
                             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))
@@ -844,13 +862,15 @@ class SockMangager(Abstract):
     
     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):
@@ -858,7 +878,7 @@ class SockMangager(Abstract):
             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:
@@ -894,7 +914,7 @@ class SockMangager(Abstract):
             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: