提交 4a13f2f6 authored 作者: 陈泽健's avatar 陈泽健

补充MQTT的消息监听,监听到消息后,会通过日志输出消息体内容,可用于所有预定项目测试使用。

上级 c755ce7f
......@@ -99,4 +99,6 @@
- 处理模拟器无纸化无法读取PAK问题,实现通过模拟器进行app自动化巡检流程。
- 处理appium自动化截屏。
- 输出富创项目的MQTT会议推送相关代码处理。
- 输出富创项目的MQTT人脸签到订阅测试脚本,输出富创项目两个MQTT相关服务的README文档。
\ No newline at end of file
- 输出富创项目的MQTT人脸签到订阅测试脚本,输出富创项目两个MQTT相关服务的README文档。
29. 2024-12-13
- 补充MQTT的消息监听,监听到消息后,会通过日志输出消息体内容,可用于所有预定项目测试使用。
\ No newline at end of file
import paho.mqtt.client as mqtt
import logging
# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# MQTT 客户端回调函数
def on_connect(client, userdata, flags, rc):
logging.info("已连接到MQTT服务器")
client.subscribe(topic)
def on_message(client, userdata, msg):
logging.info(f"接收到消息: {msg.payload.decode()}")
if __name__ == "__main__":
broker_address = "192.168.5.218"
port = 1883
topic = "/meeting/sign/result" # 替换为你需要订阅的主题
# 创建 MQTT 客户端实例
mqtt_client = mqtt.Client()
# 设置回调函数
mqtt_client.on_connect = on_connect
mqtt_client.on_message = on_message
try:
# 连接到 MQTT 服务器
mqtt_client.connect(broker_address, port)
# 启动网络循环
mqtt_client.loop_forever()
except Exception as e:
logging.error(f"连接MQTT服务器时发生错误: {e}")
finally:
# 断开与 MQTT 服务器的连接
mqtt_client.disconnect()
\ No newline at end of file
from 预定系统.Base.Mqtt_Send import *
from 预定系统.Base.base import *
import paho.mqtt.client as mqtt
import logging
# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# MQTT 客户端回调函数
def on_connect(client, userdata, flags, rc):
logging.info("已连接到MQTT服务器")
client.subscribe(topic)
def on_message(client, userdata, msg):
logging.info(f"接收到消息: {msg.payload.decode()}")
if __name__ == "__main__":
broker_address = "192.168.5.218"
port = 1883
num_repeats = 1000 # 重复执行的次数
interval_between_repeats = 0.2 # 每次重复之间的间隔时间(秒)
face_signin_topic = "face_signin_topic" # 替换为你需要订阅的主题
topic = "/androidPanel/" # 替换为你需要订阅的主题
# 创建 MQTT 客户端实例
mqtt_client = Mqtt(broker_address, port)
mqtt_client = mqtt.Client()
# 设置回调函数
mqtt_client.on_connect = on_connect
mqtt_client.on_message = on_message
try:
# 连接到 MQTT 服务器
mqtt_client.connect()
logging.info("已连接到MQTT服务器")
# 订阅主题
mqtt_client.subscribe(face_signin_topic)
logging.info(f"已订阅主题: {face_signin_topic}")
# 循环等待消息
for _ in range(num_repeats):
received_message = mqtt_client.wait_for_message(face_signin_topic, timeout=5)
if received_message:
logging.info(f"接收到消息: {received_message}")
# 在这里处理接收到的消息
else:
logging.warning("超时时间内未接收到消息。")
mqtt_client.connect(broker_address, port)
# 等待一段时间后再次尝试
time.sleep(interval_between_repeats)
# 启动网络循环
mqtt_client.loop_forever()
except Exception as e:
logging.error(f"发送消息时发生错误: {e}")
logging.error(f"连接MQTT服务器时发生错误: {e}")
finally:
# 断开与 MQTT 服务器的连接
mqtt_client.disconnect()
mqtt_client.disconnect()
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论