104 lines
3.3 KiB
C++
104 lines
3.3 KiB
C++
#include "ConfigManager.h"
|
||
#include "VrLog.h"
|
||
#include "VrFileUtils.h"
|
||
#include "PathManager.h"
|
||
#include <QCoreApplication>
|
||
|
||
bool ConfigManager::Initialize(const std::string& configFilePath)
|
||
{
|
||
// TunnelChannel 应用不需要共享内存监控
|
||
// 直接加载配置文件
|
||
|
||
std::string actualConfigPath = configFilePath;
|
||
|
||
// 如果没有指定配置文件路径,使用PathManager获取路径
|
||
if (actualConfigPath.empty()) {
|
||
QString configPath = PathManager::GetInstance().GetConfigFilePath();
|
||
actualConfigPath = configPath.toStdString();
|
||
}
|
||
|
||
// 保存配置文件路径到成员变量(用于后续保存)
|
||
m_configFilePath = actualConfigPath;
|
||
|
||
LOG_INFO("TunnelChannel ConfigManager: Loading config from %s\n", actualConfigPath.c_str());
|
||
|
||
// 创建VrConfig实例(用于后续保存配置)
|
||
if (!IVrConfig::CreateInstance(&m_pVrConfig) || !m_pVrConfig) {
|
||
LOG_ERR("Failed to create VrConfig instance\n");
|
||
return false;
|
||
}
|
||
|
||
// 检查配置文件是否存在
|
||
if (!CVrFileUtils::IsFileExist(actualConfigPath.c_str())) {
|
||
LOG_WARN("Config file not found: %s, using default config\n", actualConfigPath.c_str());
|
||
return _InitializeDefaultConfig();
|
||
}
|
||
|
||
// 加载配置文件
|
||
if (!LoadConfigFromFile(actualConfigPath)) {
|
||
LOG_WARN("Failed to load config file, using default config\n");
|
||
return _InitializeDefaultConfig();
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
bool ConfigManager::LoadConfigFromFile(const std::string& filePath)
|
||
{
|
||
// 使用已创建的 m_pVrConfig 实例加载配置
|
||
if (!m_pVrConfig) {
|
||
LOG_ERR("VrConfig instance not available\n");
|
||
return false;
|
||
}
|
||
|
||
// 加载配置到临时结果
|
||
ConfigResult tempResult;
|
||
int loadResult = m_pVrConfig->LoadConfig(filePath, tempResult);
|
||
|
||
if (loadResult != LOAD_CONFIG_SUCCESS) {
|
||
LOG_ERR("Failed to load config file: %s, error code: %d\n", filePath.c_str(), loadResult);
|
||
return false;
|
||
}
|
||
|
||
// 将加载的配置存储到成员变量
|
||
{
|
||
std::lock_guard<std::mutex> lock(m_configMutex);
|
||
m_systemConfig.configResult = tempResult;
|
||
m_configFilePath = filePath;
|
||
}
|
||
|
||
LOG_INFO("Config loaded successfully from: %s\n", filePath.c_str());
|
||
LOG_INFO(" - 3D Cameras: %zu\n", tempResult.cameraList.size());
|
||
LOG_INFO(" - Hik Cameras: %zu\n", tempResult.hikCameraList.size());
|
||
|
||
// 通知配置变化
|
||
_NotifyConfigChanged();
|
||
|
||
return true;
|
||
}
|
||
|
||
std::vector<HikCameraConfig> ConfigManager::GetHikCameraList() const
|
||
{
|
||
std::lock_guard<std::mutex> lock(m_configMutex);
|
||
return m_systemConfig.configResult.hikCameraList;
|
||
}
|
||
|
||
bool ConfigManager::_InitializeDefaultConfig()
|
||
{
|
||
LOG_INFO("TunnelChannel ConfigManager: Initializing default config\n");
|
||
|
||
std::lock_guard<std::mutex> lock(m_configMutex);
|
||
|
||
// 初始化默认 ConfigResult
|
||
m_systemConfig.configResult = ConfigResult();
|
||
|
||
// 3D相机不设置默认配置,由 Presenter 自动搜索打开第一个相机
|
||
// 海康相机不设置默认配置,需要在配置文件中指定
|
||
|
||
LOG_INFO("TunnelChannel default configuration initialized\n");
|
||
LOG_INFO(" - 3D Cameras: auto search\n");
|
||
LOG_INFO(" - Hik Cameras: not configured\n");
|
||
|
||
return true;
|
||
}
|