feat(config): 添加配置管理和MQTT模拟服务功能

- 实现了应用配置的数据类结构(MqttConfig, TmsConfig, AppConfig)
- 创建了配置加载和解析功能,支持从YAML文件读取配置
- 添加了TDengine数据库配置和连接池管理
- 实现了MQTT客户端依赖注入和服务构建
- 创建了钻孔实时数据的ORM映射和SQL构建功能
- 实现了TDengine Writer用于数据写入超级表
- 添加了MQTT模拟服务,支持发布、订阅和数据转发功能
- 创建了随机数据发送器用于测试
- 实现了消息持久化到本地文件功能
- 配置了数据库连接池和SQL执行功能
This commit is contained in:
2026-03-12 09:58:00 +08:00
commit d5d1cb0b7d
19 changed files with 1224 additions and 0 deletions

121
README.md Normal file
View File

@@ -0,0 +1,121 @@
# MQTT Mock Service
Simulates a service that:
- Subscribes to the ingest topic (server -> broker)
- Writes received messages to a local file (JSONL)
- Writes received messages to TDengine (super table)
- Forwards messages to another topic for downstream subscribers
- Optionally sends ack messages
Also includes:
- A random data sender (every 3 seconds by default)
- A simple subscriber that prints incoming payloads
## Setup
```
python -m venv .venv
.\.venv\Scripts\activate
pip install -r requirements.txt
```
## Run (service mode)
```
python mqtt_mock.py --config config.yaml --mode listen
```
When TDengine config exists in `config.yaml`, each message on `pub-topic` is inserted into:
- Super table: `drilling_realtime_st`
- Sub table: auto-generated as `drilling_realtime_<device_code>`
- Tag value: fixed `device_code` from config (`GJ-304-0088`)
## Run (also publish test data)
```
python mqtt_mock.py --config config.yaml --mode both --interval 2
```
## Run random sender (3s interval)
```
python mqtt_sender.py --config config.yaml --interval 3
```
## Run subscriber
```
python mqtt_subscriber.py --config config.yaml
```
## Payload format (server -> broker)
```
{
"meta": {
"test_id": "550e8400e29b41d4a716446655440000",
"equipment_sn": "GJ-304-0088"
},
"data": {
"record_time": 1751964764000,
"wellid": "",
"stknum": 0,
"recid": 0,
"seqid": 0,
"actual_date": 0,
"actual_time": 0,
"actcod": 0,
"deptbitm": 0,
"deptbitv": 0,
"deptmeas": 0,
"deptvert": 0,
"blkpos": 0,
"ropa": 0,
"hkla": 0,
"hklx": 0,
"woba": 0,
"wobx": 0,
"torqa": 0,
"torqx": 0,
"rpma": 0,
"sppa": 0,
"chkp": 0,
"spm1": 0,
"spm2": 0,
"spm3": 0,
"tvolact": 0,
"tvolcact": 0,
"mfop": 0,
"mfoa": 0,
"mfia": 0,
"mdoa": 0,
"mdia": 0,
"mtoa": 0,
"mtia": 0,
"mcoa": 0,
"mcia": 0,
"stkc": 0,
"lagstks": 0,
"deptretm": 0,
"gasa": 0,
"space1": 0,
"space2": 0,
"space3": 0,
"space4": 0,
"space5": 0
}
}
```
## Config
- `pub-topic`: ingest topic from server
- `sub-topic`: forward topic for other services to subscribe
- `ack-topic`: optional ack topic
- `data-file`: local append-only file (JSON Lines)
- `equipment-sn`: default equipment_sn for simulated payloads
- `test-id`: default test_id for simulated payloads
- `tdengine.url`: e.g. `jdbc:TAOS-RS://192.168.1.87:6041/tms`
- `tdengine.username`: DB user
- `tdengine.password`: DB password
- `tdengine.database`: DB name (optional if URL already has `/tms`)
- `tdengine.stable`: default `drilling_realtime_st`
## Options
- `--mode`: publish | listen | both
- `--interval`: publish interval seconds
- `--count`: publish count (0 = forever)
- `--data-file`: override data-file in config