MQTT会议推送.py 1.9 KB
Newer Older
陈泽健's avatar
陈泽健 committed
1 2 3 4 5 6
from 预定系统.Base.Mqtt_Send import *
from 预定系统.Base.base import *

# 获取当前脚本所在的目录
current_dir = os.path.dirname(os.path.abspath(__file__))

7
# 构建会议预约CSV文件的绝对路径
8
csv_file_path = os.path.join(current_dir, '../../测试数据/MQTT模块/富创项目/富创项目_会议预约推送.csv')
9
# 构建会议修改CSV文件的绝对路径
10 11
# csv_file_path = os.path.join(current_dir, '../../测试数据/MQTT模块/富创项目/富创项目_会议修改推送.csv')
# # 构建会议删除CSV文件的绝对路径
12
# csv_file_path = os.path.join(current_dir, '../../测试数据/MQTT模块/富创项目/富创项目_会议删除推送.csv')
陈泽健's avatar
陈泽健 committed
13 14 15 16 17

if __name__ == "__main__":
    # 读取配置文件
    configs = Mqtt.read_config_from_csv(csv_file_path)

18
    broker_address = "192.168.1.193"
陈泽健's avatar
陈泽健 committed
19
    port = 1883
20
    num_repeats = 1000  # 重复执行的次数
陈泽健's avatar
陈泽健 committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    interval_between_repeats = 0.2  # 每次重复之间的间隔时间(秒)

    # 创建 MQTT 客户端实例
    mqtt_client = Mqtt(broker_address, port)

    try:
        # 连接到 MQTT 服务器
        mqtt_client.connect()
        logging.info("已连接到MQTT服务器")

        # 遍历配置文件中的每一行数据
        for config in configs:
            # 构建消息内容-
            # 构建消息内容
            topic = config["topic"]
            logging.info(config)
            message = Mqtt.build_message(config, "00:00", topic)

            # 发送消息
            mqtt_client.publish(topic, message)

            # 每次发送之间可以设置一个间隔时间
            time.sleep(interval_between_repeats)

        # 每次重复之间设置一个间隔时间
        time.sleep(interval_between_repeats)

    except Exception as e:
        logging.error(f"发送消息时发生错误: {e}")

    finally:
        # 断开与 MQTT 服务器的连接
53
        mqtt_client.disconnect()