fix: DBNMQTTool — 所有 publish 路径增加通用异常捕获

问题: _custom_publish / _proto_publish / _sim_publish 只 catch
json.JSONDecodeError, MqttClient.publish() 抛出的 ConnectionError
等异常未被捕获, 在 Qt 信号槽中导致静默崩溃(Windows 无 traceback).

修复:
- _custom_publish: +except Exception, 避免 toPlainText() 重复调用
- _proto_publish: +except Exception, 避免 toPlainText() 重复调用
- _sim_publish: +except Exception
- _custom_subscribe/_custom_unsubscribe: +except Exception, +_client 空检查
This commit is contained in:
wangfq
2026-07-07 14:15:36 +08:00
parent 5a2ef0be5d
commit 6eb93637c7
+42 -24
View File
@@ -453,6 +453,8 @@ class MainWindow(QMainWindow):
self._log(f"[模拟] → {topic}")
except json.JSONDecodeError as e:
QMessageBox.warning(self, "JSON 错误", str(e))
except Exception as e:
QMessageBox.critical(self, "发布失败", f"{type(e).__name__}: {e}")
def _sim_send_loop(self):
sn = self._sim_sn.text()
@@ -576,15 +578,18 @@ class MainWindow(QMainWindow):
if not topic or not self._mqtt.connected:
QMessageBox.warning(self, "提示", "请输入 Topic 并连接 Broker")
return
raw = self._proto_payload.toPlainText().strip()
try:
if self._proto_payload.toPlainText().strip():
payload = json.loads(self._proto_payload.toPlainText())
if raw:
payload = json.loads(raw)
else:
payload = build_request(CMD_DEV_INFO_QUERY)
self._mqtt.publish(topic, payload)
self._log(f"[协议] → {topic}")
except json.JSONDecodeError as e:
QMessageBox.warning(self, "JSON 错误", str(e))
except Exception as e:
QMessageBox.critical(self, "发布失败", f"{type(e).__name__}: {e}")
# ================================================================
# 自定义 Topic 订阅/发布
@@ -677,48 +682,61 @@ class MainWindow(QMainWindow):
topic = self._custom_pub_topic.text()
if not topic:
return
raw = self._custom_payload.toPlainText().strip()
try:
payload = json.loads(self._custom_payload.toPlainText()) if self._custom_payload.toPlainText().strip() else {}
payload = json.loads(raw) if raw else {}
qos = int(self._custom_qos.currentText())
self._mqtt.publish(topic, payload, qos=qos)
self._log(f"[自定义] → {topic}")
except json.JSONDecodeError as e:
QMessageBox.warning(self, "JSON 错误", str(e))
except Exception as e:
QMessageBox.critical(self, "发布失败", f"{type(e).__name__}: {e}")
self._log(f"[自定义] 发布失败: {e}")
def _custom_subscribe(self):
if not self._mqtt.connected:
if not self._mqtt.connected or not self._mqtt._client:
QMessageBox.warning(self, "提示", "请先连接 Broker")
return
topic = self._custom_sub_topic.text()
if not topic:
return
qos = int(self._custom_sub_qos.currentText())
self._mqtt._client.subscribe(topic, qos=qos)
self._custom_subs[topic] = qos
# 更新列表
found = False
for i in range(self._custom_sub_list.topLevelItemCount()):
item = self._custom_sub_list.topLevelItem(i)
if item.text(0) == topic:
item.setText(1, str(qos))
found = True
break
if not found:
self._custom_sub_list.addTopLevelItem(QTreeWidgetItem([topic, str(qos)]))
self._log(f"[订阅] {topic} (QoS={qos})")
try:
qos = int(self._custom_sub_qos.currentText())
self._mqtt._client.subscribe(topic, qos=qos)
self._custom_subs[topic] = qos
# 更新列表
found = False
for i in range(self._custom_sub_list.topLevelItemCount()):
item = self._custom_sub_list.topLevelItem(i)
if item.text(0) == topic:
item.setText(1, str(qos))
found = True
break
if not found:
self._custom_sub_list.addTopLevelItem(QTreeWidgetItem([topic, str(qos)]))
self._log(f"[订阅] {topic} (QoS={qos})")
except Exception as e:
QMessageBox.critical(self, "订阅失败", f"{type(e).__name__}: {e}")
self._log(f"[订阅] 失败: {e}")
def _custom_unsubscribe(self):
items = self._custom_sub_list.selectedItems()
if not items:
return
if not self._mqtt._client:
return
for item in items:
topic = item.text(0)
if topic in self._custom_subs:
self._mqtt._client.unsubscribe(topic)
del self._custom_subs[topic]
idx = self._custom_sub_list.indexOfTopLevelItem(item)
self._custom_sub_list.takeTopLevelItem(idx)
self._log(f"[取消订阅] {topic}")
try:
if topic in self._custom_subs:
self._mqtt._client.unsubscribe(topic)
del self._custom_subs[topic]
idx = self._custom_sub_list.indexOfTopLevelItem(item)
self._custom_sub_list.takeTopLevelItem(idx)
self._log(f"[取消订阅] {topic}")
except Exception as e:
self._log(f"[取消订阅] 失败: {e}")
# ================================================================
# MQTT