98 lines
3.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "ConfigManager.h"
#include "VrLog.h"
#include "VrFileUtils.h"
#include <QCoreApplication>
bool ConfigManager::Initialize(const std::string& configFilePath)
{
// TunnelChannel 应用不需要共享内存监控
// 直接加载配置文件
std::string actualConfigPath = configFilePath;
// 如果没有指定配置文件路径,使用默认路径
if (actualConfigPath.empty()) {
// 获取应用程序所在目录
QString appDir = QCoreApplication::applicationDirPath();
actualConfigPath = (appDir + "/config/config.xml").toStdString();
}
LOG_INFO("TunnelChannel ConfigManager: Loading config from %s\n", actualConfigPath.c_str());
// 检查配置文件是否存在
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)
{
// 使用 IVrConfig 接口加载配置
IVrConfig* pConfig = nullptr;
if (!IVrConfig::CreateInstance(&pConfig) || !pConfig) {
LOG_ERR("Failed to create IVrConfig instance\n");
return false;
}
// 加载配置到临时结果
ConfigResult tempResult;
int loadResult = pConfig->LoadConfig(filePath, tempResult);
if (loadResult != LOAD_CONFIG_SUCCESS) {
LOG_ERR("Failed to load config file: %s, error code: %d\n", filePath.c_str(), loadResult);
delete pConfig;
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();
delete pConfig;
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;
}