This commit is contained in:
cxh 2025-11-13 13:21:10 +08:00
parent 88ff6f8b16
commit b38fa77a83
2 changed files with 29 additions and 28 deletions

View File

@ -1,14 +1,16 @@
// app_config.hpp // app_config.hpp
#pragma once #pragma once
#include <string> #include <limits.h>
#include <map> #include <unistd.h>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <map>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <unistd.h>
#include <limits.h>
#include <stdexcept> #include <stdexcept>
#include <string>
#include "logger.hpp" #include "logger.hpp"
using json = nlohmann::json; using json = nlohmann::json;
@ -37,10 +39,10 @@ struct VehicleMQTTTopics
std::string heartbeat_up; std::string heartbeat_up;
std::string video_down; 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; heartbeat_up = "/kun/vehicle/video/status/" + vehId;
video_down = "/kun/vehicle/video/push/" + vehId; video_down = "/kun/vehicle/video/request/" + vehId;
} }
}; };
@ -68,7 +70,7 @@ struct AppConfig
std::vector<Camera> cameras; std::vector<Camera> cameras;
MQTTConfig mqtt; MQTTConfig mqtt;
static AppConfig load_from_file(const std::string &filepath) static AppConfig load_from_file(const std::string& filepath)
{ {
AppConfig cfg; AppConfig cfg;
@ -85,7 +87,7 @@ struct AppConfig
// 读取摄像头 // 读取摄像头
if (j.contains("cameras")) if (j.contains("cameras"))
{ {
for (auto &c : j["cameras"]) for (auto& c : j["cameras"])
{ {
Camera cam; Camera cam;
cam.device = c.value("device", ""); cam.device = c.value("device", "");
@ -96,9 +98,8 @@ struct AppConfig
cam.bitrate = c.value("bitrate", 2000000); cam.bitrate = c.value("bitrate", 2000000);
cam.enabled = c.value("enabled", false); cam.enabled = c.value("enabled", false);
cfg.cameras.push_back(cam); cfg.cameras.push_back(cam);
LOG_INFO("[Config] Loaded camera: " + cam.name + LOG_INFO("[Config] Loaded camera: " + cam.name + " (" + cam.device +
" (" + cam.device + "), enabled=" + std::to_string(cam.enabled) + "), enabled=" + std::to_string(cam.enabled) + ", bitrate=" + std::to_string(cam.bitrate));
", bitrate=" + std::to_string(cam.bitrate));
} }
} }
@ -109,7 +110,7 @@ struct AppConfig
throw std::runtime_error("Config file missing 'mqtt_server'"); 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.vehicle_id = std::to_string(m.value("veh_id", 0));
cfg.mqtt.server_ip = m.value("address", ""); cfg.mqtt.server_ip = m.value("address", "");
cfg.mqtt.server_port = m.value("port", 1883); 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.keep_alive = m.value("mqtt_heart_threshold", 2000);
cfg.mqtt.topics.fill_with_veh_id(cfg.mqtt.vehicle_id); cfg.mqtt.topics.fill_with_veh_id(cfg.mqtt.vehicle_id);
LOG_INFO("[Config] Loaded MQTT server: " + cfg.mqtt.server_ip + LOG_INFO("[Config] Loaded MQTT server: " + cfg.mqtt.server_ip + ":" + std::to_string(cfg.mqtt.server_port));
":" + std::to_string(cfg.mqtt.server_port));
LOG_INFO("[Config] MQTT client ID: " + cfg.mqtt.client_id); LOG_INFO("[Config] MQTT client ID: " + cfg.mqtt.client_id);
LOG_INFO("[Config] MQTT Credentials - username: " + cfg.mqtt.username + LOG_INFO("[Config] MQTT Credentials - username: " + cfg.mqtt.username + ", password: " + cfg.mqtt.password);
", password: " + cfg.mqtt.password); LOG_INFO("[Config] MQTT Topics: " + cfg.mqtt.topics.heartbeat_up + ", " + cfg.mqtt.topics.video_down);
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)); LOG_INFO("[Config] MQTT keepAlive: " + std::to_string(cfg.mqtt.keep_alive));
return cfg; return cfg;
@ -155,11 +153,10 @@ inline std::string get_executable_dir()
return full_path.substr(0, pos); 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(); std::string dir = get_executable_dir();
if (dir.back() != '/') if (dir.back() != '/') dir += '/';
dir += '/';
return dir + filename; return dir + filename;
} }

View File

@ -58,12 +58,16 @@ static void send_heartbeat()
// 发布心跳 // 发布心跳
mqtt_client->publish(g_app_config.mqtt.topics.heartbeat_up, hb.dump(), 0); 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) + // LOG_INFO("[MQTT] Sent video heartbeat (" + std::to_string(running_count) + "/" + std::to_string(total) +
" running): " + hb.dump()); // " running): " + hb.dump());
} }
// MQTT 回调 // 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); } static void on_mqtt_disconnected() { LOG_WARN("[MQTT] Disconnected from broker: " + g_app_config.mqtt.server_ip); }