server/backend/pairingHandler.py

29 lines
1.2 KiB
Python
Raw Permalink Normal View History

import random, string, logging
class MaPairingHandler:
def __init__(self, mqtt, db):
self._mqtt = mqtt
self._db = db
def handlePairingRequest(self, topic, message):
if "actor" in topic:
self._handleActorPairRequest(message)
elif "panel" in topic:
pass
#self.
else:
logging.warn(f"Invalid pairing request: {topic}, {message}, Device type not know.")
def _handleActorPairRequest(self, id):
logging.info(f"Pair request for type actor with ID {id}")
if not self._db.userExists(f"actor-{id}"):
logging.info(f"Pair request {id} accepted")
password = ''.join(random.choices(string.ascii_uppercase + string.ascii_lowercase + string.digits, k=20))
self._db.addUser(f"actor-{id}", password, [f"mlmAccess/actor/{id}/status"], [f"mlmAccess/actor/{id}/action"])
self._mqtt.publish("mlmAccess/pair/response/actor", password)
else:
logging.warning(f"Rejecting pair request for ID {id}: ID already exists")
self._mqtt.publish("mlmAccess/pair/response/actor", "")
def _mqttOnConnect(self, client, userdata, flags, rc):
client.subscribe("mlmAccess/pair/request/actor")