# 设备配置HTTP API使用示例 本文档展示如何通过HTTP API来设置和获取设备参数。 ## API端点 基础URL: `http://localhost:5002/api/config` ## 1. 获取所有设备配置 **GET** `/devices` ```bash curl -X GET http://localhost:5002/api/config/devices ``` **响应示例:** ```json { "success": true, "data": { "imu": { "device_type": "real", "port": "COM6", "baudrate": 9600, "timeout": 1.0, "calibration_samples": 100 }, "pressure": { "device_type": "real", "port": "COM5", "baudrate": 115200, "timeout": 1.0, "calibration_samples": 50 }, "camera": { "device_index": 1, "width": 1280, "height": 720, "fps": 30, "buffer_size": 1, "fourcc": "MJPG" }, "femtobolt": { "color_resolution": "1080P", "depth_mode": "NFOV_UNBINNED", "fps": 30, "depth_range_min": 1200, "depth_range_max": 1500, "synchronized_images_only": false } } } ``` ## 2. 获取单个设备配置 **GET** `/devices/{device_name}` 支持的设备名称: `imu`, `pressure`, `camera`, `femtobolt` ```bash curl -X GET http://localhost:5002/api/config/devices/imu ``` ## 3. 设置IMU配置 **POST** `/devices/imu` ```bash curl -X POST http://localhost:5002/api/config/devices/imu \ -H "Content-Type: application/json" \ -d '{ "device_type": "real", "port": "COM6", "baudrate": 9600 }' ``` **请求参数:** - `device_type`: 设备类型 ("real" 或 "mock") - `port`: 串口号 (如 "COM6") - `baudrate`: 波特率 (如 9600) **响应示例:** ```json { "success": true, "message": "IMU配置更新成功", "config": { "device_type": "real", "port": "COM6", "baudrate": 9600, "timeout": 1.0, "calibration_samples": 100 } } ``` ## 4. 设置压力板配置 **POST** `/devices/pressure` ```bash curl -X POST http://localhost:5002/api/config/devices/pressure \ -H "Content-Type: application/json" \ -d '{ "device_type": "real", "use_mock": false, "port": "COM5", "baudrate": 115200 }' ``` **请求参数:** - `device_type`: 设备类型 ("real" 或 "mock") - `use_mock`: 是否使用模拟数据 (true 或 false) - `port`: 串口号 (如 "COM5") - `baudrate`: 波特率 (如 115200) ## 5. 设置相机配置 **POST** `/devices/camera` ```bash curl -X POST http://localhost:5002/api/config/devices/camera \ -H "Content-Type: application/json" \ -d '{ "device_index": 1, "width": 1280, "height": 720, "fps": 30 }' ``` **请求参数:** - `device_index`: 相机设备索引 (如 1) - `width`: 图像宽度 (如 1280) - `height`: 图像高度 (如 720) - `fps`: 帧率 (如 30) ## 6. 设置FemtoBolt配置 **POST** `/devices/femtobolt` ```bash curl -X POST http://localhost:5002/api/config/devices/femtobolt \ -H "Content-Type: application/json" \ -d '{ "color_resolution": "1080P", "depth_mode": "NFOV_UNBINNED", "fps": 30, "depth_range_min": 1200, "depth_range_max": 1500 }' ``` **请求参数:** - `color_resolution`: 颜色分辨率 (如 "1080P") - `depth_mode`: 深度模式 (如 "NFOV_UNBINNED") - `fps`: 帧率 (如 30) - `depth_range_min`: 最小深度范围 (如 1200) - `depth_range_max`: 最大深度范围 (如 1500) ## 7. 重新加载配置 **POST** `/reload` ```bash curl -X POST http://localhost:5002/api/config/reload ``` ## 8. 验证配置 **GET** `/validate` ```bash curl -X GET http://localhost:5002/api/config/validate ``` **响应示例:** ```json { "success": true, "data": { "errors": [], "warnings": [], "valid": true } } ``` ## 错误处理 当请求失败时,API会返回错误信息: ```json { "success": false, "message": "错误描述" } ``` 常见的HTTP状态码: - `200`: 成功 - `400`: 请求参数错误 - `500`: 服务器内部错误 ## 启动配置API服务 ```bash # 进入设备目录 cd backend/devices # 运行配置API服务 python config_api.py ``` 服务将在 `http://localhost:5002` 启动。 ## Python客户端示例 ```python import requests import json # 设置IMU配置 def set_imu_config(): url = "http://localhost:5002/api/config/devices/imu" data = { "device_type": "real", "port": "COM6", "baudrate": 9600 } response = requests.post(url, json=data) result = response.json() if result['success']: print("IMU配置设置成功") print(json.dumps(result['config'], indent=2)) else: print(f"设置失败: {result['message']}") # 获取所有设备配置 def get_all_configs(): url = "http://localhost:5002/api/config/devices" response = requests.get(url) result = response.json() if result['success']: print("所有设备配置:") print(json.dumps(result['data'], indent=2)) else: print(f"获取失败: {result['message']}") if __name__ == "__main__": set_imu_config() get_all_configs() ```