You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
5.3 KiB
150 lines
5.3 KiB
"""
|
|
系统服务模块
|
|
"""
|
|
from typing import Dict, Any, Optional
|
|
from app.utils.system_utils import SystemUtils
|
|
from app.utils.structured_log import get_structured_logger
|
|
|
|
logger = get_structured_logger(__name__)
|
|
|
|
class SystemService:
|
|
"""系统管理服务"""
|
|
|
|
def __init__(self):
|
|
self.system_utils = SystemUtils()
|
|
|
|
async def get_machine_code(self) -> Dict[str, Any]:
|
|
"""获取机器码"""
|
|
try:
|
|
logger.info("开始获取机器码")
|
|
|
|
system_type = SystemUtils.get_system_type()
|
|
machine_code = SystemUtils.get_machine_code()
|
|
|
|
if machine_code:
|
|
method = self._get_method_description(system_type)
|
|
logger.info(f"机器码获取成功: {machine_code}, 方法: {method}")
|
|
|
|
return {
|
|
"success": True,
|
|
"message": "机器码获取成功",
|
|
"data": {
|
|
"machine_code": machine_code,
|
|
"system_type": system_type,
|
|
"method": method
|
|
}
|
|
}
|
|
else:
|
|
logger.warning("机器码获取失败")
|
|
return {
|
|
"success": False,
|
|
"message": "机器码获取失败",
|
|
"data": {
|
|
"machine_code": None,
|
|
"system_type": system_type,
|
|
"method": None,
|
|
"error": "无法获取机器码"
|
|
}
|
|
}
|
|
|
|
except Exception as e:
|
|
logger.error(f"获取机器码异常: {e}")
|
|
return {
|
|
"success": False,
|
|
"message": f"获取机器码失败: {str(e)}",
|
|
"data": {
|
|
"machine_code": None,
|
|
"system_type": SystemUtils.get_system_type(),
|
|
"method": None,
|
|
"error": str(e)
|
|
}
|
|
}
|
|
|
|
async def get_system_info(self) -> Dict[str, Any]:
|
|
"""获取系统信息"""
|
|
try:
|
|
logger.info("开始获取系统信息")
|
|
|
|
system_info = SystemUtils.get_system_info()
|
|
|
|
if "error" not in system_info:
|
|
logger.info("系统信息获取成功")
|
|
return {
|
|
"success": True,
|
|
"message": "系统信息获取成功",
|
|
"data": system_info
|
|
}
|
|
else:
|
|
logger.warning("系统信息获取失败")
|
|
return {
|
|
"success": False,
|
|
"message": "系统信息获取失败",
|
|
"data": system_info
|
|
}
|
|
|
|
except Exception as e:
|
|
logger.error(f"获取系统信息异常: {e}")
|
|
return {
|
|
"success": False,
|
|
"message": f"获取系统信息失败: {str(e)}",
|
|
"data": {
|
|
"system_type": "unknown",
|
|
"error": str(e)
|
|
}
|
|
}
|
|
|
|
def _get_method_description(self, system_type: str) -> str:
|
|
"""获取方法描述"""
|
|
if system_type == "windows":
|
|
return "wmic csproduct get uuid"
|
|
elif system_type == "linux":
|
|
return "dmidecode -s system-serial-number / cat /sys/class/dmi/id/product_serial / hostid"
|
|
else:
|
|
return "unknown"
|
|
|
|
async def get_detailed_machine_code_info(self) -> Dict[str, Any]:
|
|
"""获取详细的机器码信息"""
|
|
try:
|
|
logger.info("开始获取详细机器码信息")
|
|
|
|
system_type = SystemUtils.get_system_type()
|
|
machine_code = SystemUtils.get_machine_code()
|
|
system_info = SystemUtils.get_system_info()
|
|
|
|
result = {
|
|
"success": True,
|
|
"message": "详细机器码信息获取成功",
|
|
"data": {
|
|
"machine_code": machine_code,
|
|
"system_type": system_type,
|
|
"method": self._get_method_description(system_type),
|
|
"system_info": system_info,
|
|
"timestamp": self._get_current_timestamp()
|
|
}
|
|
}
|
|
|
|
if not machine_code:
|
|
result["success"] = False
|
|
result["message"] = "机器码获取失败"
|
|
result["data"]["error"] = "无法获取机器码"
|
|
|
|
logger.info("详细机器码信息获取完成")
|
|
return result
|
|
|
|
except Exception as e:
|
|
logger.error(f"获取详细机器码信息异常: {e}")
|
|
return {
|
|
"success": False,
|
|
"message": f"获取详细机器码信息失败: {str(e)}",
|
|
"data": {
|
|
"machine_code": None,
|
|
"system_type": SystemUtils.get_system_type(),
|
|
"method": None,
|
|
"error": str(e)
|
|
}
|
|
}
|
|
|
|
def _get_current_timestamp(self) -> str:
|
|
"""获取当前时间戳"""
|
|
from datetime import datetime
|
|
return datetime.now().isoformat()
|
|
|