2021-08-19 08:29:23 +00:00
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
import logging, coloredlogs
|
|
|
|
|
|
|
|
coloredlogs.install(level='INFO', fmt='%(asctime)s - [%(levelname)s] %(message)s')
|
|
|
|
|
2021-08-23 11:59:38 +00:00
|
|
|
PAIR_ID = "1"
|
|
|
|
DEVICE_TYPE = "actor" # actor or panel
|
|
|
|
PAIR_SECRET = "pair-secret"
|
2021-08-19 08:29:23 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
|
2021-08-23 11:59:38 +00:00
|
|
|
client.subscribe(f"mlmAccess/pair/response/{DEVICE_TYPE}")
|
2021-08-19 08:29:23 +00:00
|
|
|
|
|
|
|
logging.info("Requesting pairing")
|
2021-08-23 11:59:38 +00:00
|
|
|
client.publish(f"mlmAccess/pair/request/{DEVICE_TYPE}", PAIR_ID)
|
2021-08-19 08:29:23 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2021-08-23 11:59:38 +00:00
|
|
|
client = mqtt.Client(client_id=f"pair")
|
|
|
|
client.username_pw_set(f"pair", f"pair-secret")
|
2021-08-19 08:29:23 +00:00
|
|
|
|
|
|
|
client.on_connect = on_connect
|
|
|
|
client.on_message = on_message
|
|
|
|
|
|
|
|
logging.info("Initializing MQTT")
|
|
|
|
client.connect("localhost", 1883, 60)
|
|
|
|
|
|
|
|
client.loop_forever()
|