import random
import time

from paho.mqtt import client as mqtt_client


broker = '127.0.0.1'
port = 1883
topics = ["zigbee2mqtt/ComfortMonitor_1/42/set", "zigbee2mqtt/ComfortMonitor_2/42/set"]
client_id = f'python-mqtt-310-pub'

def connect_mqtt():
    def on_connect(client, userdata, flags, rc):
        if rc == 0:
            print("Connected to MQTT Broker!")
        else:
            print("Failed to connect, return code %d\n", rc)

    client = mqtt_client.Client(client_id)
    client.on_connect = on_connect
    client.connect(broker, port)
    return client


def publish(client):
    msg_count = 0
    while True:
        time.sleep(60)

        for topic in topics:
            msg = '{"read":{"attributes":["measuredValue"],"cluster":"msTemperatureMeasurement","options":{}}}'
            result = client.publish(topic, msg)
            status = result[0]
            if status == 0:
                print(f"Send `{msg}` to topic `{topic}`")
            else:
                print(f"Failed to send message to topic {topic}")
            msg_count += 1

            time.sleep(5)
            msg = '{"read":{"attributes":["measuredValue"],"cluster":"msRelativeHumidity","options":{}}}'
            result = client.publish(topic, msg)
            status = result[0]
            if status == 0:
                print(f"Send `{msg}` to topic `{topic}`")
            else:
                print(f"Failed to send message to topic {topic}")
            msg_count += 1

            time.sleep(5)
            msg = '{"read":{"attributes":["measuredValue"],"cluster":"msPressureMeasurement","options":{}}}'
            result = client.publish(topic, msg)
            status = result[0]
            if status == 0:
                print(f"Send `{msg}` to topic `{topic}`")
            else:
                print(f"Failed to send message to topic {topic}")
            msg_count += 1

            time.sleep(5)
            msg = '{"read":{"attributes":["measuredValue"],"cluster":"msIlluminanceMeasurement","options":{}}}'
            result = client.publish(topic, msg)
            status = result[0]
            if status == 0:
                print(f"Send `{msg}` to topic `{topic}`")
            else:
                print(f"Failed to send message to topic {topic}")
            msg_count += 1

            time.sleep(5)
            msg = '{"read":{"attributes":["measuredValue"],"cluster":"msCO2","options":{}}}'
            result = client.publish(topic, msg)
            status = result[0]
            if status == 0:
                print(f"Send `{msg}` to topic `{topic}`")
            else:
                print(f"Failed to send message to topic {topic}")
            msg_count += 1

            time.sleep(5)
            msg = '{"read":{"attributes":["measuredValue"],"cluster":"pm25Measurement","options":{}}}'
            result = client.publish(topic, msg)
            status = result[0]
            if status == 0:
                print(f"Send `{msg}` to topic `{topic}`")
            else:
                print(f"Failed to send message to topic {topic}")
            msg_count += 1


def run():
    client = connect_mqtt()
    client.loop_start()
    publish(client)


if __name__ == '__main__':
    run()
