diff --git a/include/app_config.hpp b/include/app_config.hpp index ecc44ef..5a041e2 100644 --- a/include/app_config.hpp +++ b/include/app_config.hpp @@ -76,9 +76,6 @@ struct AppConfig std::vector cameras; MQTTConfig mqtt; - // ⭐ 运行期从 orin 获取的 VID(优先生效) - std::string runtime_vid; - static AppConfig load_from_file(const std::string& filepath) { AppConfig cfg; diff --git a/src/mqtt_client_wrapper.cpp b/src/mqtt_client_wrapper.cpp index 6728dd1..680681e 100644 --- a/src/mqtt_client_wrapper.cpp +++ b/src/mqtt_client_wrapper.cpp @@ -19,47 +19,6 @@ std::atomic g_streaming{false}; std::string g_dispatch_id; std::mutex g_dispatch_id_mutex; -static std::atomic g_mqtt_activated{false}; -static std::string g_last_vid; -static VehicleMQTTTopics g_prev_topics; - -static bool try_activate_mqtt() -{ - if (g_app_config.runtime_vid.empty()) return false; - if (!mqtt_client || !mqtt_client->isConnected()) return false; - - // 如果已激活且 VID 未变化,什么都不做 - if (g_mqtt_activated.load() && g_last_vid == g_app_config.runtime_vid) return true; - - // ---------- 1. 如果是 VID 变化,先取消旧订阅 ---------- - if (g_mqtt_activated.load()) - { - LOG_INFO("[MQTT] VID changed, unsubscribe old topics: " + g_last_vid); - - mqtt_client->unsubscribe(g_prev_topics.video_down); - mqtt_client->unsubscribe(g_prev_topics.record_query); - mqtt_client->unsubscribe(g_prev_topics.record_play); - mqtt_client->unsubscribe(g_prev_topics.vehicle_ctrl); - mqtt_client->unsubscribe(g_prev_topics.heartbeat_up); - } - - // ---------- 2. 切换到新 VID ---------- - g_app_config.mqtt.topics.fill_with_veh_id(g_app_config.runtime_vid); - g_prev_topics = g_app_config.mqtt.topics; - g_last_vid = g_app_config.runtime_vid; - - LOG_INFO("[MQTT] Activated with VID=" + g_last_vid); - - // ---------- 3. 订阅新 topic ---------- - mqtt_client->subscribe(g_app_config.mqtt.topics.video_down); - mqtt_client->subscribe(g_app_config.mqtt.topics.record_query); - mqtt_client->subscribe(g_app_config.mqtt.topics.record_play); - mqtt_client->subscribe(g_app_config.mqtt.topics.vehicle_ctrl); - - g_mqtt_activated.store(true); - return true; -} - static void send_heartbeat() { if (!mqtt_client || !mqtt_client->isConnected()) return; @@ -110,7 +69,14 @@ static void send_heartbeat() static void on_mqtt_connected() { LOG_INFO("[MQTT] Connected to broker: " + g_app_config.mqtt.server_ip); - g_mqtt_activated.store(false); // 重连后需要重新激活 + + // 一次性订阅(VID 来自配置文件,永远不变) + mqtt_client->subscribe(g_app_config.mqtt.topics.video_down); + mqtt_client->subscribe(g_app_config.mqtt.topics.record_query); + mqtt_client->subscribe(g_app_config.mqtt.topics.record_play); + mqtt_client->subscribe(g_app_config.mqtt.topics.vehicle_ctrl); + + LOG_INFO("[MQTT] Subscribed all topics for VID=" + g_app_config.mqtt.vehicle_id); } static void on_mqtt_disconnected() { LOG_WARN("[MQTT] Disconnected from broker: " + g_app_config.mqtt.server_ip); } @@ -401,12 +367,8 @@ void mqtt_client_thread_func() // 主循环:心跳 while (g_running && mqtt_client->isConnected()) { - try_activate_mqtt(); // VID 到来时触发订阅 + send_heartbeat(); - if (g_mqtt_activated.load()) - { - send_heartbeat(); - } auto sleep_time = heartbeat_interval; while (sleep_time.count() > 0 && g_running && mqtt_client->isConnected()) { diff --git a/src/serial_AT.cpp b/src/serial_AT.cpp index f6943bd..470998c 100644 --- a/src/serial_AT.cpp +++ b/src/serial_AT.cpp @@ -65,44 +65,19 @@ void start_http_server(int port) return; } - // ---------- 2. 解析请求体(尝试获取 VID) ---------- - std::string vid; + // ---------- 2. 请求体当前不承载身份(忽略即可) ---------- if (!req.body.empty()) { - try - { - auto jreq = nlohmann::json::parse(req.body); - if (jreq.contains("vid") && jreq["vid"].is_string()) - { - vid = jreq["vid"].get(); - LOG_INFO("[http] register vid = " + vid); - - // ⭐ 运行期 VID:只记录,不判断 - g_app_config.runtime_vid = vid; - } - } - catch (const std::exception& e) - { - LOG_WARN(std::string("[http] invalid json payload: ") + e.what()); - } - } - else - { - LOG_WARN("[http] empty register payload"); + LOG_INFO("[http] register payload ignored"); } - // ---------- 3. 返回 B 的身份 ---------- - std::string imei = get_imei(); - + // ---------- 3. 返回 B 的身份(唯一事实源) ---------- nlohmann::json jres; jres["ok"] = true; - jres["server"] = {{"device_type", "neardi"}, {"imei", imei}, {"fw", "b-v2.1.0"}}; - - // 可选:把 vid 回显(方便 A / 调试) - if (!vid.empty()) - { - jres["peer"] = {{"vid", vid}}; - } + jres["device"] = {{"device_type", "neardi"}, + {"imei", get_imei()}, + {"vid", g_app_config.mqtt.vehicle_id}, + {"fw", "b-v2.1.0"}}; res.set_content(jres.dump(), "application/json"); });