server/backend/pairingHandler.py
Dorian Zedler cbffbc92a6
- Some better logging
- Split pairing into seperate class
2021-08-19 10:29:23 +02:00

26 lines
No EOL
1.2 KiB
Python

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)
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")