refactor(config): 重构配置模块并优化应用依赖注入
- 将配置相关类移动到model模块 - 实现依赖注入容器管理各组件依赖关系 - 重构配置加载逻辑支持多层级键值查找 - 更新主应用入口支持命令行参数解析 - 统一日志输出格式替换原有打印语句 - 引入钻井实时数据模型简化数据处理 - 移除硬编码字段映射改用动态配置方式 - 优化数据库写入逻辑基于新的数据模型
This commit is contained in:
@@ -1,119 +1,20 @@
|
||||
import argparse
|
||||
import logging
|
||||
import random
|
||||
import socket
|
||||
import time
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
from config.dependencies import build_wits_sender_dependencies
|
||||
from config import build_wits_sender_dependencies
|
||||
from model import WITS_FIELD_MAPPING, WitsData
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
BEGIN_MARK = "&&\r\n"
|
||||
END_MARK = "!!\r\n"
|
||||
|
||||
|
||||
@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"),
|
||||
]
|
||||
|
||||
|
||||
def rand_int(a, b):
|
||||
return random.randint(a, b)
|
||||
|
||||
@@ -219,17 +120,10 @@ def run_wits_sender(args, deps):
|
||||
host = args.host or wits_config.host
|
||||
port = args.port or wits_config.port
|
||||
timeout = args.timeout or wits_config.timeout
|
||||
|
||||
if not host or not port:
|
||||
raise ValueError("WITS target host/port is empty. Configure wits.host/wits.port or tms.server-ip/tms.server-port")
|
||||
|
||||
print("WITS sender config:")
|
||||
print(f" host: {host}")
|
||||
print(f" port: {port}")
|
||||
print(f" timeout: {timeout}s")
|
||||
print(f" source-file: {source_file or '(generated)'}")
|
||||
print(f" interval: {args.interval}s")
|
||||
print(f" count: {args.count or 'forever'}")
|
||||
logger.info("WITS sender config host=%s port=%s timeout=%ss source_file=%s interval=%ss count=%s", host, port, timeout, source_file or "(generated)", args.interval, args.count or "forever")
|
||||
|
||||
seq = 0
|
||||
try:
|
||||
@@ -240,29 +134,33 @@ def run_wits_sender(args, deps):
|
||||
else:
|
||||
packet = build_wits_packet(build_random_wits_data(device_code))
|
||||
send_packet(host, port, timeout, packet)
|
||||
print(f"TX WITS #{seq} -> {host}:{port}")
|
||||
print(packet)
|
||||
logger.info("TX WITS #%s -> %s:%s", seq, host, port)
|
||||
if logger.isEnabledFor(logging.DEBUG):
|
||||
logger.debug("WITS packet:\n%s", packet)
|
||||
if args.count and seq >= args.count:
|
||||
break
|
||||
time.sleep(args.interval)
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
logger.info("WITS sender interrupted")
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser(description="WITS TCP sender")
|
||||
ap.add_argument("--config", default="config.yaml", help="Path to config yaml")
|
||||
ap.add_argument("--host", default="", help="Override target host")
|
||||
ap.add_argument("--port", type=int, default=0, help="Override target port")
|
||||
ap.add_argument("--timeout", type=int, default=0, help="Override socket timeout")
|
||||
ap.add_argument("--source-file", default="", help="Send raw WITS packet from file")
|
||||
ap.add_argument("--interval", type=float, default=3.0, help="Send interval in seconds")
|
||||
ap.add_argument("--count", type=int, default=1, help="Send count (0 = forever)")
|
||||
args = ap.parse_args()
|
||||
def add_arguments(parser):
|
||||
parser.add_argument("--config", default="config.yaml", help="Path to config yaml")
|
||||
parser.add_argument("--host", default="", help="Override target host")
|
||||
parser.add_argument("--port", type=int, default=0, help="Override target port")
|
||||
parser.add_argument("--timeout", type=int, default=0, help="Override socket timeout")
|
||||
parser.add_argument("--source-file", default="", help="Send raw WITS packet from file")
|
||||
parser.add_argument("--interval", type=float, default=3.0, help="Send interval in seconds")
|
||||
parser.add_argument("--count", type=int, default=1, help="Send count (0 = forever)")
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
parser = argparse.ArgumentParser(description="WITS TCP sender")
|
||||
add_arguments(parser)
|
||||
args = parser.parse_args(argv)
|
||||
deps = build_wits_sender_dependencies(args.config)
|
||||
run_wits_sender(args, deps)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user