diff --git a/backend/database.py b/backend/database.py index cd9d20c1..6a9b5ddb 100644 --- a/backend/database.py +++ b/backend/database.py @@ -1090,6 +1090,45 @@ class DatabaseManager: logger.error(f'获取最新检测数据失败: {e}') return [] + def get_detection_data_by_id(self, data_id: str) -> Optional[Dict]: + """根据主键ID查询检测数据详情""" + conn = self.get_connection() + cursor = conn.cursor() + + try: + cursor.execute(''' + SELECT * FROM detection_data + WHERE id = ? + ''', (data_id,)) + + row = cursor.fetchone() + if not row: + return None + + data_point = dict(row) + # 解析JSON字段 + try: + if data_point.get('head_pose'): + data_point['head_pose'] = json.loads(data_point['head_pose']) + except: + pass + try: + if data_point.get('body_pose'): + data_point['body_pose'] = json.loads(data_point['body_pose']) + except: + pass + try: + if data_point.get('foot_data'): + data_point['foot_data'] = json.loads(data_point['foot_data']) + except: + pass + + return data_point + + except Exception as e: + logger.error(f'根据ID获取检测数据失败: {e}') + return None + def delete_detection_data(self, data_id: str): """按主键删除检测数据记录""" conn = self.get_connection() diff --git a/backend/devices/config_api_examples.md b/backend/devices/config_api_examples.md deleted file mode 100644 index 2c57ec12..00000000 --- a/backend/devices/config_api_examples.md +++ /dev/null @@ -1,260 +0,0 @@ -# 设备配置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() -``` \ No newline at end of file diff --git a/backend/main.py b/backend/main.py index 1aca4097..7d01d907 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1138,6 +1138,75 @@ class AppServer: self.logger.error(f'获取检测历史失败: {e}') return jsonify({'success': False, 'error': str(e)}), 500 + @self.app.route('/api/history/sessions/', methods=['GET']) + def get_session_data(session_id): + """获取会话详细数据""" + try: + session_data = self.db_manager.get_session_data(session_id) + if session_data is None: + return jsonify({'success': False, 'error': '会话不存在'}), 404 + + return jsonify({ + 'success': True, + 'data': session_data + }) + + except Exception as e: + self.logger.error(f'获取会话数据失败: {e}') + return jsonify({'success': False, 'error': str(e)}), 500 + + + + @self.app.route('/api/detection/data//latest', methods=['GET']) + def get_latest_detection_data(session_id): + """获取最新的检测数据""" + try: + limit = int(flask_request.args.get('limit', 5)) + data = self.db_manager.get_latest_detection_data(session_id, limit) + + return jsonify({ + 'success': True, + 'data': data + }) + + except Exception as e: + self.logger.error(f'获取最新检测数据失败: {e}') + return jsonify({'success': False, 'error': str(e)}), 500 + + @self.app.route('/api/detection/data/detail/', methods=['GET']) + def get_detection_data_by_id(data_id): + """根据主键ID查询检测数据详情""" + try: + data = self.db_manager.get_detection_data_by_id(data_id) + if data is None: + return jsonify({'success': False, 'error': '检测数据不存在'}), 404 + + return jsonify({ + 'success': True, + 'data': data + }) + + except Exception as e: + self.logger.error(f'获取检测数据详情失败: {e}') + return jsonify({'success': False, 'error': str(e)}), 500 + + @self.app.route('/api/detection/data/', methods=['DELETE']) + def delete_detection_data(data_id): + """删除检测数据记录""" + try: + self.db_manager.delete_detection_data(data_id) + + return jsonify({ + 'success': True, + 'message': '检测数据删除成功' + }) + + except ValueError as e: + return jsonify({'success': False, 'error': str(e)}), 404 + except Exception as e: + self.logger.error(f'删除检测数据失败: {e}') + return jsonify({'success': False, 'error': str(e)}), 500 + # ==================== 错误处理 ==================== @self.app.errorhandler(404)