From 6eb93637c717bb2f754b96b0f3c6ab8b499acf57 Mon Sep 17 00:00:00 2001 From: wangfq Date: Tue, 7 Jul 2026 14:15:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20DBNMQTTool=20=E2=80=94=20=E6=89=80?= =?UTF-8?q?=E6=9C=89=20publish=20=E8=B7=AF=E5=BE=84=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E9=80=9A=E7=94=A8=E5=BC=82=E5=B8=B8=E6=8D=95=E8=8E=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题: _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 空检查 --- DBNMQTTool/main.py | 66 +++++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 24 deletions(-) diff --git a/DBNMQTTool/main.py b/DBNMQTTool/main.py index 3940b78..b6fd6ed 100644 --- a/DBNMQTTool/main.py +++ b/DBNMQTTool/main.py @@ -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