from 预定系统.Base.Mqtt_Send import *
from 预定系统.Base.base import *

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"  # 替换为你需要订阅的主题

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

    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("超时时间内未接收到消息。")

            # 等待一段时间后再次尝试
            time.sleep(interval_between_repeats)

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

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