#include "mqtt_router.h" #include "spdlog/spdlog.h" #include "utils/mqtt_topic_matcher.h" MqttRouter::MqttRouter(MqttClient& client) : m_client(client) { m_data_handler = std::make_unique(m_client); m_command_handler = std::make_unique(m_client); m_client.set_message_callback([this](mqtt::const_message_ptr msg) { this->on_message_arrived(std::move(msg)); }); spdlog::info("MqttRouter initialized with all handlers."); } void MqttRouter::start() { m_client.subscribe("devices/+/data"); m_client.subscribe("commands/my-edge-proxy-device-01/#"); spdlog::info("MqttRouter has subscribed to all topics."); } void MqttRouter::on_message_arrived(mqtt::const_message_ptr msg) { const auto& topic = msg->get_topic(); // 定义我们的主题过滤器 const std::string data_filter = "devices/+/data"; const std::string command_filter = "commands/my-edge-proxy-device-01/#"; if (MqttUtils::topic_matches(data_filter, topic)) { m_data_handler->handle(msg); } else if (MqttUtils::topic_matches(command_filter, topic)) { m_command_handler->handle(msg); } else { spdlog::warn("MqttRouter received message on unhandled topic: {}", topic); } }