创建项目及AI生成基本代码
This commit is contained in:
6
config/__init__.py
Normal file
6
config/__init__.py
Normal file
@@ -0,0 +1,6 @@
|
||||
# 配置文件目录
|
||||
# 存放不同环境的配置文件
|
||||
|
||||
from .config import Config, config
|
||||
|
||||
__all__ = ['Config', 'config']
|
||||
62
config/config.py
Normal file
62
config/config.py
Normal file
@@ -0,0 +1,62 @@
|
||||
#!/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()
|
||||
69
config/config.yaml
Normal file
69
config/config.yaml
Normal file
@@ -0,0 +1,69 @@
|
||||
# 通信中继器配置文件
|
||||
|
||||
# 中继器配置
|
||||
relay:
|
||||
# 中继设备唯一标识符,用于WebSocket连接时的身份标识
|
||||
device_id: "1"
|
||||
# 中继器名称
|
||||
name: "猪场1"
|
||||
|
||||
# WebSocket配置
|
||||
websocket:
|
||||
# WebSocket服务器地址
|
||||
host: "localhost"
|
||||
# WebSocket端口
|
||||
port: 8086
|
||||
# WebSocket请求超时时间(秒)
|
||||
timeout: 5
|
||||
|
||||
# 模拟模式配置
|
||||
simulation:
|
||||
# 是否开启模拟模式
|
||||
enabled: true
|
||||
# 模拟区域主控设备列表(没有冒号的地址)
|
||||
controllers:
|
||||
- id: "2"
|
||||
name: "猪舍1"
|
||||
type: "pig_pen_controller"
|
||||
lora_address: "121"
|
||||
status: "running"
|
||||
- id: "5"
|
||||
name: "做料1"
|
||||
type: "feed_mill_controller"
|
||||
lora_address: "123"
|
||||
status: "stopped"
|
||||
- id: "8"
|
||||
name: "猪舍2"
|
||||
type: "pig_pen_controller"
|
||||
lora_address: "122"
|
||||
status: "running"
|
||||
# 模拟普通设备列表(带冒号的地址,格式为总线号:总线地址)
|
||||
devices:
|
||||
- id: "3"
|
||||
name: "风机1"
|
||||
type: "fan"
|
||||
rs485_bus: "1"
|
||||
rs485_address: "12"
|
||||
controller_id: "2"
|
||||
status: "stopped"
|
||||
- id: "9"
|
||||
name: "风机2"
|
||||
type: "fan"
|
||||
rs485_bus: "1"
|
||||
rs485_address: "11"
|
||||
controller_id: "2"
|
||||
status: "running"
|
||||
- id: "10"
|
||||
name: "水帘1"
|
||||
type: "water_curtain"
|
||||
rs485_bus: "1"
|
||||
rs485_address: "11"
|
||||
controller_id: "8"
|
||||
status: "stopped"
|
||||
- id: "11"
|
||||
name: "风机3"
|
||||
type: "fan"
|
||||
rs485_bus: "2"
|
||||
rs485_address: "21"
|
||||
controller_id: "2"
|
||||
status: "stopped"
|
||||
Reference in New Issue
Block a user