#!/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): 配置文件路径 """ # 获取项目根目录 self.project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) self.config_file = config_file self.config_path = os.path.join(self.project_root, config_file) # 加载配置文件 self._config = self._load_config() def _load_config(self): """加载配置文件""" with open(self.config_path, 'r', encoding='utf-8') as f: return yaml.safe_load(f) def reload_config(self): """重新加载配置文件""" self._config = self._load_config() return self._config @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 relay_config(self): """获取中继器配置""" return self._config.get('relay', {}) # 全局配置实例 config = Config()