Update fab_access/mqtt_client.py

This commit is contained in:
Luca Lutz 2022-11-12 16:04:24 +00:00
parent 82a899fd84
commit 4f52acb51b

View File

@ -1,8 +1,17 @@
from paho.mqtt import client as mqtt_client
from config import Config
import json
import random
import string
class MqttHandler:
@staticmethod
def get_random_string(length):
# choose from all lowercase letter
letters = string.ascii_lowercase
result_str = ''.join(random.choice(letters) for i in range(length))
return(result_str)
@staticmethod
def setup(msg_handler):
MqttHandler.msg_handler = msg_handler
@ -16,7 +25,7 @@ class MqttHandler:
else:
print('Failed to connect, return code %d\n', rc)
MqttHandler.client = mqtt_client.Client(Config.mqtt_client_id)
MqttHandler.client = mqtt_client.Client(Config.mqtt_client_id+get_random_string(5))
MqttHandler.client.username_pw_set('admin', 'user')
MqttHandler.client.on_connect = on_connect
MqttHandler.client.username_pw_set(Config.mqtt_user_name, Config.mqtt_password)