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: