测试版本

This commit is contained in:
flower
2026-02-02 18:58:23 +08:00
parent d2b7b833b6
commit 80873ce026
14 changed files with 270 additions and 249 deletions

View File

@@ -1,72 +0,0 @@
--- Session Started: 2026-02-02 15:40:47 ---
--- Session Started: 2026-02-02 16:12:35 ---
--- Session Started: 2026-02-02 16:14:58 ---
--- Session Started: 2026-02-02 16:16:59 ---
[2026-02-02 16:17:00] Sent: photo_20260202_161700_000.jpg Size: 551838 Total Sent: 1
[2026-02-02 16:17:00] Sent: photo_20260202_161700_001.jpg Size: 411948 Total Sent: 2
[2026-02-02 16:17:01] Sent: photo_20260202_161701_002.jpg Size: 376731 Total Sent: 3
[2026-02-02 16:17:01] Sent: photo_20260202_161701_003.jpg Size: 377754 Total Sent: 4
[2026-02-02 16:17:02] Sent: photo_20260202_161702_004.jpg Size: 371593 Total Sent: 5
[2026-02-02 16:17:02] Sent: photo_20260202_161702_005.jpg Size: 385420 Total Sent: 6
--- Session Started: 2026-02-02 16:23:14 ---
[2026-02-02 16:23:14] Sent: photo_20260202_161700_000.jpg Size: 551838 Total Sent: 1
[2026-02-02 16:23:14] Sent: photo_20260202_161700_001.jpg Size: 411948 Total Sent: 2
[2026-02-02 16:23:14] Sent: photo_20260202_161701_002.jpg Size: 376731 Total Sent: 3
[2026-02-02 16:23:14] Sent: photo_20260202_161701_003.jpg Size: 377754 Total Sent: 4
[2026-02-02 16:23:14] Sent: photo_20260202_161702_004.jpg Size: 371593 Total Sent: 5
[2026-02-02 16:23:14] Sent: photo_20260202_161702_005.jpg Size: 385420 Total Sent: 6
[2026-02-02 16:23:14] Sent: photo_20260202_162314_000.jpg Size: 594448 Total Sent: 7
[2026-02-02 16:23:15] Sent: photo_20260202_162315_001.jpg Size: 416092 Total Sent: 8
[2026-02-02 16:23:15] Sent: photo_20260202_162315_002.jpg Size: 448451 Total Sent: 9
[2026-02-02 16:23:16] Sent: photo_20260202_162316_003.jpg Size: 447612 Total Sent: 10
[2026-02-02 16:23:16] Sent: photo_20260202_162316_004.jpg Size: 441193 Total Sent: 11
[2026-02-02 16:23:17] Sent: photo_20260202_162317_005.jpg Size: 465881 Total Sent: 12
[2026-02-02 16:23:17] Sent: photo_20260202_162317_006.jpg Size: 446361 Total Sent: 13
[2026-02-02 16:23:18] Sent: photo_20260202_162318_007.jpg Size: 415256 Total Sent: 14
[2026-02-02 16:23:18] Sent: photo_20260202_162318_008.jpg Size: 415862 Total Sent: 15
[2026-02-02 16:23:19] Sent: photo_20260202_162319_009.jpg Size: 415374 Total Sent: 16
--- Session Started: 2026-02-02 16:28:39 ---
[2026-02-02 16:28:39] Sent: photo_000_20260202_162839_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 584359 Total Sent: 1
[2026-02-02 16:28:40] Sent: photo_001_20260202_162840_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 583744 Total Sent: 2
[2026-02-02 16:28:40] Sent: photo_002_20260202_162840_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 583338 Total Sent: 3
[2026-02-02 16:28:41] Sent: photo_003_20260202_162841_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 571135 Total Sent: 4
[2026-02-02 16:28:41] Sent: photo_004_20260202_162841_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 570012 Total Sent: 5
[2026-02-02 16:28:42] Sent: photo_005_20260202_162842_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 570419 Total Sent: 6
[2026-02-02 16:28:42] Sent: photo_006_20260202_162842_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 582702 Total Sent: 7
[2026-02-02 16:28:43] Sent: photo_007_20260202_162843_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 582108 Total Sent: 8
--- Session Started: 2026-02-02 16:32:10 ---
[2026-02-02 16:32:13] Sent: photo_000_20260202_163213_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 559873 Total Sent: 1
[2026-02-02 16:32:15] Sent: photo_001_20260202_163215_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 559766 Total Sent: 2
--- Session Started: 2026-02-02 16:32:54 ---
[2026-02-02 16:32:55] Sent: photo_000_20260202_163213_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 559873 Total Sent: 1
[2026-02-02 16:32:55] Sent: photo_001_20260202_163215_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 559766 Total Sent: 2
[2026-02-02 16:32:56] Sent: photo_000_20260202_163256_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 558412 Total Sent: 3
[2026-02-02 16:32:57] Sent: photo_001_20260202_163257_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 601159 Total Sent: 4
[2026-02-02 16:32:58] Sent: photo_002_20260202_163258_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 558166 Total Sent: 5
[2026-02-02 16:32:59] Sent: photo_003_20260202_163259_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 600601 Total Sent: 6
[2026-02-02 16:33:00] Sent: photo_004_20260202_163300_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 600806 Total Sent: 7
--- Session Started: 2026-02-02 16:34:57 ---
--- Session Started: 2026-02-02 16:36:29 ---
[2026-02-02 16:39:13] Sent: photo_000_20260202_163913_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 500250 Total Sent: 1
[2026-02-02 16:39:14] Sent: photo_001_20260202_163914_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 499410 Total Sent: 2
[2026-02-02 16:39:15] Sent: photo_002_20260202_163915_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 499874 Total Sent: 3
[2026-02-02 16:39:16] Sent: photo_003_20260202_163916_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 499487 Total Sent: 4
[2026-02-02 16:39:17] Sent: photo_004_20260202_163917_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 498681 Total Sent: 5
[2026-02-02 16:39:52] Sent: photo_005_20260202_163952_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 603995 Total Sent: 6
[2026-02-02 16:39:54] Sent: photo_006_20260202_163954_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 595479 Total Sent: 7
[2026-02-02 16:39:56] Sent: photo_007_20260202_163956_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 643671 Total Sent: 8
[2026-02-02 16:39:58] Sent: photo_008_20260202_163958_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 623669 Total Sent: 9
[2026-02-02 16:40:00] Sent: photo_009_20260202_164000_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 601373 Total Sent: 10
[2026-02-02 16:40:17] Sent: photo_010_20260202_164017_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 630691 Total Sent: 11
[2026-02-02 16:40:19] Sent: photo_011_20260202_164019_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 687730 Total Sent: 12
[2026-02-02 16:40:21] Sent: photo_012_20260202_164021_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 653137 Total Sent: 13
[2026-02-02 16:40:23] Sent: photo_013_20260202_164023_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 628483 Total Sent: 14
--- Session Started: 2026-02-02 16:53:03 ---
[2026-02-02 16:53:30] Sent: photo_000_20260202_165330_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 473529 Total Sent: 1
[2026-02-02 16:53:32] Sent: photo_001_20260202_165332_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 473852 Total Sent: 2
[2026-02-02 16:53:34] Sent: photo_002_20260202_165334_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 473945 Total Sent: 3
[2026-02-02 16:53:36] Sent: photo_003_20260202_165336_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 606768 Total Sent: 4
[2026-02-02 16:54:24] Sent: photo_004_20260202_165424_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 583563 Total Sent: 5
[2026-02-02 16:54:26] Sent: photo_005_20260202_165426_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 602259 Total Sent: 6
[2026-02-02 16:54:28] Sent: photo_006_20260202_165428_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 597611 Total Sent: 7
[2026-02-02 16:54:30] Sent: photo_007_20260202_165430_GPS_lat0.000000_lon0.000000_alt0.0.jpg Size: 583412 Total Sent: 8

View File

@@ -246,15 +246,12 @@ private:
void handle_status_api(const httplib::Request& req, httplib::Response& res);
/**
* @brief 简单的 JSON 解析辅助函数
* @param json_str JSON 字符串
* @param key 键名
* @param default_value 默认值
* @return 解析的值或默认值
* @brief 简单的 JSON 解析辅助函数 - double 特化
*/
template<typename T>
T parse_json_value(const std::string& json_str, const std::string& key, T default_value) {
// 默认实现返回默认值特化版本在cpp中实现
return default_value;
}
double parse_json_value_double(const std::string& json_str, const std::string& key, double default_value);
/**
* @brief 简单的 JSON 解析辅助函数 - int 特化
*/
int parse_json_value_int(const std::string& json_str, const std::string& key, int default_value);
};

View File

@@ -519,14 +519,12 @@ void CameraDriverNode::capture_photo(TransmissionMode mode) {
// 获取 GPS 信息
double lat = 0.0, lon = 0.0, alt = 0.0;
bool has_gps = false;
{
std::lock_guard<std::mutex> gps_lock(gps_mutex_);
if (has_gps_fix_) {
lat = latest_gps_fix_.latitude;
lon = latest_gps_fix_.longitude;
alt = latest_gps_fix_.altitude;
has_gps = true;
}
}
@@ -751,6 +749,103 @@ void CameraDriverNode::http_server_worker() {
}
}
/**
* @brief 处理状态查询 API 请求
*/
void CameraDriverNode::handle_status_api(const httplib::Request& /* req */, httplib::Response& res) {
try {
// 构建 JSON 响应
std::ostringstream json;
json << "{"
<< "\"device_name\":\"" << device_name_ << "\","
<< "\"image_width\":" << image_width_ << ","
<< "\"image_height\":" << image_height_ << ","
<< "\"current_fps\":" << std::fixed << std::setprecision(1) << current_fps_ << ","
<< "\"total_frames\":" << frame_counter_ << ","
<< "\"total_photos\":" << total_photos_saved_.load() << ","
<< "\"is_capturing\":" << (is_capturing_ ? "true" : "false")
<< "}";
res.status = 200;
res.set_content(json.str(), "application/json");
} catch (const std::exception& e) {
std::string error_json = R"({"error":": )" + std::string(e.what()) + "\"}";
res.status = 500;
res.set_content(error_json, "application/json");
RCLCPP_ERROR(this->get_logger(), "HTTP API 状态查询错误: %s", e.what());
}
}
/**
* @brief JSON 解析辅助函数 - double 特化
*/
double CameraDriverNode::parse_json_value_double(const std::string& json_str, const std::string& key, double default_value) {
std::string search_key = "\"" + key + "\":";
size_t pos = json_str.find(search_key);
if (pos == std::string::npos) {
return default_value;
}
pos += search_key.length();
// 跳过空格
while (pos < json_str.length() && (json_str[pos] == ' ' || json_str[pos] == '\t')) {
pos++;
}
// 提取数字
size_t end_pos = pos;
while (end_pos < json_str.length() &&
(std::isdigit(json_str[end_pos]) || json_str[end_pos] == '.' ||
json_str[end_pos] == '-' || json_str[end_pos] == '+' || json_str[end_pos] == 'e' || json_str[end_pos] == 'E')) {
end_pos++;
}
if (end_pos > pos) {
try {
return std::stod(json_str.substr(pos, end_pos - pos));
} catch (...) {
return default_value;
}
}
return default_value;
}
/**
* @brief JSON 解析辅助函数 - int 特化
*/
int CameraDriverNode::parse_json_value_int(const std::string& json_str, const std::string& key, int default_value) {
std::string search_key = "\"" + key + "\":";
size_t pos = json_str.find(search_key);
if (pos == std::string::npos) {
return default_value;
}
pos += search_key.length();
// 跳过空格
while (pos < json_str.length() && (json_str[pos] == ' ' || json_str[pos] == '\t')) {
pos++;
}
// 提取整数
size_t end_pos = pos;
while (end_pos < json_str.length() &&
(std::isdigit(json_str[end_pos]) || json_str[end_pos] == '-' || json_str[end_pos] == '+')) {
end_pos++;
}
if (end_pos > pos) {
try {
return std::stoi(json_str.substr(pos, end_pos - pos));
} catch (...) {
return default_value;
}
}
return default_value;
}
/**
* @brief 处理触发拍照 API 请求
*/
@@ -760,10 +855,10 @@ void CameraDriverNode::handle_trigger_capture_api(const httplib::Request& req, h
std::string json_body = req.body;
// 提取参数(简单的 JSON 解析)
double duration = parse_json_value<double>(json_body, "duration", 0.0);
int count = parse_json_value<int>(json_body, "count", 0);
double interval = parse_json_value<double>(json_body, "interval", 0.0);
int transmission_mode = parse_json_value<int>(json_body, "transmission_mode", 0);
double duration = parse_json_value_double(json_body, "duration", 0.0);
int count = parse_json_value_int(json_body, "count", 0);
double interval = parse_json_value_double(json_body, "interval", 0.0);
int transmission_mode = parse_json_value_int(json_body, "transmission_mode", 0);
// 参数验证
if (duration < 0 || count < 0 || interval < 0) {
@@ -809,7 +904,7 @@ void CameraDriverNode::handle_trigger_capture_api(const httplib::Request& req, h
// 构建 JSON 响应
std::ostringstream json_stream;
json_stream << R"({"success":true,"message":" ()" << mode_str << R"()","total_photos":)"
json_stream << "{\"success\":true,\"message\":\"拍照任务已启动 (" << mode_str << ")\",\"total_photos\":"
<< total_photos_saved_.load() << "}";
std::string response_json = json_stream.str();
@@ -828,102 +923,6 @@ void CameraDriverNode::handle_trigger_capture_api(const httplib::Request& req, h
}
}
/**
* @brief 处理状态查询 API 请求
*/
void CameraDriverNode::handle_status_api(const httplib::Request& req, httplib::Response& res) {
try {
// 构建 JSON 响应
std::ostringstream json;
json << "{"
<< "\"device_name\":\"" << device_name_ << "\","
<< "\"image_width\":" << image_width_ << ","
<< "\"image_height\":" << image_height_ << ","
<< "\"current_fps\":" << std::fixed << std::setprecision(1) << current_fps_ << ","
<< "\"total_frames\":" << frame_counter_ << ","
<< "\"total_photos\":" << total_photos_saved_.load() << ","
<< "\"is_capturing\":" << (is_capturing_ ? "true" : "false")
<< "}";
res.status = 200;
res.set_content(json.str(), "application/json");
} catch (const std::exception& e) {
std::string error_json = R"({"error":": )" + std::string(e.what()) + "\"}";
res.status = 500;
res.set_content(error_json, "application/json");
RCLCPP_ERROR(this->get_logger(), "HTTP API 状态查询错误: %s", e.what());
}
}
/**
* @brief JSON 解析辅助函数(简单实现)
*/
template<>
double CameraDriverNode::parse_json_value<double>(const std::string& json_str, const std::string& key, double default_value) {
std::string search_key = "\"" + key + "\":";
size_t pos = json_str.find(search_key);
if (pos == std::string::npos) {
return default_value;
}
pos += search_key.length();
// 跳过空格
while (pos < json_str.length() && (json_str[pos] == ' ' || json_str[pos] == '\t')) {
pos++;
}
// 提取数字
size_t end_pos = pos;
while (end_pos < json_str.length() &&
(std::isdigit(json_str[end_pos]) || json_str[end_pos] == '.' ||
json_str[end_pos] == '-' || json_str[end_pos] == '+' || json_str[end_pos] == 'e' || json_str[end_pos] == 'E')) {
end_pos++;
}
if (end_pos > pos) {
try {
return std::stod(json_str.substr(pos, end_pos - pos));
} catch (...) {
return default_value;
}
}
return default_value;
}
template<>
int CameraDriverNode::parse_json_value<int>(const std::string& json_str, const std::string& key, int default_value) {
std::string search_key = "\"" + key + "\":";
size_t pos = json_str.find(search_key);
if (pos == std::string::npos) {
return default_value;
}
pos += search_key.length();
// 跳过空格
while (pos < json_str.length() && (json_str[pos] == ' ' || json_str[pos] == '\t')) {
pos++;
}
// 提取整数
size_t end_pos = pos;
while (end_pos < json_str.length() &&
(std::isdigit(json_str[end_pos]) || json_str[end_pos] == '-' || json_str[end_pos] == '+')) {
end_pos++;
}
if (end_pos > pos) {
try {
return std::stoi(json_str.substr(pos, end_pos - pos));
} catch (...) {
return default_value;
}
}
return default_value;
}
/**
* @brief 主函数
*/