feat: 添加称重数据处理逻辑,优化重量稳定性检测,更新数据库保存逻辑以支持千克单位
This commit is contained in:
parent
f86a088aa5
commit
b5bc85d4a4
@ -29,6 +29,7 @@ from PySide6.QtWidgets import (
|
|||||||
)
|
)
|
||||||
from PySide6.QtCore import Qt, QTimer, Slot
|
from PySide6.QtCore import Qt, QTimer, Slot
|
||||||
from PySide6.QtGui import QBrush, QColor
|
from PySide6.QtGui import QBrush, QColor
|
||||||
|
import time
|
||||||
|
|
||||||
# 导入UI
|
# 导入UI
|
||||||
from ui.main_window_ui import MainWindowUI
|
from ui.main_window_ui import MainWindowUI
|
||||||
@ -57,6 +58,12 @@ class MainWindow(MainWindowUI):
|
|||||||
self._loading_data_in_progress = False # 数据加载状态标志,防止循环调用
|
self._loading_data_in_progress = False # 数据加载状态标志,防止循环调用
|
||||||
self._current_order_code = None # 存储当前订单号
|
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:
|
if user_name and corp_name:
|
||||||
self.setWindowTitle(f"腾智微丝产线包装系统 ({corp_name})")
|
self.setWindowTitle(f"腾智微丝产线包装系统 ({corp_name})")
|
||||||
@ -1632,6 +1639,42 @@ class MainWindow(MainWindowUI):
|
|||||||
# 更新UI显示
|
# 更新UI显示
|
||||||
self.weight_label.setText(f"重量: {weight}g")
|
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:
|
try:
|
||||||
# 获取数据行数
|
# 获取数据行数
|
||||||
if self.process_table.rowCount() <= 2: # 没有数据行
|
if self.process_table.rowCount() <= 2: # 没有数据行
|
||||||
@ -1672,31 +1715,35 @@ class MainWindow(MainWindowUI):
|
|||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# 设置称重值单元格
|
# 将克转换为千克
|
||||||
|
weight_kg = weight / 1000.0
|
||||||
|
|
||||||
|
# 设置称重值单元格(显示克)
|
||||||
weight_item = QTableWidgetItem(str(weight))
|
weight_item = QTableWidgetItem(str(weight))
|
||||||
weight_item.setTextAlignment(Qt.AlignCenter)
|
weight_item.setTextAlignment(Qt.AlignCenter)
|
||||||
self.process_table.setItem(data_row, weight_col, weight_item)
|
self.process_table.setItem(data_row, weight_col, weight_item)
|
||||||
|
|
||||||
# 保存到数据库
|
# 保存到数据库(保存千克)
|
||||||
tray_id = self.tray_edit.currentText()
|
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
|
from dao.inspection_dao import InspectionDAO
|
||||||
inspection_dao = InspectionDAO()
|
inspection_dao = InspectionDAO()
|
||||||
gzl_zl = inspection_dao.get_gzl_zl(self._current_order_code)
|
gzl_zl = inspection_dao.get_gzl_zl(self._current_order_code)
|
||||||
net_weight = float(weight) - float(gzl_zl)
|
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), "pass")
|
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)
|
net_weight_item.setTextAlignment(Qt.AlignCenter)
|
||||||
self.process_table.setItem(data_row, net_weight_col, net_weight_item)
|
self.process_table.setItem(data_row, net_weight_col, net_weight_item)
|
||||||
|
|
||||||
# 重新连接信号
|
# 重新连接信号
|
||||||
self.process_table.cellChanged.connect(self.handle_inspection_cell_changed)
|
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:调用称重打印,进行下一步打印
|
# TODO:调用称重打印,进行下一步打印
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user