From f571282c85c4747ff8cb8725861b81a13ea85592 Mon Sep 17 00:00:00 2001 From: cxh Date: Tue, 9 Sep 2025 16:45:54 +0800 Subject: [PATCH] first commit --- src/mqtt_client_wrapper.cpp | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/mqtt_client_wrapper.cpp b/src/mqtt_client_wrapper.cpp index 9f8ba15..00f2e04 100644 --- a/src/mqtt_client_wrapper.cpp +++ b/src/mqtt_client_wrapper.cpp @@ -6,6 +6,7 @@ std::atomic mqtt_restart_required{false}; extern std::atomic g_running; +std::atomic g_streaming{false}; // 当前是否有摄像头在拉流 std::string g_dispatch_id; std::mutex g_dispatch_id_mutex; @@ -14,22 +15,13 @@ static void send_heartbeat() if (!mqtt_client || !mqtt_client->isConnected()) return; - // 判断当前状态 - int status = 2; // 默认等待中 - for (const auto &cam : g_app_config.cameras) - { - if (cam.enabled && RTSPManager::is_streaming(cam.name)) - { - status = 0; // 有推流摄像头 - break; - } - } - nlohmann::json hb_data; hb_data["time"] = Logger::get_current_time_utc8(); // UTC+8 - if (!g_dispatch_id.empty()) - hb_data["dispatchId"] = g_dispatch_id; - hb_data["status"] = status; + + // 状态: 2=没有拉流, 0=正在拉流 + hb_data["status"] = g_streaming ? 0 : 2; + + hb_data["dispatchId"] = g_streaming ? g_dispatch_id : ""; nlohmann::json msg; msg["data"] = hb_data; @@ -73,19 +65,22 @@ static void on_mqtt_message_received(const std::string &topic, const std::string return; } - // 写入 dispatchId 时加锁 - if (j["data"].contains("dispatchId")) + // 写 dispatchId 并设置 streaming 状态 { std::lock_guard lock(g_dispatch_id_mutex); - g_dispatch_id = j["data"]["dispatchId"].get(); + if (j["data"].contains("dispatchId")) + g_dispatch_id = j["data"]["dispatchId"].get(); } int status = j["data"]["status"].get(); + std::string seqNo = j["data"]["seqNo"].get(); + bool success = true; // 标记是否操作成功 if (status == 0) { + g_streaming = true; // 启动推流:挂载本地配置中 enabled 的摄像头 for (const auto &cam : g_app_config.cameras) { @@ -101,6 +96,9 @@ static void on_mqtt_message_received(const std::string &topic, const std::string } else if (status == 1) { + g_streaming = false; + std::lock_guard lock(g_dispatch_id_mutex); + g_dispatch_id.clear(); // 停止拉流就清空 dispatchId // 停止推流:卸载本地配置中 enabled 的摄像头 for (const auto &cam : g_app_config.cameras) {