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文档说明项目需求
This commit is contained in:
2026-03-12 10:20:35 +08:00
parent d5d1cb0b7d
commit 45870a2f73
14 changed files with 444 additions and 128 deletions

View File

@@ -28,10 +28,19 @@ class TmsConfig:
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
@@ -96,6 +105,12 @@ def load_app_config(path):
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,
)
@@ -106,6 +121,7 @@ __all__ = [
"AppConfig",
"MqttConfig",
"TmsConfig",
"WitsConfig",
"get_value",
"load_app_config",
"load_config",

18
config/config.py Normal file
View File

@@ -0,0 +1,18 @@
import yaml
from config.model import *
def load(path: str) -> "Config":
with open(path, "r", encoding="utf-8") as f:
data = yaml.safe_load(f)
mqtt_cfg = MqttConfig(**data["mqtt"])
tms_cfg = TmsConfig(**data["tms"])
tdengine_cfg = TdengineConfig(**data["tdengine"])
return Config(
mqtt=mqtt_cfg,
tms=tms_cfg,
tdengine=tdengine_cfg
)

View File

@@ -1,41 +0,0 @@
from dataclasses import dataclass
from config import load_app_config
from db import TDengineWriter, load_tdengine_config
@dataclass(frozen=True)
class SenderDependencies:
config: object
@dataclass(frozen=True)
class SubscriberDependencies:
config: object
@dataclass(frozen=True)
class MockDependencies:
config: object
tdengine_config: object
tdengine_writer: object
data_file: str
def build_sender_dependencies(config_path):
return SenderDependencies(config=load_app_config(config_path))
def build_subscriber_dependencies(config_path):
return SubscriberDependencies(config=load_app_config(config_path))
def build_mock_dependencies(config_path, data_file_override=""):
app_config = load_app_config(config_path)
tdengine_config = load_tdengine_config(app_config, default_device_code=app_config.tms.device_code)
return MockDependencies(
config=app_config,
tdengine_config=tdengine_config,
tdengine_writer=TDengineWriter(tdengine_config),
data_file=data_file_override or app_config.mqtt.data_file,
)

40
config/model.py Normal file
View File

@@ -0,0 +1,40 @@
from dataclasses import dataclass
@dataclass
class MqttConfig:
broker: str
client_id: str
mock_client_id: str
sender_client_id: str
subscriber_client_id: str
username: str
password: str
pub_topic: str
@dataclass
class TmsConfig:
device_code: str
equipment_sn: str
timeout: int
keepalive: int
server_ip: str
server_port: int
@dataclass
class TdengineConfig:
url: str
username: str
password: str
database: str
stable: str
device_code: str
@dataclass
class Config:
mqtt: MqttConfig
tms: TmsConfig
tdengine: TdengineConfig