server/mock-clients/mock-client-pair.py

43 lines
1.2 KiB
Python
Raw Normal View History

import paho.mqtt.client as mqtt
import logging, coloredlogs
coloredlogs.install(level='INFO', fmt='%(asctime)s - [%(levelname)s] %(message)s')
PAIR_ID = "1"
DEVICE_TYPE = "actor" # actor or panel
PAIR_SECRET = "pair-secret"
def on_connect(client, userdata, flags, rc):
global PAIR_ID
if rc != 0:
logging.error(f"Error connecting to MQTT broker: {rc}")
return
logging.info("Successfully connected to MQTT broker")
client.subscribe(f"mlmAccess/pair/response/{DEVICE_TYPE}")
logging.info("Requesting pairing")
client.publish(f"mlmAccess/pair/request/{DEVICE_TYPE}", PAIR_ID)
def on_message(client, userdata, message):
messageContent = str(message.payload.decode("utf-8"))
if len(messageContent) > 0:
logging.info(f"Pairing was successfull! The password is: {messageContent}")
exit(0)
else:
logging.error("Pairing was not successfull!")
exit(1)
client = mqtt.Client(client_id=f"pair")
client.username_pw_set(f"pair", f"pair-secret")
client.on_connect = on_connect
client.on_message = on_message
logging.info("Initializing MQTT")
client.connect("localhost", 1883, 60)
client.loop_forever()