fix: resolve SDK JSON file paths

This commit is contained in:
ob-yalian
2026-05-27 10:49:03 +08:00
parent 532438d5a7
commit d033ef5aac
+40 -9
View File
@@ -22,6 +22,7 @@
#include <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
#include <cmath> #include <cmath>
#include <cstdlib>
#include <unordered_map> #include <unordered_map>
#include <unordered_set> #include <unordered_set>
@@ -64,6 +65,20 @@ std::string getDepthFilterStatusName(const std::string &filter_name) {
return filter_name; return filter_name;
} }
std::filesystem::path resolveConfigJsonFilePath(const std::string &file_path) {
std::filesystem::path path(file_path);
if ((file_path == "~" || file_path.rfind("~/", 0) == 0) && std::getenv("HOME") != nullptr) {
path = std::filesystem::path(std::getenv("HOME"));
if (file_path.size() > 2) {
path /= file_path.substr(2);
}
}
if (path.is_relative()) {
path = std::filesystem::absolute(path);
}
return path.lexically_normal();
}
std::string getDepthFilterStatusParamName(const std::string &filter_name, std::string getDepthFilterStatusParamName(const std::string &filter_name,
const std::string &param_name) { const std::string &param_name) {
if (filter_name == "SpatialAdvancedFilter" && param_name == "disp_diff") { if (filter_name == "SpatialAdvancedFilter" && param_name == "disp_diff") {
@@ -1595,8 +1610,22 @@ bool OBCameraNode::exportConfigJsonToFile(const std::string &file_path, std::str
} }
try { try {
device_->exportSettingsAsPresetJsonFile(file_path.c_str()); const auto resolved_file_path = resolveConfigJsonFilePath(file_path);
message = "Exported config json file path: " + file_path; const auto parent_path = resolved_file_path.parent_path();
if (!parent_path.empty()) {
std::filesystem::create_directories(parent_path);
}
const auto resolved_file_path_str = resolved_file_path.string();
device_->exportSettingsAsPresetJsonFile(resolved_file_path_str.c_str());
if (!std::filesystem::exists(resolved_file_path)) {
message = "Failed to export config json file: file not found after export path=" +
resolved_file_path_str;
RCLCPP_ERROR_STREAM(logger_, message);
return false;
}
message = "Exported config json file path: " + resolved_file_path_str;
RCLCPP_INFO_STREAM(logger_, message); RCLCPP_INFO_STREAM(logger_, message);
return true; return true;
} catch (const ob::Error &e) { } catch (const ob::Error &e) {
@@ -1627,30 +1656,32 @@ void OBCameraNode::loadConfigJson() {
return; return;
} }
std::ifstream load_config_file(load_config_json_file_path_); const auto resolved_file_path = resolveConfigJsonFilePath(load_config_json_file_path_);
const auto resolved_file_path_str = resolved_file_path.string();
std::ifstream load_config_file(resolved_file_path);
if (!load_config_file.good()) { if (!load_config_file.good()) {
RCLCPP_WARN_STREAM(logger_, "Config JSON load skip file=" << load_config_json_file_path_ RCLCPP_WARN_STREAM(logger_, "Config JSON load skip file=" << resolved_file_path_str
<< " reason=file_not_found"); << " reason=file_not_found");
return; return;
} }
try { try {
device_->loadPresetFromJsonFile(load_config_json_file_path_.c_str()); device_->loadPresetFromJsonFile(resolved_file_path_str.c_str());
config_json_loaded_ = true; config_json_loaded_ = true;
RCLCPP_INFO_STREAM(logger_, "Config JSON loaded file=" << load_config_json_file_path_); RCLCPP_INFO_STREAM(logger_, "Config JSON loaded file=" << resolved_file_path_str);
} catch (const ob::Error &e) { } catch (const ob::Error &e) {
config_json_loaded_ = false; config_json_loaded_ = false;
RCLCPP_ERROR_STREAM(logger_, "Config JSON load failed file=" RCLCPP_ERROR_STREAM(logger_, "Config JSON load failed file="
<< load_config_json_file_path_ << " error=\"" << resolved_file_path_str << " error=\""
<< orbbec_camera::formatObErrorWithStatus(e) << "\""); << orbbec_camera::formatObErrorWithStatus(e) << "\"");
} catch (const std::exception &e) { } catch (const std::exception &e) {
config_json_loaded_ = false; config_json_loaded_ = false;
RCLCPP_ERROR_STREAM(logger_, "Config JSON load failed file=" << load_config_json_file_path_ RCLCPP_ERROR_STREAM(logger_, "Config JSON load failed file=" << resolved_file_path_str
<< " error=\"" << e.what() << " error=\"" << e.what()
<< "\""); << "\"");
} catch (...) { } catch (...) {
config_json_loaded_ = false; config_json_loaded_ = false;
RCLCPP_ERROR_STREAM(logger_, "Config JSON load failed file=" << load_config_json_file_path_); RCLCPP_ERROR_STREAM(logger_, "Config JSON load failed file=" << resolved_file_path_str);
} }
} }