From c091a55cb0d31e19f79c011de3189ca3fc82a441 Mon Sep 17 00:00:00 2001 From: zhu-mengmeng <15588200382@163.com> Date: Tue, 17 Jun 2025 14:12:34 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=E6=89=98=E7=9B=98?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E8=8E=B7=E5=8F=96=E9=80=BB=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=89=98=E7=9B=98=E6=96=99=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E5=90=8D=E7=A7=B0=EF=BC=8C=E4=BC=98=E5=8C=96=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E5=AF=B9=E8=AF=9D=E6=A1=86=E5=92=8C=E4=B8=BB=E7=AA=97=E5=8F=A3?= =?UTF-8?q?=E6=89=98=E7=9B=98=E5=8F=B7=E5=A4=84=E7=90=86=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E4=BA=86=20API=20=E5=92=8C=E6=9C=AC=E5=9C=B0=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=BA=90=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apis/tary_api.py | 2 +- config/app_config.json | 15 ++-- dao/pallet_type_dao.py | 83 +++++++++-------- db/jtDB.db | Bin 65536 -> 65536 bytes ui/loading_dialog_ui.py | 69 ++++---------- ui/main_window_ui.py | 9 +- ui/unloading_dialog_ui.py | 28 +++--- utils/pallet_type_manager.py | 44 +++++---- widgets/loading_dialog_widget.py | 56 ++++++++---- widgets/main_window.py | 140 +++++++++++++++++++---------- widgets/unloading_dialog_widget.py | 68 ++++++++++++-- 11 files changed, 296 insertions(+), 218 deletions(-) diff --git a/apis/tary_api.py b/apis/tary_api.py index b0ab575..acda137 100644 --- a/apis/tary_api.py +++ b/apis/tary_api.py @@ -60,7 +60,7 @@ class TaryApi: "tp_note": tray_info.get("tp_note", ""), # 托盘号 "product_name": tray_info.get("zx_name", ""), # 产品名称 "axis_type": tray_info.get("zx", ""), # 轴型 - "material": str(tray_info.get("cs", "")), # 托盘料 + "tier": str(tray_info.get("cs", "")), # 托盘料 "weight": str(tray_info.get("zl", "")), # 重量 "quantity": "" # 数量先空下 } diff --git a/config/app_config.json b/config/app_config.json index a3985df..243cfd4 100644 --- a/config/app_config.json +++ b/config/app_config.json @@ -7,7 +7,8 @@ "enable_keyboard_listener": false, "enable_camera": false }, - "base_url":"http://192.168.0.133:8080" + "base_url": "http://localhost:8084", + "mode": "standalone" }, "apis": { "get_tray_info": "/apjt/xcsc/tpda/getByTp_note/" @@ -47,10 +48,11 @@ "host": "localhost", "port": "5020" }, - "serial":{ - "keyboard":{ - "trigger_key":"Key.page_up" - },"mdz":{ + "serial": { + "keyboard": { + "trigger_key": "Key.page_up" + }, + "mdz": { "bit": 10, "code": "mdz", "data_bits": 8, @@ -61,7 +63,8 @@ "ser": "COM5", "stop_bits": 1, "timeout": 1 - },"cz":{ + }, + "cz": { "bit": 10, "code": "cz", "data_bits": 8, diff --git a/dao/pallet_type_dao.py b/dao/pallet_type_dao.py index e4cc523..3fd2f90 100644 --- a/dao/pallet_type_dao.py +++ b/dao/pallet_type_dao.py @@ -14,53 +14,20 @@ class PalletTypeDAO: if hasattr(self, 'db'): self.db.close() - def get_all_pallet_types(self, include_disabled=False): - """获取所有托盘类型 - - Args: - include_disabled: 是否包含禁用的类型 - - Returns: - list: 托盘类型列表 - """ + def get_pallet_code(self): + """获取托盘号""" try: - if include_disabled: - sql = """ - SELECT id, type_name, operation_type, description, enabled, sort_order - FROM wsbz_pallet_types + sql = """ + SELECT DISTINCT pallet_code + FROM wsbz_pallet_archives WHERE is_deleted = FALSE - ORDER BY sort_order - """ - params = () - else: - sql = """ - SELECT id, type_name, operation_type, description, enabled, sort_order - FROM wsbz_pallet_types - WHERE is_deleted = FALSE AND enabled = TRUE - ORDER BY sort_order - """ - params = () - - self.db.cursor.execute(sql, params) + """ + self.db.cursor.execute(sql) results = self.db.cursor.fetchall() - - pallet_types = [] - for row in results: - pallet_type = { - 'id': row[0], - 'type_name': row[1], - 'operation_type': row[2], - 'description': row[3], - 'enabled': bool(row[4]), - 'sort_order': row[5] - } - pallet_types.append(pallet_type) - - return pallet_types + return [row[0] for row in results] except Exception as e: - logging.error(f"获取托盘类型失败: {str(e)}") + logging.error(f"获取托盘号失败: {str(e)}") return [] - def get_pallet_type_by_pallet_id(self, pallet_code): """根据托盘号获取托盘类型 @@ -84,7 +51,37 @@ class PalletTypeDAO: except Exception as e: logging.error(f"获取托盘类型失败: {str(e)}") return None - + def get_pallet_info_by_pallet_id(self, pallet_code): + """根据托盘号获取托盘信息 + + Args: + pallet_code: 托盘号 + + Returns: + dict: 托盘信息,未找到则返回None + """ + try: + sql = """ + SELECT pallet_code, + pallet_name, + description, + axios_name, + axios_type, + tier, + size, + amount, + weight + FROM wsbz_pallet_archives t1 + WHERE pallet_code = ? AND t1.is_deleted = FALSE + """ + params = (pallet_code,) + self.db.cursor.execute(sql, params) + row = self.db.cursor.fetchone() + if row: + return row + except Exception as e: + logging.error(f"获取托盘类型失败: {str(e)}") + return None def get_pallet_types_by_operation(self, operation_type, include_disabled=False): """根据操作类型获取托盘类型 diff --git a/db/jtDB.db b/db/jtDB.db index fdbd6c241a9ed7db0734cda5f7496a43c362dad7..f553f26f16ce21bc47d18b3d62ee542362ed091e 100644 GIT binary patch delta 945 zcmZo@U}*NLUI2f}S`11Iyd0%p_8Ir8 z{fy#Ft~y)_Kv0}nl?tLkpe(T{IU})1!_+_%BAJ+*Uz%3}k<831Nli~J;?hwlPt8ov zC{aku$xkfVT=TbuX>%^y0}EzuX6MO;_M($xf3h=kNN{h?x1VGn40H(_??eXvTE3rr z^Z8tPU-M4fEGSURD;&Wg#Go$8$!IKTEXe_OF~eksx9$QF?5N_C1Lk=NN3fv^0@Y9c z15(V2Dn9uiP;nz8ix7h<%xGyzH4zFhhD?@fS+}R7n6_}$zi6Uuo&4K$|h*SWK z_R0GhMJBGd+g$haC}S{ru@BLen^=-sl$n@=mS#3r|7``Pg<}T=*bJ=v;zT;P=H?9AB7z3HbjlM{JP;?ktQCjojS>jwY; delta 698 zcmZo@U}-(=7$>*fXWI2h+J@a6GY^Sk{oXhsWf|--qd2*q>=;RAO z*qJ#bI5+3pPcjhZU&X-2JB@*N8h;4iU%q90{=7dn3ko#x3MMe~GpI^(avDn-OL8zU zs4`9#d+#FU6%ycLWNKh$U|?X(63W5Ppf1VD2v#zAVXYT$C_9qKq^^MG#7Vc^Z-_u%`;H;d1i z_YKg^5xhcm%mN5EORK6fDuNt6`C7deFOA)rgydF8Q1PM!)usjF3e1~8@tQQb=D_{Q z3IgH$Ev7t>sN{u5<>tDB*BIF|^GZ_FQ;Q}y{4(2I{kIhuea8+8uo+kxnp&A|Ho3lo x1IGSzUqpc88v}1Gw=2gtU=Y0E;BWB(rVnRl#twOyq0*Zt|4?QU 0 else "", + "product_name": pallet_info[1] if len(pallet_info) > 1 else "", + "axis_type": pallet_info[4] if len(pallet_info) > 4 else "", + "tier": str(pallet_info[5]) if len(pallet_info) > 5 else "", + "weight": str(pallet_info[8]) if len(pallet_info) > 8 else "", + "quantity": "" + } + } + logging.info(f"托盘信息响应: {response}") logging.info(f"response.success={response.get('success')}, response.data存在={response.get('data') is not None}") @@ -72,20 +98,17 @@ class LoadingDialog(LoadingDialogUI): # 显示托盘相关信息 - 只更新轴型、托盘料和重量,不更新订单号和产品 axis_type = str(tray_data.get("axis_type", "--")) - material = str(tray_data.get("material", "--")) + tier = str(tray_data.get("tier", "--")) weight = str(tray_data.get("weight", "--")) - logging.info(f"显示托盘信息: 轴型={axis_type}, 托盘料={material}, 重量={weight}") + logging.info(f"显示托盘信息: 轴型={axis_type}, 托盘料={tier}, 重量={weight}") # 只设置轴型、托盘料和重量字段,不设置产品名称 self.axis_value.setText(axis_type) - self.pallet_material_value.setText(material) + self.pallet_tier_value.setText(tier) self.quantity_value.setText("") # 数量为空 self.weight_value.setText(f"{weight} kg") - # 不再根据托盘号设置订单号 - # self.order_input.setText(tray_code) - # 发送托盘号到主窗口 from widgets.main_window import MainWindow main_window = self.parent @@ -106,8 +129,6 @@ class LoadingDialog(LoadingDialogUI): main_window.tray_edit.setCurrentText(tray_code) logging.info(f"设置主窗口当前托盘号: {tray_code}") - # 成功获取信息后,将焦点设置到订单号输入框上 - self.order_input.setFocus() else: # 获取托盘信息失败 error_msg = response.get("message", "获取托盘信息失败") @@ -124,8 +145,9 @@ class LoadingDialog(LoadingDialogUI): logging.info(f"捕获到回车键事件,当前焦点部件: {self.focusWidget()}") # 如果焦点在托盘输入框上,触发查询 + # 注释掉此处的on_tray_query调用,因为已经通过returnPressed信号处理了 if self.focusWidget() == self.tray_input: - self.on_tray_query() + # self.on_tray_query() - 通过returnPressed信号已经处理,这里不需要再调用 event.accept() # 消费掉这个事件 return diff --git a/widgets/main_window.py b/widgets/main_window.py index 91d1a68..3680751 100644 --- a/widgets/main_window.py +++ b/widgets/main_window.py @@ -124,8 +124,8 @@ class MainWindow(MainWindowUI): # 设置中央部件为堆叠部件 self.setCentralWidget(self.stacked_widget) - # 添加托盘类型选择下拉框 - self.add_pallet_type_selectors() + # # 添加托盘类型选择下拉框 + # self.add_pallet_type_selectors() # 连接信号和槽 self.connect_signals() @@ -169,50 +169,53 @@ class MainWindow(MainWindowUI): # 注册串口数据回调函数 self.register_serial_callbacks() + + # 加载托盘号列表 + self.load_pallet_codes() - def add_pallet_type_selectors(self): - """添加托盘类型选择下拉框""" - # 创建上料托盘类型选择下拉框 - self.input_pallet_type_label = QLabel("上料托盘类型:") - self.input_pallet_type_label.setFont(self.normal_font) - self.input_pallet_type_label.setVisible(False) - self.material_form_layout.addRow(self.input_pallet_type_label) + # def add_pallet_type_selectors(self): + # """添加托盘类型选择下拉框""" + # # 创建上料托盘类型选择下拉框 + # self.input_pallet_type_label = QLabel("上料托盘类型:") + # self.input_pallet_type_label.setFont(self.normal_font) + # self.input_pallet_type_label.setVisible(False) + # self.material_form_layout.addRow(self.input_pallet_type_label) - self.input_pallet_type_combo = QComboBox() - self.input_pallet_type_combo.setFont(self.normal_font) - self.input_pallet_type_combo.setVisible(False) - self.material_form_layout.addRow("", self.input_pallet_type_combo) + # self.input_pallet_type_combo = QComboBox() + # self.input_pallet_type_combo.setFont(self.normal_font) + # self.input_pallet_type_combo.setVisible(False) + # self.material_form_layout.addRow("", self.input_pallet_type_combo) - # 创建下料托盘类型选择下拉框 - self.output_pallet_type_label = QLabel("下料托盘类型:") - self.output_pallet_type_label.setFont(self.normal_font) - self.output_pallet_type_label.setVisible(False) - self.output_form_layout.addRow(self.output_pallet_type_label) + # # 创建下料托盘类型选择下拉框 + # self.output_pallet_type_label = QLabel("下料托盘类型:") + # self.output_pallet_type_label.setFont(self.normal_font) + # self.output_pallet_type_label.setVisible(False) + # self.output_form_layout.addRow(self.output_pallet_type_label) - self.output_pallet_type_combo = QComboBox() - self.output_pallet_type_combo.setFont(self.normal_font) - self.output_pallet_type_combo.setVisible(False) - self.output_form_layout.addRow("", self.output_pallet_type_combo) + # self.output_pallet_type_combo = QComboBox() + # self.output_pallet_type_combo.setFont(self.normal_font) + # self.output_pallet_type_combo.setVisible(False) + # self.output_form_layout.addRow("", self.output_pallet_type_combo) - # 加载托盘类型数据 - self.update_pallet_types() + # # 加载托盘类型数据 + # self.update_pallet_types() - def update_pallet_types(self): - """更新托盘类型下拉框""" - # 重新加载托盘类型数据 - self.pallet_type_manager.reload_pallet_types() + # def update_pallet_types(self): + # """更新托盘类型下拉框""" + # # 重新加载托盘类型数据 + # self.pallet_type_manager.reload_pallet_types() - # 更新上料托盘类型 - input_types = self.pallet_type_manager.get_pallet_types_by_operation("input") - self.input_pallet_type_combo.clear() - for pallet_type in input_types: - self.input_pallet_type_combo.addItem(pallet_type['type_name'], pallet_type['id']) + # # 更新上料托盘类型 + # input_types = self.pallet_type_manager.get_pallet_types_by_operation("input") + # self.input_pallet_type_combo.clear() + # for pallet_type in input_types: + # self.input_pallet_type_combo.addItem(pallet_type['type_name'], pallet_type['id']) - # 更新下料托盘类型 - output_types = self.pallet_type_manager.get_pallet_types_by_operation("output") - self.output_pallet_type_combo.clear() - for pallet_type in output_types: - self.output_pallet_type_combo.addItem(pallet_type['type_name'], pallet_type['id']) + # # 更新下料托盘类型 + # output_types = self.pallet_type_manager.get_pallet_types_by_operation("output") + # self.output_pallet_type_combo.clear() + # for pallet_type in output_types: + # self.output_pallet_type_combo.addItem(pallet_type['type_name'], pallet_type['id']) def load_config(self): """加载配置文件""" @@ -299,8 +302,54 @@ class MainWindow(MainWindowUI): if not self.camera_display.camera_manager.isGrabbing: self.camera_display.start_display() + # 加载托盘号列表 + self.load_pallet_codes() + logging.info("显示主页面") + def load_pallet_codes(self): + """从托盘类型管理器加载托盘号并更新到tray_edit""" + try: + # 获取当前文本,以便保留用户选择 + current_text = self.tray_edit.currentText() + + # 清空当前项目 + self.tray_edit.clear() + + # 获取托盘号 + pallet_codes = self.pallet_type_manager.get_pallet_code() + + if pallet_codes and len(pallet_codes) > 0: + # 添加托盘号到下拉框 + for code in pallet_codes: + self.tray_edit.addItem(code) + + # 如果有之前的选择,尝试恢复它 + if current_text: + index = self.tray_edit.findText(current_text) + if index >= 0: + self.tray_edit.setCurrentIndex(index) + logging.info(f"已加载托盘号,共 {len(pallet_codes)} 个") + else: + # 如果没有托盘号,添加默认项 + default_codes = ["托盘1", "托盘2", "托盘3", "托盘4", "托盘5", "托盘6"] + for code in default_codes: + self.tray_edit.addItem(code) + logging.warning("未找到托盘号,使用默认值") + + # 尝试恢复之前的选择 + if current_text: + index = self.tray_edit.findText(current_text) + if index >= 0: + self.tray_edit.setCurrentIndex(index) + except Exception as e: + logging.error(f"加载托盘号失败: {str(e)}") + # 如果加载失败,添加默认项 + self.tray_edit.clear() + default_codes = ["托盘1", "托盘2", "托盘3", "托盘4", "托盘5", "托盘6"] + for code in default_codes: + self.tray_edit.addItem(code) + def show_settings_page(self): """显示设置页面""" # 创建设置窗口 @@ -325,15 +374,15 @@ class MainWindow(MainWindowUI): # 更新串口管理器配置 self.serial_manager.reload_config() + # 重新加载托盘号 + self.load_pallet_codes() + logging.info("设置已更新,重新加载配置") def handle_input(self): """处理上料按钮点击事件""" # 获取托盘号 tray_id = self.tray_edit.currentText() - if not tray_id: - QMessageBox.warning(self, "提示", "请先选择或输入托盘号") - return # 启动监听(不论后续是否确认上料) # 启动Modbus监控 @@ -358,7 +407,7 @@ class MainWindow(MainWindowUI): # 如果用户确认,则执行上料操作 if result == QDialog.Accepted: # 获取托盘料值作为拆垛层数 - stow_num = dialog.pallet_material_value.text() + stow_num = dialog.pallet_tier_value.text() if stow_num == "--" or not stow_num: QMessageBox.warning(self, "错误", "未获取到托盘料信息,请重试") return @@ -370,7 +419,7 @@ class MainWindow(MainWindowUI): self.init_seq[tray_id] = 1 if not pallet_type: - QMessageBox.warning(self, "错误", "未查到对应下料托盘类型") + QMessageBox.warning(self, "错误", "未查到对应托盘类型") return # 执行Modbus操作 @@ -398,9 +447,6 @@ class MainWindow(MainWindowUI): """处理下料按钮点击事件""" # 获取托盘号 tray_id = self.tray_edit.currentText() - if not tray_id: - QMessageBox.warning(self, "提示", "请先选择或输入托盘号") - return # 启动监听(不论后续是否确认下料) # 启动Modbus监控 @@ -425,7 +471,7 @@ class MainWindow(MainWindowUI): # 如果用户确认,则执行下料操作 if result == QDialog.Accepted: # 获取托盘料值作为下料层数 - stow_num = dialog.pallet_material_value.text() + stow_num = dialog.pallet_tier_value.text() if stow_num == "--" or not stow_num: QMessageBox.warning(self, "错误", "未获取到托盘料信息,请重试") return diff --git a/widgets/unloading_dialog_widget.py b/widgets/unloading_dialog_widget.py index c880c1d..2483d18 100644 --- a/widgets/unloading_dialog_widget.py +++ b/widgets/unloading_dialog_widget.py @@ -3,6 +3,8 @@ from apis.tary_api import TaryApi from PySide6.QtCore import Qt, Signal from PySide6.QtWidgets import QMessageBox, QDialog import logging +from utils.app_mode import AppMode +from utils.pallet_type_manager import PalletTypeManager class UnloadingDialog(UnloadingDialogUI): # 定义一个信号,用于向主窗口传递托盘号 @@ -34,7 +36,8 @@ class UnloadingDialog(UnloadingDialogUI): """设置事件连接""" # 托盘号输入框回车事件触发查询 self.tray_input.returnPressed.connect(self.handle_tray_return_pressed) - self.tray_input.editingFinished.connect(self.on_tray_query) + # 移除editingFinished事件,避免重复触发查询 + # self.tray_input.editingFinished.connect(self.on_tray_query) # 确认按钮点击事件 self.confirm_button.clicked.connect(self.accept) @@ -60,8 +63,36 @@ class UnloadingDialog(UnloadingDialogUI): logging.info(f"查询托盘号: {tray_code}") - # 调用API获取托盘信息 - response = self.tary_api.get_tary_info(tray_code) + if AppMode.is_api(): + self.tary_api = TaryApi() + # 调用API获取托盘信息 + response = self.tary_api.get_tary_info(tray_code) + else: + pallet_type_manager = PalletTypeManager.get_instance() + pallet_info = pallet_type_manager.get_pallet_info_by_pallet_id(tray_code) + + # 检查返回的数据类型并进行适当处理 + if pallet_info is None: + response = { + "success": False, + "message": "未找到托盘信息" + } + else: + # 如果返回的是元组(数据库查询结果),将其转换为字典 + # 根据dao/pallet_type_dao.py中get_pallet_info_by_pallet_id方法的SQL查询 + # 返回的字段顺序为:pallet_code, pallet_name, description, axios_name, axios_type, tier, size, amount, weight + response = { + "success": True, + "data": { + "tp_note": pallet_info[0] if len(pallet_info) > 0 else "", + "product_name": pallet_info[1] if len(pallet_info) > 1 else "", + "axis_type": pallet_info[4] if len(pallet_info) > 4 else "", + "tier": str(pallet_info[5]) if len(pallet_info) > 5 else "", + "weight": str(pallet_info[8]) if len(pallet_info) > 8 else "", + "quantity": "" + } + } + logging.info(f"托盘信息响应: {response}") logging.info(f"response.success={response.get('success')}, response.data存在={response.get('data') is not None}") @@ -72,14 +103,14 @@ class UnloadingDialog(UnloadingDialogUI): # 显示托盘相关信息 axis_type = str(tray_data.get("axis_type", "--")) - material = str(tray_data.get("material", "--")) + tier = str(tray_data.get("tier", "--")) weight = str(tray_data.get("weight", "--")) - logging.info(f"显示托盘信息: 轴型={axis_type}, 托盘料={material}, 重量={weight}") + logging.info(f"显示托盘信息: 轴型={axis_type}, 托盘料={tier}, 重量={weight}") # 设置显示值 self.axis_value.setText(axis_type) - self.pallet_material_value.setText(material) + self.pallet_tier_value.setText(tier) self.quantity_value.setText("") # 数量为空 self.weight_value.setText(f"{weight} kg") @@ -109,4 +140,27 @@ class UnloadingDialog(UnloadingDialogUI): QMessageBox.warning(self, "查询失败", error_msg) except Exception as e: logging.error(f"查询托盘信息异常: {str(e)}") - QMessageBox.critical(self, "查询异常", f"查询托盘信息时发生异常: {str(e)}") \ No newline at end of file + QMessageBox.critical(self, "查询异常", f"查询托盘信息时发生异常: {str(e)}") + + def keyPressEvent(self, event): + """重写键盘事件处理,防止回车关闭对话框""" + # 如果按下回车键 + if event.key() == Qt.Key_Return or event.key() == Qt.Key_Enter: + logging.info(f"捕获到回车键事件,当前焦点部件: {self.focusWidget()}") + + # 如果焦点在托盘输入框上,触发查询 + if self.focusWidget() == self.tray_input: + # 不需要重复调用on_tray_query,因为returnPressed信号已经处理了 + event.accept() # 消费掉这个事件 + return + + # 如果焦点在确认按钮上,则允许默认行为(确认) + if self.focusWidget() == self.confirm_button: + return super().keyPressEvent(event) + + # 其他情况下,阻止回车事件传播 + event.accept() # 消费掉这个事件 + return + + # 其他键位事件交给父类处理 + super().keyPressEvent(event) \ No newline at end of file