refactor(config): 重构配置模块并优化应用依赖注入

- 将配置相关类移动到model模块
- 实现依赖注入容器管理各组件依赖关系
- 重构配置加载逻辑支持多层级键值查找
- 更新主应用入口支持命令行参数解析
- 统一日志输出格式替换原有打印语句
- 引入钻井实时数据模型简化数据处理
- 移除硬编码字段映射改用动态配置方式
- 优化数据库写入逻辑基于新的数据模型
This commit is contained in:
2026-03-12 10:41:26 +08:00
parent 6d13da4cc2
commit 6557479a2f
16 changed files with 783 additions and 589 deletions

View File

@@ -0,0 +1,14 @@
from model.config import AppConfig, MqttConfig, TdengineConfig, TmsConfig, WitsConfig
from model.drilling import DrillingRealtimeData
from model.wits import WITS_FIELD_MAPPING, WitsData
__all__ = [
"AppConfig",
"DrillingRealtimeData",
"MqttConfig",
"TdengineConfig",
"TmsConfig",
"WITS_FIELD_MAPPING",
"WitsConfig",
"WitsData",
]

72
model/config.py Normal file
View File

@@ -0,0 +1,72 @@
from dataclasses import dataclass
from urllib.parse import urlparse
@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 TdengineConfig:
url: str = ""
username: str = ""
password: str = ""
database: str = ""
stable: str = "drilling_realtime_st"
device_code: str = "GJ-304-0088"
pool_size: int = 2
timeout: int = 10
@property
def base_url(self):
if not self.url:
return ""
raw = str(self.url).strip()
if raw.lower().startswith("jdbc:taos-rs://"):
raw = "http://" + raw[len("jdbc:TAOS-RS://") :]
elif "://" not in raw:
raw = "http://" + raw
parsed = urlparse(raw)
return f"{parsed.scheme or 'http'}://{parsed.hostname or '127.0.0.1'}:{parsed.port or 6041}"
@property
def enabled(self):
return bool(self.base_url and self.database and self.username)
@dataclass(frozen=True)
class AppConfig:
mqtt: MqttConfig
tms: TmsConfig
wits: WitsConfig
tdengine: TdengineConfig
raw: dict

176
model/drilling.py Normal file
View File

@@ -0,0 +1,176 @@
import time
from dataclasses import dataclass
from datetime import datetime
DATA_KEYS = [
"ts",
"wellid",
"stknum",
"recid",
"seqid",
"actual_date",
"actual_time",
"actcod",
"deptbitm",
"deptbitv",
"deptmeas",
"deptvert",
"blkpos",
"ropa",
"hkla",
"hklx",
"woba",
"wobx",
"torqa",
"torqx",
"rpma",
"sppa",
"chkp",
"spm1",
"spm2",
"spm3",
"tvolact",
"tvolcact",
"mfop",
"mfoa",
"mfia",
"mdoa",
"mdia",
"mtoa",
"mtia",
"mcoa",
"mcia",
"stkc",
"lagstks",
"deptretm",
"gasa",
"space1",
"space2",
"space3",
"space4",
"space5",
]
@dataclass(frozen=True)
class DrillingRealtimeData:
ts: int
wellid: str
stknum: int
recid: int
seqid: int
actual_date: float
actual_time: float
actcod: int
deptbitm: float
deptbitv: float
deptmeas: float
deptvert: float
blkpos: float
ropa: float
hkla: float
hklx: float
woba: float
wobx: float
torqa: float
torqx: float
rpma: int
sppa: float
chkp: float
spm1: int
spm2: int
spm3: int
tvolact: float
tvolcact: float
mfop: int
mfoa: float
mfia: float
mdoa: float
mdia: float
mtoa: float
mtia: float
mcoa: float
mcia: float
stkc: int
lagstks: int
deptretm: float
gasa: float
space1: float
space2: float
space3: float
space4: float
space5: float
@classmethod
def empty(cls, wellid=""):
now = datetime.utcnow()
return cls(
ts=int(time.time() * 1000),
wellid=wellid,
stknum=0,
recid=0,
seqid=0,
actual_date=float(now.strftime("%Y%m%d")),
actual_time=float(now.strftime("%H%M%S")),
actcod=0,
deptbitm=0.0,
deptbitv=0.0,
deptmeas=0.0,
deptvert=0.0,
blkpos=0.0,
ropa=0.0,
hkla=0.0,
hklx=0.0,
woba=0.0,
wobx=0.0,
torqa=0.0,
torqx=0.0,
rpma=0,
sppa=0.0,
chkp=0.0,
spm1=0,
spm2=0,
spm3=0,
tvolact=0.0,
tvolcact=0.0,
mfop=0,
mfoa=0.0,
mfia=0.0,
mdoa=0.0,
mdia=0.0,
mtoa=0.0,
mtia=0.0,
mcoa=0.0,
mcia=0.0,
stkc=0,
lagstks=0,
deptretm=0.0,
gasa=0.0,
space1=0.0,
space2=0.0,
space3=0.0,
space4=0.0,
space5=0.0,
)
@classmethod
def from_payload(cls, payload):
meta = payload.get("meta") if isinstance(payload, dict) and isinstance(payload.get("meta"), dict) else {}
data = payload.get("data") if isinstance(payload, dict) and isinstance(payload.get("data"), dict) else {}
values = {key: data.get(key, 0) for key in DATA_KEYS}
values["ts"] = int(data.get("ts", data.get("record_time", int(time.time() * 1000))))
values["wellid"] = data.get("wellid") or meta.get("equipment_code") or meta.get("equipment_sn") or ""
return cls(**values)
def to_payload(self, equipment_code):
return {
"meta": {
"equipment_code": equipment_code,
"equipment_sn": equipment_code,
},
"data": {
key: getattr(self, key)
for key in DATA_KEYS
},
}

102
model/wits.py Normal file
View File

@@ -0,0 +1,102 @@
from dataclasses import dataclass
@dataclass(frozen=True)
class WitsData:
ts: int
wellid: str
stknum: int
recid: int
seqid: int
actual_date: float
actual_time: float
actual_ts: int
actcod: int
actod_label: str
deptbitm: float
deptbitv: float
deptmeas: float
deptvert: float
blkpos: float
ropa: float
hkla: float
hklx: float
woba: float
wobx: float
torqa: float
torqx: float
rpma: int
sppa: float
chkp: float
spm1: int
spm2: int
spm3: int
tvolact: float
tvolcact: float
mfop: int
mfoa: float
mfia: float
mdoa: float
mdia: float
mtoa: float
mtia: float
mcoa: float
mcia: float
stkc: int
lagstks: int
deptretm: float
gasa: float
space1: float
space2: float
space3: float
space4: float
space5: float
WITS_FIELD_MAPPING = [
(1, "wellid", "string"),
(2, "stknum", "int"),
(3, "recid", "int"),
(4, "seqid", "int"),
(5, "actual_date", "float"),
(6, "actual_time", "float"),
(7, "actcod", "int"),
(8, "deptbitm", "float"),
(9, "deptbitv", "float"),
(10, "deptmeas", "float"),
(11, "deptvert", "float"),
(12, "blkpos", "float"),
(13, "ropa", "float"),
(14, "hkla", "float"),
(15, "hklx", "float"),
(16, "woba", "float"),
(17, "wobx", "float"),
(18, "torqa", "float"),
(19, "torqx", "float"),
(20, "rpma", "int"),
(21, "sppa", "float"),
(22, "chkp", "float"),
(23, "spm1", "int"),
(24, "spm2", "int"),
(25, "spm3", "int"),
(26, "tvolact", "float"),
(27, "tvolcact", "float"),
(28, "mfop", "int"),
(29, "mfoa", "float"),
(30, "mfia", "float"),
(31, "mdoa", "float"),
(32, "mdia", "float"),
(33, "mtoa", "float"),
(34, "mtia", "float"),
(35, "mcoa", "float"),
(36, "mcia", "float"),
(37, "stkc", "int"),
(38, "lagstks", "int"),
(39, "deptretm", "float"),
(40, "gasa", "float"),
(41, "space1", "float"),
(42, "space2", "float"),
(43, "space3", "float"),
(44, "space4", "float"),
(45, "space5", "float"),
]