feat(mqtt): add MQTT broker connection support

This commit is contained in:
jinmo authored and GitHub committed 2026-08-04 19:05:05 +08:00
1 parent dff33662fc
commit 8cfe380784
125 files changed
+23056 -107

No files matched your search

+11
View File
@@ -1098,6 +1098,12 @@ async fn test_connection_with_info_inner(
Err("Message queue admin support is not compiled in this build. Rebuild with the 'mq-admin' feature."
.to_string())
}
DatabaseType::Mqtt => {
let mqtt_config = dbx_core::mqtt::types::MqttConnectionConfig::from_connection(&config)?;
let client = dbx_core::mqtt::client::MqttClient::connect(mqtt_config).await?;
client.disconnect().await;
Ok("Connection successful".to_string())
}
db_type if database_capabilities::is_agent_type(&db_type) => {
match test_agent_connection(state, &config, &host, port).await {
Ok(details) => {
@@ -1449,6 +1455,11 @@ pub async fn connect_db(
state.external_driver_pool("jdbc", &jdbc_config).await?
}
DatabaseType::Jdbc => state.external_driver_pool("jdbc", &db_config).await?,
DatabaseType::Mqtt => {
let mqtt_config = dbx_core::mqtt::types::MqttConnectionConfig::from_connection(&db_config)?;
let client = dbx_core::mqtt::client::MqttClient::connect(mqtt_config).await?;
PoolKind::Mqtt(client)
}
db_type => return Err(format!("Unsupported database type: {db_type:?}")),
};
+2
View File
@@ -837,6 +837,7 @@ fn path_string(path: &Path) -> String {
path.to_string_lossy().to_string()
}
#[allow(clippy::needless_return)]
pub(crate) fn locate_command(command: &str) -> Option<String> {
#[cfg(windows)]
{
@@ -907,6 +908,7 @@ struct CommandOutput {
stderr: String,
}
#[allow(clippy::needless_return)]
fn command_output(command: &str, args: &[&str]) -> Result<CommandOutput, String> {
let direct = run_command(command, args);
if direct.as_ref().is_ok_and(|output| output.success) {
+1
View File
@@ -26,6 +26,7 @@ pub mod mcp_bridge;
pub mod mongo_cmd;
#[cfg(feature = "mq-admin")]
pub mod mq_cmd;
pub mod mqtt_cmd;
pub mod nacos_cmd;
pub mod plugins;
pub mod prompt_template;
+103
View File
@@ -0,0 +1,103 @@
use std::sync::Arc;
use tauri::State;
use crate::commands::connection::AppState;
use dbx_core::connection::PoolKind;
use dbx_core::mqtt::service;
use dbx_core::mqtt::types::*;
/// 从 connections map 中获取 MQTT 客户端
async fn get_mqtt_client(
state: &AppState,
connection_id: &str,
) -> Result<Arc<dbx_core::mqtt::client::MqttClient>, String> {
let connections = state.connections.read().await;
let pool = connections.get(connection_id).ok_or_else(|| format!("连接 {} 未建立", connection_id))?;
match pool {
PoolKind::Mqtt(client) => Ok(Arc::clone(client)),
_ => Err(format!("连接 {} 不是 MQTT 类型", connection_id)),
}
}
/// 获取 broker 基本信息
#[tauri::command]
pub async fn mqtt_get_broker_info(
state: State<'_, Arc<AppState>>,
connection_id: String,
) -> Result<MqttBrokerInfo, String> {
let client = get_mqtt_client(&state, &connection_id).await?;
service::get_broker_info(&client).await
}
/// 订阅 topic
#[tauri::command]
pub async fn mqtt_subscribe(
state: State<'_, Arc<AppState>>,
connection_id: String,
topic: String,
qos: Option<MqttQoS>,
) -> Result<(), String> {
let client = get_mqtt_client(&state, &connection_id).await?;
service::subscribe(&client, &topic, qos.unwrap_or_default()).await
}
/// 取消订阅 topic
#[tauri::command]
pub async fn mqtt_unsubscribe(
state: State<'_, Arc<AppState>>,
connection_id: String,
topic: String,
) -> Result<(), String> {
let client = get_mqtt_client(&state, &connection_id).await?;
service::unsubscribe(&client, &topic).await
}
/// 发布消息
#[tauri::command]
pub async fn mqtt_publish(
state: State<'_, Arc<AppState>>,
connection_id: String,
request: MqttPublishRequest,
) -> Result<(), String> {
let client = get_mqtt_client(&state, &connection_id).await?;
service::publish(&client, &request).await
}
/// 获取已订阅的 topic 列表
#[tauri::command]
pub async fn mqtt_list_topics(
state: State<'_, Arc<AppState>>,
connection_id: String,
) -> Result<Vec<(String, MqttQoS)>, String> {
let client = get_mqtt_client(&state, &connection_id).await?;
service::list_topics(&client).await
}
/// 获取 topic 树结构
#[tauri::command]
pub async fn mqtt_get_topic_tree(
state: State<'_, Arc<AppState>>,
connection_id: String,
) -> Result<MqttTopicNode, String> {
let client = get_mqtt_client(&state, &connection_id).await?;
service::get_topic_tree(&client).await
}
/// 获取消息列表
#[tauri::command]
pub async fn mqtt_get_messages(
state: State<'_, Arc<AppState>>,
connection_id: String,
topic_filter: Option<String>,
limit: Option<usize>,
) -> Result<Vec<MqttMessage>, String> {
let client = get_mqtt_client(&state, &connection_id).await?;
service::get_messages(&client, topic_filter.as_deref(), limit.unwrap_or(50)).await
}
/// 清空消息历史记录
#[tauri::command]
pub async fn mqtt_clear_messages(state: State<'_, Arc<AppState>>, connection_id: String) -> Result<(), String> {
let client = get_mqtt_client(&state, &connection_id).await?;
service::clear_messages(&client).await
}