yituo_video/src/mqtt_client_wrapper.cpp

159 lines
4.7 KiB
C++
Raw Normal View History

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-12-17 14:13:52 +08:00
#include <algorithm>
2025-09-10 10:34:36 +08:00
#include <chrono>
#include <nlohmann/json.hpp>
2025-12-17 14:13:52 +08:00
#include <thread>
2025-09-08 10:59:08 +08:00
std::shared_ptr<MQTTClient> mqtt_client;
2025-09-08 15:45:22 +08:00
extern std::atomic<bool> g_running;
2025-09-09 16:10:56 +08:00
2025-09-09 16:04:58 +08:00
static void send_heartbeat()
{
2025-12-17 14:13:52 +08:00
if (!mqtt_client || !mqtt_client->isConnected()) return;
2025-09-09 16:04:58 +08:00
nlohmann::json hb_data;
2025-09-10 10:34:36 +08:00
hb_data["time"] = Logger::get_current_time_utc8();
2025-12-17 14:13:52 +08:00
hb_data["status"] = 0; // 固定上报正常
hb_data["dispatchId"] = ""; // 已无任务状态
2025-09-09 16:04:58 +08:00
nlohmann::json msg;
msg["data"] = hb_data;
msg["isEnc"] = 0;
msg["type"] = 0;
mqtt_client->publish(g_app_config.mqtt.topics.heartbeat_up, msg.dump());
}
2025-11-24 14:49:37 +08:00
// MQTT 连接成功
2025-09-08 10:59:08 +08:00
static void on_mqtt_connected()
{
2025-09-08 16:40:04 +08:00
LOG_INFO("[MQTT] Connected to broker: " + g_app_config.mqtt.server_ip);
2025-09-08 15:09:33 +08:00
2025-11-20 16:29:36 +08:00
send_heartbeat();
2025-12-17 14:13:52 +08:00
const auto& topics = g_app_config.mqtt.topics;
2025-09-08 15:09:33 +08:00
mqtt_client->subscribe(topics.video_down);
mqtt_client->subscribe(topics.substream_down);
mqtt_client->subscribe(topics.reset_down);
2025-09-08 10:59:08 +08:00
}
2025-11-24 14:49:37 +08:00
// MQTT 连接断开
2025-12-17 14:13:52 +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-24 14:49:37 +08:00
// 处理消息
2025-12-17 14:13:52 +08:00
static void on_mqtt_message_received(const std::string& topic, const std::string& message)
2025-09-08 10:59:08 +08:00
{
2025-11-24 14:49:37 +08:00
LOG_INFO("[MQTT] Received message on topic [" + topic + "]");
LOG_INFO("[MQTT] Content: " + message);
2025-09-08 10:59:08 +08:00
try
{
2025-11-24 14:49:37 +08:00
// ------- video_down 应答 -------
2025-09-09 09:57:24 +08:00
if (topic == g_app_config.mqtt.topics.video_down)
{
2025-09-09 10:20:04 +08:00
auto j = nlohmann::json::parse(message);
2025-11-24 14:49:37 +08:00
auto seqNo = j["data"].value("seqNo", "");
2025-09-09 10:20:04 +08:00
2025-11-24 14:49:37 +08:00
LOG_INFO("[MQTT] video_down received, RTSP always running (no action).");
2025-09-09 13:58:13 +08:00
2025-09-09 15:07:51 +08:00
nlohmann::json reply_data;
2025-11-24 14:49:37 +08:00
reply_data["time"] = Logger::get_current_time_utc8();
reply_data["result"] = 0;
2025-09-09 15:07:51 +08:00
reply_data["seqNo"] = seqNo;
2025-09-09 13:58:13 +08:00
nlohmann::json reply;
2025-09-09 15:07:51 +08:00
reply["data"] = reply_data;
reply["isEnc"] = 0;
reply["type"] = 0;
2025-09-09 13:58:13 +08:00
2025-11-24 14:49:37 +08:00
mqtt_client->publish(g_app_config.mqtt.topics.video_down_ack, reply.dump());
return;
2025-09-09 09:57:24 +08:00
}
2025-11-24 14:49:37 +08:00
// ------- substream_down 应答 -------
if (topic == g_app_config.mqtt.topics.substream_down)
2025-09-09 09:57:24 +08:00
{
2025-11-24 14:49:37 +08:00
LOG_INFO("[MQTT] substream_down received (ignored).");
return;
2025-09-09 09:57:24 +08:00
}
2025-11-24 14:49:37 +08:00
// ------- reset/down 应答 -------
if (topic == g_app_config.mqtt.topics.reset_down)
2025-09-09 09:57:24 +08:00
{
2025-09-09 15:07:51 +08:00
auto j = nlohmann::json::parse(message);
2025-11-24 14:49:37 +08:00
auto seqNo = j["data"].value("seqNo", "");
2025-09-09 15:07:51 +08:00
2025-11-24 14:49:37 +08:00
LOG_WARN("[MQTT] reset/down received (ignored).");
2025-09-09 15:07:51 +08:00
nlohmann::json reply_data;
reply_data["time"] = Logger::get_current_time_utc8();
2025-11-24 14:49:37 +08:00
reply_data["result"] = 0;
2025-09-09 15:07:51 +08:00
reply_data["seqNo"] = seqNo;
nlohmann::json reply;
reply["data"] = reply_data;
reply["isEnc"] = 0;
reply["type"] = 0;
2025-11-24 14:49:37 +08:00
mqtt_client->publish(g_app_config.mqtt.topics.reset_down_ack, reply.dump());
return;
2025-09-09 09:57:24 +08:00
}
2025-11-24 14:49:37 +08:00
LOG_WARN("[MQTT] Unknown topic: " + topic);
2025-09-08 10:59:08 +08:00
}
2025-12-17 14:13:52 +08:00
catch (const std::exception& e)
2025-09-08 10:59:08 +08:00
{
2025-11-24 14:49:37 +08:00
LOG_ERROR(std::string("[MQTT] JSON processing failed: ") + e.what());
2025-09-08 10:59:08 +08:00
}
}
void mqtt_client_thread_func()
{
2025-12-17 14:13:52 +08:00
const auto& cfg = g_app_config.mqtt;
2025-11-24 14:49:37 +08:00
auto heartbeat_interval = std::chrono::milliseconds((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-12-17 14:13:52 +08:00
mqtt_client = std::make_shared<MQTTClient>(cfg);
2025-09-08 10:59:08 +08:00
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-11-24 14:49:37 +08:00
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-09-10 10:34:36 +08:00
}
2025-09-08 10:59:08 +08:00
2025-11-24 14:49:37 +08:00
// 心跳循环
2025-09-10 10:34:36 +08:00
while (g_running && mqtt_client->isConnected())
2025-09-08 10:59:08 +08:00
{
2025-09-10 13:33:29 +08:00
send_heartbeat();
2025-09-10 13:21:43 +08:00
2025-11-24 14:49:37 +08:00
auto remaining = heartbeat_interval;
while (remaining.count() > 0 && g_running && mqtt_client->isConnected())
2025-09-10 11:06:42 +08:00
{
2025-11-24 14:49:37 +08:00
auto step = std::min(remaining, std::chrono::milliseconds(50));
std::this_thread::sleep_for(step);
remaining -= step;
2025-09-10 11:06:42 +08:00
}
2025-09-08 10:59:08 +08:00
}
2025-09-10 11:06:42 +08:00
if (mqtt_client)
{
2025-11-24 14:49:37 +08:00
mqtt_client->force_disconnect();
2025-09-10 11:06:42 +08:00
mqtt_client.reset();
}
2025-09-10 11:18:29 +08:00
2025-11-24 14:49:37 +08:00
// 重连间隔
2025-12-17 14:13:52 +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-11-24 14:49:37 +08:00
2025-09-08 15:45:22 +08:00
LOG_INFO("[MQTT] Client thread exiting.");
2025-09-08 10:59:08 +08:00
}