From b38fa77a8355a7bc1a287dc4d8f63648f829c40a Mon Sep 17 00:00:00 2001 From: cxh Date: Thu, 13 Nov 2025 13:21:10 +0800 Subject: [PATCH] mqtt --- include/app_config.hpp | 47 +++++++++++++++++-------------------- src/mqtt_client_wrapper.cpp | 10 +++++--- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/include/app_config.hpp b/include/app_config.hpp index f21570b..2acfcdd 100644 --- a/include/app_config.hpp +++ b/include/app_config.hpp @@ -1,14 +1,16 @@ // app_config.hpp #pragma once -#include -#include +#include +#include + #include #include +#include #include -#include -#include #include +#include + #include "logger.hpp" using json = nlohmann::json; @@ -28,7 +30,7 @@ struct Camera int width, height, fps; int bitrate; bool enabled; - StreamType stream_type; // 新增字段 + StreamType stream_type; // 新增字段 }; // ------------------- MQTT Topic ------------------- @@ -37,10 +39,10 @@ struct VehicleMQTTTopics std::string heartbeat_up; std::string video_down; - void fill_with_veh_id(const std::string &vehId) + void fill_with_veh_id(const std::string& vehId) { - heartbeat_up = "/kun/vehicle/video/heartbeat/" + vehId; - video_down = "/kun/vehicle/video/push/" + vehId; + heartbeat_up = "/kun/vehicle/video/status/" + vehId; + video_down = "/kun/vehicle/video/request/" + vehId; } }; @@ -56,8 +58,8 @@ struct MQTTConfig std::string password; int keep_alive; - int qos = 1; // 默认 QoS 级别 - bool clean_session = true; // 默认 clean session + int qos = 1; // 默认 QoS 级别 + bool clean_session = true; // 默认 clean session VehicleMQTTTopics topics; }; @@ -68,7 +70,7 @@ struct AppConfig std::vector cameras; MQTTConfig mqtt; - static AppConfig load_from_file(const std::string &filepath) + static AppConfig load_from_file(const std::string& filepath) { AppConfig cfg; @@ -85,7 +87,7 @@ struct AppConfig // 读取摄像头 if (j.contains("cameras")) { - for (auto &c : j["cameras"]) + for (auto& c : j["cameras"]) { Camera cam; cam.device = c.value("device", ""); @@ -96,9 +98,8 @@ struct AppConfig cam.bitrate = c.value("bitrate", 2000000); cam.enabled = c.value("enabled", false); cfg.cameras.push_back(cam); - LOG_INFO("[Config] Loaded camera: " + cam.name + - " (" + cam.device + "), enabled=" + std::to_string(cam.enabled) + - ", bitrate=" + std::to_string(cam.bitrate)); + LOG_INFO("[Config] Loaded camera: " + cam.name + " (" + cam.device + + "), enabled=" + std::to_string(cam.enabled) + ", bitrate=" + std::to_string(cam.bitrate)); } } @@ -109,7 +110,7 @@ struct AppConfig throw std::runtime_error("Config file missing 'mqtt_server'"); } - auto &m = j["mqtt_server"]; + auto& m = j["mqtt_server"]; cfg.mqtt.vehicle_id = std::to_string(m.value("veh_id", 0)); cfg.mqtt.server_ip = m.value("address", ""); cfg.mqtt.server_port = m.value("port", 1883); @@ -120,13 +121,10 @@ struct AppConfig cfg.mqtt.keep_alive = m.value("mqtt_heart_threshold", 2000); cfg.mqtt.topics.fill_with_veh_id(cfg.mqtt.vehicle_id); - LOG_INFO("[Config] Loaded MQTT server: " + cfg.mqtt.server_ip + - ":" + std::to_string(cfg.mqtt.server_port)); + LOG_INFO("[Config] Loaded MQTT server: " + cfg.mqtt.server_ip + ":" + std::to_string(cfg.mqtt.server_port)); LOG_INFO("[Config] MQTT client ID: " + cfg.mqtt.client_id); - LOG_INFO("[Config] MQTT Credentials - username: " + cfg.mqtt.username + - ", password: " + cfg.mqtt.password); - LOG_INFO("[Config] MQTT Topics: " + cfg.mqtt.topics.heartbeat_up + ", " + - cfg.mqtt.topics.video_down); + LOG_INFO("[Config] MQTT Credentials - username: " + cfg.mqtt.username + ", password: " + cfg.mqtt.password); + LOG_INFO("[Config] MQTT Topics: " + cfg.mqtt.topics.heartbeat_up + ", " + cfg.mqtt.topics.video_down); LOG_INFO("[Config] MQTT keepAlive: " + std::to_string(cfg.mqtt.keep_alive)); return cfg; @@ -155,11 +153,10 @@ inline std::string get_executable_dir() return full_path.substr(0, pos); } -inline std::string get_executable_dir_file_path(const std::string &filename) +inline std::string get_executable_dir_file_path(const std::string& filename) { std::string dir = get_executable_dir(); - if (dir.back() != '/') - dir += '/'; + if (dir.back() != '/') dir += '/'; return dir + filename; } diff --git a/src/mqtt_client_wrapper.cpp b/src/mqtt_client_wrapper.cpp index ebd7264..480baf0 100644 --- a/src/mqtt_client_wrapper.cpp +++ b/src/mqtt_client_wrapper.cpp @@ -58,12 +58,16 @@ static void send_heartbeat() // 发布心跳 mqtt_client->publish(g_app_config.mqtt.topics.heartbeat_up, hb.dump(), 0); - LOG_INFO("[MQTT] Sent video heartbeat (" + std::to_string(running_count) + "/" + std::to_string(total) + - " running): " + hb.dump()); + // LOG_INFO("[MQTT] Sent video heartbeat (" + std::to_string(running_count) + "/" + std::to_string(total) + + // " running): " + hb.dump()); } // MQTT 回调 -static void on_mqtt_connected() { LOG_INFO("[MQTT] Connected to broker: " + g_app_config.mqtt.server_ip); } +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) +} static void on_mqtt_disconnected() { LOG_WARN("[MQTT] Disconnected from broker: " + g_app_config.mqtt.server_ip); }