2025-09-08 15:09:33 +08:00
|
|
|
|
// mqtt_client_wrapper.cpp
|
2025-09-08 10:59:08 +08:00
|
|
|
|
#include "mqtt_client_wrapper.hpp"
|
2025-10-15 15:01:55 +08:00
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
2025-09-10 10:34:36 +08:00
|
|
|
|
#include <chrono>
|
2025-10-15 17:01:43 +08:00
|
|
|
|
#include <condition_variable>
|
|
|
|
|
|
#include <mutex>
|
2025-09-10 10:34:36 +08:00
|
|
|
|
#include <nlohmann/json.hpp>
|
2025-10-15 17:01:43 +08:00
|
|
|
|
#include <queue>
|
2025-10-15 15:01:55 +08:00
|
|
|
|
#include <thread>
|
2025-09-08 10:59:08 +08:00
|
|
|
|
|
2025-11-14 15:40:14 +08:00
|
|
|
|
#include "record_manager.hpp"
|
2025-11-14 17:18:49 +08:00
|
|
|
|
#include "rtmp_manager.hpp"
|
2025-11-14 15:40:14 +08:00
|
|
|
|
|
2025-09-08 10:59:08 +08:00
|
|
|
|
std::shared_ptr<MQTTClient> mqtt_client;
|
2025-11-14 15:40:14 +08:00
|
|
|
|
|
2025-09-08 15:45:22 +08:00
|
|
|
|
extern std::atomic<bool> g_running;
|
2025-09-10 10:34:36 +08:00
|
|
|
|
std::atomic<bool> g_streaming{false};
|
2025-09-09 16:10:56 +08:00
|
|
|
|
std::string g_dispatch_id;
|
|
|
|
|
|
std::mutex g_dispatch_id_mutex;
|
|
|
|
|
|
|
2026-01-05 13:46:03 +08:00
|
|
|
|
static std::atomic<bool> g_mqtt_activated{false};
|
|
|
|
|
|
static std::string g_last_vid;
|
2026-01-05 14:03:24 +08:00
|
|
|
|
static VehicleMQTTTopics g_prev_topics;
|
2026-01-05 13:46:03 +08:00
|
|
|
|
|
|
|
|
|
|
static bool try_activate_mqtt()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (g_app_config.runtime_vid.empty()) return false;
|
2026-01-05 14:01:42 +08:00
|
|
|
|
if (!mqtt_client || !mqtt_client->isConnected()) return false;
|
2026-01-05 13:46:03 +08:00
|
|
|
|
|
2026-01-05 14:01:42 +08:00
|
|
|
|
// 如果已激活且 VID 未变化,什么都不做
|
2026-01-05 13:46:03 +08:00
|
|
|
|
if (g_mqtt_activated.load() && g_last_vid == g_app_config.runtime_vid) return true;
|
|
|
|
|
|
|
2026-01-05 14:01:42 +08:00
|
|
|
|
// ---------- 1. 如果是 VID 变化,先取消旧订阅 ----------
|
|
|
|
|
|
if (g_mqtt_activated.load())
|
|
|
|
|
|
{
|
|
|
|
|
|
LOG_INFO("[MQTT] VID changed, unsubscribe old topics: " + g_last_vid);
|
|
|
|
|
|
|
|
|
|
|
|
mqtt_client->unsubscribe(g_prev_topics.video_down);
|
|
|
|
|
|
mqtt_client->unsubscribe(g_prev_topics.record_query);
|
|
|
|
|
|
mqtt_client->unsubscribe(g_prev_topics.record_play);
|
|
|
|
|
|
mqtt_client->unsubscribe(g_prev_topics.vehicle_ctrl);
|
|
|
|
|
|
mqtt_client->unsubscribe(g_prev_topics.heartbeat_up);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---------- 2. 切换到新 VID ----------
|
2026-01-05 13:46:03 +08:00
|
|
|
|
g_app_config.mqtt.topics.fill_with_veh_id(g_app_config.runtime_vid);
|
2026-01-05 14:01:42 +08:00
|
|
|
|
g_prev_topics = g_app_config.mqtt.topics;
|
2026-01-05 13:46:03 +08:00
|
|
|
|
g_last_vid = g_app_config.runtime_vid;
|
|
|
|
|
|
|
|
|
|
|
|
LOG_INFO("[MQTT] Activated with VID=" + g_last_vid);
|
|
|
|
|
|
|
2026-01-05 14:01:42 +08:00
|
|
|
|
// ---------- 3. 订阅新 topic ----------
|
2026-01-05 13:46:03 +08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-09 16:04:58 +08:00
|
|
|
|
static void send_heartbeat()
|
|
|
|
|
|
{
|
2025-10-15 15:01:55 +08:00
|
|
|
|
if (!mqtt_client || !mqtt_client->isConnected()) return;
|
2025-09-09 16:04:58 +08:00
|
|
|
|
|
2025-10-16 13:19:58 +08:00
|
|
|
|
// 获取当前时间戳(13位毫秒)
|
|
|
|
|
|
auto now = std::chrono::system_clock::now();
|
|
|
|
|
|
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
|
2025-09-09 16:45:54 +08:00
|
|
|
|
|
2025-10-17 15:52:47 +08:00
|
|
|
|
// 获取 RTMP 状态
|
|
|
|
|
|
auto channels_info = RTMPManager::get_all_channels_status();
|
2025-10-17 15:26:08 +08:00
|
|
|
|
|
|
|
|
|
|
nlohmann::json channels = nlohmann::json::array();
|
2025-10-17 15:52:47 +08:00
|
|
|
|
int total = static_cast<int>(channels_info.size());
|
|
|
|
|
|
int running_count = 0;
|
2025-10-17 15:26:08 +08:00
|
|
|
|
|
2025-11-13 13:11:24 +08:00
|
|
|
|
for (const auto& ch : channels_info)
|
2025-10-17 15:26:08 +08:00
|
|
|
|
{
|
|
|
|
|
|
nlohmann::json item;
|
2025-10-17 15:52:47 +08:00
|
|
|
|
item["loc"] = ch.loc;
|
|
|
|
|
|
item["running"] = ch.running;
|
|
|
|
|
|
|
|
|
|
|
|
if (ch.running)
|
|
|
|
|
|
{
|
2025-11-13 13:11:24 +08:00
|
|
|
|
item["reason"] = nullptr;
|
2025-10-17 15:52:47 +08:00
|
|
|
|
running_count++;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
item["reason"] = ch.reason.empty() ? "Unknown error" : ch.reason;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-17 15:26:08 +08:00
|
|
|
|
channels.push_back(item);
|
|
|
|
|
|
}
|
2025-09-09 16:04:58 +08:00
|
|
|
|
|
2025-10-16 13:19:58 +08:00
|
|
|
|
nlohmann::json hb;
|
|
|
|
|
|
hb["timestamp"] = ms;
|
2025-10-17 15:52:47 +08:00
|
|
|
|
hb["status"] = (running_count == 0) ? 0 // 全部失败
|
|
|
|
|
|
: (running_count == total ? 1 : 2); // 全部正常 or 部分异常
|
2025-10-17 15:26:08 +08:00
|
|
|
|
hb["channels"] = channels;
|
2025-09-09 16:04:58 +08:00
|
|
|
|
|
2025-10-17 15:52:47 +08:00
|
|
|
|
// 发布心跳
|
|
|
|
|
|
mqtt_client->publish(g_app_config.mqtt.topics.heartbeat_up, hb.dump(), 0);
|
2025-11-13 13:21:10 +08:00
|
|
|
|
// LOG_INFO("[MQTT] Sent video heartbeat (" + std::to_string(running_count) + "/" + std::to_string(total) +
|
|
|
|
|
|
// " running): " + hb.dump());
|
2025-09-09 16:04:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-10 13:21:43 +08:00
|
|
|
|
// MQTT 回调
|
2025-11-13 13:21:10 +08:00
|
|
|
|
static void on_mqtt_connected()
|
|
|
|
|
|
{
|
|
|
|
|
|
LOG_INFO("[MQTT] Connected to broker: " + g_app_config.mqtt.server_ip);
|
2026-01-05 13:46:03 +08:00
|
|
|
|
g_mqtt_activated.store(false); // 重连后需要重新激活
|
2025-11-13 13:21:10 +08:00
|
|
|
|
}
|
2025-09-08 10:59:08 +08:00
|
|
|
|
|
2025-10-15 15:01:55 +08:00
|
|
|
|
static void on_mqtt_disconnected() { LOG_WARN("[MQTT] Disconnected from broker: " + g_app_config.mqtt.server_ip); }
|
2025-09-08 10:59:08 +08:00
|
|
|
|
|
2025-11-14 15:13:05 +08:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-14 16:00:45 +08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const auto& d = req["data"];
|
|
|
|
|
|
int loc = d.value("loc", -1);
|
|
|
|
|
|
int64_t start = d.value("startTime", -1LL);
|
|
|
|
|
|
int64_t end = d.value("endTime", -1LL);
|
|
|
|
|
|
|
2025-11-14 17:05:24 +08:00
|
|
|
|
if (loc < 0 || start <= 0 || end <= 0 || start >= end)
|
2025-11-14 16:00:45 +08:00
|
|
|
|
{
|
|
|
|
|
|
LOG_WARN("[record_query] invalid parameters");
|
|
|
|
|
|
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"
|
2025-11-14 16:05:51 +08:00
|
|
|
|
std::string stream = RecordManager::loc_to_stream(loc);
|
2025-11-14 16:00:45 +08:00
|
|
|
|
if (stream.empty())
|
|
|
|
|
|
{
|
|
|
|
|
|
LOG_WARN("[record_query] invalid loc: " + std::to_string(loc));
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
|
|
|
|
nlohmann::json data;
|
|
|
|
|
|
data["loc"] = loc;
|
|
|
|
|
|
data["segments"] = nlohmann::json::array();
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
resp["data"] = data;
|
|
|
|
|
|
|
|
|
|
|
|
// 5. 发送 MQTT
|
|
|
|
|
|
mqtt_client->publish(g_app_config.mqtt.topics.record_query, resp.dump(-1), 1);
|
|
|
|
|
|
}
|
2025-11-14 15:13:05 +08:00
|
|
|
|
|
2025-11-14 17:18:49 +08:00
|
|
|
|
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"];
|
|
|
|
|
|
|
|
|
|
|
|
int loc = d.value("loc", -1);
|
|
|
|
|
|
std::string segmentId = d.value("segmentId", "");
|
|
|
|
|
|
|
|
|
|
|
|
if (loc < 0 || segmentId.empty())
|
|
|
|
|
|
{
|
|
|
|
|
|
LOG_WARN("[record_play] invalid parameters");
|
|
|
|
|
|
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())
|
|
|
|
|
|
{
|
|
|
|
|
|
LOG_WARN("[record_play] no files found for segmentId: " + segmentId);
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
|
|
|
|
nlohmann::json data;
|
|
|
|
|
|
data["loc"] = loc;
|
|
|
|
|
|
data["segmentId"] = segmentId;
|
|
|
|
|
|
data["startTime"] = seg.start_ms;
|
|
|
|
|
|
data["endTime"] = seg.end_ms;
|
|
|
|
|
|
data["duration"] = duration;
|
|
|
|
|
|
|
2025-11-14 18:00:20 +08:00
|
|
|
|
int index = 1;
|
|
|
|
|
|
|
2025-11-14 17:18:49 +08:00
|
|
|
|
// 6. 单文件列表
|
|
|
|
|
|
nlohmann::json files = nlohmann::json::array();
|
|
|
|
|
|
for (auto& f : seg.files)
|
|
|
|
|
|
{
|
|
|
|
|
|
nlohmann::json item;
|
2025-11-14 18:00:20 +08:00
|
|
|
|
item["index"] = index++;
|
2025-11-14 17:18:49 +08:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
data["files"] = files;
|
|
|
|
|
|
resp["data"] = data;
|
|
|
|
|
|
|
|
|
|
|
|
// 7. 回复 MQTT
|
|
|
|
|
|
mqtt_client->publish(g_app_config.mqtt.topics.record_play, resp.dump(), 1);
|
|
|
|
|
|
}
|
2025-11-14 15:13:05 +08:00
|
|
|
|
|
2025-12-29 17:45:57 +08:00
|
|
|
|
static void handle_vehicle_ctrl_request(const nlohmann::json& req)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::string cmd = req.value("command", "");
|
|
|
|
|
|
int value = req.value("value", 0);
|
|
|
|
|
|
|
|
|
|
|
|
// value 固定为 1,不是 1 直接忽略
|
|
|
|
|
|
if (value != 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
LOG_WARN("[vehicle_ctrl] ignore command=" + cmd + " value=" + std::to_string(value));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (cmd == "startCtrl")
|
|
|
|
|
|
{
|
|
|
|
|
|
LOG_INFO("[vehicle_ctrl] startCtrl → start all RTMP");
|
|
|
|
|
|
RTMPManager::start_all();
|
|
|
|
|
|
g_streaming.store(true);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (cmd == "stopCtrl")
|
|
|
|
|
|
{
|
|
|
|
|
|
LOG_INFO("[vehicle_ctrl] stopCtrl → stop all RTMP");
|
|
|
|
|
|
RTMPManager::stop_all();
|
|
|
|
|
|
g_streaming.store(false);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LOG_WARN("[vehicle_ctrl] unknown command: " + cmd);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-13 13:11:24 +08:00
|
|
|
|
static void on_mqtt_message_received(const std::string& topic, const std::string& message)
|
2025-09-08 10:59:08 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-10-14 17:13:08 +08:00
|
|
|
|
auto j = nlohmann::json::parse(message);
|
|
|
|
|
|
|
2025-12-30 09:29:11 +08:00
|
|
|
|
LOG_INFO("[MQTT] Received message on [" + topic + "]");
|
|
|
|
|
|
LOG_INFO("[MQTT] Payload: " + j.dump(-1));
|
|
|
|
|
|
|
|
|
|
|
|
// ===============================
|
|
|
|
|
|
// 1. vehicle_ctrl:放宽协议限制
|
|
|
|
|
|
// ===============================
|
|
|
|
|
|
if (topic == g_app_config.mqtt.topics.vehicle_ctrl)
|
2025-11-14 15:13:05 +08:00
|
|
|
|
{
|
2025-12-30 09:29:11 +08:00
|
|
|
|
// vehicle_ctrl 只要求有 command
|
|
|
|
|
|
if (!j.contains("command"))
|
|
|
|
|
|
{
|
|
|
|
|
|
LOG_WARN("[MQTT] vehicle_ctrl missing 'command'");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
handle_vehicle_ctrl_request(j);
|
2025-11-14 15:13:05 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-30 09:29:11 +08:00
|
|
|
|
// ==========================================
|
|
|
|
|
|
// 2. 其他 topic:必须是 type=request
|
|
|
|
|
|
// ==========================================
|
|
|
|
|
|
if (!j.contains("type") || j["type"] != "request")
|
|
|
|
|
|
{
|
|
|
|
|
|
// 非 request 的消息直接忽略
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-11-14 15:13:05 +08:00
|
|
|
|
|
2025-12-30 09:29:11 +08:00
|
|
|
|
// ===============================
|
|
|
|
|
|
// 3. 按 topic 分发
|
|
|
|
|
|
// ===============================
|
2025-11-14 15:13:05 +08:00
|
|
|
|
if (topic == g_app_config.mqtt.topics.video_down)
|
|
|
|
|
|
{
|
|
|
|
|
|
handle_video_down_request(j);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (topic == g_app_config.mqtt.topics.record_query)
|
|
|
|
|
|
{
|
|
|
|
|
|
handle_record_query_request(j);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (topic == g_app_config.mqtt.topics.record_play)
|
2025-11-13 13:41:24 +08:00
|
|
|
|
{
|
2025-11-14 15:13:05 +08:00
|
|
|
|
handle_record_play_request(j);
|
2025-11-13 13:41:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-11-14 15:13:05 +08:00
|
|
|
|
LOG_WARN("[MQTT] Unknown topic: " + topic);
|
2025-11-13 13:41:24 +08:00
|
|
|
|
}
|
2025-10-14 17:40:48 +08:00
|
|
|
|
}
|
2025-11-13 13:11:24 +08:00
|
|
|
|
catch (const std::exception& e)
|
2025-10-14 17:40:48 +08:00
|
|
|
|
{
|
|
|
|
|
|
LOG_ERROR(std::string("[MQTT] Failed to process incoming JSON: ") + e.what());
|
2025-09-08 10:59:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void mqtt_client_thread_func()
|
|
|
|
|
|
{
|
2025-11-13 13:11:24 +08:00
|
|
|
|
const auto& cfg = g_app_config.mqtt;
|
2025-09-09 16:32:28 +08:00
|
|
|
|
auto heartbeat_interval = std::chrono::milliseconds(static_cast<int>(cfg.keep_alive * 0.9));
|
2025-09-08 15:09:33 +08:00
|
|
|
|
|
2025-09-08 15:45:22 +08:00
|
|
|
|
while (g_running)
|
|
|
|
|
|
{
|
2025-09-08 10:59:08 +08:00
|
|
|
|
mqtt_client = std::make_unique<MQTTClient>(cfg);
|
|
|
|
|
|
mqtt_client->setConnectCallback(on_mqtt_connected);
|
|
|
|
|
|
mqtt_client->setDisconnectCallback(on_mqtt_disconnected);
|
|
|
|
|
|
mqtt_client->setMessageCallback(on_mqtt_message_received);
|
|
|
|
|
|
|
2025-09-10 13:33:29 +08:00
|
|
|
|
// 等待连接
|
2025-09-10 10:34:36 +08:00
|
|
|
|
while (g_running && !mqtt_client->isConnected())
|
|
|
|
|
|
{
|
2025-09-10 13:33:29 +08:00
|
|
|
|
mqtt_client->connect();
|
2025-09-10 11:06:42 +08:00
|
|
|
|
for (int i = 0; i < 10 && g_running && !mqtt_client->isConnected(); i++)
|
2025-09-10 13:21:43 +08:00
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
|
|
|
|
|
|
2025-10-15 15:01:55 +08:00
|
|
|
|
if (!g_running && !mqtt_client->isConnected()) mqtt_client->force_disconnect();
|
2025-09-10 10:34:36 +08:00
|
|
|
|
}
|
2025-09-08 10:59:08 +08:00
|
|
|
|
|
2025-09-10 13:33:29 +08:00
|
|
|
|
// 主循环:心跳
|
2025-09-10 10:34:36 +08:00
|
|
|
|
while (g_running && mqtt_client->isConnected())
|
2025-09-08 10:59:08 +08:00
|
|
|
|
{
|
2026-01-05 13:46:03 +08:00
|
|
|
|
try_activate_mqtt(); // VID 到来时触发订阅
|
2025-09-10 13:21:43 +08:00
|
|
|
|
|
2026-01-05 13:46:03 +08:00
|
|
|
|
if (g_mqtt_activated.load())
|
|
|
|
|
|
{
|
|
|
|
|
|
send_heartbeat();
|
|
|
|
|
|
}
|
2025-09-10 11:06:42 +08:00
|
|
|
|
auto sleep_time = heartbeat_interval;
|
|
|
|
|
|
while (sleep_time.count() > 0 && g_running && mqtt_client->isConnected())
|
|
|
|
|
|
{
|
2025-09-10 13:21:43 +08:00
|
|
|
|
auto chunk = std::min(sleep_time, std::chrono::milliseconds(50));
|
2025-09-10 11:06:42 +08:00
|
|
|
|
std::this_thread::sleep_for(chunk);
|
|
|
|
|
|
sleep_time -= chunk;
|
|
|
|
|
|
}
|
2025-09-10 13:21:43 +08:00
|
|
|
|
|
2025-10-15 15:01:55 +08:00
|
|
|
|
if (!g_running && mqtt_client->isConnected()) mqtt_client->force_disconnect();
|
2025-09-08 10:59:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-10 13:33:29 +08:00
|
|
|
|
// 清理
|
2025-09-10 11:06:42 +08:00
|
|
|
|
if (mqtt_client)
|
|
|
|
|
|
{
|
2025-09-10 11:18:29 +08:00
|
|
|
|
if (g_running)
|
|
|
|
|
|
mqtt_client->disconnect();
|
|
|
|
|
|
else
|
|
|
|
|
|
mqtt_client->force_disconnect();
|
2025-09-10 11:06:42 +08:00
|
|
|
|
mqtt_client.reset();
|
|
|
|
|
|
}
|
2025-09-10 11:18:29 +08:00
|
|
|
|
|
2025-10-15 15:01:55 +08:00
|
|
|
|
if (!g_running) break;
|
2025-09-10 11:18:29 +08:00
|
|
|
|
|
2025-09-10 13:33:29 +08:00
|
|
|
|
// 短暂等待再重连
|
2025-10-15 15:01:55 +08:00
|
|
|
|
for (int i = 0; i < 5 && g_running; i++) std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
2025-09-08 10:59:08 +08:00
|
|
|
|
}
|
2025-09-08 15:45:22 +08:00
|
|
|
|
LOG_INFO("[MQTT] Client thread exiting.");
|
2025-09-08 10:59:08 +08:00
|
|
|
|
}
|