修改流水号处理方式

This commit is contained in:
cxh 2026-01-07 16:47:03 +08:00
parent acb10586fe
commit d5768b2511

View File

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