This commit is contained in:
cxh 2025-11-14 15:13:05 +08:00
parent 16a05d9fc6
commit c70d14eadb

View File

@ -73,57 +73,79 @@ static void on_mqtt_connected()
static void on_mqtt_disconnected() { LOG_WARN("[MQTT] Disconnected from broker: " + g_app_config.mqtt.server_ip); } static void on_mqtt_disconnected() { LOG_WARN("[MQTT] Disconnected from broker: " + g_app_config.mqtt.server_ip); }
static void handle_video_down_request(const nlohmann::json& req)
{
if (!req.contains("data") || !req["data"].is_object())
{
LOG_WARN("[video_down] Missing data");
return;
}
int sw = req["data"].value("switch", 0);
if (sw != 1)
{
LOG_INFO("[video_down] switch != 1, ignore");
return;
}
// 取得当前所有 RTMP 播放地址
auto channels_info = RTMPManager::get_all_channels_status();
// 构造响应
nlohmann::json resp;
resp["type"] = "response";
resp["seqNo"] = req.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_client->publish(g_app_config.mqtt.topics.video_down, resp.dump(-1), 1);
LOG_INFO("[video_down] response sent");
}
static void handle_record_query_request(const nlohmann::json& req) {}
static void handle_record_play_request(const nlohmann::json& req) {}
static void on_mqtt_message_received(const std::string& topic, const std::string& message) static void on_mqtt_message_received(const std::string& topic, const std::string& message)
{ {
try try
{ {
auto j = nlohmann::json::parse(message); auto j = nlohmann::json::parse(message);
// -------- 判断字段 type 是否为 "request" -------- // 必须是 request
if (j.contains("type") && j["type"].is_string() && j["type"] == "request") if (!j.contains("type") || j["type"] != "request")
{ {
LOG_INFO("[MQTT] Received message on topic [" + topic + "], len = " + std::to_string(message.size())); LOG_INFO("[MQTT] Ignored non-request message");
LOG_INFO("[MQTT] Message content: " + j.dump(-1)); return;
}
// -------- 判断 data.switch 是否为 1 -------- LOG_INFO("[MQTT] Received request on [" + topic + "]");
if (j.contains("data") && j["data"].is_object()) LOG_INFO("[MQTT] Payload: " + j.dump(-1));
{
const auto& d = j["data"];
if (d.contains("switch") && d["switch"].is_number_integer()) // 根据 topic 分发
{ if (topic == g_app_config.mqtt.topics.video_down)
int sw = d["switch"]; {
handle_video_down_request(j);
if (sw == 1) }
{ else if (topic == g_app_config.mqtt.topics.record_query)
// ---- 获取 RTMP 状态 ---- {
auto channels_info = RTMPManager::get_all_channels_status(); handle_record_query_request(j);
}
// ---- 构造 response ---- else if (topic == g_app_config.mqtt.topics.record_play)
nlohmann::json resp; {
resp["type"] = "response"; handle_record_play_request(j);
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);
}
}
}
} }
else else
{ {
LOG_INFO("[MQTT] Message is NOT a request."); LOG_WARN("[MQTT] Unknown topic: " + topic);
} }
} }
catch (const std::exception& e) catch (const std::exception& e)