refactor: simplify mqtt pipeline state and remove event table
All checks were successful
Deploy to Production / deploy (push) Successful in 4m55s
All checks were successful
Deploy to Production / deploy (push) Successful in 4m55s
This commit is contained in:
@@ -1,24 +1,32 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
import paho.mqtt.client as mqtt # type: ignore[import-not-found,import-untyped]
|
||||
from sqlmodel import Session
|
||||
|
||||
from app.core.config import settings
|
||||
from app.core.db import engine
|
||||
from app.mqtt.repository import insert_raw_message
|
||||
from app.mqtt.topic import extract_device_id
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
TOPIC_PATTERN = re.compile(r"^terminal/(?P<device_id>[^/]+)/raw$")
|
||||
|
||||
|
||||
def extract_device_id(topic: str) -> str:
|
||||
match = TOPIC_PATTERN.match(topic)
|
||||
if match is None:
|
||||
raise ValueError("topic must match terminal/{device_id}/raw")
|
||||
return match.group("device_id")
|
||||
|
||||
|
||||
def _on_connect(
|
||||
client: mqtt.Client,
|
||||
client: Any,
|
||||
_userdata: object,
|
||||
_flags: dict[str, int],
|
||||
reason_code: int,
|
||||
_properties: mqtt.Properties | None = None,
|
||||
_properties: object | None = None,
|
||||
) -> None:
|
||||
if reason_code != 0:
|
||||
logger.error("MQTT connect failed: rc=%s", reason_code)
|
||||
@@ -31,9 +39,9 @@ def _on_connect(
|
||||
|
||||
|
||||
def _on_message(
|
||||
_client: mqtt.Client,
|
||||
_client: Any,
|
||||
_userdata: object,
|
||||
message: mqtt.MQTTMessage,
|
||||
message: Any,
|
||||
) -> None:
|
||||
try:
|
||||
device_id = extract_device_id(message.topic)
|
||||
@@ -52,7 +60,14 @@ def _on_message(
|
||||
)
|
||||
|
||||
|
||||
def create_client() -> mqtt.Client:
|
||||
def create_client() -> Any:
|
||||
try:
|
||||
import paho.mqtt.client as mqtt # type: ignore[import-not-found,import-untyped]
|
||||
except ModuleNotFoundError as exc: # pragma: no cover - runtime guard
|
||||
raise RuntimeError(
|
||||
"paho-mqtt is required to run mqtt-ingestor. Install backend dependencies first."
|
||||
) from exc
|
||||
|
||||
client = mqtt.Client(
|
||||
callback_api_version=mqtt.CallbackAPIVersion.VERSION2,
|
||||
client_id=settings.MQTT_CLIENT_ID,
|
||||
|
||||
Reference in New Issue
Block a user