From 44daee9f9577ba531c52171c00d6d1bfffcada95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=80=E6=A2=A6=E5=8D=83=E5=B9=B4?= <421281095@qq.com> Date: Fri, 9 Jan 2026 15:22:14 +0800 Subject: [PATCH] =?UTF-8?q?feat(detection):=20=E5=B0=86=E6=A3=80=E6=B5=8B?= =?UTF-8?q?=E7=BB=93=E6=9E=9C=E9=80=9A=E8=BF=87MQTT=E5=8F=91=E9=80=81?= =?UTF-8?q?=E8=80=8C=E4=B8=8D=E6=98=AFHTTP=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除原有的HTTP POST请求逻辑 - 添加MQTT客户端连接检查 - 构造ai/task/{taskid}/achievement主题用于发布消息 - 使用json.dumps序列化负载数据并确保ASCII字符正确处理 - 添加MQTT消息发布状态检查和错误处理 - 记录MQTT消息发送成功或失败的日志信息 - 当MQTT客户端未连接时跳过消息发送并记录警告 --- detectionThread.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/detectionThread.py b/detectionThread.py index 645069b..ead2586 100644 --- a/detectionThread.py +++ b/detectionThread.py @@ -822,14 +822,23 @@ class DetectionThread(threading.Thread): if self.latest_drone_data: payload["drone_info"] = self.latest_drone_data - headers = {"Content-Type": "application/json"} - # 设置较短的超时时间 - response = requests.post(self.res_api, json=payload, headers=headers, timeout=5) - - if response.status_code == 200: - logger.debug(f"已上传帧至 MinIO: {object_path} | 耗时: {time.time() - start_time:.2f}s") + # 发送MQTT消息 + if self.mqtt_client and self.mqtt_connected: + try: + # 构造MQTT主题 + mqtt_topic = f'ai/task/{self.taskid}/achievement' + + # 发布消息 + result = self.mqtt_client.publish(mqtt_topic, json.dumps(payload, ensure_ascii=False)) + + if result.rc == mqtt.MQTT_ERR_SUCCESS: + logger.debug(f'已上传帧至 MinIO: {object_path} | MQTT消息已发送到主题: {mqtt_topic} | 耗时: {time.time() - start_time:.2f}s') + else: + logger.warning(f'MQTT消息发送失败: {mqtt.error_string(result.rc)}') + except Exception as e: + logger.error(f'MQTT消息发送异常: {str(e)}') else: - logger.warning(f"API调用失败: {response.status_code} - {response.text}") + logger.warning('MQTT客户端未连接,跳过消息发送') except requests.exceptions.Timeout: logger.warning(f"API调用超时: {self.res_api}") except Exception as e: