Files
tdEngine_mqtt_mock/config/__init__.py
wsy182 45870a2f73 feat(wits): 添加WITS TCP发送功能和配置重构
- 新增WitsConfig数据类用于WITS配置管理
- 在AppConfig中集成wits配置选项
- 重命名dependencies.py为config.py并重构配置加载逻辑
- 移除db/config.py文件中的TDengine配置相关代码
- 创建新的model.py文件定义MqttConfig、TmsConfig和TdengineConfig模型
- 更新MQTT模块导入路径从config.dependencies到config.config
- 添加WITS发送器脚本wits_sender.py实现TCP数据包发送
- 在README.md中添加WITS发送器使用说明和配置选项
- 添加WITS样本数据文件data/wits_sample.txt
- 添加requirements.md文档说明项目需求
2026-03-12 10:20:35 +08:00

130 lines
3.9 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 WitsConfig:
host: str
port: int
timeout: int
source_file: str
@dataclass(frozen=True)
class AppConfig:
mqtt: MqttConfig
tms: TmsConfig
wits: WitsConfig
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",)),
),
wits=WitsConfig(
host=get_value(raw, ("wits", "host"), ("tms", "server-ip"), ("server-ip",), default=""),
port=int(get_value(raw, ("wits", "port"), ("tms", "server-port"), ("server-port",), default=0)),
timeout=int(get_value(raw, ("wits", "timeout"), ("tms", "timeout"), ("timeout",), default=10)),
source_file=get_value(raw, ("wits", "source-file"), default=""),
),
raw=raw,
)
load_config = load_app_config
__all__ = [
"AppConfig",
"MqttConfig",
"TmsConfig",
"WitsConfig",
"get_value",
"load_app_config",
"load_config",
"load_raw_config",
]