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-19 09:27:39 +00:00
|
|
|
PAIR_ID = "101"
|
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")
|
|
|
|
|
|
|
|
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()
|