This commit is contained in:
cxh 2025-10-14 17:40:48 +08:00
parent ec4790e65d
commit 5137f45e30

View File

@ -57,9 +57,7 @@ static void on_mqtt_message_received(const std::string &topic, const std::string
{ {
auto j = nlohmann::json::parse(message); auto j = nlohmann::json::parse(message);
if (topic == g_app_config.mqtt.topics.video_down) if (topic == g_app_config.mqtt.topics.video_down && j["type"] == "request")
{
if (topic.find("/kun/vehicle/video/push/") != std::string::npos && j["type"] == "request")
{ {
std::string seqNo = j.value("seqNo", ""); std::string seqNo = j.value("seqNo", "");
auto data = j["data"]; auto data = j["data"];
@ -73,21 +71,15 @@ static void on_mqtt_message_received(const std::string &topic, const std::string
for (int ch : channels) for (int ch : channels)
{ {
// 用数组索引找到摄像头
if (ch < 0 || ch >= static_cast<int>(g_app_config.cameras.size())) if (ch < 0 || ch >= static_cast<int>(g_app_config.cameras.size()))
{
LOG_WARN("[MQTT] Invalid channel: " + std::to_string(ch));
continue; continue;
}
Camera &cam = g_app_config.cameras[ch]; Camera &cam = g_app_config.cameras[ch];
bool op_result = false; bool op_result = false;
if (switch_val == 0) if (switch_val == 0)
{ {
// 启动流 if (!RTMPManager::is_streaming(cam.name))
if (!RTMPManager::is_streaming(cam.name, type))
{ {
RTMPManager::start_camera(cam, type); RTMPManager::start_camera(cam, type);
op_result = true; op_result = true;
@ -95,8 +87,7 @@ static void on_mqtt_message_received(const std::string &topic, const std::string
} }
else else
{ {
// 停止流 if (RTMPManager::is_streaming(cam.name))
if (RTMPManager::is_streaming(cam.name, type))
{ {
RTMPManager::stop_camera(cam.name, type); RTMPManager::stop_camera(cam.name, type);
op_result = true; op_result = true;
@ -112,25 +103,18 @@ static void on_mqtt_message_received(const std::string &topic, const std::string
resp_data.push_back(ch_resp); resp_data.push_back(ch_resp);
} }
// 构造应答
nlohmann::json reply; nlohmann::json reply;
reply["type"] = "response"; reply["type"] = "response";
reply["seqNo"] = seqNo; reply["seqNo"] = seqNo;
reply["data"] = resp_data; reply["data"] = resp_data;
mqtt_client->publish(topic, reply.dump()); mqtt_client->publish(topic, reply.dump());
LOG_INFO("[MQTT] Replied to video push request: " + reply.dump());
}
else
{
LOG_WARN("[MQTT] Unknown topic or non-request type: " + topic);
} }
} }
catch (const std::exception &e) catch (const std::exception &e)
{ {
LOG_ERROR(std::string("[MQTT] Failed to process incoming JSON: ") + e.what()); LOG_ERROR(std::string("[MQTT] Failed to process incoming JSON: ") + e.what());
} }
}
} }
void mqtt_client_thread_func() void mqtt_client_thread_func()