From c6736e4d0b800e190ea614db6d3ede0309ba582e Mon Sep 17 00:00:00 2001 From: cxh Date: Thu, 13 Nov 2025 13:41:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=A7=86=E9=A2=91=E6=92=AD?= =?UTF-8?q?=E6=94=BE=E5=9C=B0=E5=9D=80=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/mqtt_client_wrapper.cpp | 48 +++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/src/mqtt_client_wrapper.cpp b/src/mqtt_client_wrapper.cpp index bd98697..3d2e368 100644 --- a/src/mqtt_client_wrapper.cpp +++ b/src/mqtt_client_wrapper.cpp @@ -77,8 +77,52 @@ static void on_mqtt_message_received(const std::string& topic, const std::string { auto j = nlohmann::json::parse(message); - LOG_INFO("[MQTT] Received message on topic [" + topic + "], len = " + std::to_string(message.size())); - LOG_INFO("[MQTT] Message content: " + j.dump(-1)); + // -------- 判断字段 type 是否为 "request" -------- + if (j.contains("type") && j["type"].is_string() && j["type"] == "request") + { + LOG_INFO("[MQTT] Received message on topic [" + topic + "], len = " + std::to_string(message.size())); + LOG_INFO("[MQTT] Message content: " + j.dump(-1)); + + // -------- 判断 data.switch 是否为 1 -------- + if (j.contains("data") && j["data"].is_object()) + { + const auto& d = j["data"]; + + if (d.contains("switch") && d["switch"].is_number_integer()) + { + int sw = d["switch"]; + + if (sw == 1) + { + // ---- 获取 RTMP 状态 ---- + auto channels_info = RTMPManager::get_all_channels_status(); + + // ---- 构造 response ---- + nlohmann::json resp; + resp["type"] = "response"; + resp["seqNo"] = j.value("seqNo", "0"); + + resp["data"] = nlohmann::json::array(); + + int loc = 0; + + for (const auto& ch : channels_info) + { + resp["data"].push_back({{"loc", loc}, {"url", ch.url}}); + loc++; + } + + // ---- 发布 MQTT 响应 ---- + std::string out = resp.dump(-1); + mqtt_client->publish(g_app_config.mqtt.topics.video_down, out, 1, false); + } + } + } + } + else + { + LOG_INFO("[MQTT] Message is NOT a request."); + } } catch (const std::exception& e) {