feat: 更新称重数据处理逻辑,优化数据库操作,调整界面元素以支持千克单位显示,调整托盘料可以输入

This commit is contained in:
zhu-mengmeng 2025-06-20 08:51:13 +08:00
parent b5bc85d4a4
commit 01f04fda29
5 changed files with 167 additions and 88 deletions

View File

@ -228,7 +228,7 @@ class InspectionDAO:
return False
# 先检查是否存在记录
check_sql = "SELECT id FROM wsbz_order_info WHERE ddmo = ?"
check_sql = "SELECT ddmo FROM wsbz_order_info WHERE ddmo = ?"
self.db.cursor.execute(check_sql, (data.get("mo", ""),))
existing_record = self.db.cursor.fetchone()
@ -236,13 +236,46 @@ class InspectionDAO:
# 如果记录存在,执行更新
sql = """
UPDATE wsbz_order_info SET
data_corp = ?, user_id = ?, user_name = ?, gzl_zl = ?, xpack = ?,
qd = ?, spack_type = ?, mxzs = ?, jt = ?, ddnote = ?, code = ?,
type = ?, lable = ?, lib = ?, gzl = ?, maxsl = ?, cz = ?,
size = ?, cd = ?, luno = ?, qfqd = ?, pono = ?, xj = ?,
ysl = ?, dycz = ?, zx_code = ?, edit_id = ?, remarks = ?, zx_name = ?
data_corp = ?, user_id = ?, user_name = ?, gzl_zl = ?,
xpack = ?, qd = ?, spack_type = ?, mxzs = ?, jt = ?,
ddnote = ?, code = ?, type = ?, lable = ?, lib = ?,
gzl = ?, maxsl = ?, cz = ?, size = ?, cd = ?, luno = ?,
qfqd = ?, pono = ?, xj = ?, ysl = ?, dycz = ?,
zx_code = ?, edit_id = ?, remarks = ?, zx_name = ?
WHERE ddmo = ?
"""
params = (
data.get("data_corp", "JT"),
data.get("user_id", ""),
data.get("user_name", ""),
data.get("zx_zl", ""),
data.get("xpack", ""),
data.get("qd", ""),
data.get("spack_type", ""),
data.get("mxzs", ""),
data.get("jt", ""),
data.get("note", ""),
data.get("code", ""),
data.get("type", ""),
data.get("template_name", ""),
data.get("lib", ""),
data.get("zx_code", ""),
data.get("maxsl", ""),
data.get("cz", ""),
data.get("size", ""),
data.get("cd", ""),
data.get("luno", ""),
data.get("qfqd", ""),
data.get("khno", ""),
data.get("size", ""),
data.get("ysl", ""),
data.get("dycz", ""),
data.get("zx_code", ""),
data.get("edit_id", ""),
data.get("remarks", ""),
data.get("zx_name", ""),
data.get("mo", "") # WHERE 条件参数
)
logging.info(f"更新订单信息: ddmo={data.get('mo', '')}")
else:
# 如果记录不存在,执行插入
@ -257,45 +290,40 @@ class InspectionDAO:
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
)
"""
params = (
data.get("data_corp", "JT"),
data.get("user_id", ""),
data.get("user_name", ""),
data.get("zx_zl", ""),
data.get("mo", ""),
data.get("xpack", ""),
data.get("qd", ""),
data.get("spack_type", ""),
data.get("mxzs", ""),
data.get("jt", ""),
data.get("note", ""),
data.get("code", ""),
data.get("type", ""),
data.get("template_name", ""),
data.get("lib", ""),
data.get("zx_code", ""),
data.get("maxsl", ""),
data.get("cz", ""),
data.get("size", ""),
data.get("cd", ""),
data.get("luno", ""),
data.get("qfqd", ""),
data.get("khno", ""),
data.get("size", ""),
data.get("ysl", ""),
data.get("dycz", ""),
data.get("zx_code", ""),
data.get("edit_id", ""),
data.get("remarks", ""),
data.get("zx_name", "")
)
logging.info(f"插入新订单信息: ddmo={data.get('mo', '')}")
params = (
data.get("data_corp", "JT"),
data.get("user_id", ""),
data.get("user_name", ""),
data.get("zx_zl", ""),
data.get("mo", ""),
data.get("xpack", ""),
data.get("qd", ""),
data.get("spack_type", ""),
data.get("mxzs", ""),
data.get("jt", ""),
data.get("note", ""),
data.get("code", ""),
data.get("type", ""),
data.get("template_name", ""),
data.get("lib", ""),
data.get("zx_code", ""),
data.get("maxsl", ""),
data.get("cz", ""),
data.get("size", ""),
data.get("cd", ""),
data.get("luno", ""),
data.get("qfqd", ""),
data.get("khno", ""),
data.get("size", ""),
data.get("ysl", ""),
data.get("dycz", ""),
data.get("zx_code", ""),
data.get("edit_id", ""),
data.get("remarks", ""),
data.get("zx_name", ""),
)
# 如果是更新操作,需要添加 WHERE 条件的参数
if existing_record:
params = params + (data.get("mo", ""),)
self.db.cursor.execute(sql, params)
self.db.conn.commit()
return True

Binary file not shown.

View File

@ -2,7 +2,7 @@ from pymodbus.client import ModbusTcpClient
client = ModbusTcpClient('localhost', port=5020)
client.connect()
client.write_registers(address=11, values=[111])
client.write_registers(address=11, values=[2222])
client.write_registers(address=13, values=[1])
# client.write_registers(address=6, values=[1])

View File

@ -177,10 +177,22 @@ class LoadingDialogUI(QDialog):
self.pallet_tier_label.setFixedWidth(100)
self.pallet_tier_label.setFixedHeight(40)
self.pallet_tier_value = QLabel("--")
# 将标签改为输入框
self.pallet_tier_value = QLineEdit()
self.pallet_tier_value.setFont(self.normal_font)
self.pallet_tier_value.setStyleSheet(value_style)
self.pallet_tier_value.setStyleSheet("""
QLineEdit {
padding: 5px;
background-color: white;
color: #333333;
}
QLineEdit:focus {
border: 1px solid #66afe9;
outline: none;
}
""")
self.pallet_tier_value.setFixedHeight(40)
self.pallet_tier_value.setPlaceholderText(" -- ")
tier_layout.addWidget(self.pallet_tier_label)
tier_layout.addWidget(self.pallet_tier_value, 1)

View File

@ -59,10 +59,10 @@ class MainWindow(MainWindowUI):
self._current_order_code = None # 存储当前订单号
# 称重相关变量
self._last_weight = None # 上一次的称重值
self._weight_stable_time = None # 量开始稳定的时间
self._current_weight = None # 当前称重值(千克)
self._last_weight_time = None # 最后一次称重时间
self._weight_stable_threshold = 2 # 重量稳定阈值(秒)
self._weight_tolerance = 1 # 重量波动容差(克)
self._stability_check_timer = None # 用于检查重量稳定性的定时器
# 设置窗口标题
if user_name and corp_name:
@ -1064,6 +1064,7 @@ class MainWindow(MainWindowUI):
except Exception as e:
logging.error(f"保存检验数据失败: {str(e)}")
# 显示错误消息
QMessageBox.warning(self, "保存失败", f"保存检验数据错误: {str(e)[:50]}...")
self.statusBar().showMessage(f"保存检验数据错误: {str(e)[:50]}...", 3000)
def _safe_load_data(self):
@ -1632,38 +1633,41 @@ class MainWindow(MainWindowUI):
self.machine_handlers.weight_changed.connect(self.handle_weight_data)
self.machine_handlers.label_signal_changed.connect(self.handle_label_signal)
def _convert_to_kg(self, weight_in_g):
"""
将克转换为千克
Args:
weight_in_g: 重量
Returns:
float: 重量千克
"""
return round(weight_in_g / 1000.0, 3) # 保留3位小数
@Slot(int)
def handle_weight_data(self, weight):
def handle_weight_data(self, weight_in_g):
"""处理称重数据变化"""
logging.info(f"[处理] 称重数据: {weight}g")
# 更新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
# 转换重量单位并立即更新UI显示
weight_in_kg = self._convert_to_kg(weight_in_g)
logging.info(f"[显示] 称重数据: {weight_in_kg}kg (原始值: {weight_in_g}g)")
self.weight_label.setText(f"重量: {weight_in_kg}kg")
# 检查重量变化是否在容差范围内
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
# 更新当前重量和时间
self._current_weight = weight_in_kg
self._last_weight_time = current_time
# 取消之前的定时器(如果存在)
if self._stability_check_timer is not None:
self._stability_check_timer.stop()
self._stability_check_timer.deleteLater()
# 创建新的定时器
self._stability_check_timer = QTimer()
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) # 转换为毫秒
except Exception as e:
logging.error(f"处理称重数据时发生错误: {str(e)}")
@ -1673,8 +1677,35 @@ class MainWindow(MainWindowUI):
except:
pass
def _process_stable_weight(self, weight):
"""处理稳定的称重数据"""
def _check_weight_stability(self, original_weight_kg):
"""
检查重量是否稳定
Args:
original_weight_kg: 开始检查时的重量千克
"""
try:
# 如果当前重量与定时器启动时的重量相同,说明这段时间内没有新的重量数据
if self._current_weight == original_weight_kg:
logging.info(f"重量 {original_weight_kg}kg 在{self._weight_stable_threshold}秒内保持稳定")
self._process_stable_weight(original_weight_kg)
# 调用打印方法
self._print_weight_label(original_weight_kg)
else:
logging.info(f"重量在{self._weight_stable_threshold}秒内发生变化,从 {original_weight_kg}kg 变为 {self._current_weight}kg")
except Exception as e:
logging.error(f"检查重量稳定性时发生错误: {str(e)}")
finally:
# 清理定时器
if self._stability_check_timer is not None:
self._stability_check_timer.deleteLater()
self._stability_check_timer = None
def _process_stable_weight(self, weight_kg):
"""
处理稳定的称重数据
Args:
weight_kg: 稳定的重量值千克
"""
try:
# 获取数据行数
if self.process_table.rowCount() <= 2: # 没有数据行
@ -1714,16 +1745,13 @@ class MainWindow(MainWindowUI):
self.process_table.cellChanged.disconnect(self.handle_inspection_cell_changed)
except:
pass
# 将克转换为千克
weight_kg = weight / 1000.0
# 设置称重值单元格(显示克)
weight_item = QTableWidgetItem(str(weight))
# 设置称重值单元格(显示千克)
weight_item = QTableWidgetItem(str(weight_kg))
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_kg), "pass")
@ -1734,18 +1762,16 @@ class MainWindow(MainWindowUI):
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_g = net_weight_kg * 1000
net_weight_item = QTableWidgetItem(str(int(net_weight_g)))
# 设置净重单元格(显示千克)
net_weight_item = QTableWidgetItem(str(net_weight_kg))
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 ({weight_kg}kg) 写入行 {data_row}, 列 {weight_col}")
logging.info(f"已将稳定的称重数据 {weight_kg}kg 写入行 {data_row}, 列 {weight_col}")
# TODO调用称重打印进行下一步打印
except Exception as e:
logging.error(f"处理称重数据时发生错误: {str(e)}")
# 确保重新连接信号
@ -1753,6 +1779,19 @@ class MainWindow(MainWindowUI):
self.process_table.cellChanged.connect(self.handle_inspection_cell_changed)
except:
pass
def _print_weight_label(self, weight_kg):
"""
打印重量标签
Args:
weight_kg: 稳定的重量值千克
"""
try:
logging.info(f"开始打印重量标签,重量:{weight_kg}kg")
# TODO: 实现打印逻辑
pass
except Exception as e:
logging.error(f"打印重量标签时发生错误: {str(e)}")
@Slot(int, str)
def handle_label_signal(self, signal, status):