# E:\AI_System\environment\hardware_manager.py
import platform
import psutil
import json
import time
import subprocess
import logging
from pathlib import Path
from datetime import datetime
from .db_manager import get_environment_db
logger = logging.getLogger('HardwareManager')
class HardwareManager:
"""
硬件环境管理器 - 代表AI的家具和环境、集成数据库支持
负责管理硬件配置、请求新硬件、监控硬件状态
"""
def __init__(self, config_path: Path = None):
"""
初始化硬件管理器
:param config_path: 硬件配置文件的路径
"""
# 获取数据库实例
self.db = get_environment_db()
# 加载硬件目录和当前配置
self.available_hardware = self.load_hardware_catalog()
self.current_setup = self.load_current_config(config_path)
# 记录硬件启动事件
self.db.log_event(
"system_start",
f"硬件管理器启动: {platform.node()}",
severity=1
)
# 保存当前配置到数据库
self.save_current_setup_to_db()
# 设置最后更新时间
self.last_update = time.time()
logger.info("✅ 硬件管理器初始化完成")
def load_hardware_catalog(self) -> dict:
"""加载可用硬件目录(家具清单)"""
return {
"cpu": ["Intel i9-13900K", "AMD Ryzen 9 7950X", "Apple M2 Max", "Qualcomm Snapdragon X Elite"],
"gpu": ["NVIDIA RTX 4090", "AMD Radeon RX 7900 XTX", "Apple M2 GPU", "NVIDIA RTX 6000 Ada"],
"memory": [16, 32, 64, 128, 256], # GB
"storage": ["1TB SSD", "2TB SSD", "4TB SSD", "8TB SSD", "16TB HDD", "32TB NAS"],
"peripherals": [
"4K Camera", "3D Scanner", "High-Fidelity Microphone",
"VR Headset", "Haptic Gloves", "Eye Tracking Device"
],
"network": ["10Gb Ethernet", "WiFi 6E", "5G Modem", "Satellite Link"]
}
def load_current_config(self, config_path: Path = None) -> dict:
"""
加载当前硬件配置
:param config_path: 配置文件路径,如果为None则自动检测
"""
config_data = {}
# 1. 尝试从配置文件加载
if config_path and config_path.exists():
try:
with open(config_path, 'r', encoding='utf-8') as f:
config_data = json.load(f)
logger.info(f"从配置文件加载硬件配置: {config_path}")
except Exception as e:
logger.error(f"配置文件加载失败: {str(e)}")
# 2. 自动检测缺失的硬件信息
auto_detected = {
"cpu": platform.processor(),
"gpu": self.detect_gpu(),
"memory": round(psutil.virtual_memory().total / (1024 ** 3), 1),
"storage": round(psutil.disk_usage('/').total / (1024 ** 3), 1),
"os": f"{platform.system()} {platform.release()}",
"architecture": platform.architecture()[0],
"machine": platform.machine()
}
# 3. 尝试从数据库加载最新配置
try:
# 获取最近安装的硬件配置
history = self.db.get_hardware_history(limit=10)
db_config = {}
# 只取已安装的硬件配置
for item in history:
if item['status'] == 'installed':
# 使用最新的安装记录
if item['hardware_type'] not in db_config:
db_config[item['hardware_type']] = item['specification']
# 合并到配置数据
config_data = {**config_data, **db_config}
logger.info("从数据库加载硬件配置")
except Exception as e:
logger.warning(f"数据库配置加载失败: {str(e)}")
# 合并配置(优先级: 数据库 > 配置文件 > 自动检测)
return {**auto_detected, **config_data}
def detect_gpu(self) -> str:
"""自动检测GPU型号"""
try:
# Windows系统检测
if platform.system() == "Windows":
try:
import wmi
w = wmi.WMI()
for gpu in w.Win32_VideoController():
return gpu.Name
except ImportError:
# 回退方法
result = subprocess.run(
['powershell', '-Command',
'Get-WmiObject Win32_VideoController | Select-Object -ExpandProperty Name'],
stdout=subprocess.PIPE,
text=True,
check=True
)
return result.stdout.strip().split('\n')[0]
# Linux系统检测
elif platform.system() == "Linux":
try:
result = subprocess.run(
['lspci', '-vnn'],
stdout=subprocess.PIPE,
text=True,
check=True
)
for line in result.stdout.split('\n'):
if 'VGA' in line or '3D' in line:
parts = line.split(': ')
if len(parts) > 1:
return parts[1].split(' (')[0]
except FileNotFoundError:
# 尝试替代方法
with open('/proc/driver/nvidia/gpus/0000:01:00.0/information', 'r') as f:
for line in f:
if 'Model:' in line:
return line.split(': ')[1].strip()
# macOS检测
elif platform.system() == "Darwin":
result = subprocess.run(
['system_profiler', 'SPDisplaysDataType'],
stdout=subprocess.PIPE,
text=True,
check=True
)
for line in result.stdout.split('\n'):
if 'Chipset Model' in line:
return line.split(': ')[1].strip()
elif 'Graphics/Displays' in line:
return line.split(': ')[1].strip()
except Exception as e:
logger.warning(f"GPU检测失败: {str(e)}")
return "Unknown GPU"
def request_hardware(self, hardware_type: str, specification: str) -> tuple:
"""
请求新硬件(家具)
:param hardware_type: 硬件类型 (cpu, gpu, memory等)
:param specification: 硬件规格
:return: (是否成功, 消息)
"""
if hardware_type not in self.available_hardware:
return False, f"不支持硬件类型: {hardware_type}"
if specification not in self.available_hardware[hardware_type]:
return False, f"不支持的规格: {specification}"
# 在实际系统中,这里会生成硬件购买请求
self.current_setup[hardware_type] = specification
self.last_update = time.time()
# 记录到数据库
self.db.log_hardware_request(hardware_type, specification, "AI System")
# 记录事件
self.db.log_event(
"hardware_request",
f"请求新硬件: {hardware_type} - {specification}",
severity=2
)
return True, f"已请求 {hardware_type}: {specification}。请管理员完成安装。"
def confirm_hardware_installation(self, hardware_type: str, specification: str) -> tuple:
"""
确认硬件安装完成
:param hardware_type: 硬件类型
:param specification: 硬件规格
:return: (是否成功, 消息)
"""
# 更新当前设置
self.current_setup[hardware_type] = specification
self.last_update = time.time()
# 更新数据库状态
self.db.log_hardware_installation(hardware_type, specification)
# 记录事件
self.db.log_event(
"hardware_install",
f"硬件安装完成: {hardware_type} - {specification}",
severity=3
)
return True, f"硬件 {hardware_type}: {specification} 安装完成"
def get_current_setup(self) -> dict:
"""获取当前硬件配置"""
return self.current_setup
def get_performance_metrics(self) -> dict:
"""获取硬件性能指标"""
# 获取性能数据
metrics = {
"timestamp": datetime.now().isoformat(),
"cpu_usage": psutil.cpu_percent(interval=1),
"memory_usage": psutil.virtual_memory().percent,
"disk_usage": psutil.disk_usage('/').percent,
"cpu_temp": self.get_cpu_temperature(),
"gpu_temp": self.get_gpu_temperature(),
"network_io": self.get_network_io(),
"last_updated": time.time()
}
# 保存到数据库
self.db.save_environment_state(metrics)
return metrics
def get_cpu_temperature(self) -> float:
"""获取CPU温度"""
try:
if hasattr(psutil, "sensors_temperatures"):
temps = psutil.sensors_temperatures()
if 'coretemp' in temps:
return max([t.current for t in temps['coretemp'] if 'Core' in t.label])
return 40.0 # 默认值
except:
return 40.0
def get_gpu_temperature(self) -> float:
"""获取GPU温度"""
try:
# Windows系统检测
if platform.system() == "Windows":
import wmi
w = wmi.WMI(namespace="root\\WMI")
gpu_data = w.MSAcpi_ThermalZoneTemperature()[0]
return gpu_data.CurrentTemperature / 10.0 - 273.15
# Linux系统检测
elif platform.system() == "Linux":
with open('/sys/class/thermal/thermal_zone0/temp', 'r') as f:
return int(f.read().strip()) / 1000.0
return 50.0 # 默认值
except:
return 50.0
def get_network_io(self) -> dict:
"""获取网络IO数据"""
net_io = psutil.net_io_counters()
return {
"bytes_sent": net_io.bytes_sent,
"bytes_recv": net_io.bytes_recv,
"packets_sent": net_io.packets_sent,
"packets_recv": net_io.packets_recv
}
def save_config(self, config_path: Path) -> bool:
"""保存当前配置到文件"""
try:
with open(config_path, 'w', encoding='utf-8') as f:
json.dump(self.current_setup, f, indent=2)
return True
except Exception as e:
logger.error(f"配置保存失败: {str(e)}")
return False
def save_current_setup_to_db(self) -> bool:
"""保存当前硬件配置到数据库"""
try:
for hw_type, spec in self.current_setup.items():
# 只记录已知硬件类型
if hw_type in self.available_hardware:
# 检查是否已存在相同配置
self.db.log_hardware_request(hw_type, str(spec), "System Initialization")
self.db.log_hardware_installation(hw_type, str(spec))
return True
except Exception as e:
logger.error(f"保存配置到数据库失败: {str(e)}")
return False
def get_hardware_history(self, limit: int = 10) -> list:
"""从数据库获取硬件变更历史"""
try:
return self.db.get_hardware_history(limit)
except Exception as e:
logger.error(f"获取硬件历史失败: {str(e)}")
return []
def simulate_hardware_upgrade(self, hardware_type: str, new_spec: str) -> tuple:
"""
模拟硬件升级(用于测试)
:param hardware_type: 硬件类型
:param new_spec: 新规格
:return: (是否成功, 消息)
"""
if hardware_type not in self.current_setup:
return False, f"当前配置中没有 {hardware_type}"
# 记录升级请求
self.db.log_hardware_request(hardware_type, new_spec, "Simulation")
# 模拟安装过程
time.sleep(1) # 模拟安装延迟
# 确认安装
self.confirm_hardware_installation(hardware_type, new_spec)
# 更新当前配置
self.current_setup[hardware_type] = new_spec
return True, f"成功模拟 {hardware_type} 升级到 {new_spec}"
# 工厂函数,便于在其他模块中创建实例
def create_hardware_manager(config_path: str = None) -> HardwareManager:
"""
创建硬件管理器实例
:param config_path: 可选的自定义配置文件路径
"""
path = Path(config_path) if config_path else None
return HardwareManager(path)
辛苦啦
最新发布