From ac3a3f604d266027e844bcbd210010fa2f5e590a Mon Sep 17 00:00:00 2001 From: root Date: Fri, 15 Aug 2025 15:30:03 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=9C=BA=E5=99=A8=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/v1/endpoints/__init__.py | 5 +- docs/modify.md | 98 ++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 2 deletions(-) diff --git a/app/api/v1/endpoints/__init__.py b/app/api/v1/endpoints/__init__.py index b81aa0e..7c81118 100644 --- a/app/api/v1/endpoints/__init__.py +++ b/app/api/v1/endpoints/__init__.py @@ -1,12 +1,13 @@ # API endpoints package for TermControlAgent # 导入所有路由模块 -from . import devices, ssh, at, plnk, websocket +from . import devices, ssh, at, plnk, websocket, system __all__ = [ "devices", "ssh", "at", "plnk", - "websocket" + "websocket", + "system" ] \ No newline at end of file diff --git a/docs/modify.md b/docs/modify.md index de25696..3bbce1c 100644 --- a/docs/modify.md +++ b/docs/modify.md @@ -1,5 +1,103 @@ # 修改记录 +## 2025-08-15 实现系统接口获取机器码功能 + +**新增内容:** +实现系统接口获取机器码功能,支持Windows和Linux系统,提供统一的API接口。 + +**修改详情:** +1. **创建系统接口endpoint文件** (`app/api/v1/endpoints/system.py`): +```python +@router.get("/system/machine-code", summary="获取系统机器码", response_model=MachineCodeResponse) +async def get_machine_code(): + """获取当前系统的机器码(UUID) + + 支持Windows和Linux系统: + - Windows: 使用wmic csproduct get uuid命令 + - Linux: 尝试多种方法获取唯一标识符 + """ +``` + +2. **Windows系统机器码获取**: +```python +def get_windows_machine_code() -> str: + """获取Windows系统的机器码(UUID)""" + # 使用wmic命令获取UUID + result = subprocess.run( + ['wmic', 'csproduct', 'get', 'uuid'], + capture_output=True, + text=True, + check=True + ) + # 解析输出,提取UUID + output = result.stdout.strip() + lines = output.split('\n') + + # 查找UUID行(跳过标题行) + for line in lines: + line = line.strip() + if line and line.lower() != 'uuid': + # 提取UUID(移除可能的空格和特殊字符) + uuid_match = re.search(r'[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}', line) + if uuid_match: + return uuid_match.group(0) +``` + +3. **Linux系统机器码获取**: +```python +def get_linux_machine_code() -> str: + """获取Linux系统的机器码""" + # 方法1: 从/proc/cpuinfo获取CPU信息 + # 方法2: 从/sys/class/dmi/id/product_uuid获取 + # 方法3: 使用dmidecode命令 + # 方法4: 从/etc/machine-id获取 + # 方法5: 从/proc/sys/kernel/random/uuid获取随机UUID + # 如果所有方法都失败,返回系统信息组合 +``` + +4. **更新路由注册**: +- 在 `app/api/v1/endpoints/__init__.py` 中添加system模块导入 +- 在 `app/core/app/router.py` 中注册系统信息路由 + +5. **创建测试脚本** (`test_system_api.py`): +```python +def test_machine_code(): + """测试获取机器码功能""" + # 测试Windows和Linux系统的机器码获取功能 +``` + +**API接口说明:** +- **GET /api/v1/system/machine-code**: 获取系统机器码 +- **GET /api/v1/system/info**: 获取系统详细信息 + +**响应格式:** +```json +{ + "success": true, + "message": "成功获取windows系统机器码", + "data": { + "machine_code": "03000200-0400-0500-0006-000700080009", + "system": "windows", + "method": "wmic csproduct get uuid", + "platform_info": { + "node": "DESKTOP-T6EU05A", + "machine": "AMD64", + "processor": "AMD64 Family 23 Model 24 Stepping 1, AuthenticAMD", + "platform": "Windows-10-10.0.17763-SP0" + } + } +} +``` + +**优化效果:** +- ✅ 支持Windows系统使用wmic命令获取UUID +- ✅ 支持Linux系统多种方法获取机器码 +- ✅ 提供统一的API接口,自动识别操作系统 +- ✅ 包含详细的系统信息和获取方法说明 +- ✅ 完善的错误处理和日志记录 +- ✅ 创建测试脚本验证功能正确性 +- ✅ 符合项目代码规范和架构设计 + ## 2025-08-15 规范化设备事件数据格式并修复EventType使用 **优化内容:**