62 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			62 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| #!/usr/bin/env python3
 | |
| # -*- coding: utf-8 -*-
 | |
| 
 | |
| """
 | |
| 配置模块,用于加载和管理中继器的配置
 | |
| """
 | |
| 
 | |
| import yaml
 | |
| import os
 | |
| 
 | |
| 
 | |
| class Config:
 | |
|     """配置类,用于加载和访问配置项"""
 | |
|     
 | |
|     def __init__(self, config_file="config/config.yaml"):
 | |
|         """
 | |
|         初始化配置类
 | |
|         
 | |
|         Args:
 | |
|             config_file (str): 配置文件路径
 | |
|         """
 | |
|         # 获取项目根目录
 | |
|         project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 | |
|         config_path = os.path.join(project_root, config_file)
 | |
|         
 | |
|         # 加载配置文件
 | |
|         with open(config_path, 'r', encoding='utf-8') as f:
 | |
|             self._config = yaml.safe_load(f)
 | |
|     
 | |
|     @property
 | |
|     def simulation_enabled(self):
 | |
|         """获取模拟模式是否启用"""
 | |
|         return self._config.get('simulation', {}).get('enabled', False)
 | |
|     
 | |
|     @property
 | |
|     def simulation_controllers(self):
 | |
|         """获取模拟区域主控设备列表"""
 | |
|         return self._config.get('simulation', {}).get('controllers', [])
 | |
|     
 | |
|     @property
 | |
|     def simulation_devices(self):
 | |
|         """获取模拟普通设备列表"""
 | |
|         return self._config.get('simulation', {}).get('devices', [])
 | |
|     
 | |
|     @property
 | |
|     def websocket_config(self):
 | |
|         """获取WebSocket配置"""
 | |
|         return self._config.get('websocket', {})
 | |
|     
 | |
|     @property
 | |
|     def websocket_address(self):
 | |
|         """获取WebSocket服务器地址"""
 | |
|         return self.websocket_config.get('address', '127.0.0.1')
 | |
|     
 | |
|     @property
 | |
|     def websocket_port(self):
 | |
|         """获取WebSocket服务器端口"""
 | |
|         return self.websocket_config.get('port', 8080)
 | |
| 
 | |
| 
 | |
| # 全局配置实例
 | |
| config = Config() |