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
#pragma once
#include <string>
#include <map>
#include <limits.h>
#include <unistd.h>
#include <fstream>
#include <iostream>
#include <map>
#include <nlohmann/json.hpp>
#include <unistd.h>
#include <limits.h>
#include <stdexcept>
#include <string>
#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<Camera> 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;
}

View File

@ -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); }