- 实现了应用配置的数据类结构(MqttConfig, TmsConfig, AppConfig) - 创建了配置加载和解析功能,支持从YAML文件读取配置 - 添加了TDengine数据库配置和连接池管理 - 实现了MQTT客户端依赖注入和服务构建 - 创建了钻孔实时数据的ORM映射和SQL构建功能 - 实现了TDengine Writer用于数据写入超级表 - 添加了MQTT模拟服务,支持发布、订阅和数据转发功能 - 创建了随机数据发送器用于测试 - 实现了消息持久化到本地文件功能 - 配置了数据库连接池和SQL执行功能
114 lines
3.3 KiB
Python
114 lines
3.3 KiB
Python
from dataclasses import dataclass
|
|
|
|
import yaml
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class MqttConfig:
|
|
broker: str
|
|
client_id: str
|
|
mock_client_id: str
|
|
sender_client_id: str
|
|
subscriber_client_id: str
|
|
username: str | None
|
|
password: str | None
|
|
pub_topic: str | None
|
|
sub_topic: str | None
|
|
ack_topic: str | None
|
|
data_file: str
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class TmsConfig:
|
|
device_code: str
|
|
equipment_sn: str
|
|
timeout: int
|
|
keepalive: int
|
|
server_ip: str | None
|
|
server_port: int | None
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class AppConfig:
|
|
mqtt: MqttConfig
|
|
tms: TmsConfig
|
|
raw: dict
|
|
|
|
|
|
def load_raw_config(path):
|
|
with open(path, "r", encoding="utf-8") as f:
|
|
return yaml.safe_load(f) or {}
|
|
|
|
|
|
def get_value(cfg, *paths, default=None):
|
|
for path in paths:
|
|
current = cfg
|
|
found = True
|
|
for key in path:
|
|
if not isinstance(current, dict) or key not in current:
|
|
found = False
|
|
break
|
|
current = current[key]
|
|
if found and current is not None:
|
|
return current
|
|
return default
|
|
|
|
|
|
def load_app_config(path):
|
|
raw = load_raw_config(path)
|
|
base_client_id = get_value(raw, ("mqtt", "client-id"), ("client-id",), default="mqtt")
|
|
device_code = get_value(
|
|
raw,
|
|
("tms", "device-code"),
|
|
("tms", "equipment-sn"),
|
|
("device-code",),
|
|
("equipment-sn",),
|
|
default="GJ-304-0088",
|
|
)
|
|
equipment_sn = get_value(
|
|
raw,
|
|
("tms", "equipment-sn"),
|
|
("tms", "device-code"),
|
|
("equipment-sn",),
|
|
("device-code",),
|
|
default=device_code,
|
|
)
|
|
|
|
return AppConfig(
|
|
mqtt=MqttConfig(
|
|
broker=get_value(raw, ("mqtt", "broker"), ("broker",), default=""),
|
|
client_id=base_client_id,
|
|
mock_client_id=get_value(raw, ("mqtt", "mock-client-id"), ("mock-client-id",), default=f"{base_client_id}-mock"),
|
|
sender_client_id=get_value(raw, ("mqtt", "sender-client-id"), ("sender-client-id",), default=f"{base_client_id}-sender"),
|
|
subscriber_client_id=get_value(raw, ("mqtt", "subscriber-client-id"), ("subscriber-client-id",), default=f"{base_client_id}-subscriber"),
|
|
username=get_value(raw, ("mqtt", "username"), ("username",)),
|
|
password=get_value(raw, ("mqtt", "password"), ("password",)),
|
|
pub_topic=get_value(raw, ("mqtt", "pub-topic"), ("pub-topic",)),
|
|
sub_topic=get_value(raw, ("mqtt", "sub-topic"), ("sub-topic",)),
|
|
ack_topic=get_value(raw, ("mqtt", "ack-topic"), ("ack-topic",)),
|
|
data_file=get_value(raw, ("mqtt", "data-file"), ("data-file",), default=""),
|
|
),
|
|
tms=TmsConfig(
|
|
device_code=device_code,
|
|
equipment_sn=equipment_sn,
|
|
timeout=int(get_value(raw, ("tms", "timeout"), ("timeout",), default=10)),
|
|
keepalive=int(get_value(raw, ("tms", "keepalive"), ("keepalive",), default=20)),
|
|
server_ip=get_value(raw, ("tms", "server-ip"), ("server-ip",)),
|
|
server_port=get_value(raw, ("tms", "server-port"), ("server-port",)),
|
|
),
|
|
raw=raw,
|
|
)
|
|
|
|
|
|
load_config = load_app_config
|
|
|
|
__all__ = [
|
|
"AppConfig",
|
|
"MqttConfig",
|
|
"TmsConfig",
|
|
"get_value",
|
|
"load_app_config",
|
|
"load_config",
|
|
"load_raw_config",
|
|
]
|