This commit is contained in:
2024-10-24 09:23:39 +08:00
parent 641c34b1b3
commit 16c005556f
36 changed files with 7780 additions and 0 deletions

51
SCLP/auto_callback.py Normal file
View File

@@ -0,0 +1,51 @@
# -*- coding:utf-8 -*-
"""
@Author : xuxingchen
@Contact : xuxingchen@sinochem.com
@Desc : 用于自动回复mqtt用于测试模拟设备下发请求成功
"""
import json
import time
import paho.mqtt.client as mqtt
from utils import logger
from config import BROKER_HOST, BROKER_PORT, BROKER_USERNAME, BROKER_PASSWD
def on_connect(client, userdata, flags, rc):
logger.Logger.debug(f"\033[1;32mAUTO CALLBACK\033[0m 🔗 Mqtt connection! {{rc: {rc}}} 🔗")
client.subscribe("/jmlink/+/tml/service/call")
def on_disconnect(client, userdata, rc):
logger.Logger.debug(f"\033[1;31mAUTO CALLBACK\033[0m 🔌 Break mqtt connection! {{rc: {rc}}} 🔌")
def on_message(client, userdata, message):
logger.Logger.debug("\033[1;36mAUTO CALLBACK\033[0m <- 💡 接收到新数据,执行 on_message 中 ...")
msg = json.loads(message.payload.decode().replace("\x00", ""))
print(f"msg: {msg}")
if "messageId" in msg.keys():
topic = f"{message.topic}_resp"
print(topic)
auto_callback = {
"messageId": msg["messageId"],
"code": 0,
"data": {
"code": 0
}
}
client.publish(topic, json.dumps(auto_callback))
print(auto_callback)
client = mqtt.Client(client_id="auto_callback@python")
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_message = on_message
client.username_pw_set(BROKER_USERNAME, BROKER_PASSWD)
client.connect(BROKER_HOST, BROKER_PORT)
client.loop_start()
while True:
time.sleep(86400)