feat: 新增 MQTT 配置支持
- 新增 MQTTConfig 配置类 - 支持 broker_host/port/client_id 等配置 - 支持环境变量覆盖配置 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
"""
|
||||
配置管理
|
||||
"""
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
@@ -7,11 +10,13 @@ from pydantic import BaseModel
|
||||
|
||||
@dataclass
|
||||
class DatabaseConfig:
|
||||
"""数据库配置"""
|
||||
url: str = "sqlite:///./data/alert_platform.db"
|
||||
|
||||
|
||||
@dataclass
|
||||
class OSSConfig:
|
||||
"""OSS 存储配置"""
|
||||
access_key_id: str = ""
|
||||
access_key_secret: str = ""
|
||||
endpoint: str = "oss-cn-hangzhou.aliyuncs.com"
|
||||
@@ -21,6 +26,7 @@ class OSSConfig:
|
||||
|
||||
@dataclass
|
||||
class AppConfig:
|
||||
"""应用配置"""
|
||||
host: str = "0.0.0.0"
|
||||
port: int = 8000
|
||||
debug: bool = True
|
||||
@@ -28,18 +34,36 @@ class AppConfig:
|
||||
|
||||
@dataclass
|
||||
class AIModelConfig:
|
||||
"""AI 模型配置"""
|
||||
endpoint: str = ""
|
||||
api_key: str = ""
|
||||
|
||||
|
||||
@dataclass
|
||||
class MQTTConfig:
|
||||
"""MQTT 配置"""
|
||||
broker_host: str = "localhost"
|
||||
broker_port: int = 1883
|
||||
client_id: str = "alert_platform"
|
||||
username: str = ""
|
||||
password: str = ""
|
||||
alert_topic: str = "edge/alert/#"
|
||||
heartbeat_topic: str = "edge/alert/heartbeat/#"
|
||||
qos: int = 1
|
||||
enabled: bool = True
|
||||
|
||||
|
||||
class Settings(BaseModel):
|
||||
"""全局配置"""
|
||||
database: DatabaseConfig = DatabaseConfig()
|
||||
oss: OSSConfig = OSSConfig()
|
||||
app: AppConfig = AppConfig()
|
||||
ai_model: AIModelConfig = AIModelConfig()
|
||||
mqtt: MQTTConfig = MQTTConfig()
|
||||
|
||||
|
||||
def load_settings() -> Settings:
|
||||
"""从环境变量加载配置"""
|
||||
from dotenv import load_dotenv
|
||||
load_dotenv()
|
||||
|
||||
@@ -63,6 +87,17 @@ def load_settings() -> Settings:
|
||||
endpoint=os.getenv("AI_MODEL_ENDPOINT", ""),
|
||||
api_key=os.getenv("AI_MODEL_API_KEY", ""),
|
||||
),
|
||||
mqtt=MQTTConfig(
|
||||
broker_host=os.getenv("MQTT_BROKER_HOST", "localhost"),
|
||||
broker_port=int(os.getenv("MQTT_BROKER_PORT", "1883")),
|
||||
client_id=os.getenv("MQTT_CLIENT_ID", "alert_platform"),
|
||||
username=os.getenv("MQTT_USERNAME", ""),
|
||||
password=os.getenv("MQTT_PASSWORD", ""),
|
||||
alert_topic=os.getenv("MQTT_ALERT_TOPIC", "edge/alert/#"),
|
||||
heartbeat_topic=os.getenv("MQTT_HEARTBEAT_TOPIC", "edge/alert/heartbeat/#"),
|
||||
qos=int(os.getenv("MQTT_QOS", "1")),
|
||||
enabled=os.getenv("MQTT_ENABLED", "true").lower() == "true",
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user