refactor(config): 重构配置模块并优化应用依赖注入
- 将配置相关类移动到model模块 - 实现依赖注入容器管理各组件依赖关系 - 重构配置加载逻辑支持多层级键值查找 - 更新主应用入口支持命令行参数解析 - 统一日志输出格式替换原有打印语句 - 引入钻井实时数据模型简化数据处理 - 移除硬编码字段映射改用动态配置方式 - 优化数据库写入逻辑基于新的数据模型
This commit is contained in:
@@ -1,12 +1,16 @@
|
||||
import argparse
|
||||
import json
|
||||
import logging
|
||||
import time
|
||||
from datetime import datetime
|
||||
from urllib.parse import urlparse
|
||||
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
from config.config import build_subscriber_dependencies
|
||||
from config import build_subscriber_dependencies
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def parse_broker(broker):
|
||||
@@ -22,12 +26,12 @@ def parse_broker(broker):
|
||||
|
||||
|
||||
def print_flat_fields(title, value):
|
||||
print(title)
|
||||
logger.info(title)
|
||||
if isinstance(value, dict):
|
||||
for k, v in value.items():
|
||||
print(f" {k}: {v}")
|
||||
for key, val in value.items():
|
||||
logger.info(" %s: %s", key, val)
|
||||
else:
|
||||
print(f" {value}")
|
||||
logger.info(" %s", value)
|
||||
|
||||
|
||||
def run_subscriber(args, deps):
|
||||
@@ -38,49 +42,36 @@ def run_subscriber(args, deps):
|
||||
raise ValueError("No topic to subscribe. Set sub-topic or pub-topic, or pass --topic")
|
||||
|
||||
scheme, host, port = parse_broker(mqtt_config.broker)
|
||||
|
||||
print("MQTT subscriber config:")
|
||||
print(f" broker: {scheme}://{host}:{port}")
|
||||
print(f" client-id: {mqtt_config.subscriber_client_id}")
|
||||
print(f" topic: {topic}")
|
||||
logger.info("MQTT subscriber config broker=%s://%s:%s client_id=%s topic=%s", scheme, host, port, mqtt_config.subscriber_client_id, topic)
|
||||
|
||||
client = mqtt.Client(client_id=mqtt_config.subscriber_client_id, clean_session=True)
|
||||
if mqtt_config.username is not None:
|
||||
client.username_pw_set(mqtt_config.username, mqtt_config.password)
|
||||
|
||||
if scheme in ("ssl", "tls", "mqtts"):
|
||||
client.tls_set()
|
||||
|
||||
def on_connect(c, userdata, flags, rc):
|
||||
if rc == 0:
|
||||
print("Connected")
|
||||
logger.info("Connected")
|
||||
c.subscribe(topic)
|
||||
print(f"Subscribed: {topic}")
|
||||
logger.info("Subscribed %s", topic)
|
||||
else:
|
||||
print(f"Connect failed rc={rc}")
|
||||
logger.error("Connect failed rc=%s", rc)
|
||||
|
||||
def on_disconnect(c, userdata, rc):
|
||||
print(f"Disconnected callback rc={rc}")
|
||||
logger.info("Disconnected callback rc=%s", rc)
|
||||
|
||||
def on_message(c, userdata, msg):
|
||||
raw_payload = msg.payload.decode("utf-8", errors="replace")
|
||||
print("=" * 80)
|
||||
print(f"RX Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
||||
print(f"Topic: {msg.topic}")
|
||||
print(f"QoS: {msg.qos}, Retain: {msg.retain}, Bytes: {len(msg.payload)}")
|
||||
logger.info("RX time=%s topic=%s qos=%s retain=%s bytes=%s", datetime.now().strftime('%Y-%m-%d %H:%M:%S'), msg.topic, msg.qos, msg.retain, len(msg.payload))
|
||||
try:
|
||||
obj = json.loads(raw_payload)
|
||||
print("Raw JSON:")
|
||||
print(json.dumps(obj, ensure_ascii=False, indent=2))
|
||||
logger.info("Raw JSON: %s", json.dumps(obj, ensure_ascii=False))
|
||||
if isinstance(obj, dict):
|
||||
print_flat_fields("meta:", obj.get("meta"))
|
||||
print_flat_fields("data:", obj.get("data"))
|
||||
extra_keys = [key for key in obj.keys() if key not in ("meta", "data")]
|
||||
for key in extra_keys:
|
||||
print_flat_fields(f"{key}:", obj.get(key))
|
||||
except Exception:
|
||||
print("Raw payload:")
|
||||
print(raw_payload)
|
||||
logger.info("Raw payload: %s", raw_payload)
|
||||
|
||||
client.on_connect = on_connect
|
||||
client.on_disconnect = on_disconnect
|
||||
@@ -92,18 +83,22 @@ def run_subscriber(args, deps):
|
||||
while True:
|
||||
time.sleep(1)
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
logger.info("Subscriber interrupted")
|
||||
finally:
|
||||
client.loop_stop()
|
||||
client.disconnect()
|
||||
print("Disconnected")
|
||||
logger.info("Subscriber stopped")
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser(description="MQTT subscriber")
|
||||
ap.add_argument("--config", default="config.yaml", help="Path to config yaml")
|
||||
ap.add_argument("--topic", default="", help="Override topic to subscribe")
|
||||
args = ap.parse_args()
|
||||
def add_arguments(parser):
|
||||
parser.add_argument("--config", default="config.yaml", help="Path to config yaml")
|
||||
parser.add_argument("--topic", default="", help="Override topic to subscribe")
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
parser = argparse.ArgumentParser(description="MQTT subscriber")
|
||||
add_arguments(parser)
|
||||
args = parser.parse_args(argv)
|
||||
deps = build_subscriber_dependencies(args.config)
|
||||
run_subscriber(args, deps)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user