diff --git a/apis/gc_api.py b/apis/gc_api.py index 61b7bf9..b1529cb 100644 --- a/apis/gc_api.py +++ b/apis/gc_api.py @@ -38,7 +38,7 @@ class GcApi: except Exception as e: logging.error(f"获取GC信息失败: {str(e)}") return None - def get_order_info(self, order_code): + def get_order_info(self, order_code,corp_id): """ 获取订单信息 """ @@ -46,7 +46,7 @@ class GcApi: # API 配置中的键名 api_key = "get_order_info" # 构建 form-data 格式的数据 - order_dict = {"srch_mo":order_code,"data_corp":"JT"} + order_dict = {"srch_mo":order_code,"data_corp":corp_id} data = { "parms": json.dumps(order_dict), # 必须将数据序列化为JSON字符串 "pageIndex": 0, @@ -82,7 +82,7 @@ class GcApi: logging.error(f"添加订单信息失败: {str(e)}") return None - def get_xpack(self, order_id): + def get_xpack(self, order_id,corp_id): """ 获取包装号 """ @@ -92,7 +92,7 @@ class GcApi: # 构建 form-data 格式的数据 data = { "ddmo": order_id, - "data_corp":"JT" + "data_corp":corp_id } # 将工程号作为参数传递,使用 data 参数传递 form-data 格式数据 response = self.api_utils.post(api_key, data=data) diff --git a/dao/inspection_dao.py b/dao/inspection_dao.py index 26f58f4..1df316d 100644 --- a/dao/inspection_dao.py +++ b/dao/inspection_dao.py @@ -583,17 +583,17 @@ class InspectionDAO: except Exception as e: logging.error(f"删除检验数据失败: {str(e)}") self.db.conn.rollback() - def get_axios_num_by_order_id(self, gc_note): + def get_axios_num_by_order_id(self, order_id): """获取托盘号对应的轴号""" try: sql = """ - SELECT max(axis_package_id) as axios_num FROM wsbz_inspection_pack_data WHERE gc_note = ? + SELECT max(axis_package_id) as axios_num FROM wsbz_inspection_pack_data WHERE order_id = ? AND is_deleted = FALSE """ - params = (gc_note,) + params = (order_id,) self.db.cursor.execute(sql, params) result = self.db.cursor.fetchone() - return int(result[0]) if result else 0 + return int(result[0]) if result[0] else 0 except Exception as e: logging.error(f"获取轴号失败: {str(e)}") return 0 diff --git a/db/jtDB.db b/db/jtDB.db index 78223e3..96c8542 100644 Binary files a/db/jtDB.db and b/db/jtDB.db differ diff --git a/from pymodbus.py b/from pymodbus.py index 072c855..3cd0ae8 100644 --- a/from pymodbus.py +++ b/from pymodbus.py @@ -2,7 +2,7 @@ from pymodbus.client import ModbusTcpClient import time client = ModbusTcpClient('localhost', port=5020) client.connect() -client.write_registers(address=11, values=[2222]) +client.write_registers(address=11, values=[2243]) client.write_registers(address=13, values=[0]) client.write_registers(address=21, values=[0]) @@ -12,8 +12,8 @@ time.sleep(2) client.write_registers(address=20, values=[0]) # client.write_registers(address=5, values=[16]) # 贴标完成 -# client.write_registers(address=24, values=[1]) -# client.write_registers(address=13, values=[0]) +# client.write_registers(address=24, values=[1])s +client.write_registers(address=13, values=[1]) result = client.read_holding_registers(address=1, count=1) diff --git a/ui/loading_dialog_ui.py b/ui/loading_dialog_ui.py index 80cc530..00acdb0 100644 --- a/ui/loading_dialog_ui.py +++ b/ui/loading_dialog_ui.py @@ -119,7 +119,7 @@ class LoadingDialogUI(QDialog): row2 = QHBoxLayout() row2.setSpacing(0) - self.tray_label = QLabel("包装号") + self.tray_label = QLabel("托盘号") self.tray_label.setFont(self.normal_font) self.tray_label.setStyleSheet(label_style) self.tray_label.setFixedWidth(100) @@ -127,7 +127,7 @@ class LoadingDialogUI(QDialog): self.tray_input = QLineEdit() self.tray_input.setFont(self.normal_font) - self.tray_input.setPlaceholderText("请扫描包装号") + self.tray_input.setPlaceholderText("请扫描托盘号") self.tray_input.setStyleSheet(input_style) self.tray_input.setFixedHeight(45) diff --git a/widgets/loading_dialog_widget.py b/widgets/loading_dialog_widget.py index e8b45d0..0b9c590 100644 --- a/widgets/loading_dialog_widget.py +++ b/widgets/loading_dialog_widget.py @@ -76,7 +76,7 @@ class LoadingDialog(LoadingDialogUI): gc_api = GcApi() # 防止response为None导致异常 # 获取工程号信息,并且初始化数据 - order_response = gc_api.get_order_info(order_code) + order_response = gc_api.get_order_info(order_code,self.corp_id) # 生成托盘号 if(order_response.get("status",False)): # 将接口数据保存到数据库,用于后续的关联使用 @@ -91,7 +91,7 @@ class LoadingDialog(LoadingDialogUI): self.axis_value.setText(order_info['zx_name']) self.quantity_value.setText(str(order_info['sl'])) self.weight_value.setText(str(order_info['sl'])) - xpack_response = gc_api.get_xpack(order_code) + xpack_response = gc_api.get_xpack(order_code,self.corp_id) if(xpack_response.get("status",False)): xpack = xpack_response['xpack'] self.tray_input.setText(xpack) @@ -158,7 +158,7 @@ class LoadingDialog(LoadingDialogUI): # 获取包装号 pallet_code = self.tray_input.text().strip() if not pallet_code: - QMessageBox.warning(self, "提示", "请输入包装号") + QMessageBox.warning(self, "提示", "请输入托盘号") return try: @@ -176,7 +176,7 @@ class LoadingDialog(LoadingDialogUI): QMessageBox.warning(self, "提示", "保存托盘档案失败") return - logging.info(f"已保存托盘档案:包装号={pallet_code}, 层数={tier_value}") + logging.info(f"已保存托盘档案:托盘号={pallet_code}, 层数={tier_value}") except Exception as e: logging.error(f"保存托盘档案时发生错误: {str(e)}") QMessageBox.warning(self, "错误", f"保存托盘档案失败: {str(e)}") diff --git a/widgets/main_window.py b/widgets/main_window.py index 48fe46e..2fd2088 100644 --- a/widgets/main_window.py +++ b/widgets/main_window.py @@ -74,6 +74,7 @@ class MainWindow(MainWindowUI): self._total_unload_num = 0 # 总下料层数 self._current_unload_info = None # 存储当前下料信息 self._loading_info = None # 存储上料对话框的信息 + self._is_loading_active = False # 标识上料任务是否正在进行 # 连接信号到槽 self.loading_feedback_signal.connect(self._handle_loading_feedback_ui) @@ -404,9 +405,11 @@ class MainWindow(MainWindowUI): dialog.quantity_value.setText(self._loading_info.get('quantity_value', '--')) dialog.weight_value.setText(self._loading_info.get('weight_value', '--')) dialog.pallet_tier_value.setText(str(self._current_stow_num)) - # 禁用输入框,防止修改 - dialog.order_input.setEnabled(False) - dialog.tray_input.setEnabled(False) + # 只有当上料任务正在进行时才禁用输入框 + if self._is_loading_active: + # 禁用输入框,防止修改 + dialog.order_input.setEnabled(False) + dialog.tray_input.setEnabled(False) # 连接订单号信号 dialog.order_code_signal.connect(self.handle_order_code_received) @@ -416,6 +419,12 @@ class MainWindow(MainWindowUI): # 如果用户确认,则执行上料操作 if result == QDialog.Accepted: + # 从对话框中获取订单号和托盘号,并更新到主窗口 + order_code = dialog.order_input.text() + tray_code = dialog.tray_input.text() + self._current_order_code = order_code + self.tray_edit.setCurrentText(tray_code) + # 获取托盘料值作为拆垛层数 stow_num = dialog.pallet_tier_value.text() if stow_num == "--" or not stow_num: @@ -532,6 +541,7 @@ class MainWindow(MainWindowUI): success2 = modbus.write_register_until_success(client, 2, 1) if success0 and success2: + self._is_loading_active = True # 标记上料任务已开始 logging.info(f"开始上料操作:当前层数 {self._current_stow_num}") else: QMessageBox.warning(self, "错误", "开始上料操作失败") @@ -561,6 +571,7 @@ class MainWindow(MainWindowUI): success2 = modbus.write_register_until_success(client, 2, 0) if success2: + self._is_loading_active = False # 标记上料任务已停止 logging.info("停止上料操作") QMessageBox.information(self, "操作提示", "已停止上料操作") else: @@ -1229,35 +1240,29 @@ class MainWindow(MainWindowUI): logging.warning(f"未找到工程号 {gc_note} 托盘号 {tray_id} 的检验数据") return + # 获取轴号并保存 + label_value = self.get_axios_num_by_order_id(self._current_order_code) + # 从检验数据中获取贴标和称重数据 - label_value = "" weight_value = "" net_weight_value = "" for item in inspection_data: - if item['position'] == 11: # 贴标 - label_value = item['value'] - elif item['position'] == 12: # 称重 + if item['position'] == 12: # 称重 weight_value = item['value'] elif item['position'] == 13: # 净重 net_weight_value = item['value'] # 只要贴标字段有值,就可以写入包装记录 - if not label_value: + if label_value == None or label_value == "": logging.warning(f"工程号 {order_id} 托盘号 {tray_id} 的贴标字段为空,不添加到包装记录") return - # # 获取当前包装记录,检查是否已经存在相同的记录 TODO 暂时不判断,可能存在多次包装情况 - # existing_records = inspection_dao.get_package_record(tray_id) - # for record in existing_records: - # if record[0] == order_id and record[4] == label_value: - # logging.info(f"工程号 {order_id} 托盘号 {tray_id} 贴标值 {label_value} 的包装记录已存在,不重复添加") - # return - + # 获取当前时间作为完成时间 finish_time = datetime.now() # 将数据写入到数据库表 inspection_pack_data - inspection_dao.save_package_record(order_id, tray_id, label_value, weight_value,net_weight_value, finish_time,gc_note) + inspection_dao.save_package_record(order_id, tray_id, str(label_value+1), weight_value,net_weight_value, finish_time,gc_note) # 回显数据,但避免循环调用 if not getattr(self, '_loading_data_in_progress'): @@ -1639,7 +1644,25 @@ class MainWindow(MainWindowUI): self._stability_check_timer.setSingleShot(True) # 单次触发 self._stability_check_timer.timeout.connect(lambda: self._check_weight_stability(weight_in_kg)) self._stability_check_timer.start(self._weight_stable_threshold * 1000) # 转换为毫秒 - + # 获取当前选中的行或第一个数据行 + current_row = self.process_table.currentRow() + data_row = current_row if current_row >= 2 else 2 # 使用第一个数据行(索引为2) + + tray_id = self.tray_edit.currentText() + # 确保行存在 + if data_row >= self.process_table.rowCount(): + logging.warning(f"选中的行 {data_row} 超出了表格范围") + return + # 获取工程号 + gc_note = self.process_table.item(data_row, 1) + if not gc_note: + logging.warning("无法获取工程号") + return + + gc_note = gc_note.text().strip() + if not gc_note: + logging.warning("工程号为空") + return except Exception as e: logging.error(f"处理称重数据时发生错误: {str(e)}") # 确保重新连接信号 @@ -1729,8 +1752,15 @@ class MainWindow(MainWindowUI): # 保存净重到数据库(毛重-工字轮重量,单位都是千克) from dao.inspection_dao import InspectionDAO inspection_dao = InspectionDAO() - gzl_zl = inspection_dao.get_gzl_zl(self._current_order_code) - net_weight_kg = weight_kg - float(gzl_zl) + gzl_zl_raw = inspection_dao.get_gzl_zl(self._current_order_code) + gzl_zl = 0.0 + try: + if gzl_zl_raw: + gzl_zl = float(gzl_zl_raw) + except (ValueError, TypeError): + logging.warning(f"无法将工字轮重量 '{gzl_zl_raw}' 转换为浮点数,将使用默认值 0.0") + + net_weight_kg = weight_kg - gzl_zl self.save_inspection_data(self._current_order_code, gc_note, tray_id, 13, 13, str(net_weight_kg), "pass") # 设置净重单元格(显示千克) @@ -1738,6 +1768,50 @@ class MainWindow(MainWindowUI): net_weight_item.setTextAlignment(Qt.AlignCenter) self.process_table.setItem(data_row, net_weight_col, net_weight_item) + # 如果开启 api 模式,则调用接口添加到包装记录 + if AppMode.is_api(): + from dao.inspection_dao import InspectionDAO + from apis.gc_api import GcApi + inspection_dao = InspectionDAO() + # 调用接口 + gc_api = GcApi() + axios_num = self.get_axios_num_by_order_id(self._current_order_code)+1 + # 获取订单信息和其他信息,两者都已经是字典格式 + info = {} + order_info = inspection_dao.get_order_info(self._current_order_code) + info.update(order_info) + # 获取包装号 + + info['xpack'] = self.tray_edit.currentText() + info['spack'] = self.tray_edit.currentText() + order_others_info = inspection_dao.get_order_others_info(gc_note, self._current_order_code, tray_id) + info.update(order_others_info) + info['data_corp'] = order_info['data_corp'] + info['zh'] = axios_num + # 获取本机IP地址 + # import socket + # try: + # # 通过连接外部服务器获取本机IP(不实际建立连接) + # s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + # s.connect(("8.8.8.8", 80)) + # local_ip = s.getsockname()[0] + # s.close() + # info['nw_ip'] = local_ip.replace('.', '') + # except Exception as e: + # logging.error(f"获取本机IP失败: {str(e)}") + # # 如果获取失败,使用本地回环地址 + # info['nw_ip'] = '127.0.0.1'.replace('.', '') + info['nw_ip'] = '192.168.1.246' + # 调用接口添加到包装记录 + response = gc_api.add_order_info(info) + if response.get("status",False): + logging.info(f"添加订单信息成功: {response.get('data',{})}") + else: + QMessageBox.warning(self, f"提示", response.get("message",{})) + + # 保存贴标数据到数据库 + self.save_inspection_data(self._current_order_code, gc_note, tray_id, 11, 11, str(axios_num), "pass") + # 重新连接信号 self.process_table.cellChanged.connect(self.handle_inspection_cell_changed) @@ -1753,7 +1827,7 @@ class MainWindow(MainWindowUI): def _print_weight_label(self, weight_kg): """ - 打印重量标签 + 打印重量标签 注意:目前打印是写入数据库打印,不需要再次调用 Args: weight_kg: 稳定的重量值(千克) """ @@ -1826,8 +1900,7 @@ class MainWindow(MainWindowUI): self.process_table.setItem(data_row, label_col, label_item) logging.info(f"已将贴标数据 {axios_num} 写入表格单元格 [{data_row}, {label_col}]") - # 保存贴标数据到数据库 - self.save_inspection_data(self._current_order_code, gc_note, tray_id, 11, 11, axios_num, "pass") + # 调用加载到包装记录的方法 self.load_finished_record_to_package_record(self._current_order_code,gc_note, tray_id) @@ -1837,42 +1910,7 @@ class MainWindow(MainWindowUI): self.process_table.removeRow(data_row) logging.info(f"已删除处理完成的行 {data_row}") - #如果开启 api 模式,则调用接口添加到包装记录 - if AppMode.is_api(): - from dao.inspection_dao import InspectionDAO - inspection_dao = InspectionDAO() - # 调用接口 - gc_api = GcApi() - axios_num = self.get_axios_num_by_order_id(gc_note) - # 获取订单信息和其他信息,两者都已经是字典格式 - info = {} - order_info = inspection_dao.get_order_info(gc_note) - info.update(order_info) - # 获取包装号 - xpack = gc_api.get_xpack(order_info["ddmo"]) - info['xpack'] = xpack['xpack'] - info['spack'] = xpack['spack'] - order_others_info = inspection_dao.get_order_others_info(gc_note, self._current_order_code, tray_id) - info.update(order_others_info) - info['data_corp'] = 'T' - info['zh'] = axios_num - - # 获取本机IP地址 - import socket - try: - # 通过连接外部服务器获取本机IP(不实际建立连接) - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - s.connect(("8.8.8.8", 80)) - local_ip = s.getsockname()[0] - s.close() - info['nw_ip'] = local_ip.replace('.', '') - except Exception as e: - logging.error(f"获取本机IP失败: {str(e)}") - # 如果获取失败,使用本地回环地址 - info['nw_ip'] = '127.0.0.1'.replace('.', '') - - # 调用接口添加到包装记录 - gc_api.add_order_info(info) + # 重新连接单元格变更信号 self.process_table.cellChanged.connect(self.handle_inspection_cell_changed) @@ -1926,6 +1964,7 @@ class MainWindow(MainWindowUI): completed_layer_num = self._current_stow_num self._current_stow_num -= 1 if self._current_stow_num == 0: + self._is_loading_active = False # 任务完成,标记为非活动 self._loading_info = None logging.info("所有层拆垛完成,清空上料信息") message = f"第 {completed_layer_num} 层(最后一层)拆垛完成!" @@ -2165,21 +2204,6 @@ class MainWindow(MainWindowUI): def handle_ng(self, ng): """处理NG信号, 将当前处理的数据添加到包装记录中,毛重和净重设为0""" if ng == 1: - """ - # 获取当前选中的行或第一个数据行,并删除 - try: - order_id = self.process_table.item(2, 1).text().strip() - tray_id = self.tray_edit.currentText() - except Exception as e: - logging.error(f"处理NG信号时发生错误: {str(e)}") - order_id = "" - tray_id = "" - - self.inspection_manager.delete_inspection_data(order_id, tray_id) - # 触发重新查询,更新数据 - self._safe_load_data() - logging.info(f"已删除当前在处理的数据: order_id: {order_id}, tray_id: {tray_id}") - """ try: # 获取最后一条数据行 total_rows = self.process_table.rowCount()