在图像处理器中引入日志记录功能,替换了多个打印语句为日志记录,增强了错误和状态信息的可追踪性。调整了异步扫描的启动和停止逻辑,确保更好的错误处理和信息反馈。
This commit is contained in:
		
							parent
							
								
									b392453b62
								
							
						
					
					
						commit
						a2fd2daff1
					
				@ -10,6 +10,7 @@ from qreader import QReader
 | 
				
			|||||||
from threading import Thread, Lock
 | 
					from threading import Thread, Lock
 | 
				
			||||||
import time
 | 
					import time
 | 
				
			||||||
import queue
 | 
					import queue
 | 
				
			||||||
 | 
					from utils.log_helper import get_logger
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ImageSubscriber(Node):
 | 
					class ImageSubscriber(Node):
 | 
				
			||||||
@ -47,6 +48,7 @@ class ImageProcessor:
 | 
				
			|||||||
        self.qreader = QReader()
 | 
					        self.qreader = QReader()
 | 
				
			||||||
        self.spin_thread = None
 | 
					        self.spin_thread = None
 | 
				
			||||||
        self.running = True
 | 
					        self.running = True
 | 
				
			||||||
 | 
					        self.log = get_logger("图像处理器")
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
        # 异步 QR 码扫描相关
 | 
					        # 异步 QR 码扫描相关
 | 
				
			||||||
        self.scan_thread = None
 | 
					        self.scan_thread = None
 | 
				
			||||||
@ -66,7 +68,7 @@ class ImageProcessor:
 | 
				
			|||||||
            while self.running:
 | 
					            while self.running:
 | 
				
			||||||
                rclpy.spin_once(self.image_subscriber, timeout_sec=0.1)
 | 
					                rclpy.spin_once(self.image_subscriber, timeout_sec=0.1)
 | 
				
			||||||
        except Exception as e:
 | 
					        except Exception as e:
 | 
				
			||||||
            print(f"Spin thread error: {e}")
 | 
					            self.log.error(f"Spin线程错误: {e}", "错误")
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    def destroy(self):
 | 
					    def destroy(self):
 | 
				
			||||||
        self.running = False
 | 
					        self.running = False
 | 
				
			||||||
@ -94,7 +96,7 @@ class ImageProcessor:
 | 
				
			|||||||
            interval: 扫描间隔,单位秒
 | 
					            interval: 扫描间隔,单位秒
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        if self.scan_thread is not None and self.scan_thread.is_alive():
 | 
					        if self.scan_thread is not None and self.scan_thread.is_alive():
 | 
				
			||||||
            print("异步扫描已经在运行中")
 | 
					            self.log.warning("异步扫描已经在运行中", "警告")
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
        self.enable_async_scan = True
 | 
					        self.enable_async_scan = True
 | 
				
			||||||
@ -102,14 +104,14 @@ class ImageProcessor:
 | 
				
			|||||||
        self.scan_thread = Thread(target=self._async_scan_worker, args=(interval,))
 | 
					        self.scan_thread = Thread(target=self._async_scan_worker, args=(interval,))
 | 
				
			||||||
        self.scan_thread.daemon = True  # 设为守护线程,主线程结束时自动结束
 | 
					        self.scan_thread.daemon = True  # 设为守护线程,主线程结束时自动结束
 | 
				
			||||||
        self.scan_thread.start()
 | 
					        self.scan_thread.start()
 | 
				
			||||||
        print("启动异步 QR 码扫描线程")
 | 
					        self.log.info("启动异步 QR 码扫描线程", "启动")
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    def stop_async_scan(self):
 | 
					    def stop_async_scan(self):
 | 
				
			||||||
        """停止异步 QR 码扫描"""
 | 
					        """停止异步 QR 码扫描"""
 | 
				
			||||||
        self.enable_async_scan = False
 | 
					        self.enable_async_scan = False
 | 
				
			||||||
        if self.scan_thread and self.scan_thread.is_alive():
 | 
					        if self.scan_thread and self.scan_thread.is_alive():
 | 
				
			||||||
            self.scan_thread.join(timeout=1.0)
 | 
					            self.scan_thread.join(timeout=1.0)
 | 
				
			||||||
            print("异步 QR 码扫描线程已停止")
 | 
					            self.log.info("异步 QR 码扫描线程已停止", "停止")
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    def _async_scan_worker(self, interval):
 | 
					    def _async_scan_worker(self, interval):
 | 
				
			||||||
        """异步扫描工作线程"""
 | 
					        """异步扫描工作线程"""
 | 
				
			||||||
@ -131,10 +133,10 @@ class ImageProcessor:
 | 
				
			|||||||
                            if qr_data:
 | 
					                            if qr_data:
 | 
				
			||||||
                                self.last_qr_result = qr_data
 | 
					                                self.last_qr_result = qr_data
 | 
				
			||||||
                                self.last_qr_time = current_time
 | 
					                                self.last_qr_time = current_time
 | 
				
			||||||
                                print(f"异步扫描到 QR 码: {qr_data}")
 | 
					                                self.log.success(f"异步扫描到 QR 码: {qr_data}", "扫描")
 | 
				
			||||||
                    except Exception as e:
 | 
					                    except Exception as e:
 | 
				
			||||||
                        self.is_scanning = False
 | 
					                        self.is_scanning = False
 | 
				
			||||||
                        print(f"异步 QR 码扫描出错: {e}")
 | 
					                        self.log.error(f"异步 QR 码扫描出错: {e}", "错误")
 | 
				
			||||||
                
 | 
					                
 | 
				
			||||||
                last_scan_time = current_time
 | 
					                last_scan_time = current_time
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user