server/mock-actor/mock-actor-pair.py

41 lines
1.1 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 = "106"
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("mlmAccess/pair/response/actor")
logging.info("Requesting pairing")
client.publish("mlmAccess/pair/request/actor", 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="pair-actor")
client.username_pw_set("pair-actor", "pair-actor")
client.on_connect = on_connect
client.on_message = on_message
logging.info("Initializing MQTT")
client.connect("localhost", 1883, 60)
client.loop_forever()