修改vid获取逻辑

This commit is contained in:
cxh 2026-01-05 13:46:03 +08:00
parent 37a82a9b70
commit 1bc24261ab
3 changed files with 72 additions and 13 deletions

View File

@ -76,6 +76,9 @@ struct AppConfig
std::vector<Camera> cameras;
MQTTConfig mqtt;
// ⭐ 运行期从 orin 获取的 VID优先生效
std::string runtime_vid;
static AppConfig load_from_file(const std::string& filepath)
{
AppConfig cfg;

View File

@ -19,6 +19,30 @@ std::atomic<bool> g_streaming{false};
std::string g_dispatch_id;
std::mutex g_dispatch_id_mutex;
static std::atomic<bool> g_mqtt_activated{false};
static std::string g_last_vid;
static bool try_activate_mqtt()
{
if (g_app_config.runtime_vid.empty()) return false;
if (g_mqtt_activated.load() && g_last_vid == g_app_config.runtime_vid) return true;
// VID 首次 or 发生变化
g_app_config.mqtt.topics.fill_with_veh_id(g_app_config.runtime_vid);
g_last_vid = g_app_config.runtime_vid;
LOG_INFO("[MQTT] Activated with VID=" + g_last_vid);
mqtt_client->subscribe(g_app_config.mqtt.topics.video_down);
mqtt_client->subscribe(g_app_config.mqtt.topics.record_query);
mqtt_client->subscribe(g_app_config.mqtt.topics.record_play);
mqtt_client->subscribe(g_app_config.mqtt.topics.vehicle_ctrl);
g_mqtt_activated.store(true);
return true;
}
static void send_heartbeat()
{
if (!mqtt_client || !mqtt_client->isConnected()) return;
@ -69,10 +93,7 @@ static void send_heartbeat()
static void on_mqtt_connected()
{
LOG_INFO("[MQTT] Connected to broker: " + g_app_config.mqtt.server_ip);
mqtt_client->subscribe(g_app_config.mqtt.topics.video_down);
mqtt_client->subscribe(g_app_config.mqtt.topics.record_query);
mqtt_client->subscribe(g_app_config.mqtt.topics.record_play);
mqtt_client->subscribe(g_app_config.mqtt.topics.vehicle_ctrl);
g_mqtt_activated.store(false); // 重连后需要重新激活
}
static void on_mqtt_disconnected() { LOG_WARN("[MQTT] Disconnected from broker: " + g_app_config.mqtt.server_ip); }
@ -363,8 +384,12 @@ void mqtt_client_thread_func()
// 主循环:心跳
while (g_running && mqtt_client->isConnected())
{
send_heartbeat();
try_activate_mqtt(); // VID 到来时触发订阅
if (g_mqtt_activated.load())
{
send_heartbeat();
}
auto sleep_time = heartbeat_interval;
while (sleep_time.count() > 0 && g_running && mqtt_client->isConnected())
{

View File

@ -54,11 +54,9 @@ void start_http_server(int port)
http_server->Post("/api/v1/device/register",
[](const httplib::Request& req, httplib::Response& res)
{
LOG_INFO("[http] /device/register called");
// 解析 A 的信息(可选,先简单点)
LOG_INFO("[http] payload: " + req.body);
LOG_INFO("[http] /api/v1/device/register called");
// ---------- 1. IMEI 是否就绪 ----------
if (!is_imei_ready())
{
res.status = 503;
@ -66,13 +64,46 @@ void start_http_server(int port)
return;
}
// ---------- 2. 解析请求体(尝试获取 VID ----------
std::string vid;
if (!req.body.empty())
{
try
{
auto jreq = nlohmann::json::parse(req.body);
if (jreq.contains("vid") && jreq["vid"].is_string())
{
vid = jreq["vid"].get<std::string>();
LOG_INFO("[http] register vid = " + vid);
// ⭐ 运行期 VID只记录不判断
g_app_config.runtime_vid = vid;
}
}
catch (const std::exception& e)
{
LOG_WARN(std::string("[http] invalid json payload: ") + e.what());
}
}
else
{
LOG_WARN("[http] empty register payload");
}
// ---------- 3. 返回 B 的身份 ----------
std::string imei = get_imei();
nlohmann::json j;
j["ok"] = true;
j["server"] = {{"device_type", "tbox"}, {"imei", imei}, {"fw", "b-v2.1.0"}};
nlohmann::json jres;
jres["ok"] = true;
jres["server"] = {{"device_type", "neardi"}, {"imei", imei}, {"fw", "b-v2.1.0"}};
res.set_content(j.dump(-1), "application/json");
// 可选:把 vid 回显(方便 A / 调试)
if (!vid.empty())
{
jres["peer"] = {{"vid", vid}};
}
res.set_content(jres.dump(), "application/json");
});
http_thread = std::thread(