添加上料斗、下料斗存在错误信息时(表示付工那边没有连接上重量客户端),状态图标显示没连接,并且重量数据清0
This commit is contained in:
@ -22,6 +22,8 @@ class TcpClient(QObject):
|
||||
# 信号,用于向主窗口/重量弹窗发送最新重量数据
|
||||
weight_updated = Signal(dict) # 重量更新信号
|
||||
connection_status_changed = Signal(bool) # 连接状态更新信号
|
||||
upper_weight_error = Signal(bool) # 上料斗重量异常信号
|
||||
down_weight_error = Signal(bool) # 下料斗重量异常信号
|
||||
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent)
|
||||
@ -96,6 +98,9 @@ class TcpClient(QObject):
|
||||
|
||||
# 发送连接状态信号
|
||||
self.connection_status_changed.emit(True)
|
||||
# 重量错误状态信号(连接成功后恢复正常图标)
|
||||
self.upper_weight_error.emit(False)
|
||||
self.down_weight_error.emit(False)
|
||||
# 连接成功后,向服务器发送“请求初始数据”指令
|
||||
self._send_tcp_request("get_initial_data")
|
||||
|
||||
@ -110,7 +115,7 @@ class TcpClient(QObject):
|
||||
self.connection_status_changed.emit(False)
|
||||
|
||||
# 发送重量清零信号
|
||||
self.weight_updated.emit({"hopper_up_weight": 0, "hopper_down_weight": 0})
|
||||
self.weight_updated.emit({"hopper_up_weight": 0, "hopper_down_weight": ""})
|
||||
|
||||
if not self.reconnect_timer.isActive(): # 未启动重连定时器时才启动
|
||||
print(f"连接断开,将在{RECONNECT_INTERVAL / 1000}秒后启动重连...")
|
||||
@ -126,7 +131,28 @@ class TcpClient(QObject):
|
||||
try:
|
||||
status_data = json.loads(tcp_data)
|
||||
self.latest_json_data = status_data
|
||||
self.weight_updated.emit(status_data)
|
||||
|
||||
# 错误检测
|
||||
up_weight = status_data.get("hopper_up_weight")
|
||||
down_weight = status_data.get("hopper_down_weight")
|
||||
|
||||
# 顺序不能变
|
||||
if up_weight == "error" and down_weight == "error":
|
||||
self.weight_updated.emit({"hopper_up_weight": "", "hopper_down_weight": ""})
|
||||
self.upper_weight_error.emit(True)
|
||||
self.down_weight_error.emit(True)
|
||||
elif up_weight == "error":
|
||||
self.weight_updated.emit({"hopper_up_weight": "", "hopper_down_weight": down_weight})
|
||||
self.upper_weight_error.emit(True)
|
||||
elif down_weight == "error":
|
||||
self.weight_updated.emit({"hopper_up_weight": up_weight, "hopper_down_weight": ""})
|
||||
self.down_weight_error.emit(True)
|
||||
else:
|
||||
# 防止打乱服务端连接时的状态图标变换
|
||||
self.weight_updated.emit(status_data)
|
||||
self.upper_weight_error.emit(False)
|
||||
self.down_weight_error.emit(False)
|
||||
|
||||
except json.JSONDecodeError as e:
|
||||
print(f"TCP数据解析失败(非JSON格式):{e}, 原始数据:{tcp_data}")
|
||||
except Exception as e:
|
||||
@ -143,6 +169,9 @@ class TcpClient(QObject):
|
||||
|
||||
# 发送连接断开信号
|
||||
self.connection_status_changed.emit(False)
|
||||
# 错误时重置重量和错误状态
|
||||
self.weight_updated.emit({"hopper_up_weight": "", "hopper_down_weight": ""})
|
||||
self.weight_error_signal.emit(False)
|
||||
|
||||
# 首次连接失败时,启动重连定时器
|
||||
if not self.reconnect_timer.isActive():
|
||||
|
||||
Reference in New Issue
Block a user