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"
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<MQTTClient> mqtt_client;
|
|
|
|
|
std::atomic<bool> mqtt_restart_required{false};
|
|
|
|
|
|
2025-09-08 15:45:22 +08:00
|
|
|
extern std::atomic<bool> g_running;
|
2025-09-08 10:59:08 +08:00
|
|
|
|
|
|
|
|
// MQTT 回调定义
|
|
|
|
|
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
|
|
|
|
|
|
|
|
const auto &topics = g_app_config.mqtt.topics;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void on_mqtt_disconnected()
|
|
|
|
|
{
|
2025-09-08 16:40:04 +08:00
|
|
|
LOG_WARN("[MQTT] Disconnected from broker: " + g_app_config.mqtt.server_ip);
|
2025-09-08 10:59:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void on_mqtt_message_received(const std::string &topic, const std::string &message)
|
|
|
|
|
{
|
|
|
|
|
LOG_INFO("[MQTT] Received message on topic [" + topic + "], len = " + std::to_string(message.size()));
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2025-09-09 09:57:24 +08:00
|
|
|
if (topic == g_app_config.mqtt.topics.video_down)
|
|
|
|
|
{
|
2025-09-09 10:25:16 +08:00
|
|
|
// 处理 video_down
|
2025-09-09 10:20:04 +08:00
|
|
|
auto j = nlohmann::json::parse(message);
|
|
|
|
|
|
2025-09-09 10:25:16 +08:00
|
|
|
if (!j.contains("data") || !j["data"].contains("status"))
|
2025-09-09 10:20:04 +08:00
|
|
|
{
|
2025-09-09 10:25:16 +08:00
|
|
|
LOG_WARN("[MQTT] video_down JSON missing data.status");
|
2025-09-09 10:20:04 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int status = j["data"]["status"].get<int>();
|
2025-09-09 13:58:13 +08:00
|
|
|
std::string seqNo = j["seqNo"].get<std::string>();
|
|
|
|
|
bool success = true; // 标记是否操作成功
|
2025-09-09 09:57:24 +08:00
|
|
|
|
2025-09-09 10:25:16 +08:00
|
|
|
if (status == 0)
|
2025-09-09 09:57:24 +08:00
|
|
|
{
|
|
|
|
|
// 启动推流:挂载本地配置中 enabled 的摄像头
|
|
|
|
|
for (const auto &cam : g_app_config.cameras)
|
|
|
|
|
{
|
|
|
|
|
if (!cam.enabled)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (!RTSPManager::is_streaming(cam.name))
|
|
|
|
|
{
|
|
|
|
|
RTSPManager::mount_camera(cam);
|
|
|
|
|
LOG_INFO("[MQTT] Started streaming: " + cam.name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-09 10:25:16 +08:00
|
|
|
else if (status == 1)
|
2025-09-09 09:57:24 +08:00
|
|
|
{
|
|
|
|
|
// 停止推流:卸载本地配置中 enabled 的摄像头
|
|
|
|
|
for (const auto &cam : g_app_config.cameras)
|
|
|
|
|
{
|
|
|
|
|
if (!cam.enabled)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (RTSPManager::is_streaming(cam.name))
|
|
|
|
|
{
|
|
|
|
|
RTSPManager::unmount_camera(cam);
|
|
|
|
|
LOG_INFO("[MQTT] Stopped streaming: " + cam.name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
LOG_WARN("[MQTT] video_down: unknown status value " + std::to_string(status));
|
2025-09-09 13:58:13 +08:00
|
|
|
success = false;
|
2025-09-09 09:57:24 +08:00
|
|
|
}
|
2025-09-09 13:58:13 +08:00
|
|
|
|
|
|
|
|
// 获取当前时间 yyyyMMddHHmmssSSS
|
|
|
|
|
auto now = std::chrono::system_clock::now();
|
|
|
|
|
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
|
|
|
|
|
std::time_t t = std::chrono::system_clock::to_time_t(now);
|
|
|
|
|
std::tm tm_now = *std::localtime(&t);
|
|
|
|
|
std::ostringstream time_ss;
|
|
|
|
|
time_ss << std::put_time(&tm_now, "%Y%m%d%H%M%S") << std::setw(3) << std::setfill('0') << millis.count();
|
|
|
|
|
|
|
|
|
|
nlohmann::json reply;
|
|
|
|
|
reply["time"] = time_ss.str();
|
|
|
|
|
reply["result"] = success ? 0 : 1;
|
|
|
|
|
reply["seqNo"] = seqNo;
|
|
|
|
|
|
|
|
|
|
// 发送应答
|
2025-09-09 14:00:50 +08:00
|
|
|
if (mqtt_client)
|
|
|
|
|
{
|
|
|
|
|
mqtt_client->publish(g_app_config.mqtt.topics.substream_down_ack, reply.dump());
|
|
|
|
|
LOG_INFO("[MQTT] Replied to video_down: " + reply.dump());
|
|
|
|
|
}
|
2025-09-09 09:57:24 +08:00
|
|
|
}
|
|
|
|
|
else if (topic == g_app_config.mqtt.topics.substream_down)
|
|
|
|
|
{
|
|
|
|
|
// 处理 substream_down
|
|
|
|
|
LOG_INFO("[MQTT] substream_down message received (not implemented yet).");
|
|
|
|
|
}
|
|
|
|
|
else if (topic == g_app_config.mqtt.topics.reset_down)
|
|
|
|
|
{
|
|
|
|
|
// 处理 reset_down
|
|
|
|
|
LOG_INFO("[MQTT] reset_down message received (not implemented yet).");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
LOG_WARN("[MQTT] Unknown topic: " + topic);
|
|
|
|
|
}
|
2025-09-08 10:59:08 +08:00
|
|
|
}
|
|
|
|
|
catch (const std::exception &e)
|
|
|
|
|
{
|
|
|
|
|
LOG_ERROR(std::string("[MQTT] Failed to process incoming JSON: ") + e.what());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void mqtt_client_thread_func()
|
|
|
|
|
{
|
2025-09-08 15:45:22 +08:00
|
|
|
const auto &cfg = g_app_config.mqtt;
|
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);
|
|
|
|
|
|
|
|
|
|
mqtt_client->connect();
|
|
|
|
|
|
2025-09-08 15:45:22 +08:00
|
|
|
while (!mqtt_restart_required && g_running)
|
2025-09-08 10:59:08 +08:00
|
|
|
{
|
2025-09-08 15:59:15 +08:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
2025-09-08 10:59:08 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-08 15:45:22 +08:00
|
|
|
// 需要重启或退出
|
|
|
|
|
mqtt_client->disconnect();
|
2025-09-08 10:59:08 +08:00
|
|
|
mqtt_client.reset();
|
|
|
|
|
mqtt_restart_required = false;
|
|
|
|
|
}
|
2025-09-08 15:45:22 +08:00
|
|
|
LOG_INFO("[MQTT] Client thread exiting.");
|
2025-09-08 10:59:08 +08:00
|
|
|
}
|