- Some better logging
- Split pairing into seperate class
This commit is contained in:
parent
a7fa0e665d
commit
cbffbc92a6
8 changed files with 124 additions and 33 deletions
41
mock-actor/mock-actor-pair.py
Normal file
41
mock-actor/mock-actor-pair.py
Normal file
|
@ -0,0 +1,41 @@
|
|||
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()
|
|
@ -6,9 +6,9 @@ def on_connect(client, userdata, flags, rc):
|
|||
# Subscribing in on_connect() means that if we lose the connection and
|
||||
# reconnect then subscriptions will be renewed.
|
||||
print("Subscribing to topic","mlmAccess/pair/response/actor")
|
||||
print(client.subscribe("mlmAccess/pair/response/actor"))
|
||||
print(client.subscribe("mlmAccess/actor/102/action"))
|
||||
print("Publishing message to topic","mlmAccess/pair/request/actor")
|
||||
print(client.publish("mlmAccess/pair/request/actor", "101"))
|
||||
print(client.publish("mlmAccess/actor/102/status", "0", retain=True))
|
||||
|
||||
def on_message(client, userdata, message):
|
||||
print("message received " ,str(message.payload.decode("utf-8")))
|
||||
|
@ -16,8 +16,8 @@ def on_message(client, userdata, message):
|
|||
print("message qos=",message.qos)
|
||||
print("message retain flag=",message.retain)
|
||||
|
||||
client = mqtt.Client(client_id="pair-actor")
|
||||
client.username_pw_set("pair-actor", "pair-actor")
|
||||
client = mqtt.Client(client_id="actor-102")
|
||||
client.username_pw_set("actor-102", "YWT2JPOEZH")
|
||||
|
||||
client.on_connect = on_connect
|
||||
client.on_message = on_message
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue