diff --git a/src/mqtt_client_wrapper.cpp b/src/mqtt_client_wrapper.cpp index 680681e..0aed6c6 100644 --- a/src/mqtt_client_wrapper.cpp +++ b/src/mqtt_client_wrapper.cpp @@ -96,13 +96,18 @@ static void handle_video_down_request(const nlohmann::json& req) return; } - // 取得当前所有 RTMP 播放地址 + // ===== seqNo 自适应 ===== + nlohmann::json seqNo = nullptr; + if (req.contains("seqNo")) + { + seqNo = req["seqNo"]; + } + auto channels_info = RTMPManager::get_all_channels_status(); - // 构造响应 nlohmann::json resp; resp["type"] = "response"; - resp["seqNo"] = req.value("seqNo", "0"); + if (!seqNo.is_null()) resp["seqNo"] = seqNo; resp["data"] = nlohmann::json::array(); int loc = 0; @@ -112,19 +117,24 @@ static void handle_video_down_request(const nlohmann::json& req) loc++; } - // 发布回复 mqtt_client->publish(g_app_config.mqtt.topics.video_down, resp.dump(-1), 1); } static void handle_record_query_request(const nlohmann::json& req) { - // 1. 解析字段 if (!req.contains("data") || !req["data"].is_object()) { LOG_WARN("[record_query] missing data"); return; } + // ===== seqNo 自适应 ===== + nlohmann::json seqNo = nullptr; + if (req.contains("seqNo")) + { + seqNo = req["seqNo"]; + } + const auto& d = req["data"]; int loc = d.value("loc", -1); int64_t start = d.value("startTime", -1LL); @@ -136,12 +146,6 @@ static void handle_record_query_request(const nlohmann::json& req) return; } - LOG_INFO("[record_query] loc=" + std::to_string(loc) + " start=" + std::to_string(start) + - " end=" + std::to_string(end)); - - // 2. 计算 stream 名 - // loc → stream,比如: - // 0 → "AHD1_main" std::string stream = RecordManager::loc_to_stream(loc); if (stream.empty()) { @@ -149,15 +153,11 @@ static void handle_record_query_request(const nlohmann::json& req) return; } - // 3. 向 RecordManager 查询录像段 auto segs = g_record_manager->querySegments(stream, start, end); - LOG_INFO("[record_query] Found segments=" + std::to_string(segs.size())); - - // 4. 构造响应 JSON nlohmann::json resp; resp["type"] = "response"; - resp["seqNo"] = req.value("seqNo", "0"); + if (!seqNo.is_null()) resp["seqNo"] = seqNo; nlohmann::json data; data["loc"] = loc; @@ -166,31 +166,31 @@ static void handle_record_query_request(const nlohmann::json& req) int idx = 1; for (const auto& seg : segs) { - nlohmann::json s; - s["index"] = idx++; - s["segmentId"] = seg.segment_id; - s["startTime"] = seg.start_ms; - s["endTime"] = seg.end_ms; - data["segments"].push_back(s); + data["segments"].push_back( + {{"index", idx++}, {"segmentId", seg.segment_id}, {"startTime", seg.start_ms}, {"endTime", seg.end_ms}}); } resp["data"] = data; - // 5. 发送 MQTT mqtt_client->publish(g_app_config.mqtt.topics.record_query, resp.dump(-1), 1); } static void handle_record_play_request(const nlohmann::json& req) { - // 1. 解析参数 if (!req.contains("data") || !req["data"].is_object()) { LOG_WARN("[record_play] missing data"); return; } - const auto& d = req["data"]; + // ===== seqNo 自适应 ===== + nlohmann::json seqNo = nullptr; + if (req.contains("seqNo")) + { + seqNo = req["seqNo"]; + } + const auto& d = req["data"]; int loc = d.value("loc", -1); std::string segmentId = d.value("segmentId", ""); @@ -200,9 +200,6 @@ static void handle_record_play_request(const nlohmann::json& req) return; } - LOG_INFO("[record_play] loc=" + std::to_string(loc) + " segmentId=" + segmentId); - - // 2. 根据 segmentId 获取录像段 auto seg = g_record_manager->getSegment(segmentId); if (seg.files.empty()) { @@ -210,19 +207,16 @@ static void handle_record_play_request(const nlohmann::json& req) return; } - // 3. 计算总时长 int64_t duration = seg.end_ms - seg.start_ms; - // 4. 获取播放地址前缀 std::string ip = get_ip_address("enP2p33s0"); if (ip.empty()) ip = "127.0.0.1"; int http_port = g_record_manager->getHttpPort(); - // 5. 构造响应 nlohmann::json resp; resp["type"] = "response"; - resp["seqNo"] = req.value("seqNo", "0"); + if (!seqNo.is_null()) resp["seqNo"] = seqNo; nlohmann::json data; data["loc"] = loc; @@ -231,23 +225,19 @@ static void handle_record_play_request(const nlohmann::json& req) data["endTime"] = seg.end_ms; data["duration"] = duration; - int index = 1; - - // 6. 单文件列表 nlohmann::json files = nlohmann::json::array(); + int index = 1; for (auto& f : seg.files) { - nlohmann::json item; - item["index"] = index++; - item["url"] = "http://" + ip + ":" + std::to_string(http_port) + f.path; - item["startTime"] = f.start_ms; - item["endTime"] = f.end_ms; - files.push_back(item); + files.push_back({{"index", index++}, + {"url", "http://" + ip + ":" + std::to_string(http_port) + f.path}, + {"startTime", f.start_ms}, + {"endTime", f.end_ms}}); } + data["files"] = files; resp["data"] = data; - // 7. 回复 MQTT mqtt_client->publish(g_app_config.mqtt.topics.record_play, resp.dump(), 1); }