完善中间件自动更新逻辑
This commit is contained in:
@@ -8,6 +8,7 @@ import uuid
|
||||
from aiomqtt import Client
|
||||
|
||||
from config.redis import redis_client
|
||||
from db.postgres import get_dept_id_by_iot_user_name, get_device_type_by_iot_user_name
|
||||
from models.MqttTopic import MqttTopic
|
||||
|
||||
# ================= 配置区域 =================
|
||||
@@ -19,9 +20,9 @@ TLS_CONTEXT = ssl.create_default_context()
|
||||
# 默认连接后要订阅的 topic 配置
|
||||
DEFAULT_SUBSCRIPTIONS = [
|
||||
MqttTopic.from_parts(
|
||||
project=None,
|
||||
dept_id=None,
|
||||
domain="status",
|
||||
device_type="edge",
|
||||
device_type=None,
|
||||
device_id=None,
|
||||
resource="info",
|
||||
)
|
||||
@@ -57,7 +58,7 @@ def get_device_id_simple():
|
||||
|
||||
|
||||
async def mqtt_publish(
|
||||
project: str,
|
||||
dept_id: str,
|
||||
domain: str,
|
||||
device_type: str,
|
||||
device_id: str,
|
||||
@@ -68,7 +69,7 @@ async def mqtt_publish(
|
||||
"""发布消息(使用全局客户端)"""
|
||||
if not MQTT_CLIENT:
|
||||
raise RuntimeError("MQTT client is not initialized")
|
||||
topic = f"{project}/{domain}/{device_type}/{device_id}/{resource}"
|
||||
topic = f"{dept_id}/{domain}/{device_type}/{device_id}/{resource}"
|
||||
await MQTT_CLIENT.publish(topic, payload, qos=qos)
|
||||
print(f"Published to {topic}: {payload}")
|
||||
|
||||
@@ -97,9 +98,23 @@ async def _mqtt_handle_messages():
|
||||
print("收到消息:" + str(topic))
|
||||
|
||||
# 处理基础状态信息
|
||||
if topic.domain == "status" and topic.resource == "info":
|
||||
if topic.is_status():
|
||||
# 这里收到的数据是这样的:"x/status/x/deviceID/info"
|
||||
payload = json.loads(message.payload.decode())
|
||||
redis_client.set_device_info(topic.device_id, payload)
|
||||
dept_id = get_dept_id_by_iot_user_name(topic.device_id)
|
||||
dept_edge = get_device_type_by_iot_user_name(topic.device_id)
|
||||
payload["dept_id"] = dept_id
|
||||
payload_str = json.dumps(payload, ensure_ascii=False)
|
||||
# 返回给设备
|
||||
await mqtt_publish(
|
||||
dept_id,
|
||||
topic.domain,
|
||||
dept_edge,
|
||||
topic.device_id,
|
||||
"receipt",
|
||||
payload_str,
|
||||
)
|
||||
|
||||
|
||||
async def mqtt_client_async():
|
||||
|
||||
Reference in New Issue
Block a user