|
@@ -9,7 +9,9 @@ from databases import SqlQuery, PhotoRecord, DeviceConfig, CRUD, insert_photo_re
|
|
|
from .capture.module_digicam import DigiCam
|
|
from .capture.module_digicam import DigiCam
|
|
|
from .capture.module_watch_dog import FileEventHandler
|
|
from .capture.module_watch_dog import FileEventHandler
|
|
|
from sockets.connect_manager import ConnectionManager
|
|
from sockets.connect_manager import ConnectionManager
|
|
|
-
|
|
|
|
|
|
|
+import settings,os
|
|
|
|
|
+import logging
|
|
|
|
|
+logger = logging.getLogger(__name__)
|
|
|
class LineControl(BaseClass):
|
|
class LineControl(BaseClass):
|
|
|
# sign_data = Signal(dict)
|
|
# sign_data = Signal(dict)
|
|
|
|
|
|
|
@@ -193,6 +195,7 @@ class LineControl(BaseClass):
|
|
|
self.sendSocketMessage(1, "前置拍照未完成,请稍后", device_status=-1)
|
|
self.sendSocketMessage(1, "前置拍照未完成,请稍后", device_status=-1)
|
|
|
return
|
|
return
|
|
|
print("收到货号信息", self.goods_art_no)
|
|
print("收到货号信息", self.goods_art_no)
|
|
|
|
|
+ self.saveScanData(self.goods_art_no)
|
|
|
self.handlerAction(button_value)
|
|
self.handlerAction(button_value)
|
|
|
self.photo_take_state = 0
|
|
self.photo_take_state = 0
|
|
|
if button_value in [3]:
|
|
if button_value in [3]:
|
|
@@ -214,7 +217,66 @@ class LineControl(BaseClass):
|
|
|
self.sendSocketMessage(code=0, msg="", data=message, device_status=2)
|
|
self.sendSocketMessage(code=0, msg="", data=message, device_status=2)
|
|
|
return
|
|
return
|
|
|
pass
|
|
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):
|
|
async def run(self):
|
|
|
self.is_running = True
|
|
self.is_running = True
|
|
|
while True:
|
|
while True:
|