From b5bc85d4a4ecd7071aa49f6602bcf56b82fa43c1 Mon Sep 17 00:00:00 2001 From: zhu-mengmeng <15588200382@163.com> Date: Fri, 20 Jun 2025 00:16:45 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E7=A7=B0=E9=87=8D?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=87=8D=E9=87=8F=E7=A8=B3=E5=AE=9A=E6=80=A7?= =?UTF-8?q?=E6=A3=80=E6=B5=8B=EF=BC=8C=E6=9B=B4=E6=96=B0=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E4=BF=9D=E5=AD=98=E9=80=BB=E8=BE=91=E4=BB=A5=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=8D=83=E5=85=8B=E5=8D=95=E4=BD=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- widgets/main_window.py | 65 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 9 deletions(-) diff --git a/widgets/main_window.py b/widgets/main_window.py index 9ffb070..ddb4688 100644 --- a/widgets/main_window.py +++ b/widgets/main_window.py @@ -29,6 +29,7 @@ from PySide6.QtWidgets import ( ) from PySide6.QtCore import Qt, QTimer, Slot from PySide6.QtGui import QBrush, QColor +import time # 导入UI from ui.main_window_ui import MainWindowUI @@ -57,6 +58,12 @@ class MainWindow(MainWindowUI): self._loading_data_in_progress = False # 数据加载状态标志,防止循环调用 self._current_order_code = None # 存储当前订单号 + # 称重相关变量 + self._last_weight = None # 上一次的称重值 + self._weight_stable_time = None # 重量开始稳定的时间 + self._weight_stable_threshold = 2 # 重量稳定阈值(秒) + self._weight_tolerance = 1 # 重量波动容差(克) + # 设置窗口标题 if user_name and corp_name: self.setWindowTitle(f"腾智微丝产线包装系统 ({corp_name})") @@ -1632,6 +1639,42 @@ class MainWindow(MainWindowUI): # 更新UI显示 self.weight_label.setText(f"重量: {weight}g") + try: + current_time = time.time() + + # 检查重量是否在容差范围内保持稳定 + if self._last_weight is None: + # 第一次收到重量数据 + self._last_weight = weight + self._weight_stable_time = current_time + return + + # 检查重量变化是否在容差范围内 + if abs(weight - self._last_weight) <= self._weight_tolerance: + # 重量稳定,检查是否达到稳定时间阈值 + if self._weight_stable_time is None: + self._weight_stable_time = current_time + elif current_time - self._weight_stable_time >= self._weight_stable_threshold: + # 重量已经稳定指定时间,可以处理数据 + self._process_stable_weight(weight) + # 重置稳定时间,等待下一次称重 + self._weight_stable_time = None + self._last_weight = None + else: + # 重量发生变化,重置稳定时间 + self._weight_stable_time = None + self._last_weight = weight + + except Exception as e: + logging.error(f"处理称重数据时发生错误: {str(e)}") + # 确保重新连接信号 + try: + self.process_table.cellChanged.connect(self.handle_inspection_cell_changed) + except: + pass + + def _process_stable_weight(self, weight): + """处理稳定的称重数据""" try: # 获取数据行数 if self.process_table.rowCount() <= 2: # 没有数据行 @@ -1672,31 +1715,35 @@ class MainWindow(MainWindowUI): except: pass - # 设置称重值单元格 + # 将克转换为千克 + weight_kg = weight / 1000.0 + + # 设置称重值单元格(显示克) weight_item = QTableWidgetItem(str(weight)) weight_item.setTextAlignment(Qt.AlignCenter) self.process_table.setItem(data_row, weight_col, weight_item) - # 保存到数据库 + # 保存到数据库(保存千克) tray_id = self.tray_edit.currentText() - self.save_inspection_data(self._current_order_code, gc_note, tray_id, 12, 12, str(weight), "pass") + self.save_inspection_data(self._current_order_code, gc_note, tray_id, 12, 12, str(weight_kg), "pass") - # 保存净重到数据库(毛重-工字轮重量,TODO :先默认工字轮重量为10g后续从接口获取) + # 保存净重到数据库(毛重-工字轮重量,单位都是千克) from dao.inspection_dao import InspectionDAO inspection_dao = InspectionDAO() gzl_zl = inspection_dao.get_gzl_zl(self._current_order_code) - net_weight = float(weight) - float(gzl_zl) - self.save_inspection_data(self._current_order_code, gc_note, tray_id, 13, 13, str(net_weight), "pass") + net_weight_kg = weight_kg - float(gzl_zl) + self.save_inspection_data(self._current_order_code, gc_note, tray_id, 13, 13, str(net_weight_kg), "pass") - # 设置净重单元格 - net_weight_item = QTableWidgetItem(str(net_weight)) + # 设置净重单元格(显示克) + net_weight_g = net_weight_kg * 1000 + net_weight_item = QTableWidgetItem(str(int(net_weight_g))) net_weight_item.setTextAlignment(Qt.AlignCenter) self.process_table.setItem(data_row, net_weight_col, net_weight_item) # 重新连接信号 self.process_table.cellChanged.connect(self.handle_inspection_cell_changed) - logging.info(f"已将称重数据 {weight}g 写入行 {data_row}, 列 {weight_col}") + logging.info(f"已将稳定的称重数据 {weight}g ({weight_kg}kg) 写入行 {data_row}, 列 {weight_col}") # TODO:调用称重打印,进行下一步打印 except Exception as e: