Selaa lähdekoodia

增加扫码目录

rambo 4 päivää sitten
vanhempi
commit
c0883b04b8
4 muutettua tiedostoa jossa 83 lisäystä ja 5 poistoa
  1. 4 1
      python/config.ini
  2. 1 1
      python/mcu/DeviceControl.py
  3. 64 2
      python/mcu/LineControl.py
  4. 14 1
      python/settings.py

+ 4 - 1
python/config.ini

@@ -50,4 +50,7 @@ stop = 9
 output_dir = ..\..\..\output\
 
 [customer_template]
-template_url = http://localhost:3001
+template_url = http://localhost:3001
+
+[scan_config]
+; scan_dir = ..\..\..\scans\

+ 1 - 1
python/mcu/DeviceControl.py

@@ -20,7 +20,7 @@ from .LineControl import LineControl
 import copy
 import logging
 from mcu.capture.smart_shooter_class import SmartShooter
-
+import logging
 logger = logging.getLogger(__name__)
 
 

+ 64 - 2
python/mcu/LineControl.py

@@ -9,7 +9,9 @@ from databases import SqlQuery, PhotoRecord, DeviceConfig, CRUD, insert_photo_re
 from .capture.module_digicam import DigiCam
 from .capture.module_watch_dog import FileEventHandler
 from sockets.connect_manager import ConnectionManager
-
+import settings,os
+import logging
+logger = logging.getLogger(__name__)
 class LineControl(BaseClass):
     # sign_data = Signal(dict)
 
@@ -193,6 +195,7 @@ class LineControl(BaseClass):
                     self.sendSocketMessage(1, "前置拍照未完成,请稍后", device_status=-1)
                     return
                 print("收到货号信息", self.goods_art_no)
+                self.saveScanData(self.goods_art_no)
                 self.handlerAction(button_value)
                 self.photo_take_state = 0
             if button_value in [3]:
@@ -214,7 +217,66 @@ class LineControl(BaseClass):
             self.sendSocketMessage(code=0, msg="", data=message, device_status=2)
             return
         pass
-
+    def saveScanData(self, scan_data):
+        """保存扫码数据"""
+        if scan_data == None or scan_data == "":
+            return
+        scan_path = settings.SCAN_DIR
+        if scan_path ==None or scan_path == "":
+            return
+        try:
+            # 验证文件名的有效性
+            # 替换可能导致问题的字符
+            invalid_chars = '<>:"/\\|?*'
+            safe_scan_data = scan_data
+            for char in invalid_chars:
+                safe_scan_data = safe_scan_data.replace(char, '_')
+            # 确保文件名不为空
+            if not safe_scan_data.strip():
+                logger.info(f"扫码文件,无效的文件名")
+                return
+            
+            # 获取目录下所有txt文件
+            txt_files = [f for f in os.listdir(scan_path) if f.lower().endswith('.txt')]
+            txt_filename = safe_scan_data + ".txt"
+            new_txt_path = os.path.join(scan_path, txt_filename)
+            # 如果只有一个txt文件,则重命名并覆盖内容
+            if len(txt_files) == 1:
+                try:
+                    old_txt_path = os.path.join(scan_path, txt_files[0])
+                    os.rename(old_txt_path, new_txt_path)
+                    
+                    # 覆盖文件内容
+                    with open(new_txt_path, 'w', encoding='utf-8') as f:
+                        f.write(scan_data)
+                        
+                except Exception as e:
+                    logger.info(f"扫码文件,重命名或写入文件失败: {e}")
+                    
+                    # 如果重命名失败,创建新文件
+                    with open(new_txt_path, 'w', encoding='utf-8') as f:
+                        f.write(scan_data)
+            
+            # 如果有多个txt文件,删除它们并创建新文件
+            elif len(txt_files) > 1:
+                for txt_file in txt_files:
+                    try:
+                        txt_file_path = os.path.join(scan_path, txt_file)
+                        os.remove(txt_file_path)
+                    except Exception as e:
+                        logger.info(f"扫码文件,删除文件失败 {txt_file}: {e}")
+                
+                # 创建新的txt文件
+                with open(new_txt_path, 'w', encoding='utf-8') as f:
+                    f.write(scan_data)
+            
+            # 如果没有txt文件,直接创建新文件
+            else:
+                with open(new_txt_path, 'w', encoding='utf-8') as f:
+                    f.write(scan_data)
+                
+        except Exception as e:
+            logger.info(f"扫码文件,保存文件时发生错误: {e}")
     async def run(self):
         self.is_running = True
         while True:

+ 14 - 1
python/settings.py

@@ -359,7 +359,20 @@ def handle_remove_readonly(func, path, exc):
     os.chmod(path, stat.S_IWRITE)
     func(path)
 
-
+try:
+    __scan_dir = config.get("scan_config", "scan_dir")
+    if __scan_dir == "" or __scan_dir is None:
+        SCAN_DIR = None
+    else:
+        scan_path_obj = Path(os.path.abspath(__scan_dir))
+        SCAN_DIR = scan_path_obj.absolute()
+        if not os.path.exists(SCAN_DIR):
+            # 创建多级目录
+            os.makedirs(SCAN_DIR, exist_ok=True)
+except:
+    SCAN_DIR = None
+    print('扫码配置不存在')
+print("SCAN_DIR",SCAN_DIR)
 CUSTOMER_TEMPLATE_URL = config.get("customer_template", "template_url")