From ec4790e65d853115f53bd94c45c5e757bb2d57cc Mon Sep 17 00:00:00 2001 From: cxh Date: Tue, 14 Oct 2025 17:37:56 +0800 Subject: [PATCH] temp --- src/mqtt_client_wrapper.cpp | 65 +++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 36 deletions(-) diff --git a/src/mqtt_client_wrapper.cpp b/src/mqtt_client_wrapper.cpp index 5a3cdb3..ca780dc 100644 --- a/src/mqtt_client_wrapper.cpp +++ b/src/mqtt_client_wrapper.cpp @@ -66,56 +66,49 @@ static void on_mqtt_message_received(const std::string &topic, const std::string int switch_val = data.value("switch", 0); int streamType = data.value("streamType", 0); // 0主 1子 - StreamType type = (streamTypeInt == 0) ? StreamType::MAIN : StreamType::SUB; + StreamType type = (streamType == 0) ? StreamType::MAIN : StreamType::SUB; auto channels = data.value("channels", std::vector{}); nlohmann::json resp_data = nlohmann::json::array(); for (int ch : channels) { - // 根据通道号找到摄像头 - auto it = std::find_if(g_app_config.cameras.begin(), g_app_config.cameras.end(), - [ch](const Camera &c) - { return c.channel == ch; }); - - nlohmann::json ch_resp; - ch_resp["loc"] = ch; - - if (it != g_app_config.cameras.end()) + // 用数组索引找到摄像头 + if (ch < 0 || ch >= static_cast(g_app_config.cameras.size())) { - Camera &cam = *it; - bool op_result = false; + LOG_WARN("[MQTT] Invalid channel: " + std::to_string(ch)); + continue; + } - if (switch_val == 0) - { - // 启动流 - if (!RTMPManager::is_streaming(cam.name, type)) - { - RTMPManager::start_camera(cam, type); - op_result = true; - } - } - else - { - // 停止流 - if (RTMPManager::is_streaming(cam.name, type)) - { - RTMPManager::stop_camera(cam.name, type); - op_result = true; - } - } + Camera &cam = g_app_config.cameras[ch]; - ch_resp["url"] = op_result ? RTMPManager::get_stream_url(cam.name, type) : ""; - ch_resp["result"] = op_result ? 0 : 1; - ch_resp["reason"] = op_result ? "" : "already in requested state"; + bool op_result = false; + + if (switch_val == 0) + { + // 启动流 + if (!RTMPManager::is_streaming(cam.name, type)) + { + RTMPManager::start_camera(cam, type); + op_result = true; + } } else { - ch_resp["url"] = ""; - ch_resp["result"] = 1; - ch_resp["reason"] = "channel not found"; + // 停止流 + if (RTMPManager::is_streaming(cam.name, type)) + { + RTMPManager::stop_camera(cam.name, type); + op_result = true; + } } + nlohmann::json ch_resp; + ch_resp["loc"] = ch; + ch_resp["url"] = op_result ? RTMPManager::get_stream_url(cam.name, type) : ""; + ch_resp["result"] = op_result ? 0 : 1; + ch_resp["reason"] = op_result ? "" : "already in requested state"; + resp_data.push_back(ch_resp); }